1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-10-06 09:07:19 +02:00

Replace SFML with SDL2

This entirely replaces all remaining SFML pieces with SDL2 and
brings OpenRW up to OpenGL 3.3
This commit is contained in:
Christoph Heiss 2016-06-22 12:29:39 +02:00
parent 938a2e4bfc
commit 649f7b144d
30 changed files with 458 additions and 639 deletions

View File

@ -68,13 +68,19 @@ ENDIF()
find_package(OpenGL REQUIRED)
find_package(OpenAL REQUIRED)
find_package(Bullet REQUIRED)
find_package(SFML 2 COMPONENTS system window graphics network REQUIRED)
find_package(MAD REQUIRED)
find_package(GLM REQUIRED)
find_package(LibSndFile REQUIRED)
find_package(SDL2 REQUIRED)
find_package(PNG REQUIRED)
include_directories("${GLM_INCLUDE_DIRS}")
# CMake otherwise fails with CMP0004
string(STRIP "${SDL2_LIBRARIES}" SDL2_LIBRARIES)
include_directories(
${SDL2_INCLUDE_DIR}
${GLM_INCLUDE_DIRS}
)
# External-internal dependencies
add_subdirectory(cmake/external)

View File

@ -36,10 +36,10 @@ MAD is licensed under the GNU General Public License
* http://www.underbit.com/products/mad/
SFML is licensed under the zlib license
* http://www.sfml-dev.org/license.php
Bullet Physics is licensed under the zlib license
* http://bulletphysics.org/mediawiki-1.5.8/index.php/LICENSE
SDL 2.0 is licensed under the zlib license
* https://www.libsdl.org/license.php

View File

