diff --git a/SFML3Example/CMakeLists.txt b/SFML3Example/CMakeLists.txt new file mode 100644 index 0000000..e180972 --- /dev/null +++ b/SFML3Example/CMakeLists.txt @@ -0,0 +1,50 @@ +cmake_minimum_required(VERSION 3.5) + +project(SFML3_example) +SET(PROJECT_NAME SFML3_example) + +if(NOT CMAKE_BUILD_TYPE) + SET(CMAKE_BUILD_TYPE Debug CACHE STRING "Choose the type of build (Debug or Release)" FORCE) +endif() + +SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/modules/") +SET(PROJECT_STATIC_RUNTIME FALSE CACHE BOOL "Use statically linked standard/runtime libraries?") +#SET(PROJECT_STATIC_TMX FALSE CACHE BOOL "Use statically linked tmxlite library?") + +if(CMAKE_COMPILER_IS_GNUCXX OR APPLE) + if(PROJECT_STATIC_RUNTIME) + SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=c++14 -static") + else() + SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=c++14") + endif() +endif() + +SET (CMAKE_CXX_FLAGS_DEBUG "-g -D_DEBUG_") +SET (CMAKE_CXX_FLAGS_RELEASE "-O4 -DNDEBUG") + +find_package(TMXLITE REQUIRED) +find_package(SFML 2 REQUIRED graphics window system) + +include_directories( + ${TMXLITE_INCLUDE_DIR} + ${SFML_INCLUDE_DIR}) + +set(PROJECT_SRC src/main.cpp) + +if(WIN32) + add_executable(${PROJECT_NAME} WIN32 ${PROJECT_SRC}) +else() + add_executable(${PROJECT_NAME} ${PROJECT_SRC}) +endif() + +target_link_libraries(${PROJECT_NAME} + ${TMXLITE_LIBRARIES} + ${SFML_LIBRARIES}) + +#install executable +install(TARGETS ${PROJECT_NAME} + RUNTIME DESTINATION .) + +#install game data +install(DIRECTORY assets + DESTINATION .) diff --git a/SFML3Example/SFMLExample.vcxproj b/SFML3Example/SFMLExample.vcxproj new file mode 100644 index 0000000..7a5c32d --- /dev/null +++ b/SFML3Example/SFMLExample.vcxproj @@ -0,0 +1,156 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {2298a657-d09a-438e-b976-5ee827966124} + SFMLExample + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + + + false + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _CRT_SECURE_NO_WARNINGS;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + ../tmxlite/include;../extlibs/sfml/include + + + Console + true + ../extlibs/sfml/lib;../tmxlite/x64/bin/DebugStatic + libtmxlite-s-d.lib;sfml-system-d.lib;sfml-window-d.lib;sfml-graphics-d.lib;%(AdditionalDependencies) + + + + + Level3 + true + true + true + _CRT_SECURE_NO_WARNINGS;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + ../tmxlite/include;../extlibs/sfml/include + + + Console + true + true + true + ../extlibs/sfml/lib;../tmxlite/x64/bin/ReleaseStatic + libtmxlite-s.lib;sfml-system.lib;sfml-window.lib;sfml-graphics.lib;%(AdditionalDependencies) + + + + + + + + + + + + \ No newline at end of file diff --git a/SFML3Example/SFMLExample.vcxproj.filters b/SFML3Example/SFMLExample.vcxproj.filters new file mode 100644 index 0000000..ac62e2a --- /dev/null +++ b/SFML3Example/SFMLExample.vcxproj.filters @@ -0,0 +1,27 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + + + Header Files + + + \ No newline at end of file diff --git a/SFML3Example/SFMLExample/CMakeLists.txt b/SFML3Example/SFMLExample/CMakeLists.txt new file mode 100644 index 0000000..77a0577 --- /dev/null +++ b/SFML3Example/SFMLExample/CMakeLists.txt @@ -0,0 +1,50 @@ +cmake_minimum_required(VERSION 3.5) + +project(SFML_example) +SET(PROJECT_NAME SFML_example) + +if(NOT CMAKE_BUILD_TYPE) + SET(CMAKE_BUILD_TYPE Debug CACHE STRING "Choose the type of build (Debug or Release)" FORCE) +endif() + +SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/modules/") +SET(PROJECT_STATIC_RUNTIME FALSE CACHE BOOL "Use statically linked standard/runtime libraries?") +#SET(PROJECT_STATIC_TMX FALSE CACHE BOOL "Use statically linked tmxlite library?") + +if(CMAKE_COMPILER_IS_GNUCXX OR APPLE) + if(PROJECT_STATIC_RUNTIME) + SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=c++14 -static") + else() + SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=c++14") + endif() +endif() + +SET (CMAKE_CXX_FLAGS_DEBUG "-g -D_DEBUG_") +SET (CMAKE_CXX_FLAGS_RELEASE "-O4 -DNDEBUG") + +find_package(TMXLITE REQUIRED) +find_package(SFML 2 REQUIRED graphics window system) + +include_directories( + ${TMXLITE_INCLUDE_DIR} + ${SFML_INCLUDE_DIR}) + +set(PROJECT_SRC src/main.cpp) + +if(WIN32) + add_executable(${PROJECT_NAME} WIN32 ${PROJECT_SRC}) +else() + add_executable(${PROJECT_NAME} ${PROJECT_SRC}) +endif() + +target_link_libraries(${PROJECT_NAME} + ${TMXLITE_LIBRARIES} + ${SFML_LIBRARIES}) + +#install executable +install(TARGETS ${PROJECT_NAME} + RUNTIME DESTINATION .) + +#install game data +install(DIRECTORY assets + DESTINATION .) diff --git a/SFML3Example/SFMLExample/SFMLExample.vcxproj b/SFML3Example/SFMLExample/SFMLExample.vcxproj new file mode 100644 index 0000000..7a5c32d --- /dev/null +++ b/SFML3Example/SFMLExample/SFMLExample.vcxproj @@ -0,0 +1,156 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {2298a657-d09a-438e-b976-5ee827966124} + SFMLExample + 10.0 + + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + Application + true + v143 + Unicode + + + Application + false + v143 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + + + false + + + + Level3 + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + true + _CRT_SECURE_NO_WARNINGS;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + ../tmxlite/include;../extlibs/sfml/include + + + Console + true + ../extlibs/sfml/lib;../tmxlite/x64/bin/DebugStatic + libtmxlite-s-d.lib;sfml-system-d.lib;sfml-window-d.lib;sfml-graphics-d.lib;%(AdditionalDependencies) + + + + + Level3 + true + true + true + _CRT_SECURE_NO_WARNINGS;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + ../tmxlite/include;../extlibs/sfml/include + + + Console + true + true + true + ../extlibs/sfml/lib;../tmxlite/x64/bin/ReleaseStatic + libtmxlite-s.lib;sfml-system.lib;sfml-window.lib;sfml-graphics.lib;%(AdditionalDependencies) + + + + + + + + + + + + \ No newline at end of file diff --git a/SFML3Example/SFMLExample/SFMLExample.vcxproj.filters b/SFML3Example/SFMLExample/SFMLExample.vcxproj.filters new file mode 100644 index 0000000..ac62e2a --- /dev/null +++ b/SFML3Example/SFMLExample/SFMLExample.vcxproj.filters @@ -0,0 +1,27 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + + + Header Files + + + \ No newline at end of file diff --git a/SFML3Example/SFMLExample/assets/demo.tmx b/SFML3Example/SFMLExample/assets/demo.tmx new file mode 100644 index 0000000..6691792 --- /dev/null +++ b/SFML3Example/SFMLExample/assets/demo.tmx @@ -0,0 +1,34 @@ + + + + + + + + + + + + + + + + + + + + + eJxjYBg8QAOIGcnQxwvEPEiYj0h9nECsggVzkeGGwQoAJB8BMA== + + + + + eJxjYBg+gBmJzT9grhhcAAAUlAAT + + + + + eJxjYBjaQBCIBaCYG4k9CiAAAGXcAF0= + + + diff --git a/SFML3Example/SFMLExample/assets/images/tilemap/platform.tsx b/SFML3Example/SFMLExample/assets/images/tilemap/platform.tsx new file mode 100644 index 0000000..6a48b12 --- /dev/null +++ b/SFML3Example/SFMLExample/assets/images/tilemap/platform.tsx @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + + diff --git a/SFML3Example/SFMLExample/assets/images/tilemap/tileset.png b/SFML3Example/SFMLExample/assets/images/tilemap/tileset.png new file mode 100644 index 0000000..f13825d Binary files /dev/null and b/SFML3Example/SFMLExample/assets/images/tilemap/tileset.png differ diff --git a/SFML3Example/SFMLExample/assets/images/tilemap/tileset.txt b/SFML3Example/SFMLExample/assets/images/tilemap/tileset.txt new file mode 100644 index 0000000..1676dad --- /dev/null +++ b/SFML3Example/SFMLExample/assets/images/tilemap/tileset.txt @@ -0,0 +1,2 @@ +tileset by Alucard +http://opengameart.org/content/pixel-art-platformer-complete-pack \ No newline at end of file diff --git a/SFML3Example/SFMLExample/assets/images/tilemap/tileset02.png b/SFML3Example/SFMLExample/assets/images/tilemap/tileset02.png new file mode 100644 index 0000000..9f594e4 Binary files /dev/null and b/SFML3Example/SFMLExample/assets/images/tilemap/tileset02.png differ diff --git a/SFML3Example/SFMLExample/assets/platform.tmx b/SFML3Example/SFMLExample/assets/platform.tmx new file mode 100644 index 0000000..2b40b85 --- /dev/null +++ b/SFML3Example/SFMLExample/assets/platform.tmx @@ -0,0 +1,94 @@ + + + + + + + + + + + + eJzt2VsKwyAQBdCsoo8VNN3/BvvbQC1Em9iZOQeE/AQcyFXjLAuc69YxIKuePMgLVckCtNkz4LvrjgGVXHYMqGZPPuSFSuQCPrt3Dshudt/jOfAuZLfOngD8MfmANvkY43ya22P2BIKzvkCbfECbfECb8ykAAABnqNb/qlYvY6rdX0avV77PVe3+Mnq90fMNv/a+Z8gHbK2NZ2B7Jox+PqQO/8rQZi0HAAAAiCf7nWf2+jhW9v5u9vo41sj3E2Ftlg9GjPTMIvTbIswRYKYXswgSvA== + + + + + + + + eJztm9tOw0AMBc0DPAEfAKIY6P9/I6kgqA25NInXa3dnpCOeqJxmZ+89CgCMcfwNAPwHPwCmwQ+AafDDlrvaBYAp+GHLZ5fDIF9VK4I94AfU4qXL60LeqlX3A34ATIMfvrA+yQV++MC6JCf4AdkpOSbjB2RmbFy2HJvxw5bh+2IOlRv8iA9rehueujwO8rzwP/gRk6l5A+PSNua+z7nvEj/KsqXPuhW2jHvRxkr8KMfWPmsN0dqTdnkvlA+/x/ijZT/G+vYM/XuksxSVyzZ8LBxvT1r1g/n9PlR8fJiKlyOt+gHbUKnrhbcj+AFLqPjNn6LNt/AjLyXniCpxnfAcS/ADelRyOeHhCH7ACZX67dvSkdO+99T+5FzO9y7vzz63BaKdFURBpX67tozVecv5Z94Kpzl5/3yHkb/s3V6iUr89Zwi0h0r9dpclUI+18+S58/1r5pAqedfg+NEOKuXuKl07ryb4sYZr+nKL+1kq9d85wY81LJ23WZy9qdCHZ0x2ou/dquBF5mRGJeZvB3pU6r9f0p4fKuv6ZG9H1tZH4iYTKtvbnYcje+ojMZMFlf3PWtIRi/pIvERHxbZPLuGIGtZHYiUiKmXPtKzW7X2dtd8hKZcH2XYf2CrDMzd1fPY9jnjWSeqmxl2HKHcgstRJ/PMNRu5JzA== + + + + + eJzt2+FqwjAUBtDihO2fbnPv/6rbYGUxNOuqbW9izoEgIsqF+jVpczsMAAAAAACkPqILgIq9RxdA1U7RBQSTDyiTjz4dowtoxJr5eP0a52T0PjfX7PDzuuUxajmDY+3mD7byFF3AHcba5YN7nSfGt0PxG/WLzke6FnsLqgFKovNBXVwrXhvnvsswPT9ODed5lvC/YU/5+t45HwCgX+la0HUJsIaWexBga3M9CM+7VAH1SNedf/Ug9JCNUm8G9Mxzm1CmrwfKWsrHXN/RlP/s7S/9TfqR58N9PPiV5+PWZ4n0YPGI1soHPKJL9r7lZ+0AAAAgZQ+Bnr3MfO4eKb3LM5Lujy29Rzq3t3ZLX8VeTkPd9QHU4hO/Kwa1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/SFML3Example/SFMLExample/cmake/modules/FindSFML.cmake b/SFML3Example/SFMLExample/cmake/modules/FindSFML.cmake new file mode 100644 index 0000000..48873ef --- /dev/null +++ b/SFML3Example/SFMLExample/cmake/modules/FindSFML.cmake @@ -0,0 +1,366 @@ +# 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 MATCH ".*#define SFML_VERSION_MAJOR ([0-9]+).*#define SFML_VERSION_MINOR ([0-9]+).*#define SFML_VERSION_PATCH ([0-9]+).*" SFML_CONFIG_HPP_CONTENTS "${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}") + 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() diff --git a/SFML3Example/SFMLExample/cmake/modules/FindTMXLITE.cmake b/SFML3Example/SFMLExample/cmake/modules/FindTMXLITE.cmake new file mode 100644 index 0000000..6ca7154 --- /dev/null +++ b/SFML3Example/SFMLExample/cmake/modules/FindTMXLITE.cmake @@ -0,0 +1,10 @@ +find_path(TMXLITE_INCLUDE_DIR NAMES tmxlite/Config.hpp PATH_SUFFIXES include) + +find_library(TMXLITE_LIBRARY_DEBUG NAMES tmxlite-d) +find_library(TMXLITE_LIBRARY_RELEASE NAMES tmxlite) + +include(SelectLibraryConfigurations) +select_library_configurations(TMXLITE) + +include(FindPackageHandleStandardArgs) +FIND_PACKAGE_HANDLE_STANDARD_ARGS(TMXLITE DEFAULT_MSG TMXLITE_LIBRARY TMXLITE_INCLUDE_DIR) \ No newline at end of file diff --git a/SFML3Example/SFMLExample/meson.build b/SFML3Example/SFMLExample/meson.build new file mode 100644 index 0000000..7b3dd5d --- /dev/null +++ b/SFML3Example/SFMLExample/meson.build @@ -0,0 +1,17 @@ +sfml_graphics_dep = dependency('sfml-graphics') +sfml_window_dep = dependency('sfml-window') +sfml_system_dep = dependency('sfml-system') + +pvt_incdir = include_directories('src') + +executable('SFML_example', + 'src/main.cpp', + install: false, + include_directories: pvt_incdir, + dependencies: [ + tmxlite_dep, + sfml_graphics_dep, + sfml_window_dep, + sfml_system_dep, + ], +) diff --git a/SFML3Example/SFMLExample/readme.md b/SFML3Example/SFMLExample/readme.md new file mode 100644 index 0000000..2069582 --- /dev/null +++ b/SFML3Example/SFMLExample/readme.md @@ -0,0 +1,10 @@ +SFML Example +------------ + +Test program demonstrating how one may possibly implement +an orthogonal map renderer using SFML. + +Note that this example provides very basic rendering, and +does not support all the features supported by tmxlite, +such as tile flipping/rotation or tilesets which are created +as a collection of images \ No newline at end of file diff --git a/SFML3Example/SFMLExample/src/SFMLOrthogonalLayer.hpp b/SFML3Example/SFMLExample/src/SFMLOrthogonalLayer.hpp new file mode 100644 index 0000000..450064b --- /dev/null +++ b/SFML3Example/SFMLExample/src/SFMLOrthogonalLayer.hpp @@ -0,0 +1,651 @@ +/********************************************************************* +(c) Matt Marchant & contributors 2016 - 2024 +http://trederia.blogspot.com + +tmxlite - Zlib license. + +This software is provided 'as-is', without any express or +implied warranty. In no event will the authors be held +liable for any damages arising from the use of this software. + +Permission is granted to anyone to use this software for any purpose, +including commercial applications, and to alter it and redistribute +it freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; +you must not claim that you wrote the original software. +If you use this software in a product, an acknowledgment +in the product documentation would be appreciated but +is not required. + +2. Altered source versions must be plainly marked as such, +and must not be misrepresented as being the original software. + +3. This notice may not be removed or altered from any +source distribution. +*********************************************************************/ + +/* +Creates an SFML drawable from an Orthogonal tmx map layer. +This is an example of drawing with SFML - not all features +are implemented. +*/ + +#ifndef SFML_ORTHO_HPP_ +#define SFML_ORTHO_HPP_ + +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + + +class MapLayer final : public sf::Drawable +{ +public: + + MapLayer(const tmx::Map& map, std::size_t idx) + { + const auto& layers = map.getLayers(); + if (map.getOrientation() != tmx::Orientation::Orthogonal) + { + std::cout << "Map is not orthogonal - nothing will be drawn" << std::endl; + } + else if (idx >= layers.size()) + { + std::cout << "Layer index " << idx << " is out of range, layer count is " << layers.size() << std::endl; + } + else if (layers[idx]->getType() != tmx::Layer::Type::Tile) + { + std::cout << "layer " << idx << " is not a Tile layer..." << std::endl; + } + + else + { + //round the chunk size to the nearest tile + const auto tileSize = map.getTileSize(); + m_chunkSize.x = std::floor(m_chunkSize.x / tileSize.x) * tileSize.x; + m_chunkSize.y = std::floor(m_chunkSize.y / tileSize.y) * tileSize.y; + m_mapTileSize.x = map.getTileSize().x; + m_mapTileSize.y = map.getTileSize().y; + const auto& layer = layers[idx]->getLayerAs(); + createChunks(map, layer); + + auto mapSize = map.getBounds(); + m_globalBounds.size = {mapSize.width, mapSize.height}; + } + } + + ~MapLayer() = default; + MapLayer(const MapLayer&) = delete; + MapLayer& operator = (const MapLayer&) = delete; + + const sf::FloatRect& getGlobalBounds() const { return m_globalBounds; } + + void setTile(std::int32_t tileX, std::int32_t tileY, tmx::TileLayer::Tile tile, bool refresh = true) + { + sf::Vector2u chunkLocale; + const auto& selectedChunk = getChunkAndTransform(tileX, tileY, chunkLocale); + selectedChunk->setTile(chunkLocale.x, chunkLocale.y, tile, refresh); + } + + tmx::TileLayer::Tile getTile(std::int32_t tileX, std::int32_t tileY) + { + sf::Vector2u chunkLocale; + const auto& selectedChunk = getChunkAndTransform(tileX, tileY, chunkLocale); + return selectedChunk->getTile(chunkLocale.x, chunkLocale.y); + } + void setColor(std::int32_t tileX, std::int32_t tileY, sf::Color color, bool refresh = true) + { + sf::Vector2u chunkLocale; + const auto& selectedChunk = getChunkAndTransform(tileX, tileY, chunkLocale); + selectedChunk->setColor(chunkLocale.x, chunkLocale.y, color, refresh); + } + + sf::Color getColor(std::int32_t tileX, std::int32_t tileY) + { + sf::Vector2u chunkLocale; + const auto& selectedChunk = getChunkAndTransform(tileX, tileY, chunkLocale); + return selectedChunk->getColor(chunkLocale.x, chunkLocale.y); + } + + void setOffset(sf::Vector2f offset) { m_offset = offset; } + sf::Vector2f getOffset() const { return m_offset; } + + void update(sf::Time elapsed) + { + for (auto& c : m_visibleChunks) + { + for (AnimationState& as : c->getActiveAnimations()) + { + as.currentTime += elapsed; + + tmx::TileLayer::Tile tile; + std::int32_t animTime = 0; + auto x = as.animTile.animation.frames.begin(); + while (animTime < as.currentTime.asMilliseconds()) + { + if (x == as.animTile.animation.frames.end()) + { + x = as.animTile.animation.frames.begin(); + as.currentTime -= sf::milliseconds(animTime); + animTime = 0; + } + + tile.ID = x->tileID; + animTime += x->duration; + x++; + } + + setTile(as.tileCords.x, as.tileCords.y, tile); + } + } + } + +private: + //increasing m_chunkSize by 4; fixes render problems when mapsize != chunksize + //sf::Vector2f m_chunkSize = sf::Vector2f(1024.f, 1024.f); + sf::Vector2f m_chunkSize = sf::Vector2f(512.f, 512.f); + sf::Vector2u m_chunkCount; + sf::Vector2u m_mapTileSize; // general Tilesize of Map + sf::FloatRect m_globalBounds; + sf::Vector2f m_offset; + + using TextureResource = std::map>; + TextureResource m_textureResource; + + struct AnimationState + { + sf::Vector2u tileCords; + sf::Time startTime; + sf::Time currentTime; + tmx::Tileset::Tile animTile; + std::uint8_t flipFlags; + }; + + class Chunk final : public sf::Transformable, public sf::Drawable + { + public: + using Ptr = std::unique_ptr; + using Tile = std::array; + + Chunk(const tmx::TileLayer& layer, std::vector tilesets, + const sf::Vector2f& position, const sf::Vector2f& tileCount, + const sf::Vector2u& tileSize, std::size_t rowSize, + TextureResource& tr, const std::map& animTiles) + : m_animTiles(animTiles) + { + setPosition(position); + layerOpacity = static_cast(layer.getOpacity() / 1.f * 255.f); + + sf::Color vertColour = sf::Color(200 ,200, 200, layerOpacity); + auto offset = layer.getOffset(); + layerOffset = { static_cast(offset.x), static_cast(offset.y) }; + chunkTileCount = { tileCount.x, tileCount.y }; + mapTileSize = tileSize; + + const auto& tileIDs = layer.getTiles(); + + //go through the tiles and create all arrays (for latter manipulation) + for (const auto& ts : tilesets) + { + if(ts->getImagePath().empty()) + { + tmx::Logger::log("This example does not support Collection of Images tilesets", tmx::Logger::Type::Info); + tmx::Logger::log("Chunks using " + ts->getName() + " will not be created", tmx::Logger::Type::Info); + continue; + } + m_chunkArrays.emplace_back(std::make_unique(*tr.find(ts->getImagePath())->second, *ts)); + } + std::size_t xPos = static_cast(position.x / tileSize.x); + std::size_t yPos = static_cast(position.y / tileSize.y); + for (auto y = yPos; y < yPos + tileCount.y; ++y) + { + for (auto x = xPos; x < xPos + tileCount.x; ++x) + { + auto idx = (y * rowSize + x); + m_chunkTileIDs.emplace_back(tileIDs[idx]); + m_chunkColors.emplace_back(vertColour); + } + } + generateTiles(true); + } + + void generateTiles(bool registerAnimation = false) + { + if (registerAnimation) + { + m_activeAnimations.clear(); + } + for (const auto& ca : m_chunkArrays) + { + std::uint32_t idx = 0; + std::uint32_t xPos = static_cast(getPosition().x / mapTileSize.x); + std::uint32_t yPos = static_cast(getPosition().y / mapTileSize.y); + for (auto y = yPos; y < yPos + chunkTileCount.y; ++y) + { + for (auto x = xPos; x < xPos + chunkTileCount.x; ++x) + { + if (idx < m_chunkTileIDs.size() && m_chunkTileIDs[idx].ID >= ca->m_firstGID + && m_chunkTileIDs[idx].ID <= ca->m_lastGID) + { + if (registerAnimation && m_animTiles.find(m_chunkTileIDs[idx].ID) != m_animTiles.end()) + { + AnimationState as; + as.animTile = m_animTiles[m_chunkTileIDs[idx].ID]; + as.startTime = sf::milliseconds(0); + as.tileCords = sf::Vector2u(x,y); + m_activeAnimations.push_back(as); + } + + sf::Vector2f tileOffset(static_cast(x) * mapTileSize.x, static_cast(y) * mapTileSize.y + mapTileSize.y - ca->tileSetSize.y); + + auto idIndex = m_chunkTileIDs[idx].ID - ca->m_firstGID; + sf::Vector2f tileIndex(sf::Vector2i(idIndex % ca->tsTileCount.x, idIndex / ca->tsTileCount.x)); + tileIndex.x *= ca->tileSetSize.x; + tileIndex.y *= ca->tileSetSize.y; + Tile tile = + { + sf::Vertex(tileOffset - getPosition(), m_chunkColors[idx], tileIndex), + sf::Vertex(tileOffset - getPosition() + sf::Vector2f(static_cast(ca->tileSetSize.x), 0.f), m_chunkColors[idx], tileIndex + sf::Vector2f(static_cast(ca->tileSetSize.x), 0.f)), + sf::Vertex(tileOffset - getPosition() + sf::Vector2f(sf::Vector2u(ca->tileSetSize.x, ca->tileSetSize.y)), m_chunkColors[idx], tileIndex + sf::Vector2f(sf::Vector2u(ca->tileSetSize.x, ca->tileSetSize.y))), + sf::Vertex(tileOffset - getPosition(), m_chunkColors[idx], tileIndex), + sf::Vertex(tileOffset - getPosition() + sf::Vector2f(sf::Vector2u(ca->tileSetSize.x, ca->tileSetSize.y)), m_chunkColors[idx], tileIndex + sf::Vector2f(sf::Vector2u(ca->tileSetSize.x, ca->tileSetSize.y))), + sf::Vertex(tileOffset - getPosition() + sf::Vector2f(0.f,static_cast(ca->tileSetSize.y)), m_chunkColors[idx], tileIndex + sf::Vector2f(0.f, static_cast(ca->tileSetSize.y))) + }; + doFlips(m_chunkTileIDs[idx].flipFlags,&tile[0].texCoords,&tile[1].texCoords,&tile[2].texCoords,&tile[3].texCoords,&tile[4].texCoords,&tile[5].texCoords); + ca->addTile(tile); + } + idx++; + } + } + } + } + ~Chunk() = default; + Chunk(const Chunk&) = delete; + Chunk& operator = (const Chunk&) = delete; + + std::vector& getActiveAnimations() + { + return m_activeAnimations; + } + + tmx::TileLayer::Tile getTile(std::int32_t x, std::int32_t y) const + { + return m_chunkTileIDs[calcIndexFrom(x,y)]; + } + + void setTile(std::int32_t x, std::int32_t y, tmx::TileLayer::Tile tile, bool refresh) + { + m_chunkTileIDs[calcIndexFrom(x,y)] = tile; + maybeRegenerate(refresh); + } + + sf::Color getColor(std::int32_t x, std::int32_t y) const + { + return m_chunkColors[calcIndexFrom(x,y)]; + } + + void setColor(std::int32_t x, std::int32_t y, sf::Color color, bool refresh) + { + m_chunkColors[calcIndexFrom(x,y)] = color; + maybeRegenerate(refresh); + } + + void maybeRegenerate(bool refresh) + { + if (refresh) + { + for (const auto& ca : m_chunkArrays) + { + ca->reset(); + } + generateTiles(); + } + } + + std::int32_t calcIndexFrom(std::int32_t x, std::int32_t y) const + { + return x + y * static_cast(chunkTileCount.x); + } + + bool empty() const + { + return m_chunkArrays.empty(); + } + + void flipY(sf::Vector2f* v0, sf::Vector2f* v1, sf::Vector2f* v2, sf::Vector2f* v3, sf::Vector2f* v4, sf::Vector2f* v5) + { + //Flip Y + sf::Vector2f tmp0 = *v0; + v0->y = v5->y; + v3->y = v5->y; + v5->y = tmp0.y; + sf::Vector2f tmp2 = *v2; + v2->y = v1->y; + v4->y = v1->y; + v1->y = tmp2.y; + } + + void flipX(sf::Vector2f* v0, sf::Vector2f* v1, sf::Vector2f* v2, sf::Vector2f* v3, sf::Vector2f* v4, sf::Vector2f* v5) + { + //Flip X + sf::Vector2f tmp0 = *v0; + v0->x = v1->x; + v3->x = v1->x; + v1->x = tmp0.x; + sf::Vector2f tmp2 = *v2; + v2->x = v5->x; + v4->x = v5->x; + v5->x = tmp2.x; + } + + void flipD(sf::Vector2f* v0, sf::Vector2f* v1, sf::Vector2f* v2, sf::Vector2f* v3, sf::Vector2f* v4, sf::Vector2f* v5) + { + //Diagonal flip + sf::Vector2f tmp2 = *v2; + *v2 = *v4; + *v4 = tmp2; + sf::Vector2f tmp0 = *v0; + *v0 = *v3; + *v3 = tmp0; + sf::Vector2f tmp1 = *v1; + *v1 = *v5; + *v5 = tmp1; + } + + void doFlips(std::uint8_t bits, sf::Vector2f* v0, sf::Vector2f* v1, sf::Vector2f* v2, sf::Vector2f* v3, sf::Vector2f* v4, sf::Vector2f* v5) + { + //0000 = no change + //0100 = vertical = swap y axis + //1000 = horizontal = swap x axis + //1100 = horiz + vert = swap both axes = horiz+vert = rotate 180 degrees + //0010 = diag = rotate 90 degrees right and swap x axis + //0110 = diag+vert = rotate 270 degrees right + //1010 = horiz+diag = rotate 90 degrees right + //1110 = horiz+vert+diag = rotate 90 degrees right and swap y axis + if(!(bits & tmx::TileLayer::FlipFlag::Horizontal) && + !(bits & tmx::TileLayer::FlipFlag::Vertical) && + !(bits & tmx::TileLayer::FlipFlag::Diagonal) ) + { + //Shortcircuit tests for nothing to do + return; + } + else if(!(bits & tmx::TileLayer::FlipFlag::Horizontal) && + (bits & tmx::TileLayer::FlipFlag::Vertical) && + !(bits & tmx::TileLayer::FlipFlag::Diagonal) ) + { + //0100 + flipY(v0,v1,v2,v3,v4,v5); + } + else if((bits & tmx::TileLayer::FlipFlag::Horizontal) && + !(bits & tmx::TileLayer::FlipFlag::Vertical) && + !(bits & tmx::TileLayer::FlipFlag::Diagonal) ) + { + //1000 + flipX(v0,v1,v2,v3,v4,v5); + } + else if((bits & tmx::TileLayer::FlipFlag::Horizontal) && + (bits & tmx::TileLayer::FlipFlag::Vertical) && + !(bits & tmx::TileLayer::FlipFlag::Diagonal) ) + { + //1100 + flipY(v0,v1,v2,v3,v4,v5); + flipX(v0,v1,v2,v3,v4,v5); + } + else if(!(bits & tmx::TileLayer::FlipFlag::Horizontal) && + !(bits & tmx::TileLayer::FlipFlag::Vertical) && + (bits & tmx::TileLayer::FlipFlag::Diagonal) ) + { + //0010 + flipD(v0,v1,v2,v3,v4,v5); + } + else if(!(bits & tmx::TileLayer::FlipFlag::Horizontal) && + (bits & tmx::TileLayer::FlipFlag::Vertical) && + (bits & tmx::TileLayer::FlipFlag::Diagonal) ) + { + //0110 + flipX(v0,v1,v2,v3,v4,v5); + flipD(v0,v1,v2,v3,v4,v5); + } + else if((bits & tmx::TileLayer::FlipFlag::Horizontal) && + !(bits & tmx::TileLayer::FlipFlag::Vertical) && + (bits & tmx::TileLayer::FlipFlag::Diagonal) ) + { + //1010 + flipY(v0,v1,v2,v3,v4,v5); + flipD(v0,v1,v2,v3,v4,v5); + } + else if((bits & tmx::TileLayer::FlipFlag::Horizontal) && + (bits & tmx::TileLayer::FlipFlag::Vertical) && + (bits & tmx::TileLayer::FlipFlag::Diagonal) ) + { + //1110 + flipY(v0,v1,v2,v3,v4,v5); + flipX(v0,v1,v2,v3,v4,v5); + flipD(v0,v1,v2,v3,v4,v5); + } + } + + private: + class ChunkArray final : public sf::Drawable + { + public: + using Ptr = std::unique_ptr; + + tmx::Vector2u tileSetSize; + sf::Vector2u tsTileCount; + std::uint32_t m_firstGID, m_lastGID; + + explicit ChunkArray(const sf::Texture& t, const tmx::Tileset& ts) + : m_texture(t) + { + auto texSize = getTextureSize(); + tileSetSize = ts.getTileSize(); + tsTileCount.x = texSize.x / tileSetSize.x; + tsTileCount.y = texSize.y / tileSetSize.y; + m_firstGID = ts.getFirstGID(); + m_lastGID = ts.getLastGID(); + } + + ~ChunkArray() = default; + ChunkArray(const ChunkArray&) = delete; + ChunkArray& operator = (const ChunkArray&) = delete; + + void reset() + { + m_vertices.clear(); + } + + void addTile(const Chunk::Tile& tile) + { + for (const auto& v : tile) + { + m_vertices.push_back(v); + } + } + + sf::Vector2u getTextureSize() const + { + return m_texture.getSize(); + } + + private: + const sf::Texture& m_texture; + std::vector m_vertices; + void draw(sf::RenderTarget& rt, sf::RenderStates states) const override + { + states.texture = &m_texture; + rt.draw(m_vertices.data(), m_vertices.size(), sf::PrimitiveType::Triangles, states); + } + }; + + std::uint8_t layerOpacity; // opacity of the layer + sf::Vector2f layerOffset; // Layer offset + sf::Vector2u mapTileSize; // general Tilesize of Map + sf::Vector2f chunkTileCount; // chunk tilecount + std::vector m_chunkTileIDs; // stores all tiles in this chunk for later manipulation + std::vector m_chunkColors; // stores colors for extended color effects + std::map m_animTiles; // animation catalogue + std::vector m_activeAnimations; // Animations to be done in this chunk + std::vector m_chunkArrays; + + void draw(sf::RenderTarget& rt, sf::RenderStates states) const override + { + states.transform *= getTransform(); + for (const auto& a : m_chunkArrays) + { + rt.draw(*a, states); + } + } + }; + + std::vector m_chunks; + mutable std::vector m_visibleChunks; + + Chunk::Ptr& getChunkAndTransform(std::int32_t x, std::int32_t y, sf::Vector2u& chunkRelative) + { + std::uint32_t chunkX = (x * m_mapTileSize.x) / static_cast(m_chunkSize.x); + std::uint32_t chunkY = (y * m_mapTileSize.y) / static_cast(m_chunkSize.y); + chunkRelative.x = ((x * m_mapTileSize.x) - chunkX * static_cast(m_chunkSize.x)) / m_mapTileSize.x ; + chunkRelative.y = ((y * m_mapTileSize.y) - chunkY * static_cast(m_chunkSize.y)) / m_mapTileSize.y ; + return m_chunks[chunkX + chunkY * m_chunkCount.x]; + } + + void createChunks(const tmx::Map& map, const tmx::TileLayer& layer) + { + //look up all the tile sets and load the textures + const auto& tileSets = map.getTilesets(); + const auto& layerIDs = layer.getTiles(); + std::uint32_t maxID = std::numeric_limits::max(); + std::vector usedTileSets; + + for (auto i = tileSets.rbegin(); i != tileSets.rend(); ++i) + { + for (const auto& tile : layerIDs) + { + if (tile.ID >= i->getFirstGID() && tile.ID < maxID) + { + usedTileSets.push_back(&(*i)); + break; + } + } + maxID = i->getFirstGID(); + } + + sf::Image fallback({2, 2}, sf::Color::Magenta); + for (const auto& ts : usedTileSets) + { + const auto& path = ts->getImagePath(); + + std::unique_ptr newTexture = std::make_unique(); + sf::Image img; + if (!img.loadFromFile(path)) + { + if (!newTexture->loadFromImage(fallback)) { + throw std::runtime_error("Unable to load fallback image"); + } + } else { + if (ts->hasTransparency()) + { + auto transparency = ts->getTransparencyColour(); + img.createMaskFromColor({ transparency.r, transparency.g, transparency.b, transparency.a }); + } + if (!newTexture->loadFromImage(img)) { + throw std::runtime_error("Unable to load image: " + path); + } + } + m_textureResource.insert(std::make_pair(path, std::move(newTexture))); + } + + //calculate the number of chunks in the layer + //and create each one + const auto bounds = map.getBounds(); + m_chunkCount.x = static_cast(std::ceil(bounds.width / m_chunkSize.x)); + m_chunkCount.y = static_cast(std::ceil(bounds.height / m_chunkSize.y)); + + sf::Vector2u tileSize(map.getTileSize().x, map.getTileSize().y); + + for (auto y = 0u; y < m_chunkCount.y; ++y) + { + sf::Vector2f tileCount(m_chunkSize.x / tileSize.x, m_chunkSize.y / tileSize.y); + for (auto x = 0u; x < m_chunkCount.x; ++x) + { + // calculate size of each Chunk (clip against map) + if ((x + 1) * m_chunkSize.x > bounds.width) + { + tileCount.x = (bounds.width - x * m_chunkSize.x) / map.getTileSize().x; + } + if ((y + 1) * m_chunkSize.y > bounds.height) + { + tileCount.y = (bounds.height - y * m_chunkSize.y) / map.getTileSize().y; + } + //m_chunks.emplace_back(std::make_unique(layer, usedTileSets, + // sf::Vector2f(x * m_chunkSize.x, y * m_chunkSize.y), tileCount, map.getTileCount().x, m_textureResource)); + m_chunks.emplace_back(std::make_unique(layer, usedTileSets, + sf::Vector2f(x * m_chunkSize.x, y * m_chunkSize.y), tileCount, tileSize, map.getTileCount().x, m_textureResource, map.getAnimatedTiles())); + } + } + } + + void updateVisibility(const sf::View& view) const + { + sf::Vector2f viewCorner = view.getCenter(); + viewCorner -= view.getSize() / 2.f; + + std::int32_t posX = static_cast(std::floor(viewCorner.x / m_chunkSize.x)); + std::int32_t posY = static_cast(std::floor(viewCorner.y / m_chunkSize.y)); + std::int32_t posX2 = static_cast(std::ceil((viewCorner.x + view.getSize().x) / m_chunkSize.x)); + std::int32_t posY2 = static_cast(std::ceil((viewCorner.y + view.getSize().x)/ m_chunkSize.y)); + + std::vector visible; + for (auto y = posY; y < posY2; ++y) + { + for (auto x = posX; x < posX2; ++x) + { + std::size_t idx = y * std::int32_t(m_chunkCount.x) + x; + if (idx >= 0u && idx < m_chunks.size() && !m_chunks[idx]->empty()) + { + visible.push_back(m_chunks[idx].get()); + } + } + } + + std::swap(m_visibleChunks, visible); + } + + + void draw(sf::RenderTarget& rt, sf::RenderStates states) const override + { + states.transform.translate(m_offset); + + //calc view coverage and draw nearest chunks + updateVisibility(rt.getView()); + for (const auto& c : m_visibleChunks) + { + rt.draw(*c, states); + } + } +}; + +#endif //SFML_ORTHO_HPP_ diff --git a/SFML3Example/SFMLExample/src/main.cpp b/SFML3Example/SFMLExample/src/main.cpp new file mode 100644 index 0000000..2c66d18 --- /dev/null +++ b/SFML3Example/SFMLExample/src/main.cpp @@ -0,0 +1,95 @@ +/********************************************************************* +(c) Matt Marchant & contributors 2016 - 2024 +http://trederia.blogspot.com + +tmxlite - Zlib license. + +This software is provided 'as-is', without any express or +implied warranty. In no event will the authors be held +liable for any damages arising from the use of this software. + +Permission is granted to anyone to use this software for any purpose, +including commercial applications, and to alter it and redistribute +it freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; +you must not claim that you wrote the original software. +If you use this software in a product, an acknowledgment +in the product documentation would be appreciated but +is not required. + +2. Altered source versions must be plainly marked as such, +and must not be misrepresented as being the original software. + +3. This notice may not be removed or altered from any +source distribution. +*********************************************************************/ + + +#include "SFMLOrthogonalLayer.hpp" + +#include +#include +#include + + +int main() +{ + sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window"); + + tmx::Map map; + map.load("assets/demo.tmx"); + + MapLayer layerZero(map, 0); + MapLayer layerOne(map, 1); + MapLayer layerTwo(map, 2); + + sf::Clock globalClock; + sf::Clock wiggleClock; + + bool doWiggle = false; + + while (window.isOpen()) + { + sf::Event event; + while (window.pollEvent(event)) + { + if (event.type == sf::Event::Closed) + { + window.close(); + } + + else if( event.type == sf::Event::KeyPressed ) + { + switch(event.key.code) + { + case sf::Keyboard::W: + // toggle doWiggle + doWiggle = !doWiggle; + break; + } + } + } + + + sf::Time duration = globalClock.restart(); + layerZero.update(duration); + + sf::Vector2f newOffset = sf::Vector2f(0.f, 0.f); + if (doWiggle) + { + newOffset = sf::Vector2f(std::cos(wiggleClock.getElapsedTime().asSeconds()) * 100.f, 0.f); + } + layerZero.setOffset(newOffset); + layerOne.setOffset(newOffset); + layerTwo.setOffset(newOffset); + + window.clear(sf::Color::Black); + window.draw(layerZero); + window.draw(layerOne); + window.draw(layerTwo); + window.display(); + } + + return 0; +} diff --git a/SFML3Example/assets/demo.tmx b/SFML3Example/assets/demo.tmx new file mode 100644 index 0000000..6691792 --- /dev/null +++ b/SFML3Example/assets/demo.tmx @@ -0,0 +1,34 @@ + + + + + + + + + + + + + + + + + + + + + eJxjYBg8QAOIGcnQxwvEPEiYj0h9nECsggVzkeGGwQoAJB8BMA== + + + + + eJxjYBg+gBmJzT9grhhcAAAUlAAT + + + + + eJxjYBjaQBCIBaCYG4k9CiAAAGXcAF0= + + + diff --git a/SFML3Example/assets/images/tilemap/platform.tsx b/SFML3Example/assets/images/tilemap/platform.tsx new file mode 100644 index 0000000..6a48b12 --- /dev/null +++ b/SFML3Example/assets/images/tilemap/platform.tsx @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + + + diff --git a/SFML3Example/assets/images/tilemap/tileset.png b/SFML3Example/assets/images/tilemap/tileset.png new file mode 100644 index 0000000..f13825d Binary files /dev/null and b/SFML3Example/assets/images/tilemap/tileset.png differ diff --git a/SFML3Example/assets/images/tilemap/tileset.txt b/SFML3Example/assets/images/tilemap/tileset.txt new file mode 100644 index 0000000..1676dad --- /dev/null +++ b/SFML3Example/assets/images/tilemap/tileset.txt @@ -0,0 +1,2 @@ +tileset by Alucard +http://opengameart.org/content/pixel-art-platformer-complete-pack \ No newline at end of file diff --git a/SFML3Example/assets/images/tilemap/tileset02.png b/SFML3Example/assets/images/tilemap/tileset02.png new file mode 100644 index 0000000..9f594e4 Binary files /dev/null and b/SFML3Example/assets/images/tilemap/tileset02.png differ diff --git a/SFML3Example/assets/platform.tmx b/SFML3Example/assets/platform.tmx new file mode 100644 index 0000000..2b40b85 --- /dev/null +++ b/SFML3Example/assets/platform.tmx @@ -0,0 +1,94 @@ + + + + + + + + + + + + eJzt2VsKwyAQBdCsoo8VNN3/BvvbQC1Em9iZOQeE/AQcyFXjLAuc69YxIKuePMgLVckCtNkz4LvrjgGVXHYMqGZPPuSFSuQCPrt3Dshudt/jOfAuZLfOngD8MfmANvkY43ya22P2BIKzvkCbfECbfECb8ykAAABnqNb/qlYvY6rdX0avV77PVe3+Mnq90fMNv/a+Z8gHbK2NZ2B7Jox+PqQO/8rQZi0HAAAAiCf7nWf2+jhW9v5u9vo41sj3E2Ftlg9GjPTMIvTbIswRYKYXswgSvA== + + + + + + + + eJztm9tOw0AMBc0DPAEfAKIY6P9/I6kgqA25NInXa3dnpCOeqJxmZ+89CgCMcfwNAPwHPwCmwQ+AafDDlrvaBYAp+GHLZ5fDIF9VK4I94AfU4qXL60LeqlX3A34ATIMfvrA+yQV++MC6JCf4AdkpOSbjB2RmbFy2HJvxw5bh+2IOlRv8iA9rehueujwO8rzwP/gRk6l5A+PSNua+z7nvEj/KsqXPuhW2jHvRxkr8KMfWPmsN0dqTdnkvlA+/x/ijZT/G+vYM/XuksxSVyzZ8LBxvT1r1g/n9PlR8fJiKlyOt+gHbUKnrhbcj+AFLqPjNn6LNt/AjLyXniCpxnfAcS/ADelRyOeHhCH7ACZX67dvSkdO+99T+5FzO9y7vzz63BaKdFURBpX67tozVecv5Z94Kpzl5/3yHkb/s3V6iUr89Zwi0h0r9dpclUI+18+S58/1r5pAqedfg+NEOKuXuKl07ryb4sYZr+nKL+1kq9d85wY81LJ23WZy9qdCHZ0x2ou/dquBF5mRGJeZvB3pU6r9f0p4fKuv6ZG9H1tZH4iYTKtvbnYcje+ojMZMFlf3PWtIRi/pIvERHxbZPLuGIGtZHYiUiKmXPtKzW7X2dtd8hKZcH2XYf2CrDMzd1fPY9jnjWSeqmxl2HKHcgstRJ/PMNRu5JzA== + + + + + eJzt2+FqwjAUBtDihO2fbnPv/6rbYGUxNOuqbW9izoEgIsqF+jVpczsMAAAAAACkPqILgIq9RxdA1U7RBQSTDyiTjz4dowtoxJr5eP0a52T0PjfX7PDzuuUxajmDY+3mD7byFF3AHcba5YN7nSfGt0PxG/WLzke6FnsLqgFKovNBXVwrXhvnvsswPT9ODed5lvC/YU/5+t45HwCgX+la0HUJsIaWexBga3M9CM+7VAH1SNedf/Ug9JCNUm8G9Mxzm1CmrwfKWsrHXN/RlP/s7S/9TfqR58N9PPiV5+PWZ4n0YPGI1soHPKJL9r7lZ+0AAAAgZQ+Bnr3MfO4eKb3LM5Lujy29Rzq3t3ZLX8VeTkPd9QHU4hO/Kwa1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/SFML3Example/cmake/modules/FindSFML.cmake b/SFML3Example/cmake/modules/FindSFML.cmake new file mode 100644 index 0000000..48873ef --- /dev/null +++ b/SFML3Example/cmake/modules/FindSFML.cmake @@ -0,0 +1,366 @@ +# 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 MATCH ".*#define SFML_VERSION_MAJOR ([0-9]+).*#define SFML_VERSION_MINOR ([0-9]+).*#define SFML_VERSION_PATCH ([0-9]+).*" SFML_CONFIG_HPP_CONTENTS "${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}") + 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() diff --git a/SFML3Example/cmake/modules/FindTMXLITE.cmake b/SFML3Example/cmake/modules/FindTMXLITE.cmake new file mode 100644 index 0000000..6ca7154 --- /dev/null +++ b/SFML3Example/cmake/modules/FindTMXLITE.cmake @@ -0,0 +1,10 @@ +find_path(TMXLITE_INCLUDE_DIR NAMES tmxlite/Config.hpp PATH_SUFFIXES include) + +find_library(TMXLITE_LIBRARY_DEBUG NAMES tmxlite-d) +find_library(TMXLITE_LIBRARY_RELEASE NAMES tmxlite) + +include(SelectLibraryConfigurations) +select_library_configurations(TMXLITE) + +include(FindPackageHandleStandardArgs) +FIND_PACKAGE_HANDLE_STANDARD_ARGS(TMXLITE DEFAULT_MSG TMXLITE_LIBRARY TMXLITE_INCLUDE_DIR) \ No newline at end of file diff --git a/SFML3Example/meson.build b/SFML3Example/meson.build new file mode 100644 index 0000000..7b3dd5d --- /dev/null +++ b/SFML3Example/meson.build @@ -0,0 +1,17 @@ +sfml_graphics_dep = dependency('sfml-graphics') +sfml_window_dep = dependency('sfml-window') +sfml_system_dep = dependency('sfml-system') + +pvt_incdir = include_directories('src') + +executable('SFML_example', + 'src/main.cpp', + install: false, + include_directories: pvt_incdir, + dependencies: [ + tmxlite_dep, + sfml_graphics_dep, + sfml_window_dep, + sfml_system_dep, + ], +) diff --git a/SFML3Example/readme.md b/SFML3Example/readme.md new file mode 100644 index 0000000..2069582 --- /dev/null +++ b/SFML3Example/readme.md @@ -0,0 +1,10 @@ +SFML Example +------------ + +Test program demonstrating how one may possibly implement +an orthogonal map renderer using SFML. + +Note that this example provides very basic rendering, and +does not support all the features supported by tmxlite, +such as tile flipping/rotation or tilesets which are created +as a collection of images \ No newline at end of file diff --git a/SFML3Example/src/SFMLOrthogonalLayer.hpp b/SFML3Example/src/SFMLOrthogonalLayer.hpp new file mode 100644 index 0000000..10b5687 --- /dev/null +++ b/SFML3Example/src/SFMLOrthogonalLayer.hpp @@ -0,0 +1,648 @@ +/********************************************************************* +(c) Matt Marchant & contributors 2016 - 2024 +http://trederia.blogspot.com + +tmxlite - Zlib license. + +This software is provided 'as-is', without any express or +implied warranty. In no event will the authors be held +liable for any damages arising from the use of this software. + +Permission is granted to anyone to use this software for any purpose, +including commercial applications, and to alter it and redistribute +it freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; +you must not claim that you wrote the original software. +If you use this software in a product, an acknowledgment +in the product documentation would be appreciated but +is not required. + +2. Altered source versions must be plainly marked as such, +and must not be misrepresented as being the original software. + +3. This notice may not be removed or altered from any +source distribution. +*********************************************************************/ + +/* +Creates an SFML drawable from an Orthogonal tmx map layer. +This is an example of drawing with SFML - not all features +are implemented. +*/ + +#ifndef SFML_ORTHO_HPP_ +#define SFML_ORTHO_HPP_ + +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + + +class MapLayer final : public sf::Drawable +{ +public: + + MapLayer(const tmx::Map& map, std::size_t idx) + { + const auto& layers = map.getLayers(); + if (map.getOrientation() != tmx::Orientation::Orthogonal) + { + std::cout << "Map is not orthogonal - nothing will be drawn" << std::endl; + } + else if (idx >= layers.size()) + { + std::cout << "Layer index " << idx << " is out of range, layer count is " << layers.size() << std::endl; + } + else if (layers[idx]->getType() != tmx::Layer::Type::Tile) + { + std::cout << "layer " << idx << " is not a Tile layer..." << std::endl; + } + + else + { + //round the chunk size to the nearest tile + const auto tileSize = map.getTileSize(); + m_chunkSize.x = std::floor(m_chunkSize.x / tileSize.x) * tileSize.x; + m_chunkSize.y = std::floor(m_chunkSize.y / tileSize.y) * tileSize.y; + m_mapTileSize.x = map.getTileSize().x; + m_mapTileSize.y = map.getTileSize().y; + const auto& layer = layers[idx]->getLayerAs(); + createChunks(map, layer); + + auto mapSize = map.getBounds(); + m_globalBounds.width = mapSize.width; + m_globalBounds.height = mapSize.height; + } + } + + ~MapLayer() = default; + MapLayer(const MapLayer&) = delete; + MapLayer& operator = (const MapLayer&) = delete; + + const sf::FloatRect& getGlobalBounds() const { return m_globalBounds; } + + void setTile(std::int32_t tileX, std::int32_t tileY, tmx::TileLayer::Tile tile, bool refresh = true) + { + sf::Vector2u chunkLocale; + const auto& selectedChunk = getChunkAndTransform(tileX, tileY, chunkLocale); + selectedChunk->setTile(chunkLocale.x, chunkLocale.y, tile, refresh); + } + + tmx::TileLayer::Tile getTile(std::int32_t tileX, std::int32_t tileY) + { + sf::Vector2u chunkLocale; + const auto& selectedChunk = getChunkAndTransform(tileX, tileY, chunkLocale); + return selectedChunk->getTile(chunkLocale.x, chunkLocale.y); + } + void setColor(std::int32_t tileX, std::int32_t tileY, sf::Color color, bool refresh = true) + { + sf::Vector2u chunkLocale; + const auto& selectedChunk = getChunkAndTransform(tileX, tileY, chunkLocale); + selectedChunk->setColor(chunkLocale.x, chunkLocale.y, color, refresh); + } + + sf::Color getColor(std::int32_t tileX, std::int32_t tileY) + { + sf::Vector2u chunkLocale; + const auto& selectedChunk = getChunkAndTransform(tileX, tileY, chunkLocale); + return selectedChunk->getColor(chunkLocale.x, chunkLocale.y); + } + + void setOffset(sf::Vector2f offset) { m_offset = offset; } + sf::Vector2f getOffset() const { return m_offset; } + + void update(sf::Time elapsed) + { + for (auto& c : m_visibleChunks) + { + for (AnimationState& as : c->getActiveAnimations()) + { + as.currentTime += elapsed; + + tmx::TileLayer::Tile tile; + std::int32_t animTime = 0; + auto x = as.animTile.animation.frames.begin(); + while (animTime < as.currentTime.asMilliseconds()) + { + if (x == as.animTile.animation.frames.end()) + { + x = as.animTile.animation.frames.begin(); + as.currentTime -= sf::milliseconds(animTime); + animTime = 0; + } + + tile.ID = x->tileID; + animTime += x->duration; + x++; + } + + setTile(as.tileCords.x, as.tileCords.y, tile); + } + } + } + +private: + //increasing m_chunkSize by 4; fixes render problems when mapsize != chunksize + //sf::Vector2f m_chunkSize = sf::Vector2f(1024.f, 1024.f); + sf::Vector2f m_chunkSize = sf::Vector2f(512.f, 512.f); + sf::Vector2u m_chunkCount; + sf::Vector2u m_mapTileSize; // general Tilesize of Map + sf::FloatRect m_globalBounds; + sf::Vector2f m_offset; + + using TextureResource = std::map>; + TextureResource m_textureResource; + + struct AnimationState + { + sf::Vector2u tileCords; + sf::Time startTime; + sf::Time currentTime; + tmx::Tileset::Tile animTile; + std::uint8_t flipFlags; + }; + + class Chunk final : public sf::Transformable, public sf::Drawable + { + public: + using Ptr = std::unique_ptr; + using Tile = std::array; + + Chunk(const tmx::TileLayer& layer, std::vector tilesets, + const sf::Vector2f& position, const sf::Vector2f& tileCount, + const sf::Vector2u& tileSize, std::size_t rowSize, + TextureResource& tr, const std::map& animTiles) + : m_animTiles(animTiles) + { + setPosition(position); + layerOpacity = static_cast(layer.getOpacity() / 1.f * 255.f); + + sf::Color vertColour = sf::Color(200 ,200, 200, layerOpacity); + auto offset = layer.getOffset(); + layerOffset = { static_cast(offset.x), static_cast(offset.y) }; + chunkTileCount = { tileCount.x, tileCount.y }; + mapTileSize = tileSize; + + const auto& tileIDs = layer.getTiles(); + + //go through the tiles and create all arrays (for latter manipulation) + for (const auto& ts : tilesets) + { + if(ts->getImagePath().empty()) + { + tmx::Logger::log("This example does not support Collection of Images tilesets", tmx::Logger::Type::Info); + tmx::Logger::log("Chunks using " + ts->getName() + " will not be created", tmx::Logger::Type::Info); + continue; + } + m_chunkArrays.emplace_back(std::make_unique(*tr.find(ts->getImagePath())->second, *ts)); + } + std::size_t xPos = static_cast(position.x / tileSize.x); + std::size_t yPos = static_cast(position.y / tileSize.y); + for (auto y = yPos; y < yPos + tileCount.y; ++y) + { + for (auto x = xPos; x < xPos + tileCount.x; ++x) + { + auto idx = (y * rowSize + x); + m_chunkTileIDs.emplace_back(tileIDs[idx]); + m_chunkColors.emplace_back(vertColour); + } + } + generateTiles(true); + } + + void generateTiles(bool registerAnimation = false) + { + if (registerAnimation) + { + m_activeAnimations.clear(); + } + for (const auto& ca : m_chunkArrays) + { + std::uint32_t idx = 0; + std::uint32_t xPos = static_cast(getPosition().x / mapTileSize.x); + std::uint32_t yPos = static_cast(getPosition().y / mapTileSize.y); + for (auto y = yPos; y < yPos + chunkTileCount.y; ++y) + { + for (auto x = xPos; x < xPos + chunkTileCount.x; ++x) + { + if (idx < m_chunkTileIDs.size() && m_chunkTileIDs[idx].ID >= ca->m_firstGID + && m_chunkTileIDs[idx].ID <= ca->m_lastGID) + { + if (registerAnimation && m_animTiles.find(m_chunkTileIDs[idx].ID) != m_animTiles.end()) + { + AnimationState as; + as.animTile = m_animTiles[m_chunkTileIDs[idx].ID]; + as.startTime = sf::milliseconds(0); + as.tileCords = sf::Vector2u(x,y); + m_activeAnimations.push_back(as); + } + + sf::Vector2f tileOffset(static_cast(x) * mapTileSize.x, static_cast(y) * mapTileSize.y + mapTileSize.y - ca->tileSetSize.y); + + auto idIndex = m_chunkTileIDs[idx].ID - ca->m_firstGID; + sf::Vector2f tileIndex(sf::Vector2i(idIndex % ca->tsTileCount.x, idIndex / ca->tsTileCount.x)); + tileIndex.x *= ca->tileSetSize.x; + tileIndex.y *= ca->tileSetSize.y; + Tile tile = + { + sf::Vertex(tileOffset - getPosition(), m_chunkColors[idx], tileIndex), + sf::Vertex(tileOffset - getPosition() + sf::Vector2f(static_cast(ca->tileSetSize.x), 0.f), m_chunkColors[idx], tileIndex + sf::Vector2f(static_cast(ca->tileSetSize.x), 0.f)), + sf::Vertex(tileOffset - getPosition() + sf::Vector2f(sf::Vector2u(ca->tileSetSize.x, ca->tileSetSize.y)), m_chunkColors[idx], tileIndex + sf::Vector2f(sf::Vector2u(ca->tileSetSize.x, ca->tileSetSize.y))), + sf::Vertex(tileOffset - getPosition(), m_chunkColors[idx], tileIndex), + sf::Vertex(tileOffset - getPosition() + sf::Vector2f(sf::Vector2u(ca->tileSetSize.x, ca->tileSetSize.y)), m_chunkColors[idx], tileIndex + sf::Vector2f(sf::Vector2u(ca->tileSetSize.x, ca->tileSetSize.y))), + sf::Vertex(tileOffset - getPosition() + sf::Vector2f(0.f,static_cast(ca->tileSetSize.y)), m_chunkColors[idx], tileIndex + sf::Vector2f(0.f, static_cast(ca->tileSetSize.y))) + }; + doFlips(m_chunkTileIDs[idx].flipFlags,&tile[0].texCoords,&tile[1].texCoords,&tile[2].texCoords,&tile[3].texCoords,&tile[4].texCoords,&tile[5].texCoords); + ca->addTile(tile); + } + idx++; + } + } + } + } + ~Chunk() = default; + Chunk(const Chunk&) = delete; + Chunk& operator = (const Chunk&) = delete; + + std::vector& getActiveAnimations() + { + return m_activeAnimations; + } + + tmx::TileLayer::Tile getTile(std::int32_t x, std::int32_t y) const + { + return m_chunkTileIDs[calcIndexFrom(x,y)]; + } + + void setTile(std::int32_t x, std::int32_t y, tmx::TileLayer::Tile tile, bool refresh) + { + m_chunkTileIDs[calcIndexFrom(x,y)] = tile; + maybeRegenerate(refresh); + } + + sf::Color getColor(std::int32_t x, std::int32_t y) const + { + return m_chunkColors[calcIndexFrom(x,y)]; + } + + void setColor(std::int32_t x, std::int32_t y, sf::Color color, bool refresh) + { + m_chunkColors[calcIndexFrom(x,y)] = color; + maybeRegenerate(refresh); + } + + void maybeRegenerate(bool refresh) + { + if (refresh) + { + for (const auto& ca : m_chunkArrays) + { + ca->reset(); + } + generateTiles(); + } + } + + std::int32_t calcIndexFrom(std::int32_t x, std::int32_t y) const + { + return x + y * static_cast(chunkTileCount.x); + } + + bool empty() const + { + return m_chunkArrays.empty(); + } + + void flipY(sf::Vector2f* v0, sf::Vector2f* v1, sf::Vector2f* v2, sf::Vector2f* v3, sf::Vector2f* v4, sf::Vector2f* v5) + { + //Flip Y + sf::Vector2f tmp0 = *v0; + v0->y = v5->y; + v3->y = v5->y; + v5->y = tmp0.y; + sf::Vector2f tmp2 = *v2; + v2->y = v1->y; + v4->y = v1->y; + v1->y = tmp2.y; + } + + void flipX(sf::Vector2f* v0, sf::Vector2f* v1, sf::Vector2f* v2, sf::Vector2f* v3, sf::Vector2f* v4, sf::Vector2f* v5) + { + //Flip X + sf::Vector2f tmp0 = *v0; + v0->x = v1->x; + v3->x = v1->x; + v1->x = tmp0.x; + sf::Vector2f tmp2 = *v2; + v2->x = v5->x; + v4->x = v5->x; + v5->x = tmp2.x; + } + + void flipD(sf::Vector2f* v0, sf::Vector2f* v1, sf::Vector2f* v2, sf::Vector2f* v3, sf::Vector2f* v4, sf::Vector2f* v5) + { + //Diagonal flip + sf::Vector2f tmp2 = *v2; + *v2 = *v4; + *v4 = tmp2; + sf::Vector2f tmp0 = *v0; + *v0 = *v3; + *v3 = tmp0; + sf::Vector2f tmp1 = *v1; + *v1 = *v5; + *v5 = tmp1; + } + + void doFlips(std::uint8_t bits, sf::Vector2f* v0, sf::Vector2f* v1, sf::Vector2f* v2, sf::Vector2f* v3, sf::Vector2f* v4, sf::Vector2f* v5) + { + //0000 = no change + //0100 = vertical = swap y axis + //1000 = horizontal = swap x axis + //1100 = horiz + vert = swap both axes = horiz+vert = rotate 180 degrees + //0010 = diag = rotate 90 degrees right and swap x axis + //0110 = diag+vert = rotate 270 degrees right + //1010 = horiz+diag = rotate 90 degrees right + //1110 = horiz+vert+diag = rotate 90 degrees right and swap y axis + if(!(bits & tmx::TileLayer::FlipFlag::Horizontal) && + !(bits & tmx::TileLayer::FlipFlag::Vertical) && + !(bits & tmx::TileLayer::FlipFlag::Diagonal) ) + { + //Shortcircuit tests for nothing to do + return; + } + else if(!(bits & tmx::TileLayer::FlipFlag::Horizontal) && + (bits & tmx::TileLayer::FlipFlag::Vertical) && + !(bits & tmx::TileLayer::FlipFlag::Diagonal) ) + { + //0100 + flipY(v0,v1,v2,v3,v4,v5); + } + else if((bits & tmx::TileLayer::FlipFlag::Horizontal) && + !(bits & tmx::TileLayer::FlipFlag::Vertical) && + !(bits & tmx::TileLayer::FlipFlag::Diagonal) ) + { + //1000 + flipX(v0,v1,v2,v3,v4,v5); + } + else if((bits & tmx::TileLayer::FlipFlag::Horizontal) && + (bits & tmx::TileLayer::FlipFlag::Vertical) && + !(bits & tmx::TileLayer::FlipFlag::Diagonal) ) + { + //1100 + flipY(v0,v1,v2,v3,v4,v5); + flipX(v0,v1,v2,v3,v4,v5); + } + else if(!(bits & tmx::TileLayer::FlipFlag::Horizontal) && + !(bits & tmx::TileLayer::FlipFlag::Vertical) && + (bits & tmx::TileLayer::FlipFlag::Diagonal) ) + { + //0010 + flipD(v0,v1,v2,v3,v4,v5); + } + else if(!(bits & tmx::TileLayer::FlipFlag::Horizontal) && + (bits & tmx::TileLayer::FlipFlag::Vertical) && + (bits & tmx::TileLayer::FlipFlag::Diagonal) ) + { + //0110 + flipX(v0,v1,v2,v3,v4,v5); + flipD(v0,v1,v2,v3,v4,v5); + } + else if((bits & tmx::TileLayer::FlipFlag::Horizontal) && + !(bits & tmx::TileLayer::FlipFlag::Vertical) && + (bits & tmx::TileLayer::FlipFlag::Diagonal) ) + { + //1010 + flipY(v0,v1,v2,v3,v4,v5); + flipD(v0,v1,v2,v3,v4,v5); + } + else if((bits & tmx::TileLayer::FlipFlag::Horizontal) && + (bits & tmx::TileLayer::FlipFlag::Vertical) && + (bits & tmx::TileLayer::FlipFlag::Diagonal) ) + { + //1110 + flipY(v0,v1,v2,v3,v4,v5); + flipX(v0,v1,v2,v3,v4,v5); + flipD(v0,v1,v2,v3,v4,v5); + } + } + + private: + class ChunkArray final : public sf::Drawable + { + public: + using Ptr = std::unique_ptr; + + tmx::Vector2u tileSetSize; + sf::Vector2u tsTileCount; + std::uint32_t m_firstGID, m_lastGID; + + explicit ChunkArray(const sf::Texture& t, const tmx::Tileset& ts) + : m_texture(t) + { + auto texSize = getTextureSize(); + tileSetSize = ts.getTileSize(); + tsTileCount.x = texSize.x / tileSetSize.x; + tsTileCount.y = texSize.y / tileSetSize.y; + m_firstGID = ts.getFirstGID(); + m_lastGID = ts.getLastGID(); + } + + ~ChunkArray() = default; + ChunkArray(const ChunkArray&) = delete; + ChunkArray& operator = (const ChunkArray&) = delete; + + void reset() + { + m_vertices.clear(); + } + + void addTile(const Chunk::Tile& tile) + { + for (const auto& v : tile) + { + m_vertices.push_back(v); + } + } + + sf::Vector2u getTextureSize() const + { + return m_texture.getSize(); + } + + private: + const sf::Texture& m_texture; + std::vector m_vertices; + void draw(sf::RenderTarget& rt, sf::RenderStates states) const override + { + states.texture = &m_texture; + rt.draw(m_vertices.data(), m_vertices.size(), sf::Triangles, states); + } + }; + + sf::Uint8 layerOpacity; // opacity of the layer + sf::Vector2f layerOffset; // Layer offset + sf::Vector2u mapTileSize; // general Tilesize of Map + sf::Vector2f chunkTileCount; // chunk tilecount + std::vector m_chunkTileIDs; // stores all tiles in this chunk for later manipulation + std::vector m_chunkColors; // stores colors for extended color effects + std::map m_animTiles; // animation catalogue + std::vector m_activeAnimations; // Animations to be done in this chunk + std::vector m_chunkArrays; + + void draw(sf::RenderTarget& rt, sf::RenderStates states) const override + { + states.transform *= getTransform(); + for (const auto& a : m_chunkArrays) + { + rt.draw(*a, states); + } + } + }; + + std::vector m_chunks; + mutable std::vector m_visibleChunks; + + Chunk::Ptr& getChunkAndTransform(std::int32_t x, std::int32_t y, sf::Vector2u& chunkRelative) + { + std::uint32_t chunkX = (x * m_mapTileSize.x) / static_cast(m_chunkSize.x); + std::uint32_t chunkY = (y * m_mapTileSize.y) / static_cast(m_chunkSize.y); + chunkRelative.x = ((x * m_mapTileSize.x) - chunkX * static_cast(m_chunkSize.x)) / m_mapTileSize.x ; + chunkRelative.y = ((y * m_mapTileSize.y) - chunkY * static_cast(m_chunkSize.y)) / m_mapTileSize.y ; + return m_chunks[chunkX + chunkY * m_chunkCount.x]; + } + + void createChunks(const tmx::Map& map, const tmx::TileLayer& layer) + { + //look up all the tile sets and load the textures + const auto& tileSets = map.getTilesets(); + const auto& layerIDs = layer.getTiles(); + std::uint32_t maxID = std::numeric_limits::max(); + std::vector usedTileSets; + + for (auto i = tileSets.rbegin(); i != tileSets.rend(); ++i) + { + for (const auto& tile : layerIDs) + { + if (tile.ID >= i->getFirstGID() && tile.ID < maxID) + { + usedTileSets.push_back(&(*i)); + break; + } + } + maxID = i->getFirstGID(); + } + + sf::Image fallback; + fallback.create(2, 2, sf::Color::Magenta); + for (const auto& ts : usedTileSets) + { + const auto& path = ts->getImagePath(); + + std::unique_ptr newTexture = std::make_unique(); + sf::Image img; + if (!img.loadFromFile(path)) + { + newTexture->loadFromImage(fallback); + } + else + { + if (ts->hasTransparency()) + { + auto transparency = ts->getTransparencyColour(); + img.createMaskFromColor({ transparency.r, transparency.g, transparency.b, transparency.a }); + } + newTexture->loadFromImage(img); + } + m_textureResource.insert(std::make_pair(path, std::move(newTexture))); + } + + //calculate the number of chunks in the layer + //and create each one + const auto bounds = map.getBounds(); + m_chunkCount.x = static_cast(std::ceil(bounds.width / m_chunkSize.x)); + m_chunkCount.y = static_cast(std::ceil(bounds.height / m_chunkSize.y)); + + sf::Vector2u tileSize(map.getTileSize().x, map.getTileSize().y); + + for (auto y = 0u; y < m_chunkCount.y; ++y) + { + sf::Vector2f tileCount(m_chunkSize.x / tileSize.x, m_chunkSize.y / tileSize.y); + for (auto x = 0u; x < m_chunkCount.x; ++x) + { + // calculate size of each Chunk (clip against map) + if ((x + 1) * m_chunkSize.x > bounds.width) + { + tileCount.x = (bounds.width - x * m_chunkSize.x) / map.getTileSize().x; + } + if ((y + 1) * m_chunkSize.y > bounds.height) + { + tileCount.y = (bounds.height - y * m_chunkSize.y) / map.getTileSize().y; + } + //m_chunks.emplace_back(std::make_unique(layer, usedTileSets, + // sf::Vector2f(x * m_chunkSize.x, y * m_chunkSize.y), tileCount, map.getTileCount().x, m_textureResource)); + m_chunks.emplace_back(std::make_unique(layer, usedTileSets, + sf::Vector2f(x * m_chunkSize.x, y * m_chunkSize.y), tileCount, tileSize, map.getTileCount().x, m_textureResource, map.getAnimatedTiles())); + } + } + } + + void updateVisibility(const sf::View& view) const + { + sf::Vector2f viewCorner = view.getCenter(); + viewCorner -= view.getSize() / 2.f; + + std::int32_t posX = static_cast(std::floor(viewCorner.x / m_chunkSize.x)); + std::int32_t posY = static_cast(std::floor(viewCorner.y / m_chunkSize.y)); + std::int32_t posX2 = static_cast(std::ceil((viewCorner.x + view.getSize().x) / m_chunkSize.x)); + std::int32_t posY2 = static_cast(std::ceil((viewCorner.y + view.getSize().x)/ m_chunkSize.y)); + + std::vector visible; + for (auto y = posY; y < posY2; ++y) + { + for (auto x = posX; x < posX2; ++x) + { + std::size_t idx = y * std::int32_t(m_chunkCount.x) + x; + if (idx >= 0u && idx < m_chunks.size() && !m_chunks[idx]->empty()) + { + visible.push_back(m_chunks[idx].get()); + } + } + } + + std::swap(m_visibleChunks, visible); + } + + + void draw(sf::RenderTarget& rt, sf::RenderStates states) const override + { + states.transform.translate(m_offset); + + //calc view coverage and draw nearest chunks + updateVisibility(rt.getView()); + for (const auto& c : m_visibleChunks) + { + rt.draw(*c, states); + } + } +}; + +#endif //SFML_ORTHO_HPP_ diff --git a/SFML3Example/src/main.cpp b/SFML3Example/src/main.cpp new file mode 100644 index 0000000..2c66d18 --- /dev/null +++ b/SFML3Example/src/main.cpp @@ -0,0 +1,95 @@ +/********************************************************************* +(c) Matt Marchant & contributors 2016 - 2024 +http://trederia.blogspot.com + +tmxlite - Zlib license. + +This software is provided 'as-is', without any express or +implied warranty. In no event will the authors be held +liable for any damages arising from the use of this software. + +Permission is granted to anyone to use this software for any purpose, +including commercial applications, and to alter it and redistribute +it freely, subject to the following restrictions: + +1. The origin of this software must not be misrepresented; +you must not claim that you wrote the original software. +If you use this software in a product, an acknowledgment +in the product documentation would be appreciated but +is not required. + +2. Altered source versions must be plainly marked as such, +and must not be misrepresented as being the original software. + +3. This notice may not be removed or altered from any +source distribution. +*********************************************************************/ + + +#include "SFMLOrthogonalLayer.hpp" + +#include +#include +#include + + +int main() +{ + sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window"); + + tmx::Map map; + map.load("assets/demo.tmx"); + + MapLayer layerZero(map, 0); + MapLayer layerOne(map, 1); + MapLayer layerTwo(map, 2); + + sf::Clock globalClock; + sf::Clock wiggleClock; + + bool doWiggle = false; + + while (window.isOpen()) + { + sf::Event event; + while (window.pollEvent(event)) + { + if (event.type == sf::Event::Closed) + { + window.close(); + } + + else if( event.type == sf::Event::KeyPressed ) + { + switch(event.key.code) + { + case sf::Keyboard::W: + // toggle doWiggle + doWiggle = !doWiggle; + break; + } + } + } + + + sf::Time duration = globalClock.restart(); + layerZero.update(duration); + + sf::Vector2f newOffset = sf::Vector2f(0.f, 0.f); + if (doWiggle) + { + newOffset = sf::Vector2f(std::cos(wiggleClock.getElapsedTime().asSeconds()) * 100.f, 0.f); + } + layerZero.setOffset(newOffset); + layerOne.setOffset(newOffset); + layerTwo.setOffset(newOffset); + + window.clear(sf::Color::Black); + window.draw(layerZero); + window.draw(layerOne); + window.draw(layerTwo); + window.display(); + } + + return 0; +}