@ -2,7 +2,7 @@
# This script defines the following variables:
# - MAD_LIBRARY: The mad library
# - MAD_FOUND: true if all the required modules are found
# - MAD_INCLUDE_DIR: the path where SFML headers are located (the directory containing the SFML/Config.hpp file)
# - MAD_INCLUDE_DIR: the path where MAD headers are located
#
set(FIND_MAD_PATHS

View File

@ -1,368 +0,0 @@
# This script locates the SFML library
# ------------------------------------
#
# Usage
# -----
#
# When you try to locate the SFML libraries, you must specify which modules you want to use (system, window, graphics, network, audio, main).
# If none is given, the SFML_LIBRARIES variable will be empty and you'll end up linking to nothing.
# example:
# find_package(SFML COMPONENTS graphics window system) // find the graphics, window and system modules
#
# You can enforce a specific version, either MAJOR.MINOR or only MAJOR.
# If nothing is specified, the version won't be checked (i.e. any version will be accepted).
# example:
# find_package(SFML COMPONENTS ...) // no specific version required
# find_package(SFML 2 COMPONENTS ...) // any 2.x version
# find_package(SFML 2.4 COMPONENTS ...) // version 2.4 or greater
#
# By default, the dynamic libraries of SFML will be found. To find the static ones instead,
# you must set the SFML_STATIC_LIBRARIES variable to TRUE before calling find_package(SFML ...).
# Since you have to link yourself all the SFML dependencies when you link it statically, the following
# additional variables are defined: SFML_XXX_DEPENDENCIES and SFML_DEPENDENCIES (see their detailed
# description below).
# In case of static linking, the SFML_STATIC macro will also be defined by this script.
# example:
# set(SFML_STATIC_LIBRARIES TRUE)
# find_package(SFML 2 COMPONENTS network system)
#
# On Mac OS X if SFML_STATIC_LIBRARIES is not set to TRUE then by default CMake will search for frameworks unless
# CMAKE_FIND_FRAMEWORK is set to "NEVER" for example. Please refer to CMake documentation for more details.
# Moreover, keep in mind that SFML frameworks are only available as release libraries unlike dylibs which
# are available for both release and debug modes.
#
# If SFML is not installed in a standard path, you can use the SFML_ROOT CMake (or environment) variable
# to tell CMake where SFML is.
#
# Output
# ------
#
# This script defines the following variables:
# - For each specified module XXX (system, window, graphics, network, audio, main):
# - SFML_XXX_LIBRARY_DEBUG: the name of the debug library of the xxx module (set to SFML_XXX_LIBRARY_RELEASE is no debug version is found)
# - SFML_XXX_LIBRARY_RELEASE: the name of the release library of the xxx module (set to SFML_XXX_LIBRARY_DEBUG is no release version is found)
# - SFML_XXX_LIBRARY: the name of the library to link to for the xxx module (includes both debug and optimized names if necessary)
# - SFML_XXX_FOUND: true if either the debug or release library of the xxx module is found
# - SFML_XXX_DEPENDENCIES: the list of libraries the module depends on, in case of static linking
# - SFML_LIBRARIES: the list of all libraries corresponding to the required modules
# - SFML_FOUND: true if all the required modules are found
# - SFML_INCLUDE_DIR: the path where SFML headers are located (the directory containing the SFML/Config.hpp file)
# - SFML_DEPENDENCIES: the list of libraries SFML depends on, in case of static linking
#
# example:
# find_package(SFML 2 COMPONENTS system window graphics audio REQUIRED)
# include_directories(${SFML_INCLUDE_DIR})
# add_executable(myapp ...)
# target_link_libraries(myapp ${SFML_LIBRARIES})
# define the SFML_STATIC macro if static build was chosen
if(SFML_STATIC_LIBRARIES)
add_definitions(-DSFML_STATIC)
endif()
# define the list of search paths for headers and libraries
set(FIND_SFML_PATHS
${SFML_ROOT}
$ENV{SFML_ROOT}
~/Library/Frameworks
/Library/Frameworks
/usr/local
/usr
/sw
/opt/local
/opt/csw
/opt)
# find the SFML include directory
find_path(SFML_INCLUDE_DIR SFML/Config.hpp
PATH_SUFFIXES include
PATHS ${FIND_SFML_PATHS})
# check the version number
set(SFML_VERSION_OK TRUE)
if(SFML_FIND_VERSION AND SFML_INCLUDE_DIR)
# extract the major and minor version numbers from SFML/Config.hpp
# we have to handle framework a little bit differently:
if("${SFML_INCLUDE_DIR}" MATCHES "SFML.framework")
set(SFML_CONFIG_HPP_INPUT "${SFML_INCLUDE_DIR}/Headers/Config.hpp")
else()
set(SFML_CONFIG_HPP_INPUT "${SFML_INCLUDE_DIR}/SFML/Config.hpp")
endif()
FILE(READ "${SFML_CONFIG_HPP_INPUT}" SFML_CONFIG_HPP_CONTENTS)
STRING(REGEX REPLACE ".*#define SFML_VERSION_MAJOR ([0-9]+).*" "\\1" SFML_VERSION_MAJOR "${SFML_CONFIG_HPP_CONTENTS}")
STRING(REGEX REPLACE ".*#define SFML_VERSION_MINOR ([0-9]+).*" "\\1" SFML_VERSION_MINOR "${SFML_CONFIG_HPP_CONTENTS}")
STRING(REGEX REPLACE ".*#define SFML_VERSION_PATCH ([0-9]+).*" "\\1" SFML_VERSION_PATCH "${SFML_CONFIG_HPP_CONTENTS}")
if (NOT "${SFML_VERSION_PATCH}" MATCHES "^[0-9]+$")
set(SFML_VERSION_PATCH 0)
endif()
math(EXPR SFML_REQUESTED_VERSION "${SFML_FIND_VERSION_MAJOR} * 10000 + ${SFML_FIND_VERSION_MINOR} * 100 + ${SFML_FIND_VERSION_PATCH}")
# if we could extract them, compare with the requested version number
if (SFML_VERSION_MAJOR)
# transform version numbers to an integer
math(EXPR SFML_VERSION "${SFML_VERSION_MAJOR} * 10000 + ${SFML_VERSION_MINOR} * 100 + ${SFML_VERSION_PATCH}")
# compare them
if(SFML_VERSION LESS SFML_REQUESTED_VERSION)
set(SFML_VERSION_OK FALSE)
endif()
else()
# SFML version is < 2.0
if (SFML_REQUESTED_VERSION GREATER 10900)
set(SFML_VERSION_OK FALSE)
set(SFML_VERSION_MAJOR 1)
set(SFML_VERSION_MINOR x)
set(SFML_VERSION_PATCH x)
endif()
endif()
endif()
# find the requested modules
set(SFML_FOUND TRUE) # will be set to false if one of the required modules is not found
foreach(FIND_SFML_COMPONENT ${SFML_FIND_COMPONENTS})
string(TOLOWER ${FIND_SFML_COMPONENT} FIND_SFML_COMPONENT_LOWER)
string(TOUPPER ${FIND_SFML_COMPONENT} FIND_SFML_COMPONENT_UPPER)
set(FIND_SFML_COMPONENT_NAME sfml-${FIND_SFML_COMPONENT_LOWER})
# no suffix for sfml-main, it is always a static library
if(FIND_SFML_COMPONENT_LOWER STREQUAL "main")
# release library
find_library(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE
NAMES ${FIND_SFML_COMPONENT_NAME}
PATH_SUFFIXES lib64 lib
PATHS ${FIND_SFML_PATHS})
# debug library
find_library(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG
NAMES ${FIND_SFML_COMPONENT_NAME}-d
PATH_SUFFIXES lib64 lib
PATHS ${FIND_SFML_PATHS})
else()
# static release library
find_library(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_STATIC_RELEASE
NAMES ${FIND_SFML_COMPONENT_NAME}-s
PATH_SUFFIXES lib64 lib
PATHS ${FIND_SFML_PATHS})
# static debug library
find_library(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_STATIC_DEBUG
NAMES ${FIND_SFML_COMPONENT_NAME}-s-d
PATH_SUFFIXES lib64 lib
PATHS ${FIND_SFML_PATHS})
# dynamic release library
find_library(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DYNAMIC_RELEASE
NAMES ${FIND_SFML_COMPONENT_NAME}
PATH_SUFFIXES lib64 lib
PATHS ${FIND_SFML_PATHS})
# dynamic debug library
find_library(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DYNAMIC_DEBUG
NAMES ${FIND_SFML_COMPONENT_NAME}-d
PATH_SUFFIXES lib64 lib
PATHS ${FIND_SFML_PATHS})
# choose the entries that fit the requested link type
if(SFML_STATIC_LIBRARIES)
if(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_STATIC_RELEASE)
set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_STATIC_RELEASE})
endif()
if(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_STATIC_DEBUG)
set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_STATIC_DEBUG})
endif()
else()
if(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DYNAMIC_RELEASE)
set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DYNAMIC_RELEASE})
endif()
if(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DYNAMIC_DEBUG)
set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DYNAMIC_DEBUG})
endif()
endif()
endif()
if (SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG OR SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE)
# library found
set(SFML_${FIND_SFML_COMPONENT_UPPER}_FOUND TRUE)
# if both are found, set SFML_XXX_LIBRARY to contain both
if (SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG AND SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE)
set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY debug ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG}
optimized ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE})
endif()
# if only one debug/release variant is found, set the other to be equal to the found one
if (SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG AND NOT SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE)
# debug and not release
set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG})
set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG})
endif()
if (SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE AND NOT SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG)
# release and not debug
set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE})
set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE})
endif()
else()
# library not found
set(SFML_FOUND FALSE)
set(SFML_${FIND_SFML_COMPONENT_UPPER}_FOUND FALSE)
set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY "")
set(FIND_SFML_MISSING "${FIND_SFML_MISSING} SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY")
endif()
# mark as advanced
MARK_AS_ADVANCED(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY
SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE
SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG
SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_STATIC_RELEASE
SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_STATIC_DEBUG
SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DYNAMIC_RELEASE
SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DYNAMIC_DEBUG)
# add to the global list of libraries
set(SFML_LIBRARIES ${SFML_LIBRARIES} "${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY}")
endforeach()
# in case of static linking, we must also define the list of all the dependencies of SFML libraries
if(SFML_STATIC_LIBRARIES)
# detect the OS
if(${CMAKE_SYSTEM_NAME} MATCHES "Windows")
set(FIND_SFML_OS_WINDOWS 1)
elseif(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
set(FIND_SFML_OS_LINUX 1)
elseif(${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD")
set(FIND_SFML_OS_FREEBSD 1)
elseif(${CMAKE_SYSTEM_NAME} MATCHES "Darwin")
set(FIND_SFML_OS_MACOSX 1)
endif()
# start with an empty list
set(SFML_DEPENDENCIES)
set(FIND_SFML_DEPENDENCIES_NOTFOUND)
# macro that searches for a 3rd-party library
macro(find_sfml_dependency output friendlyname)
# No lookup in environment variables (PATH on Windows), as they may contain wrong library versions
find_library(${output} NAMES ${ARGN} PATHS ${FIND_SFML_PATHS} PATH_SUFFIXES lib NO_SYSTEM_ENVIRONMENT_PATH)
if(${${output}} STREQUAL "${output}-NOTFOUND")
unset(output)
set(FIND_SFML_DEPENDENCIES_NOTFOUND "${FIND_SFML_DEPENDENCIES_NOTFOUND} ${friendlyname}")
endif()
endmacro()
# sfml-system
list(FIND SFML_FIND_COMPONENTS "system" FIND_SFML_SYSTEM_COMPONENT)
if(NOT ${FIND_SFML_SYSTEM_COMPONENT} EQUAL -1)
# update the list -- these are only system libraries, no need to find them
if(FIND_SFML_OS_LINUX OR FIND_SFML_OS_FREEBSD OR FIND_SFML_OS_MACOSX)
set(SFML_SYSTEM_DEPENDENCIES "pthread")
endif()
if(FIND_SFML_OS_LINUX)
set(SFML_SYSTEM_DEPENDENCIES ${SFML_SYSTEM_DEPENDENCIES} "rt")
endif()
if(FIND_SFML_OS_WINDOWS)
set(SFML_SYSTEM_DEPENDENCIES "winmm")
endif()
set(SFML_DEPENDENCIES ${SFML_SYSTEM_DEPENDENCIES} ${SFML_DEPENDENCIES})
endif()
# sfml-network
list(FIND SFML_FIND_COMPONENTS "network" FIND_SFML_NETWORK_COMPONENT)
if(NOT ${FIND_SFML_NETWORK_COMPONENT} EQUAL -1)
# update the list -- these are only system libraries, no need to find them
if(FIND_SFML_OS_WINDOWS)
set(SFML_NETWORK_DEPENDENCIES "ws2_32")
endif()
set(SFML_DEPENDENCIES ${SFML_NETWORK_DEPENDENCIES} ${SFML_DEPENDENCIES})
endif()
# sfml-window
list(FIND SFML_FIND_COMPONENTS "window" FIND_SFML_WINDOW_COMPONENT)
if(NOT ${FIND_SFML_WINDOW_COMPONENT} EQUAL -1)
# find libraries
if(FIND_SFML_OS_LINUX OR FIND_SFML_OS_FREEBSD)
find_sfml_dependency(X11_LIBRARY "X11" X11)
find_sfml_dependency(LIBXCB_LIBRARIES "XCB" xcb libxcb)
find_sfml_dependency(X11_XCB_LIBRARY "X11-xcb" X11-xcb libX11-xcb)
find_sfml_dependency(XCB_RANDR_LIBRARY "xcb-randr" xcb-randr libxcb-randr)
find_sfml_dependency(XCB_IMAGE_LIBRARY "xcb-image" xcb-image libxcb-image)
endif()
if(FIND_SFML_OS_LINUX)
find_sfml_dependency(UDEV_LIBRARIES "UDev" udev libudev)
endif()
# update the list
if(FIND_SFML_OS_WINDOWS)
set(SFML_WINDOW_DEPENDENCIES ${SFML_WINDOW_DEPENDENCIES} "opengl32" "winmm" "gdi32")
elseif(FIND_SFML_OS_LINUX)
set(SFML_WINDOW_DEPENDENCIES ${SFML_WINDOW_DEPENDENCIES} "GL" ${X11_LIBRARY} ${LIBXCB_LIBRARIES} ${X11_XCB_LIBRARY} ${XCB_RANDR_LIBRARY} ${XCB_IMAGE_LIBRARY} ${UDEV_LIBRARIES})
elseif(FIND_SFML_OS_FREEBSD)
set(SFML_WINDOW_DEPENDENCIES ${SFML_WINDOW_DEPENDENCIES} "GL" ${X11_LIBRARY} ${LIBXCB_LIBRARIES} ${X11_XCB_LIBRARY} ${XCB_RANDR_LIBRARY} ${XCB_IMAGE_LIBRARY} "usbhid")
elseif(FIND_SFML_OS_MACOSX)
set(SFML_WINDOW_DEPENDENCIES ${SFML_WINDOW_DEPENDENCIES} "-framework OpenGL -framework Foundation -framework AppKit -framework IOKit -framework Carbon")
endif()
set(SFML_DEPENDENCIES ${SFML_WINDOW_DEPENDENCIES} ${SFML_DEPENDENCIES})
endif()
# sfml-graphics
list(FIND SFML_FIND_COMPONENTS "graphics" FIND_SFML_GRAPHICS_COMPONENT)
if(NOT ${FIND_SFML_GRAPHICS_COMPONENT} EQUAL -1)
# find libraries
find_sfml_dependency(FREETYPE_LIBRARY "FreeType" freetype)
find_sfml_dependency(JPEG_LIBRARY "libjpeg" jpeg)
# update the list
set(SFML_GRAPHICS_DEPENDENCIES ${FREETYPE_LIBRARY} ${JPEG_LIBRARY})
set(SFML_DEPENDENCIES ${SFML_GRAPHICS_DEPENDENCIES} ${SFML_DEPENDENCIES})
endif()
# sfml-audio
list(FIND SFML_FIND_COMPONENTS "audio" FIND_SFML_AUDIO_COMPONENT)
if(NOT ${FIND_SFML_AUDIO_COMPONENT} EQUAL -1)
# find libraries
find_sfml_dependency(OPENAL_LIBRARY "OpenAL" openal openal32)
find_sfml_dependency(OGG_LIBRARY "Ogg" ogg)
find_sfml_dependency(VORBIS_LIBRARY "Vorbis" vorbis)
find_sfml_dependency(VORBISFILE_LIBRARY "VorbisFile" vorbisfile)
find_sfml_dependency(VORBISENC_LIBRARY "VorbisEnc" vorbisenc)
find_sfml_dependency(FLAC_LIBRARY "FLAC" FLAC)
# update the list
set(SFML_AUDIO_DEPENDENCIES ${OPENAL_LIBRARY} ${FLAC_LIBRARY} ${VORBISENC_LIBRARY} ${VORBISFILE_LIBRARY} ${VORBIS_LIBRARY} ${OGG_LIBRARY})
set(SFML_DEPENDENCIES ${SFML_DEPENDENCIES} ${SFML_AUDIO_DEPENDENCIES})
endif()
endif()
# handle errors
if(NOT SFML_VERSION_OK)
# SFML version not ok
set(FIND_SFML_ERROR "SFML found but version too low (requested: ${SFML_FIND_VERSION}, found: ${SFML_VERSION_MAJOR}.${SFML_VERSION_MINOR}.${SFML_VERSION_PATCH})")
set(SFML_FOUND FALSE)
elseif(SFML_STATIC_LIBRARIES AND FIND_SFML_DEPENDENCIES_NOTFOUND)
set(FIND_SFML_ERROR "SFML found but some of its dependencies are missing (${FIND_SFML_DEPENDENCIES_NOTFOUND})")
set(SFML_FOUND FALSE)
elseif(NOT SFML_FOUND)
# include directory or library not found
set(FIND_SFML_ERROR "Could NOT find SFML (missing: ${FIND_SFML_MISSING})")
endif()
if (NOT SFML_FOUND)
if(SFML_FIND_REQUIRED)
# fatal error
message(FATAL_ERROR ${FIND_SFML_ERROR})
elseif(NOT SFML_FIND_QUIETLY)
# error but continue
message("${FIND_SFML_ERROR}")
endif()
endif()
# handle success
if(SFML_FOUND AND NOT SFML_FIND_QUIETLY)
message(STATUS "Found SFML ${SFML_VERSION_MAJOR}.${SFML_VERSION_MINOR}.${SFML_VERSION_PATCH} in ${SFML_INCLUDE_DIR}")
endif()

View File

@ -12,15 +12,14 @@ add_library(rwengine
target_link_libraries(rwengine
rwlib
${MAD_LIBRARY}
${SFML_LIBRARIES}
${LIBSNDFILE_LIBRARY}
${OPENAL_LIBRARY}
${OPENRW_PLATFORM_LIBS})
${OPENRW_PLATFORM_LIBS}
${SDL2_LIBRARIES})
include_directories(SYSTEM
${BULLET_INCLUDE_DIR}
${MAD_INCLUDE_DIR}
${SFML_INCLUDE_DIR}
${LIBSNDFILE_INCLUDE_DIR}
${OPENAL_INCLUDE_DIR}
)

View File

@ -3,7 +3,7 @@
namespace GameShaders {
const char* WaterHQ::VertexShader = R"(
#version 130
#version 330
#extension GL_ARB_explicit_attrib_location : enable
#extension GL_ARB_uniform_buffer_object : enable
@ -53,7 +53,7 @@ void main()
mat3 rot = mat3(view);
vec3 ray = vec3(-position.x, -position.y, projection[0][0] ) * rot;
float plane = texture2D( data, TexCoords ).r;
float plane = texture( data, TexCoords ).r;
vec3 ws = planeIntercept( campos.xyz, ray, plane );
@ -63,19 +63,20 @@ void main()
})";
const char* WaterHQ::FragmentShader = R"(
#version 130
#version 330
in vec3 Normal;
in vec2 TexCoords;
uniform sampler2D texture;
uniform sampler2D tex;
out vec4 outColour;
in vec3 test;
void main() {
vec4 c = texture2D(texture, TexCoords);
vec4 c = texture(tex, TexCoords);
outColour = c;
})";
const char* Mask3D::VertexShader = R"(
#version 130
#version 330
#extension GL_ARB_explicit_attrib_location : enable
#extension GL_ARB_uniform_buffer_object : enable
@ -101,7 +102,8 @@ void main()
})";
const char* Mask3D::FragmentShader = R"(
#version 130
#version 330
in vec3 pp;
out vec4 outColour;
void main() {
@ -109,7 +111,7 @@ void main() {
})";
const char* Sky::VertexShader = R"(
#version 130
#version 330
#extension GL_ARB_explicit_attrib_location : enable
#extension GL_ARB_uniform_buffer_object : enable
@ -135,7 +137,8 @@ void main() {
})";
const char* Sky::FragmentShader = R"(
#version 130
#version 330
in vec3 Position;
uniform vec4 TopColor;
uniform vec4 BottomColor;
@ -144,15 +147,11 @@ void main() {
outColour = mix(BottomColor, TopColor, clamp(Position.z, 0, 1));
})";
/*
* GLSL 130 is required until the game can run in core context,
* SFML needs to up it's game.
*/
const char* WorldObject::VertexShader = R"(
#version 130
#version 330
#extension GL_ARB_explicit_attrib_location : enable
#extension GL_ARB_uniform_buffer_object : enable
layout(location = 0) in vec3 position;
layout(location = 1) in vec3 normal;
layout(location = 2) in vec4 _colour;
@ -194,13 +193,14 @@ void main()
})";
const char* WorldObject::FragmentShader = R"(
#version 130
#version 330
#extension GL_ARB_uniform_buffer_object : enable
in vec3 Normal;
in vec2 TexCoords;
in vec4 Colour;
in vec4 WorldSpace;
uniform sampler2D texture;
uniform sampler2D tex;
out vec4 fragOut;
layout(std140) uniform SceneData {
@ -236,7 +236,7 @@ float alphaThreshold = (1.0/255.0);
void main()
{
// Only the visibility parameter invokes the screen door.
vec4 diffuse = texture2D(texture, TexCoords);
vec4 diffuse = texture(tex, TexCoords);
if(visibility <= filterMatrix[int(gl_FragCoord.x)%4][int(gl_FragCoord.y)%4]) discard;
if(diffuse.a <= alphaThreshold) discard;
vec4 colour_dyn = diffuse * colour;
@ -245,12 +245,13 @@ void main()
})";
const char* Particle::FragmentShader = R"(
#version 130
#version 330
#extension GL_ARB_uniform_buffer_object : enable
in vec3 Normal;
in vec2 TexCoords;
in vec4 Colour;
uniform sampler2D texture;
uniform sampler2D tex;
out vec4 outColour;
layout(std140) uniform SceneData {
@ -276,7 +277,7 @@ layout(std140) uniform ObjectData {
void main()
{
vec4 c = texture2D(texture, TexCoords);
vec4 c = texture(tex, TexCoords);
c.a = clamp(0, length(c.rgb/length(vec3(1,1,1))), 1);
if(c.a <= ALPHA_DISCARD_THRESHOLD) discard;
float fogZ = (gl_FragCoord.z / gl_FragCoord.w);
@ -287,7 +288,7 @@ void main()
const char* ScreenSpaceRect::VertexShader = R"(
#version 130
#version 330
#extension GL_ARB_explicit_attrib_location : enable
#extension GL_ARB_uniform_buffer_object : enable
@ -304,22 +305,23 @@ void main()
})";
const char* ScreenSpaceRect::FragmentShader = R"(
#version 130
#version 330
in vec2 TexCoords;
in vec4 Colour;
uniform vec4 colour;
uniform sampler2D texture;
uniform sampler2D tex;
out vec4 outColour;
void main()
{
vec4 c = texture2D(texture, TexCoords);
vec4 c = texture(tex, TexCoords);
// Set colour to 0, 0, 0, 1 for textured mode.
outColour = vec4(colour.rgb + c.rgb, colour.a * c.a);
})";
const char* DefaultPostProcess::VertexShader = R"(
#version 130
#version 330
#extension GL_ARB_explicit_attrib_location : enable
#extension GL_ARB_uniform_buffer_object : enable
@ -332,9 +334,9 @@ void main()
})";
const char* DefaultPostProcess::FragmentShader = R"(
#version 130
in vec2 TexCoords;
#version 330
in vec2 TexCoords;
uniform sampler2D colour;
uniform sampler2D data;
@ -342,7 +344,7 @@ out vec4 outColour;
void main()
{
vec4 c = texture2D(colour, TexCoords);
vec4 c = texture(colour, TexCoords);
outColour = c;
})";

View File

@ -7,7 +7,7 @@
#include <objects/CharacterObject.hpp>
const char* MapVertexShader = R"(
#version 130
#version 330
#extension GL_ARB_explicit_attrib_location : enable
#extension GL_ARB_uniform_buffer_object : enable
@ -26,7 +26,8 @@ void main()
})";
const char* MapFragmentShader = R"(
#version 130
#version 330
in vec2 TexCoord;
uniform vec4 colour;
uniform sampler2D spriteTexture;

View File

@ -57,7 +57,7 @@ glm::vec4 indexToCoord(int font, int index)
const char* TextVertexShader = R"(
#version 130
#version 330
#extension GL_ARB_explicit_attrib_location : enable
#extension GL_ARB_uniform_buffer_object : enable
@ -78,7 +78,8 @@ void main()
})";
const char* TextFragmentShader = R"(
#version 130
#version 330
in vec2 TexCoord;
in vec3 Colour;
uniform vec4 colour;

View File

@ -3,8 +3,8 @@ add_executable(rwgame
RWGame.cpp
GameConfig.hpp
GameConfig.cpp
GameWindow.cpp
State.cpp
@ -23,17 +23,18 @@ add_executable(rwgame
include_directories(SYSTEM
${BULLET_INCLUDE_DIR}
${SFML_INCLUDE_DIR}
)
include_directories(
"${CMAKE_SOURCE_DIR}/rwengine/include"
)
target_link_libraries( rwgame
target_link_libraries(rwgame
rwengine
inih
${SFML_LIBRARIES}
${OPENGL_LIBRARIES}
${BULLET_LIBRARIES})
${BULLET_LIBRARIES}
${SDL2_LIBRARIES}
${PNG_LIBRARIES}
)
install(TARGETS rwgame RUNTIME DESTINATION bin)

101
rwgame/GameWindow.cpp Normal file
View File

@ -0,0 +1,101 @@
#include <png.h>
#include <fstream>
#include <core/Logger.hpp>
#include "GameWindow.hpp"
GameWindow::GameWindow() :
window(nullptr), glcontext(nullptr)
{
}
void GameWindow::create(size_t w, size_t h, bool fullscreen)
{
uint32_t style = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE;
if (fullscreen)
style |= SDL_WINDOW_FULLSCREEN;
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
window = SDL_CreateWindow("RWGame", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, style);
glcontext = SDL_GL_CreateContext(window);
}
void GameWindow::close()
{
SDL_GL_DeleteContext(glcontext);
SDL_DestroyWindow(window);
window = nullptr;
}
void GameWindow::showCursor()
{
SDL_SetRelativeMouseMode(SDL_FALSE);
SDL_ShowCursor(SDL_ENABLE);
SDL_SetWindowGrab(window, SDL_FALSE);
}
void GameWindow::hideCursor()
{
SDL_SetRelativeMouseMode(SDL_TRUE);
SDL_ShowCursor(SDL_DISABLE);
SDL_SetWindowGrab(window, SDL_TRUE);
}
void GameWindow::captureToFile(const std::string& path, GameRenderer* renderer)
{
Logger log;
std::ofstream file(path);
if (!file.is_open()) {
log.error("Game", "Could not open screenshot file!");
return;
}
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
nullptr, nullptr, nullptr);
png_infop info_ptr = png_create_info_struct(png_ptr);
png_set_write_fn(png_ptr, &file, {
[](png_structp png_ptr, png_bytep data, png_size_t length) {
reinterpret_cast<std::ofstream*>(png_get_io_ptr(png_ptr))->
write(reinterpret_cast<char*>(data), length);
} }, { [](png_structp) { } } );
auto size = getSize();
png_set_IHDR(png_ptr, info_ptr, size.x, size.y, 8, PNG_COLOR_TYPE_RGB,
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
png_write_info(png_ptr, info_ptr);
unsigned char* buffer = renderer->getRenderer()->readPixels(size);
for (int y = size.y-1; y >= 0; y--)
png_write_row(png_ptr, &buffer[y * size.x * 3]);
png_write_end(png_ptr, nullptr);
png_destroy_write_struct(&png_ptr, &info_ptr);
delete[] buffer;
}
glm::ivec2 GameWindow::getSize() const
{
int x, y;
SDL_GL_GetDrawableSize(window, &x, &y);
return glm::ivec2(x, y);
}

40
rwgame/GameWindow.hpp Normal file
View File

@ -0,0 +1,40 @@
#ifndef GAMEWINDOW_HPP
#define GAMEWINDOW_HPP
#include <string>
#include <SDL2/SDL.h>
#include <glm/vec2.hpp>
#include <render/GameRenderer.hpp>
class GameWindow
{
SDL_Window* window;
SDL_GLContext glcontext;
public:
GameWindow();
void create(size_t w, size_t h, bool fullscreen);
void close();
void showCursor();
void hideCursor();
void captureToFile(const std::string& path, GameRenderer* renderer);
glm::ivec2 getSize() const;
void swap() const
{
SDL_GL_SwapWindow(window);
}
bool isOpen() const
{
return !!window;
}
};
#endif

View File

@ -26,6 +26,8 @@
#include <objects/CharacterObject.hpp>
#include <objects/VehicleObject.hpp>
#define MOUSE_SENSITIVITY_SCALE 2.5f
DebugDraw* debug;
StdOutReciever logPrinter;
@ -84,18 +86,12 @@ RWGame::RWGame(int argc, char* argv[])
benchFile = argv[i+1];
}
}
sf::Uint32 style = sf::Style::Default;
if( fullscreen )
{
style |= sf::Style::Fullscreen;
}
sf::ContextSettings cs;
cs.depthBits = 32;
cs.stencilBits = 8;
window.create(sf::VideoMode(w, h), "", style, cs);
if (SDL_Init(SDL_INIT_VIDEO) < 0)
throw std::runtime_error("Failed to initialize SDL2!");
window.create(w, h, fullscreen);
window.hideCursor();
log.addReciever(&logPrinter);
log.info("Game", "Game directory: " + config.getGameDataPath());
@ -306,21 +302,33 @@ int RWGame::run()
RW_PROFILE_FRAME_BOUNDARY();
RW_PROFILE_BEGIN("Input");
sf::Event event;
while (window.pollEvent(event)) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case sf::Event::GainedFocus:
inFocus = true;
case SDL_QUIT:
window.close();
break;
case sf::Event::LostFocus:
inFocus = false;
case SDL_WINDOWEVENT:
switch (event.window.event) {
case SDL_WINDOWEVENT_FOCUS_GAINED:
inFocus = true;
break;
case SDL_WINDOWEVENT_FOCUS_LOST:
inFocus = false;
break;
}
break;
case sf::Event::KeyPressed:
case SDL_KEYDOWN:
globalKeyEvent(event);
break;
case sf::Event::Closed:
return 0;
default: break;
case SDL_MOUSEMOTION:
event.motion.xrel *= MOUSE_SENSITIVITY_SCALE;
event.motion.yrel *= MOUSE_SENSITIVITY_SCALE;
break;
}
RW_PROFILE_BEGIN("State");
@ -328,11 +336,6 @@ int RWGame::run()
RW_PROFILE_END()
}
RW_PROFILE_END();
if(! window.isOpen() )
{
break;
}
auto now = clock.now();
float timer = std::chrono::duration<float>(now - last_clock_time).count();
@ -383,7 +386,7 @@ int RWGame::run()
renderProfile();
window.display();
window.swap();
}
if( httpserver_thread )
@ -480,10 +483,10 @@ void RWGame::render(float alpha, float time)
lastDraws = getRenderer()->getRenderer()->getDrawCount();
getRenderer()->getRenderer()->swap();
auto size = getWindow().getSize();
renderer->setViewport(size.x, size.y);
glm::ivec2 windowSize = window.getSize();
renderer->setViewport(windowSize.x, windowSize.y);
ViewCamera viewCam;
viewCam.frustum.fov = glm::radians(90.f);
if( state->currentCutscene != nullptr && state->cutsceneStartTime >= 0.f )
@ -535,8 +538,8 @@ void RWGame::render(float alpha, float time)
viewCam.rotation = glm::slerp(lastCam.rotation, nextCam.rotation, alpha);
}
viewCam.frustum.aspectRatio = window.getSize().x / (float) window.getSize().y;
viewCam.frustum.aspectRatio = windowSize.x / static_cast<float>(windowSize.y);
if ( state->isCinematic )
{
viewCam.frustum.fov *= viewCam.frustum.aspectRatio;
@ -777,40 +780,38 @@ void RWGame::renderProfile()
#endif
}
void RWGame::globalKeyEvent(const sf::Event& event)
void RWGame::globalKeyEvent(const SDL_Event& event)
{
switch (event.key.code) {
case sf::Keyboard::LBracket:
switch (event.key.keysym.sym) {
case SDLK_LEFTBRACKET:
state->basic.gameMinute -= 30.f;
break;
case sf::Keyboard::RBracket:
case SDLK_RIGHTBRACKET:
state->basic.gameMinute += 30.f;
break;
case sf::Keyboard::Num9:
case SDLK_9:
timescale *= 0.5f;
break;
case sf::Keyboard::Num0:
case SDLK_0:
timescale *= 2.0f;
break;
case sf::Keyboard::F1:
case SDLK_F1:
showDebugStats = ! showDebugStats;
break;
case sf::Keyboard::F2:
case SDLK_F2:
showDebugPaths = ! showDebugPaths;
break;
case sf::Keyboard::F3:
case SDLK_F3:
showDebugPhysics = ! showDebugPhysics;
break;
case sf::Keyboard::F12: {
case SDLK_F12: {
auto homedir = getenv("HOME");
if( homedir == nullptr ) {
std::cerr << "Unable to determine home directory for screenshot" << std::endl;
break;
}
std::string savePath(homedir);
std::string path = savePath+"/screenshot.png";
window.capture().saveToFile(path);
window.captureToFile(std::string(homedir) + "/screenshot.png", renderer);
break;
}
default: break;

View File

@ -10,8 +10,9 @@
#include "game.hpp"
#include "GameConfig.hpp"
#include "GameWindow.hpp"
#include <SFML/Graphics.hpp>
#include <SDL2/SDL.h>
class PlayerController;
class HttpServer;
@ -31,7 +32,7 @@ class RWGame
bool debugScript;
HttpServer* httpserver = nullptr;
std::thread* httpserver_thread = nullptr;
sf::RenderWindow window;
GameWindow window;
std::chrono::steady_clock clock;
std::chrono::steady_clock::time_point last_clock_time;
@ -76,7 +77,7 @@ public:
return renderer;
}
sf::RenderWindow& getWindow()
GameWindow& getWindow()
{
return window;
}
@ -139,7 +140,7 @@ private:
void renderDebugPaths(float time);
void renderProfile();
void globalKeyEvent(const sf::Event& event);
void globalKeyEvent(const SDL_Event& event);
};
#endif

View File

@ -4,6 +4,39 @@
// This serves as the "initial" camera position.
ViewCamera defaultView({-250.f, -550.f, 75.f}, glm::angleAxis(glm::radians(5.f), glm::vec3(0.f, 1.f, 0.f)));
void State::handleEvent(const SDL_Event& e)
{
auto m = getCurrentMenu();
if(!m) return;
switch(e.type) {
case SDL_MOUSEBUTTONUP:
if (e.button.button == SDL_BUTTON_LEFT)
m->click(e.button.x, e.button.y);
break;
case SDL_MOUSEMOTION:
m->hover(e.motion.x, e.motion.y);
break;
case SDL_KEYDOWN:
switch (e.key.keysym.sym) {
case SDLK_UP:
m->move(-1);
break;
case SDLK_DOWN:
m->move(1);
break;
case SDLK_RETURN:
m->activate();
break;
}
}
}
const ViewCamera& State::getCamera()
{
return defaultView;
@ -19,7 +52,7 @@ GameWorld* State::getWorld()
return game->getWorld();
}
sf::RenderWindow& State::getWindow()
GameWindow& State::getWindow()
{
return game->getWindow();
}

View File

@ -2,11 +2,11 @@
#define _GAME_STATE_HPP_
#include <functional>
#include <queue>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <render/ViewCamera.hpp>
#include "MenuSystem.hpp"
#include <glm/gtc/quaternion.hpp>
#include <SDL2/SDL.h>
#include "GameWindow.hpp"
class RWGame;
class GameWorld;
@ -56,34 +56,8 @@ struct State
}
return currentMenu;
}
virtual void handleEvent(const sf::Event& e)
{
auto m = getCurrentMenu();
if(! m) return;
switch(e.type) {
case sf::Event::MouseButtonReleased:
m->click(e.mouseButton.x, e.mouseButton.y);
break;
case sf::Event::MouseMoved:
m->hover(e.mouseMove.x, e.mouseMove.y);
break;
case sf::Event::KeyPressed:
switch(e.key.code) {
default: break;
case sf::Keyboard::Up:
m->move(-1);
break;
case sf::Keyboard::Down:
m->move(1);
break;
case sf::Keyboard::Return:
m->activate();
break;
}
default: break;
};
}
virtual void handleEvent(const SDL_Event& e);
virtual const ViewCamera& getCamera();
@ -94,7 +68,7 @@ struct State
virtual bool shouldWorldUpdate();
GameWorld* getWorld();
sf::RenderWindow& getWindow();
GameWindow& getWindow();
};
struct StateManager

View File

@ -12,6 +12,8 @@ BenchmarkState::BenchmarkState(RWGame* game, const std::string& benchfile)
void BenchmarkState::enter()
{
getWindow().hideCursor();
std::ifstream benchstream(benchfile);
unsigned int clockHour;
@ -102,7 +104,7 @@ void BenchmarkState::draw(GameRenderer* r)
State::draw(r);
}
void BenchmarkState::handleEvent(const sf::Event &e)
void BenchmarkState::handleEvent(const SDL_Event& e)
{
State::handleEvent(e);
}

View File

@ -1,6 +1,7 @@
#ifndef _RWGAME_BENCHMARKSTATE_HPP_
#define _RWGAME_BENCHMARKSTATE_HPP_
#include <SDL2/SDL_events.h>
#include "State.hpp"
class BenchmarkState : public State
@ -28,7 +29,7 @@ public:
virtual void tick(float dt);
virtual void draw(GameRenderer* r);
virtual void handleEvent(const sf::Event& event);
virtual void handleEvent(const SDL_Event& event);
const ViewCamera& getCamera();
};

View File

@ -315,6 +315,7 @@ DebugState::DebugState(RWGame* game, const glm::vec3& vp, const glm::quat& vd)
void DebugState::enter()
{
getWindow().showCursor();
}
void DebugState::exit()
@ -352,26 +353,11 @@ void DebugState::tick(float dt)
}
}*/
if( _freeLook ) {
float qpi = glm::half_pi<float>();
sf::Vector2i screenCenter{sf::Vector2i{getWindow().getSize()} / 2};
sf::Vector2i mousePos = sf::Mouse::getPosition(getWindow());
sf::Vector2i deltaMouse = mousePos - screenCenter;
sf::Mouse::setPosition(screenCenter, getWindow());
_debugLook.x -= deltaMouse.x / 100.0f;
_debugLook.y += deltaMouse.y / 100.0f;
if (_debugLook.y > qpi)
_debugLook.y = qpi;
else if (_debugLook.y < -qpi)
_debugLook.y = -qpi;
if (_freeLook) {
_debugCam.rotation = glm::angleAxis(_debugLook.x, glm::vec3(0.f, 0.f, 1.f))
* glm::angleAxis(_debugLook.y, glm::vec3(0.f, 1.f, 0.f));
* glm::angleAxis(_debugLook.y, glm::vec3(0.f, 1.f, 0.f));
_debugCam.position += _debugCam.rotation * _movement * dt * (_sonicMode ? 100.f : 10.f);
_debugCam.position += _debugCam.rotation * _movement * dt * (_sonicMode ? 1000.f : 100.f);
}
}
@ -392,56 +378,89 @@ void DebugState::draw(GameRenderer* r)
State::draw(r);
}
void DebugState::handleEvent(const sf::Event &e)
void DebugState::handleEvent(const SDL_Event& event)
{
switch(e.type) {
case sf::Event::KeyPressed:
switch(e.key.code) {
switch(event.type) {
case SDL_KEYDOWN:
switch(event.key.keysym.sym) {
default: break;
case sf::Keyboard::Escape:
case SDLK_ESCAPE:
StateManager::get().exit();
break;
case sf::Keyboard::W:
case SDLK_w:
_movement.x = 1.f;
break;
case sf::Keyboard::S:
case SDLK_s:
_movement.x =-1.f;
break;
case sf::Keyboard::A:
case SDLK_a:
_movement.y = 1.f;
break;
case sf::Keyboard::D:
case SDLK_d:
_movement.y =-1.f;
break;
case sf::Keyboard::F:
case SDLK_f:
_freeLook = !_freeLook;
if (_freeLook)
getWindow().hideCursor();
else
getWindow().showCursor();
break;
case sf::Keyboard::LShift:
_sonicMode = true;
break;
case sf::Keyboard::P:
case SDLK_p:
printCameraDetails();
break;
}
switch (event.key.keysym.mod) {
case KMOD_LSHIFT:
_sonicMode = true;
break;
default: break;
}
break;
case sf::Event::KeyReleased:
switch(e.key.code) {
case sf::Keyboard::W:
case sf::Keyboard::S:
case SDL_KEYUP:
switch(event.key.keysym.sym) {
case SDLK_w:
case SDLK_s:
_movement.x = 0.f;
break;
case sf::Keyboard::A:
case sf::Keyboard::D:
case SDLK_a:
case SDLK_d:
_movement.y = 0.f;
break;
case sf::Keyboard::LShift:
}
switch (event.key.keysym.mod) {
case KMOD_LSHIFT:
_sonicMode = false;
break;
default: break;
}
break;
case SDL_MOUSEMOTION:
if (game->hasFocus())
{
glm::ivec2 screenSize = getWindow().getSize();
glm::vec2 mouseMove(event.motion.xrel / static_cast<float>(screenSize.x),
event.motion.yrel / static_cast<float>(screenSize.y));
if (!_invertedY)
mouseMove.y = -mouseMove.y;
_debugLook.x -= mouseMove.x;
float qpi = glm::half_pi<float>();
_debugLook.y -= glm::clamp(mouseMove.y, -qpi, qpi);
}
break;
default: break;
}
State::handleEvent(e);
State::handleEvent(event);
}
void DebugState::printCameraDetails()

View File

@ -1,6 +1,7 @@
#ifndef DEBUGSTATE_HPP
#define DEBUGSTATE_HPP
#include <SDL2/SDL_events.h>
#include "State.hpp"
class DebugState : public State
@ -10,12 +11,14 @@ class DebugState : public State
glm::vec2 _debugLook;
bool _freeLook;
bool _sonicMode;
bool _invertedY;
Menu* createDebugMenu();
Menu* createMapMenu();
Menu* createVehicleMenu();
Menu* createAIMenu();
Menu* createWeaponMenu();
public:
DebugState(RWGame* game, const glm::vec3& vp = {}, const glm::quat& vd = {});
@ -25,7 +28,7 @@ public:
virtual void tick(float dt);
virtual void draw(GameRenderer* r);
virtual void handleEvent(const sf::Event& event);
virtual void handleEvent(const SDL_Event& event);
void printCameraDetails();

View File

@ -1,8 +1,6 @@
#ifndef GAME_HPP
#define GAME_HPP
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <objects/GameObject.hpp>
#include <engine/GameWorld.hpp>

View File

@ -105,7 +105,7 @@ void IngameState::enter()
started = true;
}
game->getWindow().setMouseCursorVisible(false);
getWindow().hideCursor();
}
void IngameState::exit()
@ -120,27 +120,6 @@ void IngameState::tick(float dt)
auto player = game->getPlayer();
if( player && player->isInputEnabled() )
{
sf::Vector2f screenSize(getWindow().getSize());
sf::Vector2f screenCenter(screenSize / 2.f);
sf::Vector2f mouseMove;
if (game->hasFocus())
{
sf::Vector2f mousePos(sf::Mouse::getPosition(getWindow()));
sf::Vector2f deltaMouse = (mousePos - screenCenter);
mouseMove = sf::Vector2f(deltaMouse.x / screenSize.x, deltaMouse.y / screenSize.y);
sf::Mouse::setPosition(sf::Vector2i(screenCenter), getWindow());
if(deltaMouse.x != 0 || deltaMouse.y != 0)
{
autolookTimer = kAutoLookTime;
if (!m_invertedY) {
mouseMove.y = -mouseMove.y;
}
m_cameraAngles += glm::vec2(mouseMove.x, mouseMove.y);
m_cameraAngles.y = glm::clamp(m_cameraAngles.y, kCameraPitchLimit, glm::pi<float>() - kCameraPitchLimit);
}
}
float viewDistance = 4.f;
switch( camMode )
{
@ -311,51 +290,52 @@ void IngameState::draw(GameRenderer* r)
State::draw(r);
}
void IngameState::handleEvent(const sf::Event &event)
void IngameState::handleEvent(const SDL_Event& event)
{
auto player = game->getPlayer();
switch(event.type) {
case sf::Event::KeyPressed:
switch(event.key.code) {
case sf::Keyboard::Escape:
case SDL_KEYDOWN:
switch(event.key.keysym.sym) {
case SDLK_ESCAPE:
StateManager::get().enter(new PauseState(game));
break;
case sf::Keyboard::M:
case SDLK_m:
StateManager::get().enter(new DebugState(game, _look.position, _look.rotation));
break;
case sf::Keyboard::Space:
case SDLK_SPACE:
if( getWorld()->state->currentCutscene )
{
getWorld()->state->skipCutscene = true;
}
break;
case sf::Keyboard::C:
case SDLK_c:
camMode = CameraMode((camMode+(CameraMode)1)%CAMERA_MAX);
break;
case sf::Keyboard::W:
case SDLK_w:
_movement.x = 1.f;
break;
case sf::Keyboard::S:
case SDLK_s:
_movement.x =-1.f;
break;
case sf::Keyboard::A:
case SDLK_a:
_movement.y = 1.f;
break;
case sf::Keyboard::D:
case SDLK_d:
_movement.y =-1.f;
break;
default: break;
}
break;
case sf::Event::KeyReleased:
switch(event.key.code) {
case sf::Keyboard::W:
case sf::Keyboard::S:
case SDL_KEYUP:
switch(event.key.keysym.sym) {
case SDLK_w:
case SDLK_s:
_movement.x = 0.f;
break;
case sf::Keyboard::A:
case sf::Keyboard::D:
case SDLK_a:
case SDLK_d:
_movement.y = 0.f;
break;
default: break;
@ -371,13 +351,13 @@ void IngameState::handleEvent(const sf::Event &event)
State::handleEvent(event);
}
void IngameState::handlePlayerInput(const sf::Event& event)
void IngameState::handlePlayerInput(const SDL_Event& event)
{
auto player = game->getPlayer();
switch(event.type) {
case sf::Event::KeyPressed:
switch(event.key.code) {
case sf::Keyboard::Space:
case SDL_KEYDOWN:
switch(event.key.keysym.sym) {
case SDLK_SPACE:
if( player->getCharacter()->getCurrentVehicle() ) {
player->getCharacter()->getCurrentVehicle()->setHandbraking(true);
}
@ -386,10 +366,7 @@ void IngameState::handlePlayerInput(const sf::Event& event)
player->jump();
}
break;
case sf::Keyboard::LShift:
player->setRunning(true);
break;
case sf::Keyboard::F:
case SDLK_f:
if( player->getCharacter()->getCurrentVehicle()) {
player->exitVehicle();
}
@ -404,36 +381,56 @@ void IngameState::handlePlayerInput(const sf::Event& event)
player->enterNearestVehicle();
}
break;
case SDLK_LSHIFT:
player->setRunning(true);
break;
default:
break;
}
break;
case sf::Event::KeyReleased:
switch(event.key.code) {
case sf::Keyboard::LShift:
break;
case SDL_KEYUP:
switch (event.key.keysym.sym) {
case SDLK_LSHIFT:
player->setRunning(false);
break;
default: break;
}
break;
case sf::Event::MouseButtonPressed:
switch(event.mouseButton.button) {
case sf::Mouse::Left:
break;
case SDL_MOUSEBUTTONDOWN:
switch(event.button.button) {
case SDL_BUTTON_LEFT:
player->getCharacter()->useItem(true, true);
break;
default: break;
}
break;
case sf::Event::MouseButtonReleased:
switch(event.mouseButton.button) {
case sf::Mouse::Left:
case SDL_MOUSEBUTTONUP:
switch(event.button.button) {
case SDL_BUTTON_LEFT:
player->getCharacter()->useItem(false, true);
break;
default: break;
}
break;
case sf::Event::MouseWheelMoved:
player->getCharacter()->cycleInventory(event.mouseWheel.delta > 0);
case SDL_MOUSEWHEEL:
player->getCharacter()->cycleInventory(event.wheel.y > 0);
break;
case SDL_MOUSEMOTION:
if (game->hasFocus())
{
glm::ivec2 screenSize = getWindow().getSize();
glm::vec2 mouseMove(event.motion.xrel / static_cast<float>(screenSize.x),
event.motion.yrel / static_cast<float>(screenSize.y));
autolookTimer = kAutoLookTime;
if (!m_invertedY) {
mouseMove.y = -mouseMove.y;
}
m_cameraAngles += glm::vec2(mouseMove.x, mouseMove.y);
m_cameraAngles.y = glm::clamp(m_cameraAngles.y, kCameraPitchLimit, glm::pi<float>() - kCameraPitchLimit);
}
break;
default:
break;

View File

@ -1,6 +1,7 @@
#ifndef INGAMESTATE_HPP
#define INGAMESTATE_HPP
#include <SDL2/SDL_events.h>
#include "State.hpp"
class PlayerController;
@ -52,9 +53,9 @@ public:
virtual void tick(float dt);
virtual void draw(GameRenderer* r);
virtual void handleEvent(const sf::Event& event);
virtual void handlePlayerInput(const sf::Event& event);
virtual void handleEvent(const SDL_Event& event);
virtual void handlePlayerInput(const SDL_Event& event);
virtual bool shouldWorldUpdate();
const ViewCamera& getCamera();

View File

@ -16,6 +16,7 @@ void LoadingState::enter()
}
game->newGame();
getWindow().hideCursor();
}
void LoadingState::exit()
@ -41,7 +42,7 @@ void LoadingState::setNextState(State* nextState)
next = nextState;
}
void LoadingState::handleEvent(const sf::Event &e)
void LoadingState::handleEvent(const SDL_Event& e)
{
State::handleEvent(e);
}

View File

@ -1,6 +1,7 @@
#ifndef LOADINGSTATE_HPP
#define LOADINGSTATE_HPP
#include <SDL2/SDL_events.h>
#include "State.hpp"
class LoadingState : public State
@ -20,7 +21,7 @@ public:
virtual bool shouldWorldUpdate();
virtual void handleEvent(const sf::Event& event);
virtual void handleEvent(const SDL_Event& event);
};
#endif // LOADINGSTATE_HPP

View File

@ -49,7 +49,7 @@ void MenuState::enterLoadMenu()
void MenuState::enter()
{
game->getWindow().setMouseCursorVisible(true);
getWindow().showCursor();
}
void MenuState::exit()
@ -62,12 +62,12 @@ void MenuState::tick(float dt)
}
void MenuState::handleEvent(const sf::Event &e)
void MenuState::handleEvent(const SDL_Event& e)
{
switch(e.type) {
case sf::Event::KeyPressed:
switch(e.key.code) {
case sf::Keyboard::Escape:
case SDL_KEYUP:
switch(e.key.keysym.sym) {
case SDLK_ESCAPE:
StateManager::get().exit();
default: break;
}

View File

@ -1,6 +1,7 @@
#ifndef MENUSTATE_HPP
#define MENUSTATE_HPP
#include <SDL2/SDL_events.h>
#include "State.hpp"
class MenuState : public State
@ -16,7 +17,7 @@ public:
virtual void enterMainMenu();
virtual void enterLoadMenu();
virtual void handleEvent(const sf::Event& event);
virtual void handleEvent(const SDL_Event& event);
};
#endif // MENUSTATE_HPP

View File

@ -18,7 +18,7 @@ void PauseState::enter()
{
getWorld()->setPaused(true);
game->getWindow().setMouseCursorVisible(true);
getWindow().showCursor();
}
void PauseState::exit()
@ -34,25 +34,25 @@ void PauseState::tick(float dt)
void PauseState::draw(GameRenderer* r)
{
MapRenderer::MapInfo map;
auto& vp = r->getRenderer()->getViewport();
map.worldSize = 4000.f;
map.clipToSize = false;
map.screenPosition = glm::vec2(vp.x/2, vp.y/2);
map.screenSize = std::max(vp.x, vp.y);
game->getRenderer()->map.draw(getWorld(), map);
State::draw(r);
}
void PauseState::handleEvent(const sf::Event &e)
void PauseState::handleEvent(const SDL_Event& e)
{
switch(e.type) {
case sf::Event::KeyPressed:
switch(e.key.code) {
case sf::Keyboard::Escape:
case SDL_KEYDOWN:
switch(e.key.keysym.sym) {
case SDLK_ESCAPE:
StateManager::get().exit();
break;
default: break;

View File

@ -1,6 +1,7 @@
#ifndef PAUSESTATE_HPP
#define PAUSESTATE_HPP
#include <SDL2/SDL_events.h>
#include "State.hpp"
class PauseState : public State
@ -15,7 +16,7 @@ public:
virtual void draw(GameRenderer* r);
virtual void handleEvent(const sf::Event& event);
virtual void handleEvent(const SDL_Event& event);
};
#endif // PAUSESTATE_HPP

View File

@ -49,6 +49,7 @@ set(TEST_SOURCES
# Hack in rwgame sources until there's a per-target test suite
"${CMAKE_SOURCE_DIR}/rwgame/GameConfig.cpp"
"${CMAKE_SOURCE_DIR}/rwgame/GameWindow.cpp"
)
ADD_DEFINITIONS(-DBOOST_TEST_DYN_LINK)
@ -62,15 +63,15 @@ include_directories(
"${CMAKE_SOURCE_DIR}/rwgame")
include_directories(SYSTEM
${BULLET_INCLUDE_DIR}
${SFML_INCLUDE_DIR})
${BULLET_INCLUDE_DIR})
target_link_libraries(run_tests
rwengine
inih
${OPENGL_LIBRARIES}
${SFML_LIBRARIES}
${BULLET_LIBRARIES}
${SDL2_LIBRARIES}
${PNG_LIBRARIES}
${Boost_UNIT_TEST_FRAMEWORK_LIBRARY})
add_test(UnitTests run_tests)

View File

@ -1,16 +1,14 @@
#ifndef _TESTGLOBABLS_HPP_
#define _TESTGLOBABLS_HPP_
#include <SFML/Window.hpp>
#include <engine/GameWorld.hpp>
#include <engine/GameData.hpp>
#include <engine/GameState.hpp>
#include <core/Logger.hpp>
#include <glm/gtx/string_cast.hpp>
#if RW_TEST_WITH_DATA
#define ENV_GAME_PATH_NAME ("OPENRW_GAME_PATH")
#endif
#include <SDL2/SDL.h>
#include <GameWindow.hpp>
#include <GameConfig.hpp>
std::ostream& operator<<( std::ostream& stream, glm::vec3 const& v );
@ -50,7 +48,7 @@ struct print_log_value<std::nullptr_t> {
class Global
{
public:
sf::Window wnd;
GameWindow window;
#if RW_TEST_WITH_DATA
GameData* d;
GameWorld* e;
@ -60,7 +58,12 @@ public:
#endif
Global() {
wnd.create(sf::VideoMode(640, 360), "Testing");
if (SDL_Init(SDL_INIT_VIDEO) < 0)
throw std::runtime_error("Failed to initialize SDL2!");
window.create(800, 600, false);
window.hideCursor();
#if RW_TEST_WITH_DATA
d = new GameData(&log, &work, getGamePath());
@ -87,7 +90,7 @@ public:
}
~Global() {
wnd.close();
window.close();
#if RW_TEST_WITH_DATA
delete e;
#endif
@ -96,12 +99,10 @@ public:
#if RW_TEST_WITH_DATA
static std::string getGamePath()
{
// TODO: Is this "the way to do it" on windows.
auto v = getenv(ENV_GAME_PATH_NAME);
return v ? v : "";
return GameConfig("openrw.ini").getGameDataPath();
}
#endif
static Global& get()
{
static Global g;
@ -109,4 +110,4 @@ public:
}
};
#endif
#endif