diff --git a/.gitignore b/.gitignore index 7d93a0c..6fc605f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,47 +1,47 @@ -# Compiled source # -################### -*.com -*.class -*.dll -*.exe -*.o -*.so - -# Packages # -############ -# it's better to unpack these files and commit the raw source -# git has its own built in compression methods -*.7z -*.dmg -*.gz -*.iso -*.jar -*.rar -*.tar -*.zip - -# Logs and databases # -###################### -*.log -*.sql -*.sqlite - -# OS generated files # -###################### -.DS_Store -.DS_Store? -._* -.Spotlight-V100 -.Trashes -ehthumbs.db -Thumbs.db - -# CMake Generated Files # -######################### -CMakeCache.txt -CMakeFiles -CMakeScripts -Makefile -cmake_install.cmake -install_manifest.txt -CTestTestfile.cmake +# Compiled source # +################### +*.com +*.class +*.dll +*.exe +*.o +*.so + +# Packages # +############ +# it's better to unpack these files and commit the raw source +# git has its own built in compression methods +*.7z +*.dmg +*.gz +*.iso +*.jar +*.rar +*.tar +*.zip + +# Logs and databases # +###################### +*.log +*.sql +*.sqlite + +# OS generated files # +###################### +.DS_Store +.DS_Store? +._* +.Spotlight-V100 +.Trashes +ehthumbs.db +Thumbs.db + +# CMake Generated Files # +######################### +CMakeCache.txt +CMakeFiles +CMakeScripts +Makefile +cmake_install.cmake +install_manifest.txt +CTestTestfile.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index b1acd01..8f7958b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,41 +1,41 @@ -# Check versions -message(STATUS "CMake version: ${CMAKE_VERSION}") -cmake_minimum_required(VERSION 3.1) - -# Set the project name -set(PNAME Game) -project(${PNAME}) - -# Set up variables, and find SFML -#set(SFML_ROOT root CACHE STRING "User specified path") -set(SFML_COMPONENTS graphics window system network audio) -set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}) -find_package(SFML 2.1 COMPONENTS ${SFML_COMPONENTS} REQUIRED) -message(STATUS "SFML found: ${SFML_FOUND}") - -# Find OpenCL -find_package( OpenCL REQUIRED ) -message(STATUS "OpenCL found: ${OPENCL_FOUND}") - -# Find OpenGL -find_package( OpenGL REQUIRED) -message(STATUS "OpenGL found: ${OPENGL_FOUND}") - -# Include the directories for the main program, GL, CL and SFML's headers -include_directories(${SFML_INCLUDE_DIR}) -include_directories(${OpenCL_INCLUDE_DIRS}) -include_directories(${OpenGL_INCLUDE_DIRS}) -include_directories(include) - -# Set the .cpp sources -file(GLOB SOURCES "src/*.cpp") -add_executable(${PNAME} ${SOURCES}) - -# Link CL, GL, and SFML -target_link_libraries (${PNAME} ${SFML_LIBRARIES} ${SFML_DEPENDENCIES}) -target_link_libraries (${PNAME} ${OpenCL_LIBRARY}) -target_link_libraries (${PNAME} ${OPENGL_LIBRARIES}) - -# Setup to use C++11 -set_property(TARGET ${PNAME} PROPERTY CXX_STANDARD 11) # Use C++11 - +# Check versions +message(STATUS "CMake version: ${CMAKE_VERSION}") +cmake_minimum_required(VERSION 3.1) + +# Set the project name +set(PNAME Game) +project(${PNAME}) + +# Set up variables, and find SFML +#set(SFML_ROOT root CACHE STRING "User specified path") +set(SFML_COMPONENTS graphics window system network audio) +set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}) +find_package(SFML 2.1 COMPONENTS ${SFML_COMPONENTS} REQUIRED) +message(STATUS "SFML found: ${SFML_FOUND}") + +# Find OpenCL +find_package( OpenCL REQUIRED ) +message(STATUS "OpenCL found: ${OPENCL_FOUND}") + +# Find OpenGL +find_package( OpenGL REQUIRED) +message(STATUS "OpenGL found: ${OPENGL_FOUND}") + +# Include the directories for the main program, GL, CL and SFML's headers +include_directories(${SFML_INCLUDE_DIR}) +include_directories(${OpenCL_INCLUDE_DIRS}) +include_directories(${OpenGL_INCLUDE_DIRS}) +include_directories(include) + +# Set the .cpp sources +file(GLOB SOURCES "src/*.cpp") +add_executable(${PNAME} ${SOURCES}) + +# Link CL, GL, and SFML +target_link_libraries (${PNAME} ${SFML_LIBRARIES} ${SFML_DEPENDENCIES}) +target_link_libraries (${PNAME} ${OpenCL_LIBRARY}) +target_link_libraries (${PNAME} ${OPENGL_LIBRARIES}) + +# Setup to use C++11 +set_property(TARGET ${PNAME} PROPERTY CXX_STANDARD 11) # Use C++11 + diff --git a/FindSFML.cmake b/FindSFML.cmake index 9acf23d..6bf1af6 100644 --- a/FindSFML.cmake +++ b/FindSFML.cmake @@ -1,369 +1,369 @@ -# This script locates the SFML library -# ------------------------------------ -# -# Usage -# ----- -# -# When you try to locate the SFML libraries, you must specify which modules you want to use (system, window, graphics, network, audio, main). -# If none is given, the SFML_LIBRARIES variable will be empty and you'll end up linking to nothing. -# example: -# find_package(SFML COMPONENTS graphics window system) # find the graphics, window and system modules -# -# You can enforce a specific version, either MAJOR.MINOR or only MAJOR. -# If nothing is specified, the version won't be checked (i.e. any version will be accepted). -# example: -# find_package(SFML COMPONENTS ...) # no specific version required -# find_package(SFML 2 COMPONENTS ...) # any 2.x version -# find_package(SFML 2.4 COMPONENTS ...) # version 2.4 or greater -# -# By default, the dynamic libraries of SFML will be found. To find the static ones instead, -# you must set the SFML_STATIC_LIBRARIES variable to TRUE before calling find_package(SFML ...). -# Since you have to link yourself all the SFML dependencies when you link it statically, the following -# additional variables are defined: SFML_XXX_DEPENDENCIES and SFML_DEPENDENCIES (see their detailed -# description below). -# In case of static linking, the SFML_STATIC macro will also be defined by this script. -# example: -# set(SFML_STATIC_LIBRARIES TRUE) -# find_package(SFML 2 COMPONENTS network system) -# -# On Mac OS X if SFML_STATIC_LIBRARIES is not set to TRUE then by default CMake will search for frameworks unless -# CMAKE_FIND_FRAMEWORK is set to "NEVER" for example. Please refer to CMake documentation for more details. -# Moreover, keep in mind that SFML frameworks are only available as release libraries unlike dylibs which -# are available for both release and debug modes. -# -# If SFML is not installed in a standard path, you can use the SFML_ROOT CMake (or environment) variable -# to tell CMake where SFML is. -# -# Output -# ------ -# -# This script defines the following variables: -# - For each specified module XXX (system, window, graphics, network, audio, main): -# - SFML_XXX_LIBRARY_DEBUG: the name of the debug library of the xxx module (set to SFML_XXX_LIBRARY_RELEASE is no debug version is found) -# - SFML_XXX_LIBRARY_RELEASE: the name of the release library of the xxx module (set to SFML_XXX_LIBRARY_DEBUG is no release version is found) -# - SFML_XXX_LIBRARY: the name of the library to link to for the xxx module (includes both debug and optimized names if necessary) -# - SFML_XXX_FOUND: true if either the debug or release library of the xxx module is found -# - SFML_XXX_DEPENDENCIES: the list of libraries the module depends on, in case of static linking -# - SFML_LIBRARIES: the list of all libraries corresponding to the required modules -# - SFML_FOUND: true if all the required modules are found -# - SFML_INCLUDE_DIR: the path where SFML headers are located (the directory containing the SFML/Config.hpp file) -# - SFML_DEPENDENCIES: the list of libraries SFML depends on, in case of static linking -# -# example: -# find_package(SFML 2 COMPONENTS system window graphics audio REQUIRED) -# include_directories(${SFML_INCLUDE_DIR}) -# add_executable(myapp ...) -# target_link_libraries(myapp ${SFML_LIBRARIES}) - -# define the SFML_STATIC macro if static build was chosen -if(SFML_STATIC_LIBRARIES) - add_definitions(-DSFML_STATIC) -endif() - -# define the list of search paths for headers and libraries -set(FIND_SFML_PATHS - ${SFML_ROOT} - $ENV{SFML_ROOT} - ~/Library/Frameworks - /Library/Frameworks - /usr/local - /usr - /sw - /opt/local - /opt/csw - /opt) - -# find the SFML include directory -find_path(SFML_INCLUDE_DIR SFML/Config.hpp - PATH_SUFFIXES include - PATHS ${FIND_SFML_PATHS}) - -# check the version number -set(SFML_VERSION_OK TRUE) -if(SFML_FIND_VERSION AND SFML_INCLUDE_DIR) - # extract the major and minor version numbers from SFML/Config.hpp - # we have to handle framework a little bit differently: - if("${SFML_INCLUDE_DIR}" MATCHES "SFML.framework") - set(SFML_CONFIG_HPP_INPUT "${SFML_INCLUDE_DIR}/Headers/Config.hpp") - else() - set(SFML_CONFIG_HPP_INPUT "${SFML_INCLUDE_DIR}/SFML/Config.hpp") - endif() - FILE(READ "${SFML_CONFIG_HPP_INPUT}" SFML_CONFIG_HPP_CONTENTS) - STRING(REGEX REPLACE ".*#define SFML_VERSION_MAJOR ([0-9]+).*" "\\1" SFML_VERSION_MAJOR "${SFML_CONFIG_HPP_CONTENTS}") - STRING(REGEX REPLACE ".*#define SFML_VERSION_MINOR ([0-9]+).*" "\\1" SFML_VERSION_MINOR "${SFML_CONFIG_HPP_CONTENTS}") - STRING(REGEX REPLACE ".*#define SFML_VERSION_PATCH ([0-9]+).*" "\\1" SFML_VERSION_PATCH "${SFML_CONFIG_HPP_CONTENTS}") - if (NOT "${SFML_VERSION_PATCH}" MATCHES "^[0-9]+$") - set(SFML_VERSION_PATCH 0) - endif() - math(EXPR SFML_REQUESTED_VERSION "${SFML_FIND_VERSION_MAJOR} * 10000 + ${SFML_FIND_VERSION_MINOR} * 100 + ${SFML_FIND_VERSION_PATCH}") - - # if we could extract them, compare with the requested version number - if (SFML_VERSION_MAJOR) - # transform version numbers to an integer - math(EXPR SFML_VERSION "${SFML_VERSION_MAJOR} * 10000 + ${SFML_VERSION_MINOR} * 100 + ${SFML_VERSION_PATCH}") - - # compare them - if(SFML_VERSION LESS SFML_REQUESTED_VERSION) - set(SFML_VERSION_OK FALSE) - endif() - else() - # SFML version is < 2.0 - if (SFML_REQUESTED_VERSION GREATER 10900) - set(SFML_VERSION_OK FALSE) - set(SFML_VERSION_MAJOR 1) - set(SFML_VERSION_MINOR x) - set(SFML_VERSION_PATCH x) - endif() - endif() -endif() - -# find the requested modules -set(SFML_FOUND TRUE) # will be set to false if one of the required modules is not found -foreach(FIND_SFML_COMPONENT ${SFML_FIND_COMPONENTS}) - string(TOLOWER ${FIND_SFML_COMPONENT} FIND_SFML_COMPONENT_LOWER) - string(TOUPPER ${FIND_SFML_COMPONENT} FIND_SFML_COMPONENT_UPPER) - set(FIND_SFML_COMPONENT_NAME sfml-${FIND_SFML_COMPONENT_LOWER}) - - # no suffix for sfml-main, it is always a static library - if(FIND_SFML_COMPONENT_LOWER STREQUAL "main") - # release library - find_library(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE - NAMES ${FIND_SFML_COMPONENT_NAME} - PATH_SUFFIXES lib64 lib - PATHS ${FIND_SFML_PATHS}) - - # debug library - find_library(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG - NAMES ${FIND_SFML_COMPONENT_NAME}-d - PATH_SUFFIXES lib64 lib - PATHS ${FIND_SFML_PATHS}) - else() - # static release library - find_library(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_STATIC_RELEASE - NAMES ${FIND_SFML_COMPONENT_NAME}-s - PATH_SUFFIXES lib64 lib - PATHS ${FIND_SFML_PATHS}) - - # static debug library - find_library(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_STATIC_DEBUG - NAMES ${FIND_SFML_COMPONENT_NAME}-s-d - PATH_SUFFIXES lib64 lib - PATHS ${FIND_SFML_PATHS}) - - # dynamic release library - find_library(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DYNAMIC_RELEASE - NAMES ${FIND_SFML_COMPONENT_NAME} - PATH_SUFFIXES lib64 lib - PATHS ${FIND_SFML_PATHS}) - - # dynamic debug library - find_library(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DYNAMIC_DEBUG - NAMES ${FIND_SFML_COMPONENT_NAME}-d - PATH_SUFFIXES lib64 lib - PATHS ${FIND_SFML_PATHS}) - - # choose the entries that fit the requested link type - if(SFML_STATIC_LIBRARIES) - if(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_STATIC_RELEASE) - set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_STATIC_RELEASE}) - endif() - if(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_STATIC_DEBUG) - set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_STATIC_DEBUG}) - endif() - else() - if(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DYNAMIC_RELEASE) - set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DYNAMIC_RELEASE}) - endif() - if(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DYNAMIC_DEBUG) - set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DYNAMIC_DEBUG}) - endif() - endif() - endif() - - if (SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG OR SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE) - # library found - set(SFML_${FIND_SFML_COMPONENT_UPPER}_FOUND TRUE) - - # if both are found, set SFML_XXX_LIBRARY to contain both - if (SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG AND SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE) - set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY debug ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG} - optimized ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE}) - endif() - - # if only one debug/release variant is found, set the other to be equal to the found one - if (SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG AND NOT SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE) - # debug and not release - set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG}) - set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG}) - endif() - if (SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE AND NOT SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG) - # release and not debug - set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE}) - set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE}) - endif() - else() - # library not found - set(SFML_FOUND FALSE) - set(SFML_${FIND_SFML_COMPONENT_UPPER}_FOUND FALSE) - set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY "") - set(FIND_SFML_MISSING "${FIND_SFML_MISSING} SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY") - endif() - - # mark as advanced - MARK_AS_ADVANCED(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY - SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE - SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG - SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_STATIC_RELEASE - SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_STATIC_DEBUG - SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DYNAMIC_RELEASE - SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DYNAMIC_DEBUG) - - # add to the global list of libraries - set(SFML_LIBRARIES ${SFML_LIBRARIES} "${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY}") -endforeach() - -# in case of static linking, we must also define the list of all the dependencies of SFML libraries -if(SFML_STATIC_LIBRARIES) - - # detect the OS - if(${CMAKE_SYSTEM_NAME} MATCHES "Windows") - set(FIND_SFML_OS_WINDOWS 1) - elseif(${CMAKE_SYSTEM_NAME} MATCHES "Linux") - set(FIND_SFML_OS_LINUX 1) - elseif(${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD") - set(FIND_SFML_OS_FREEBSD 1) - elseif(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") - set(FIND_SFML_OS_MACOSX 1) - endif() - - # start with an empty list - set(SFML_DEPENDENCIES) - set(FIND_SFML_DEPENDENCIES_NOTFOUND) - - # macro that searches for a 3rd-party library - macro(find_sfml_dependency output friendlyname) - # No lookup in environment variables (PATH on Windows), as they may contain wrong library versions - find_library(${output} NAMES ${ARGN} PATHS ${FIND_SFML_PATHS} PATH_SUFFIXES lib NO_SYSTEM_ENVIRONMENT_PATH) - if(${${output}} STREQUAL "${output}-NOTFOUND") - unset(output) - set(FIND_SFML_DEPENDENCIES_NOTFOUND "${FIND_SFML_DEPENDENCIES_NOTFOUND} ${friendlyname}") - endif() - endmacro() - - # sfml-system - list(FIND SFML_FIND_COMPONENTS "system" FIND_SFML_SYSTEM_COMPONENT) - if(NOT ${FIND_SFML_SYSTEM_COMPONENT} EQUAL -1) - - # update the list -- these are only system libraries, no need to find them - if(FIND_SFML_OS_LINUX OR FIND_SFML_OS_FREEBSD OR FIND_SFML_OS_MACOSX) - set(SFML_SYSTEM_DEPENDENCIES "pthread") - endif() - if(FIND_SFML_OS_LINUX) - set(SFML_SYSTEM_DEPENDENCIES ${SFML_SYSTEM_DEPENDENCIES} "rt") - endif() - if(FIND_SFML_OS_WINDOWS) - set(SFML_SYSTEM_DEPENDENCIES "winmm") - endif() - set(SFML_DEPENDENCIES ${SFML_SYSTEM_DEPENDENCIES} ${SFML_DEPENDENCIES}) - endif() - - # sfml-network - list(FIND SFML_FIND_COMPONENTS "network" FIND_SFML_NETWORK_COMPONENT) - if(NOT ${FIND_SFML_NETWORK_COMPONENT} EQUAL -1) - - # update the list -- these are only system libraries, no need to find them - if(FIND_SFML_OS_WINDOWS) - set(SFML_NETWORK_DEPENDENCIES "ws2_32") - endif() - set(SFML_DEPENDENCIES ${SFML_NETWORK_DEPENDENCIES} ${SFML_DEPENDENCIES}) - endif() - - # sfml-window - list(FIND SFML_FIND_COMPONENTS "window" FIND_SFML_WINDOW_COMPONENT) - if(NOT ${FIND_SFML_WINDOW_COMPONENT} EQUAL -1) - - # find libraries - if(FIND_SFML_OS_LINUX OR FIND_SFML_OS_FREEBSD) - find_sfml_dependency(X11_LIBRARY "X11" X11) - find_sfml_dependency(LIBXCB_LIBRARIES "XCB" xcb libxcb) - find_sfml_dependency(X11_XCB_LIBRARY "X11-xcb" X11-xcb libX11-xcb) - find_sfml_dependency(XCB_RANDR_LIBRARY "xcb-randr" xcb-randr libxcb-randr) - find_sfml_dependency(XCB_IMAGE_LIBRARY "xcb-image" xcb-image libxcb-image) - endif() - - if(FIND_SFML_OS_LINUX) - find_sfml_dependency(UDEV_LIBRARIES "UDev" udev libudev) - endif() - - # update the list - if(FIND_SFML_OS_WINDOWS) - set(SFML_WINDOW_DEPENDENCIES ${SFML_WINDOW_DEPENDENCIES} "opengl32" "winmm" "gdi32") - elseif(FIND_SFML_OS_LINUX) - set(SFML_WINDOW_DEPENDENCIES ${SFML_WINDOW_DEPENDENCIES} "GL" ${X11_LIBRARY} ${LIBXCB_LIBRARIES} ${X11_XCB_LIBRARY} ${XCB_RANDR_LIBRARY} ${XCB_IMAGE_LIBRARY} ${UDEV_LIBRARIES}) - elseif(FIND_SFML_OS_FREEBSD) - set(SFML_WINDOW_DEPENDENCIES ${SFML_WINDOW_DEPENDENCIES} "GL" ${X11_LIBRARY} ${LIBXCB_LIBRARIES} ${X11_XCB_LIBRARY} ${XCB_RANDR_LIBRARY} ${XCB_IMAGE_LIBRARY} "usbhid") - elseif(FIND_SFML_OS_MACOSX) - set(SFML_WINDOW_DEPENDENCIES ${SFML_WINDOW_DEPENDENCIES} "-framework OpenGL -framework Foundation -framework AppKit -framework IOKit -framework Carbon") - endif() - set(SFML_DEPENDENCIES ${SFML_WINDOW_DEPENDENCIES} ${SFML_DEPENDENCIES}) - endif() - - # sfml-graphics - list(FIND SFML_FIND_COMPONENTS "graphics" FIND_SFML_GRAPHICS_COMPONENT) - if(NOT ${FIND_SFML_GRAPHICS_COMPONENT} EQUAL -1) - - # find libraries - find_sfml_dependency(FREETYPE_LIBRARY "FreeType" freetype) - find_sfml_dependency(JPEG_LIBRARY "libjpeg" jpeg) - - # update the list - set(SFML_GRAPHICS_DEPENDENCIES ${FREETYPE_LIBRARY} ${JPEG_LIBRARY}) - set(SFML_DEPENDENCIES ${SFML_GRAPHICS_DEPENDENCIES} ${SFML_DEPENDENCIES}) - endif() - - # sfml-audio - list(FIND SFML_FIND_COMPONENTS "audio" FIND_SFML_AUDIO_COMPONENT) - if(NOT ${FIND_SFML_AUDIO_COMPONENT} EQUAL -1) - - # find libraries - find_sfml_dependency(OPENAL_LIBRARY "OpenAL" openal openal32) - find_sfml_dependency(OGG_LIBRARY "Ogg" ogg) - find_sfml_dependency(VORBIS_LIBRARY "Vorbis" vorbis) - find_sfml_dependency(VORBISFILE_LIBRARY "VorbisFile" vorbisfile) - find_sfml_dependency(VORBISENC_LIBRARY "VorbisEnc" vorbisenc) - find_sfml_dependency(FLAC_LIBRARY "FLAC" FLAC) - - # update the list - set(SFML_AUDIO_DEPENDENCIES ${OPENAL_LIBRARY} ${FLAC_LIBRARY} ${VORBISENC_LIBRARY} ${VORBISFILE_LIBRARY} ${VORBIS_LIBRARY} ${OGG_LIBRARY}) - set(SFML_DEPENDENCIES ${SFML_DEPENDENCIES} ${SFML_AUDIO_DEPENDENCIES}) - endif() - -endif() - -# handle errors -if(NOT SFML_VERSION_OK) - # SFML version not ok - set(FIND_SFML_ERROR "SFML found but version too low (requested: ${SFML_FIND_VERSION}, found: ${SFML_VERSION_MAJOR}.${SFML_VERSION_MINOR}.${SFML_VERSION_PATCH})") - set(SFML_FOUND FALSE) -elseif(SFML_STATIC_LIBRARIES AND FIND_SFML_DEPENDENCIES_NOTFOUND) - set(FIND_SFML_ERROR "SFML found but some of its dependencies are missing (${FIND_SFML_DEPENDENCIES_NOTFOUND})") - set(SFML_FOUND FALSE) -elseif(NOT SFML_FOUND) - # include directory or library not found - set(FIND_SFML_ERROR "Could NOT find SFML (missing: ${FIND_SFML_MISSING})") -endif() -if (NOT SFML_FOUND) - if(SFML_FIND_REQUIRED) - # fatal error - message(FATAL_ERROR ${FIND_SFML_ERROR}) - elseif(NOT SFML_FIND_QUIETLY) - # error but continue - message("${FIND_SFML_ERROR}") - endif() -endif() - -# handle success -if(SFML_FOUND AND NOT SFML_FIND_QUIETLY) - message(STATUS "Found SFML ${SFML_VERSION_MAJOR}.${SFML_VERSION_MINOR}.${SFML_VERSION_PATCH} in ${SFML_INCLUDE_DIR}") -endif() - +# This script locates the SFML library +# ------------------------------------ +# +# Usage +# ----- +# +# When you try to locate the SFML libraries, you must specify which modules you want to use (system, window, graphics, network, audio, main). +# If none is given, the SFML_LIBRARIES variable will be empty and you'll end up linking to nothing. +# example: +# find_package(SFML COMPONENTS graphics window system) # find the graphics, window and system modules +# +# You can enforce a specific version, either MAJOR.MINOR or only MAJOR. +# If nothing is specified, the version won't be checked (i.e. any version will be accepted). +# example: +# find_package(SFML COMPONENTS ...) # no specific version required +# find_package(SFML 2 COMPONENTS ...) # any 2.x version +# find_package(SFML 2.4 COMPONENTS ...) # version 2.4 or greater +# +# By default, the dynamic libraries of SFML will be found. To find the static ones instead, +# you must set the SFML_STATIC_LIBRARIES variable to TRUE before calling find_package(SFML ...). +# Since you have to link yourself all the SFML dependencies when you link it statically, the following +# additional variables are defined: SFML_XXX_DEPENDENCIES and SFML_DEPENDENCIES (see their detailed +# description below). +# In case of static linking, the SFML_STATIC macro will also be defined by this script. +# example: +# set(SFML_STATIC_LIBRARIES TRUE) +# find_package(SFML 2 COMPONENTS network system) +# +# On Mac OS X if SFML_STATIC_LIBRARIES is not set to TRUE then by default CMake will search for frameworks unless +# CMAKE_FIND_FRAMEWORK is set to "NEVER" for example. Please refer to CMake documentation for more details. +# Moreover, keep in mind that SFML frameworks are only available as release libraries unlike dylibs which +# are available for both release and debug modes. +# +# If SFML is not installed in a standard path, you can use the SFML_ROOT CMake (or environment) variable +# to tell CMake where SFML is. +# +# Output +# ------ +# +# This script defines the following variables: +# - For each specified module XXX (system, window, graphics, network, audio, main): +# - SFML_XXX_LIBRARY_DEBUG: the name of the debug library of the xxx module (set to SFML_XXX_LIBRARY_RELEASE is no debug version is found) +# - SFML_XXX_LIBRARY_RELEASE: the name of the release library of the xxx module (set to SFML_XXX_LIBRARY_DEBUG is no release version is found) +# - SFML_XXX_LIBRARY: the name of the library to link to for the xxx module (includes both debug and optimized names if necessary) +# - SFML_XXX_FOUND: true if either the debug or release library of the xxx module is found +# - SFML_XXX_DEPENDENCIES: the list of libraries the module depends on, in case of static linking +# - SFML_LIBRARIES: the list of all libraries corresponding to the required modules +# - SFML_FOUND: true if all the required modules are found +# - SFML_INCLUDE_DIR: the path where SFML headers are located (the directory containing the SFML/Config.hpp file) +# - SFML_DEPENDENCIES: the list of libraries SFML depends on, in case of static linking +# +# example: +# find_package(SFML 2 COMPONENTS system window graphics audio REQUIRED) +# include_directories(${SFML_INCLUDE_DIR}) +# add_executable(myapp ...) +# target_link_libraries(myapp ${SFML_LIBRARIES}) + +# define the SFML_STATIC macro if static build was chosen +if(SFML_STATIC_LIBRARIES) + add_definitions(-DSFML_STATIC) +endif() + +# define the list of search paths for headers and libraries +set(FIND_SFML_PATHS + ${SFML_ROOT} + $ENV{SFML_ROOT} + ~/Library/Frameworks + /Library/Frameworks + /usr/local + /usr + /sw + /opt/local + /opt/csw + /opt) + +# find the SFML include directory +find_path(SFML_INCLUDE_DIR SFML/Config.hpp + PATH_SUFFIXES include + PATHS ${FIND_SFML_PATHS}) + +# check the version number +set(SFML_VERSION_OK TRUE) +if(SFML_FIND_VERSION AND SFML_INCLUDE_DIR) + # extract the major and minor version numbers from SFML/Config.hpp + # we have to handle framework a little bit differently: + if("${SFML_INCLUDE_DIR}" MATCHES "SFML.framework") + set(SFML_CONFIG_HPP_INPUT "${SFML_INCLUDE_DIR}/Headers/Config.hpp") + else() + set(SFML_CONFIG_HPP_INPUT "${SFML_INCLUDE_DIR}/SFML/Config.hpp") + endif() + FILE(READ "${SFML_CONFIG_HPP_INPUT}" SFML_CONFIG_HPP_CONTENTS) + STRING(REGEX REPLACE ".*#define SFML_VERSION_MAJOR ([0-9]+).*" "\\1" SFML_VERSION_MAJOR "${SFML_CONFIG_HPP_CONTENTS}") + STRING(REGEX REPLACE ".*#define SFML_VERSION_MINOR ([0-9]+).*" "\\1" SFML_VERSION_MINOR "${SFML_CONFIG_HPP_CONTENTS}") + STRING(REGEX REPLACE ".*#define SFML_VERSION_PATCH ([0-9]+).*" "\\1" SFML_VERSION_PATCH "${SFML_CONFIG_HPP_CONTENTS}") + if (NOT "${SFML_VERSION_PATCH}" MATCHES "^[0-9]+$") + set(SFML_VERSION_PATCH 0) + endif() + math(EXPR SFML_REQUESTED_VERSION "${SFML_FIND_VERSION_MAJOR} * 10000 + ${SFML_FIND_VERSION_MINOR} * 100 + ${SFML_FIND_VERSION_PATCH}") + + # if we could extract them, compare with the requested version number + if (SFML_VERSION_MAJOR) + # transform version numbers to an integer + math(EXPR SFML_VERSION "${SFML_VERSION_MAJOR} * 10000 + ${SFML_VERSION_MINOR} * 100 + ${SFML_VERSION_PATCH}") + + # compare them + if(SFML_VERSION LESS SFML_REQUESTED_VERSION) + set(SFML_VERSION_OK FALSE) + endif() + else() + # SFML version is < 2.0 + if (SFML_REQUESTED_VERSION GREATER 10900) + set(SFML_VERSION_OK FALSE) + set(SFML_VERSION_MAJOR 1) + set(SFML_VERSION_MINOR x) + set(SFML_VERSION_PATCH x) + endif() + endif() +endif() + +# find the requested modules +set(SFML_FOUND TRUE) # will be set to false if one of the required modules is not found +foreach(FIND_SFML_COMPONENT ${SFML_FIND_COMPONENTS}) + string(TOLOWER ${FIND_SFML_COMPONENT} FIND_SFML_COMPONENT_LOWER) + string(TOUPPER ${FIND_SFML_COMPONENT} FIND_SFML_COMPONENT_UPPER) + set(FIND_SFML_COMPONENT_NAME sfml-${FIND_SFML_COMPONENT_LOWER}) + + # no suffix for sfml-main, it is always a static library + if(FIND_SFML_COMPONENT_LOWER STREQUAL "main") + # release library + find_library(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE + NAMES ${FIND_SFML_COMPONENT_NAME} + PATH_SUFFIXES lib64 lib + PATHS ${FIND_SFML_PATHS}) + + # debug library + find_library(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG + NAMES ${FIND_SFML_COMPONENT_NAME}-d + PATH_SUFFIXES lib64 lib + PATHS ${FIND_SFML_PATHS}) + else() + # static release library + find_library(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_STATIC_RELEASE + NAMES ${FIND_SFML_COMPONENT_NAME}-s + PATH_SUFFIXES lib64 lib + PATHS ${FIND_SFML_PATHS}) + + # static debug library + find_library(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_STATIC_DEBUG + NAMES ${FIND_SFML_COMPONENT_NAME}-s-d + PATH_SUFFIXES lib64 lib + PATHS ${FIND_SFML_PATHS}) + + # dynamic release library + find_library(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DYNAMIC_RELEASE + NAMES ${FIND_SFML_COMPONENT_NAME} + PATH_SUFFIXES lib64 lib + PATHS ${FIND_SFML_PATHS}) + + # dynamic debug library + find_library(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DYNAMIC_DEBUG + NAMES ${FIND_SFML_COMPONENT_NAME}-d + PATH_SUFFIXES lib64 lib + PATHS ${FIND_SFML_PATHS}) + + # choose the entries that fit the requested link type + if(SFML_STATIC_LIBRARIES) + if(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_STATIC_RELEASE) + set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_STATIC_RELEASE}) + endif() + if(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_STATIC_DEBUG) + set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_STATIC_DEBUG}) + endif() + else() + if(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DYNAMIC_RELEASE) + set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DYNAMIC_RELEASE}) + endif() + if(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DYNAMIC_DEBUG) + set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DYNAMIC_DEBUG}) + endif() + endif() + endif() + + if (SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG OR SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE) + # library found + set(SFML_${FIND_SFML_COMPONENT_UPPER}_FOUND TRUE) + + # if both are found, set SFML_XXX_LIBRARY to contain both + if (SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG AND SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE) + set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY debug ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG} + optimized ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE}) + endif() + + # if only one debug/release variant is found, set the other to be equal to the found one + if (SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG AND NOT SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE) + # debug and not release + set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG}) + set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG}) + endif() + if (SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE AND NOT SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG) + # release and not debug + set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE}) + set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY ${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE}) + endif() + else() + # library not found + set(SFML_FOUND FALSE) + set(SFML_${FIND_SFML_COMPONENT_UPPER}_FOUND FALSE) + set(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY "") + set(FIND_SFML_MISSING "${FIND_SFML_MISSING} SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY") + endif() + + # mark as advanced + MARK_AS_ADVANCED(SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY + SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_RELEASE + SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DEBUG + SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_STATIC_RELEASE + SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_STATIC_DEBUG + SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DYNAMIC_RELEASE + SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY_DYNAMIC_DEBUG) + + # add to the global list of libraries + set(SFML_LIBRARIES ${SFML_LIBRARIES} "${SFML_${FIND_SFML_COMPONENT_UPPER}_LIBRARY}") +endforeach() + +# in case of static linking, we must also define the list of all the dependencies of SFML libraries +if(SFML_STATIC_LIBRARIES) + + # detect the OS + if(${CMAKE_SYSTEM_NAME} MATCHES "Windows") + set(FIND_SFML_OS_WINDOWS 1) + elseif(${CMAKE_SYSTEM_NAME} MATCHES "Linux") + set(FIND_SFML_OS_LINUX 1) + elseif(${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD") + set(FIND_SFML_OS_FREEBSD 1) + elseif(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") + set(FIND_SFML_OS_MACOSX 1) + endif() + + # start with an empty list + set(SFML_DEPENDENCIES) + set(FIND_SFML_DEPENDENCIES_NOTFOUND) + + # macro that searches for a 3rd-party library + macro(find_sfml_dependency output friendlyname) + # No lookup in environment variables (PATH on Windows), as they may contain wrong library versions + find_library(${output} NAMES ${ARGN} PATHS ${FIND_SFML_PATHS} PATH_SUFFIXES lib NO_SYSTEM_ENVIRONMENT_PATH) + if(${${output}} STREQUAL "${output}-NOTFOUND") + unset(output) + set(FIND_SFML_DEPENDENCIES_NOTFOUND "${FIND_SFML_DEPENDENCIES_NOTFOUND} ${friendlyname}") + endif() + endmacro() + + # sfml-system + list(FIND SFML_FIND_COMPONENTS "system" FIND_SFML_SYSTEM_COMPONENT) + if(NOT ${FIND_SFML_SYSTEM_COMPONENT} EQUAL -1) + + # update the list -- these are only system libraries, no need to find them + if(FIND_SFML_OS_LINUX OR FIND_SFML_OS_FREEBSD OR FIND_SFML_OS_MACOSX) + set(SFML_SYSTEM_DEPENDENCIES "pthread") + endif() + if(FIND_SFML_OS_LINUX) + set(SFML_SYSTEM_DEPENDENCIES ${SFML_SYSTEM_DEPENDENCIES} "rt") + endif() + if(FIND_SFML_OS_WINDOWS) + set(SFML_SYSTEM_DEPENDENCIES "winmm") + endif() + set(SFML_DEPENDENCIES ${SFML_SYSTEM_DEPENDENCIES} ${SFML_DEPENDENCIES}) + endif() + + # sfml-network + list(FIND SFML_FIND_COMPONENTS "network" FIND_SFML_NETWORK_COMPONENT) + if(NOT ${FIND_SFML_NETWORK_COMPONENT} EQUAL -1) + + # update the list -- these are only system libraries, no need to find them + if(FIND_SFML_OS_WINDOWS) + set(SFML_NETWORK_DEPENDENCIES "ws2_32") + endif() + set(SFML_DEPENDENCIES ${SFML_NETWORK_DEPENDENCIES} ${SFML_DEPENDENCIES}) + endif() + + # sfml-window + list(FIND SFML_FIND_COMPONENTS "window" FIND_SFML_WINDOW_COMPONENT) + if(NOT ${FIND_SFML_WINDOW_COMPONENT} EQUAL -1) + + # find libraries + if(FIND_SFML_OS_LINUX OR FIND_SFML_OS_FREEBSD) + find_sfml_dependency(X11_LIBRARY "X11" X11) + find_sfml_dependency(LIBXCB_LIBRARIES "XCB" xcb libxcb) + find_sfml_dependency(X11_XCB_LIBRARY "X11-xcb" X11-xcb libX11-xcb) + find_sfml_dependency(XCB_RANDR_LIBRARY "xcb-randr" xcb-randr libxcb-randr) + find_sfml_dependency(XCB_IMAGE_LIBRARY "xcb-image" xcb-image libxcb-image) + endif() + + if(FIND_SFML_OS_LINUX) + find_sfml_dependency(UDEV_LIBRARIES "UDev" udev libudev) + endif() + + # update the list + if(FIND_SFML_OS_WINDOWS) + set(SFML_WINDOW_DEPENDENCIES ${SFML_WINDOW_DEPENDENCIES} "opengl32" "winmm" "gdi32") + elseif(FIND_SFML_OS_LINUX) + set(SFML_WINDOW_DEPENDENCIES ${SFML_WINDOW_DEPENDENCIES} "GL" ${X11_LIBRARY} ${LIBXCB_LIBRARIES} ${X11_XCB_LIBRARY} ${XCB_RANDR_LIBRARY} ${XCB_IMAGE_LIBRARY} ${UDEV_LIBRARIES}) + elseif(FIND_SFML_OS_FREEBSD) + set(SFML_WINDOW_DEPENDENCIES ${SFML_WINDOW_DEPENDENCIES} "GL" ${X11_LIBRARY} ${LIBXCB_LIBRARIES} ${X11_XCB_LIBRARY} ${XCB_RANDR_LIBRARY} ${XCB_IMAGE_LIBRARY} "usbhid") + elseif(FIND_SFML_OS_MACOSX) + set(SFML_WINDOW_DEPENDENCIES ${SFML_WINDOW_DEPENDENCIES} "-framework OpenGL -framework Foundation -framework AppKit -framework IOKit -framework Carbon") + endif() + set(SFML_DEPENDENCIES ${SFML_WINDOW_DEPENDENCIES} ${SFML_DEPENDENCIES}) + endif() + + # sfml-graphics + list(FIND SFML_FIND_COMPONENTS "graphics" FIND_SFML_GRAPHICS_COMPONENT) + if(NOT ${FIND_SFML_GRAPHICS_COMPONENT} EQUAL -1) + + # find libraries + find_sfml_dependency(FREETYPE_LIBRARY "FreeType" freetype) + find_sfml_dependency(JPEG_LIBRARY "libjpeg" jpeg) + + # update the list + set(SFML_GRAPHICS_DEPENDENCIES ${FREETYPE_LIBRARY} ${JPEG_LIBRARY}) + set(SFML_DEPENDENCIES ${SFML_GRAPHICS_DEPENDENCIES} ${SFML_DEPENDENCIES}) + endif() + + # sfml-audio + list(FIND SFML_FIND_COMPONENTS "audio" FIND_SFML_AUDIO_COMPONENT) + if(NOT ${FIND_SFML_AUDIO_COMPONENT} EQUAL -1) + + # find libraries + find_sfml_dependency(OPENAL_LIBRARY "OpenAL" openal openal32) + find_sfml_dependency(OGG_LIBRARY "Ogg" ogg) + find_sfml_dependency(VORBIS_LIBRARY "Vorbis" vorbis) + find_sfml_dependency(VORBISFILE_LIBRARY "VorbisFile" vorbisfile) + find_sfml_dependency(VORBISENC_LIBRARY "VorbisEnc" vorbisenc) + find_sfml_dependency(FLAC_LIBRARY "FLAC" FLAC) + + # update the list + set(SFML_AUDIO_DEPENDENCIES ${OPENAL_LIBRARY} ${FLAC_LIBRARY} ${VORBISENC_LIBRARY} ${VORBISFILE_LIBRARY} ${VORBIS_LIBRARY} ${OGG_LIBRARY}) + set(SFML_DEPENDENCIES ${SFML_DEPENDENCIES} ${SFML_AUDIO_DEPENDENCIES}) + endif() + +endif() + +# handle errors +if(NOT SFML_VERSION_OK) + # SFML version not ok + set(FIND_SFML_ERROR "SFML found but version too low (requested: ${SFML_FIND_VERSION}, found: ${SFML_VERSION_MAJOR}.${SFML_VERSION_MINOR}.${SFML_VERSION_PATCH})") + set(SFML_FOUND FALSE) +elseif(SFML_STATIC_LIBRARIES AND FIND_SFML_DEPENDENCIES_NOTFOUND) + set(FIND_SFML_ERROR "SFML found but some of its dependencies are missing (${FIND_SFML_DEPENDENCIES_NOTFOUND})") + set(SFML_FOUND FALSE) +elseif(NOT SFML_FOUND) + # include directory or library not found + set(FIND_SFML_ERROR "Could NOT find SFML (missing: ${FIND_SFML_MISSING})") +endif() +if (NOT SFML_FOUND) + if(SFML_FIND_REQUIRED) + # fatal error + message(FATAL_ERROR ${FIND_SFML_ERROR}) + elseif(NOT SFML_FIND_QUIETLY) + # error but continue + message("${FIND_SFML_ERROR}") + endif() +endif() + +# handle success +if(SFML_FOUND AND NOT SFML_FIND_QUIETLY) + message(STATUS "Found SFML ${SFML_VERSION_MAJOR}.${SFML_VERSION_MINOR}.${SFML_VERSION_PATCH} in ${SFML_INCLUDE_DIR}") +endif() + diff --git a/README.md b/README.md index ed53386..5757608 100644 --- a/README.md +++ b/README.md @@ -1 +1 @@ -#How to Build +#How to Build diff --git a/include/Curses.h b/include/Curses.h index 91443a9..0634101 100644 --- a/include/Curses.h +++ b/include/Curses.h @@ -1,138 +1,138 @@ -#pragma once -#include -#include - -class Curses { -public: - - struct Slot { - Slot(wchar_t unicode_value_, - sf::Color font_color_, - sf::Color backfill_color_) : - unicode_value(unicode_value_), - font_color(font_color_), - backfill_color(backfill_color_) - {}; - - wchar_t unicode_value; - sf::Color font_color; - sf::Color backfill_color; - }; - - struct Tile { - public: - Tile(sf::Vector2i position_) : - blank_standby(L'\u0020', sf::Color::Transparent, sf::Color::Black), - position(position_) - { }; - - private: - Slot blank_standby; - - int index = 0; // What index in the vector are we. Backbone for blinking and scrolling - int ratio_counter = 0; // Secondary counter to hold index positions for (ratio) length of time - int ratio_value = 0; - - std::vector slot_stack; // The icon that aligns with the index - - sf::Vector2i position; // Position of the text, and backfill - - public: - void set_ratio(int ratio_) { - ratio_value = ratio_; - } - - sf::Vector2i getPosition() const { - return position; - } - - void push_back(Slot s) { - slot_stack.push_back(s); - } - - void clear_and_set(Slot s) { - slot_stack.clear(); - slot_stack.push_back(s); - } - - void clear() { - slot_stack.clear(); - } - - sf::Color current_font_color() { - if (slot_stack.size() > 0) - return slot_stack.at(index).font_color; - else - return blank_standby.font_color; - } - - sf::Color current_backfill_color() { - if (slot_stack.size() > 0) - return slot_stack.at(index).backfill_color; - else - return blank_standby.backfill_color; - } - - wchar_t current_unicode_value() { - if (slot_stack.size() > 0) - return slot_stack.at(index).unicode_value; - else - return blank_standby.unicode_value; - } - - void inc_index() { - if (index >= slot_stack.size() - 1) { - index = 0; - } - else if (ratio_counter == ratio_value) { - ratio_counter = 0; - index++; - } - else - ratio_counter++; - } - }; - - Curses(sf::Vector2i tile_size_, sf::Vector2i grid_dimensions); - ~Curses(); - - - void Update(double delta_time_); - void Render(); - - - void setTile(Tile tile_); - void setTiles(std::vector tiles_); // Can be seperate, non-adjacent tiles - - void Clear(); - - Tile* getTile(sf::Vector2i position_); - std::vector getTiles(sf::Vector2i start_, sf::Vector2i end_); - - void ResizeTiles(sf::Vector2i size_); - void ResizeTileGrid(sf::Vector2i grid_dimensions_); - - void setBlink(int ratio_, sf::Vector2i position_); - void setScroll(int ratio_, sf::Vector2i start_, sf::Vector2i end_); - void setScroll(int ratio_, sf::Vector2i start_, std::list tiles_); - - -private: - - sf::Vector2i grid_dimensions; - sf::Vector2i tile_pixel_dimensions; - - sf::RenderWindow window; - - std::vector tiles; - - sf::Font font; - - int multi_to_linear(sf::Vector2i position_) const; - sf::Vector2i linear_to_multi(int position_) const; - - void set_tile_ratio(int ratio_, sf::Vector2i tile_position_); - void append_slots(sf::Vector2i start_, std::list values_); - -}; - +#pragma once +#include +#include + +class Curses { +public: + + struct Slot { + Slot(wchar_t unicode_value_, + sf::Color font_color_, + sf::Color backfill_color_) : + unicode_value(unicode_value_), + font_color(font_color_), + backfill_color(backfill_color_) + {}; + + wchar_t unicode_value; + sf::Color font_color; + sf::Color backfill_color; + }; + + struct Tile { + public: + Tile(sf::Vector2i position_) : + blank_standby(L'\u0020', sf::Color::Transparent, sf::Color::Black), + position(position_) + { }; + + private: + Slot blank_standby; + + int index = 0; // What index in the vector are we. Backbone for blinking and scrolling + int ratio_counter = 0; // Secondary counter to hold index positions for (ratio) length of time + int ratio_value = 0; + + std::vector slot_stack; // The icon that aligns with the index + + sf::Vector2i position; // Position of the text, and backfill + + public: + void set_ratio(int ratio_) { + ratio_value = ratio_; + } + + sf::Vector2i getPosition() const { + return position; + } + + void push_back(Slot s) { + slot_stack.push_back(s); + } + + void clear_and_set(Slot s) { + slot_stack.clear(); + slot_stack.push_back(s); + } + + void clear() { + slot_stack.clear(); + } + + sf::Color current_font_color() { + if (slot_stack.size() > 0) + return slot_stack.at(index).font_color; + else + return blank_standby.font_color; + } + + sf::Color current_backfill_color() { + if (slot_stack.size() > 0) + return slot_stack.at(index).backfill_color; + else + return blank_standby.backfill_color; + } + + wchar_t current_unicode_value() { + if (slot_stack.size() > 0) + return slot_stack.at(index).unicode_value; + else + return blank_standby.unicode_value; + } + + void inc_index() { + if (index >= slot_stack.size() - 1) { + index = 0; + } + else if (ratio_counter == ratio_value) { + ratio_counter = 0; + index++; + } + else + ratio_counter++; + } + }; + + Curses(sf::Vector2i tile_size_, sf::Vector2i grid_dimensions); + ~Curses(); + + + void Update(double delta_time_); + void Render(); + + + void setTile(Tile tile_); + void setTiles(std::vector tiles_); // Can be seperate, non-adjacent tiles + + void Clear(); + + Tile* getTile(sf::Vector2i position_); + std::vector getTiles(sf::Vector2i start_, sf::Vector2i end_); + + void ResizeTiles(sf::Vector2i size_); + void ResizeTileGrid(sf::Vector2i grid_dimensions_); + + void setBlink(int ratio_, sf::Vector2i position_); + void setScroll(int ratio_, sf::Vector2i start_, sf::Vector2i end_); + void setScroll(int ratio_, sf::Vector2i start_, std::list tiles_); + + +private: + + sf::Vector2i grid_dimensions; + sf::Vector2i tile_pixel_dimensions; + + sf::RenderWindow window; + + std::vector tiles; + + sf::Font font; + + int multi_to_linear(sf::Vector2i position_) const; + sf::Vector2i linear_to_multi(int position_) const; + + void set_tile_ratio(int ratio_, sf::Vector2i tile_position_); + void append_slots(sf::Vector2i start_, std::list values_); + +}; + diff --git a/include/Map.h b/include/Map.h index b001d9b..df3b246 100644 --- a/include/Map.h +++ b/include/Map.h @@ -1,46 +1,46 @@ -#pragma once -#include -#include -#include -#include - -class Map { -public: - Map(sf::Vector3i dim) { - list = new char[dim.x * dim.y * dim.z]; - for (int i = 0; i < dim.x * dim.y * dim.x; i++) { - list[i] = 0; - } - - for (int x = 0; x < dim.x; x++) { - for (int y = 0; y < dim.y; y++) { - for (int z = 0; z < dim.z; z++) { - if (rand() % 100 < 1) - list[x + dim.x * (y + dim.z * z)] = rand() % 6; - else - list[x + dim.x * (y + dim.z * z)] = 0; - } - } - } - - dimensions = dim; - global_light = sf::Vector3f(0.2, 0.4, 1); - } - - ~Map() { - } - - sf::Vector3i getDimensions(); - char *list; - sf::Vector3i dimensions; - - void moveLight(sf::Vector2f in); - sf::Vector3f global_light; - -protected: - -private: - -}; - - +#pragma once +#include +#include +#include +#include + +class Map { +public: + Map(sf::Vector3i dim) { + list = new char[dim.x * dim.y * dim.z]; + for (int i = 0; i < dim.x * dim.y * dim.x; i++) { + list[i] = 0; + } + + for (int x = 0; x < dim.x; x++) { + for (int y = 0; y < dim.y; y++) { + for (int z = 0; z < dim.z; z++) { + if (rand() % 100 < 1) + list[x + dim.x * (y + dim.z * z)] = rand() % 6; + else + list[x + dim.x * (y + dim.z * z)] = 0; + } + } + } + + dimensions = dim; + global_light = sf::Vector3f(0.2, 0.4, 1); + } + + ~Map() { + } + + sf::Vector3i getDimensions(); + char *list; + sf::Vector3i dimensions; + + void moveLight(sf::Vector2f in); + sf::Vector3f global_light; + +protected: + +private: + +}; + + diff --git a/include/Ray.h b/include/Ray.h index d95fd52..2f8a53a 100644 --- a/include/Ray.h +++ b/include/Ray.h @@ -1,46 +1,46 @@ -#pragma once -#include -#include -#include "Map.h" - -class Ray { - - private: - - // The Tail of the vector - sf::Vector3 origin; - - // Direction / Length of the vector - sf::Vector3 direction; - - // The incrementing points at which T intersects int(X, Y, Z) points - sf::Vector3 intersection_t; - - // The speed at which the ray climbs. - // Take the slope of the line (1 / cartesian.x/y/z) = delta_t.x/y/z - sf::Vector3 delta_t; - - // The 3d voxel position the ray is currently at - sf::Vector3 voxel; - - // The 2d pixel coordinate - sf::Vector2 pixel; - - // Reference to the voxel map - Map *map; - - // The dimensions of the voxel map - sf::Vector3 dimensions; - - public: - - Ray( - Map *m, - sf::Vector2 resolution, - sf::Vector2 pixel, - sf::Vector3 camera_position, - sf::Vector3 ray_direction - ); - - sf::Color Cast(); -}; +#pragma once +#include +#include +#include "Map.h" + +class Ray { + + private: + + // The Tail of the vector + sf::Vector3 origin; + + // Direction / Length of the vector + sf::Vector3 direction; + + // The incrementing points at which T intersects int(X, Y, Z) points + sf::Vector3 intersection_t; + + // The speed at which the ray climbs. + // Take the slope of the line (1 / cartesian.x/y/z) = delta_t.x/y/z + sf::Vector3 delta_t; + + // The 3d voxel position the ray is currently at + sf::Vector3 voxel; + + // The 2d pixel coordinate + sf::Vector2 pixel; + + // Reference to the voxel map + Map *map; + + // The dimensions of the voxel map + sf::Vector3 dimensions; + + public: + + Ray( + Map *m, + sf::Vector2 resolution, + sf::Vector2 pixel, + sf::Vector3 camera_position, + sf::Vector3 ray_direction + ); + + sf::Color Cast(); +}; diff --git a/include/RayCaster.h b/include/RayCaster.h index 9d022a7..2612d57 100644 --- a/include/RayCaster.h +++ b/include/RayCaster.h @@ -1,46 +1,46 @@ -#pragma once -#include -#include -#include - -class RayCaster { -public: - RayCaster(Map *map, - sf::Vector3 map_dimensions, - sf::Vector2 viewport_resolution); - ~RayCaster(); - - void setFOV(float fov); - void setResolution(sf::Vector2 resolution); - - sf::Color* CastRays(sf::Vector3 camera_direction, sf::Vector3 camera_position); - void moveCamera(sf::Vector2f v); -private: - - sf::Vector3 map_dimensions; - Map *map; - - // The XY resolution of the viewport - sf::Vector2 resolution; - - // The pixel array, maybe do RBGA? Are there even 4 byte data types? - sf::Color *image; - - // The direction of the camera in POLAR coordinates - sf::Vector3 camera_direction; - - - // Convert the polar coordinates to CARTESIAN - sf::Vector3 camera_direction_cartesian; - - // The world-space position of the camera - sf::Vector3 camera_position; - - // The distance in units the view plane is from the iris point - int view_plane_distance = 200; - - // Precalculated values for the view plane rays - sf::Vector3f *view_plane_vectors; - -}; - +#pragma once +#include +#include +#include + +class RayCaster { +public: + RayCaster(Map *map, + sf::Vector3 map_dimensions, + sf::Vector2 viewport_resolution); + ~RayCaster(); + + void setFOV(float fov); + void setResolution(sf::Vector2 resolution); + + sf::Color* CastRays(sf::Vector3 camera_direction, sf::Vector3 camera_position); + void moveCamera(sf::Vector2f v); +private: + + sf::Vector3 map_dimensions; + Map *map; + + // The XY resolution of the viewport + sf::Vector2 resolution; + + // The pixel array, maybe do RBGA? Are there even 4 byte data types? + sf::Color *image; + + // The direction of the camera in POLAR coordinates + sf::Vector3 camera_direction; + + + // Convert the polar coordinates to CARTESIAN + sf::Vector3 camera_direction_cartesian; + + // The world-space position of the camera + sf::Vector3 camera_position; + + // The distance in units the view plane is from the iris point + int view_plane_distance = 200; + + // Precalculated values for the view plane rays + sf::Vector3f *view_plane_vectors; + +}; + diff --git a/include/Vector3.h b/include/Vector3.h index 26bb241..b75d367 100644 --- a/include/Vector3.h +++ b/include/Vector3.h @@ -1,139 +1,139 @@ -//////////////////////////////////////////////////////////// -// -// SFML - Simple and Fast Multimedia Library -// Copyright (C) 2007-2016 Laurent Gomila (laurent@sfml-dev.org) -// -// 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. -// -//////////////////////////////////////////////////////////// - -#ifndef GAME_VECTOR3_H -#define GAME_VECTOR3_H - -template -class Vector3 -{ -public: - - // Default constructor - // Creates a Vector3(0, 0, 0). - Vector3(); - - - // Construct the vector from its coordinates - Vector3(T X, T Y, T Z); - - - // Construct the vector from another type of vector - // This constructor doesn't replace the copy constructor, - // it's called only when U != T. - // A call to this constructor will fail to compile if U - // is not convertible to T. - template - explicit Vector3(const Vector3& vector); - - // Member data - T x; - T y; - T z; -}; - - // Vector3 - // Overload of unary operator - - // left Vector to negate - // Memberwise opposite of the vector - template - Vector3 operator -(const Vector3& left); - - // Overload of binary operator += - // This operator performs a memberwise addition of both vectors, - // and assigns the result to left. - // returns Reference to left - template - Vector3& operator +=(Vector3& left, const Vector3& right); - - // Overload of binary operator -= - // This operator performs a memberwise subtraction of both vectors, - // and assigns the result to left. - // returns Reference to left - template - Vector3& operator -=(Vector3& left, const Vector3& right); - - // Overload of binary operator + - // returns Memberwise addition of both vectors - template - Vector3 operator +(const Vector3& left, const Vector3& right); - // Overload of binary operator - - // returns Memberwise subtraction of both vectors - template - Vector3 operator -(const Vector3& left, const Vector3& right); - - // Overload of binary operator * - // returns Memberwise multiplication by right - template - Vector3 operator *(const Vector3& left, T right); - - // Overload of binary operator * - // returns Memberwise multiplication by left - template - Vector3 operator *(T left, const Vector3& right); - - // Overload of binary operator *= - // This operator performs a memberwise multiplication by right, - // and assigns the result to left. - // returns Reference to left - template - Vector3& operator *=(Vector3& left, T right); - - // Overload of binary operator / - // returns Memberwise division by right - template - Vector3 operator /(const Vector3& left, T right); - - // Overload of binary operator /= - // This operator performs a memberwise division by right, - // and assigns the result to left. - // returns Reference to left - template - Vector3& operator /=(Vector3& left, T right); - - // Overload of binary operator == - // This operator compares strict equality between two vectors. - // returns True if left is equal to right - template - bool operator ==(const Vector3& left, const Vector3& right); - - // Overload of binary operator != - // This operator compares strict difference between two vectors. - // returns True if left is not equal to right - template - bool operator !=(const Vector3& left, const Vector3& right); - -#include - - // Define the most common types - typedef Vector3 Vector3i; - typedef Vector3 Vector3f; - - - -#endif - - - - +//////////////////////////////////////////////////////////// +// +// SFML - Simple and Fast Multimedia Library +// Copyright (C) 2007-2016 Laurent Gomila (laurent@sfml-dev.org) +// +// 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. +// +//////////////////////////////////////////////////////////// + +#ifndef GAME_VECTOR3_H +#define GAME_VECTOR3_H + +template +class Vector3 +{ +public: + + // Default constructor + // Creates a Vector3(0, 0, 0). + Vector3(); + + + // Construct the vector from its coordinates + Vector3(T X, T Y, T Z); + + + // Construct the vector from another type of vector + // This constructor doesn't replace the copy constructor, + // it's called only when U != T. + // A call to this constructor will fail to compile if U + // is not convertible to T. + template + explicit Vector3(const Vector3& vector); + + // Member data + T x; + T y; + T z; +}; + + // Vector3 + // Overload of unary operator - + // left Vector to negate + // Memberwise opposite of the vector + template + Vector3 operator -(const Vector3& left); + + // Overload of binary operator += + // This operator performs a memberwise addition of both vectors, + // and assigns the result to left. + // returns Reference to left + template + Vector3& operator +=(Vector3& left, const Vector3& right); + + // Overload of binary operator -= + // This operator performs a memberwise subtraction of both vectors, + // and assigns the result to left. + // returns Reference to left + template + Vector3& operator -=(Vector3& left, const Vector3& right); + + // Overload of binary operator + + // returns Memberwise addition of both vectors + template + Vector3 operator +(const Vector3& left, const Vector3& right); + // Overload of binary operator - + // returns Memberwise subtraction of both vectors + template + Vector3 operator -(const Vector3& left, const Vector3& right); + + // Overload of binary operator * + // returns Memberwise multiplication by right + template + Vector3 operator *(const Vector3& left, T right); + + // Overload of binary operator * + // returns Memberwise multiplication by left + template + Vector3 operator *(T left, const Vector3& right); + + // Overload of binary operator *= + // This operator performs a memberwise multiplication by right, + // and assigns the result to left. + // returns Reference to left + template + Vector3& operator *=(Vector3& left, T right); + + // Overload of binary operator / + // returns Memberwise division by right + template + Vector3 operator /(const Vector3& left, T right); + + // Overload of binary operator /= + // This operator performs a memberwise division by right, + // and assigns the result to left. + // returns Reference to left + template + Vector3& operator /=(Vector3& left, T right); + + // Overload of binary operator == + // This operator compares strict equality between two vectors. + // returns True if left is equal to right + template + bool operator ==(const Vector3& left, const Vector3& right); + + // Overload of binary operator != + // This operator compares strict difference between two vectors. + // returns True if left is not equal to right + template + bool operator !=(const Vector3& left, const Vector3& right); + +#include + + // Define the most common types + typedef Vector3 Vector3i; + typedef Vector3 Vector3f; + + + +#endif + + + + diff --git a/include/util.hpp b/include/util.hpp index 8b5591c..af01d75 100644 --- a/include/util.hpp +++ b/include/util.hpp @@ -1,90 +1,90 @@ -#pragma once -#include -#include -#include - -const double PI = 3.141592653589793238463; -const float PI_F = 3.14159265358979f; - -struct fps_counter { -public: - fps_counter(){ - if(!f.loadFromFile("../assets/fonts/Arial.ttf")){ - std::cout << "couldn't find the fall back Arial font in ../assets/fonts/" << std::endl; - } else { - t.setFont(f); - } - } - - void frame(double delta_time){ - frame_count++; - fps_average += (delta_time - fps_average) / frame_count; - } - - void draw(sf::RenderWindow *r){ - t.setString(std::to_string(fps_average)); - r->draw(t); - } - -private: - sf::Font f; - sf::Text t; - int frame_count = 0; - double fps_average = 0; -}; - - -inline sf::Vector3f SphereToCart(sf::Vector3f i) { - - auto r = sf::Vector3f( - (i.x * sin(i.z) * cos(i.y)), - (i.x * sin(i.z) * sin(i.y)), - (i.x * cos(i.z)) - ); - return r; -}; - - -inline sf::Vector3f CartToSphere(sf::Vector3f in) { - - auto r = sf::Vector3f( - sqrt(in.x * in.x + in.y * in.y + in.z * in.z), - atan(in.y / in.x), - atan(sqrt(in.x * in.x + in.y * in.y) / in.z) - ); - return r; -}; - - -inline sf::Vector3f Normalize(sf::Vector3f in) { - - float multiplier = sqrt(in.x * in.x + in.y * in.y + in.z * in.z); - auto r = sf::Vector3f( - in.x / multiplier, - in.y / multiplier, - in.z / multiplier - ); - return r; - -} - - -inline float DotProduct(sf::Vector3f a, sf::Vector3f b){ - return a.x * b.x + a.y * b.y + a.z * b.z; -} - -inline float Magnitude(sf::Vector3f in){ - return sqrt(in.x * in.x + in.y * in.y + in.z * in.z); -} - -inline float AngleBetweenVectors(sf::Vector3f a, sf::Vector3f b){ - return acos(DotProduct(a, b) / (Magnitude(a) * Magnitude(b))); -} - -inline float DegreesToRadians(float in) { - return in * PI / 180.0f; -} - -inline float RadiansToDegrees(float in) { - return in * 180.0f / PI; +#pragma once +#include +#include +#include + +const double PI = 3.141592653589793238463; +const float PI_F = 3.14159265358979f; + +struct fps_counter { +public: + fps_counter(){ + if(!f.loadFromFile("../assets/fonts/Arial.ttf")){ + std::cout << "couldn't find the fall back Arial font in ../assets/fonts/" << std::endl; + } else { + t.setFont(f); + } + } + + void frame(double delta_time){ + frame_count++; + fps_average += (delta_time - fps_average) / frame_count; + } + + void draw(sf::RenderWindow *r){ + t.setString(std::to_string(fps_average)); + r->draw(t); + } + +private: + sf::Font f; + sf::Text t; + int frame_count = 0; + double fps_average = 0; +}; + + +inline sf::Vector3f SphereToCart(sf::Vector3f i) { + + auto r = sf::Vector3f( + (i.x * sin(i.z) * cos(i.y)), + (i.x * sin(i.z) * sin(i.y)), + (i.x * cos(i.z)) + ); + return r; +}; + + +inline sf::Vector3f CartToSphere(sf::Vector3f in) { + + auto r = sf::Vector3f( + sqrt(in.x * in.x + in.y * in.y + in.z * in.z), + atan(in.y / in.x), + atan(sqrt(in.x * in.x + in.y * in.y) / in.z) + ); + return r; +}; + + +inline sf::Vector3f Normalize(sf::Vector3f in) { + + float multiplier = sqrt(in.x * in.x + in.y * in.y + in.z * in.z); + auto r = sf::Vector3f( + in.x / multiplier, + in.y / multiplier, + in.z / multiplier + ); + return r; + +} + + +inline float DotProduct(sf::Vector3f a, sf::Vector3f b){ + return a.x * b.x + a.y * b.y + a.z * b.z; +} + +inline float Magnitude(sf::Vector3f in){ + return sqrt(in.x * in.x + in.y * in.y + in.z * in.z); +} + +inline float AngleBetweenVectors(sf::Vector3f a, sf::Vector3f b){ + return acos(DotProduct(a, b) / (Magnitude(a) * Magnitude(b))); +} + +inline float DegreesToRadians(float in) { + return in * PI / 180.0f; +} + +inline float RadiansToDegrees(float in) { + return in * 180.0f / PI; } \ No newline at end of file diff --git a/kernels/kernel.txt b/kernels/kernel.txt index bf5518c..4ad8dc9 100644 --- a/kernels/kernel.txt +++ b/kernels/kernel.txt @@ -1,32 +1,32 @@ -__constant sampler_t sampler = - CLK_NORMALIZED_COORDS_FALSE - | CLK_ADDRESS_CLAMP_TO_EDGE - | CLK_FILTER_NEAREST; - -__constant int FILTER_SIZE = 10; - -float FilterValue (__constant const float* filterWeights, - const int x, const int y) -{ - return filterWeights[(x+FILTER_SIZE) + (y+FILTER_SIZE)*(FILTER_SIZE*2 + 1)]; -} - -__kernel void Filter ( - __read_only image2d_t input, - __constant float* filterWeights, - __write_only image2d_t output) -{ - const int2 pos = {get_global_id(0), get_global_id(1)}; - - float4 sum = (float4)(0.0f); - for(int y = -FILTER_SIZE; y <= FILTER_SIZE; y++) { - for(int x = -FILTER_SIZE; x <= FILTER_SIZE; x++) { - sum += FilterValue(filterWeights, x, y) - * read_imagef(input, sampler, pos + (int2)(x,y)); - } - } - - write_imagef (output, (int2)(pos.x, pos.y), sum); -} - - +__constant sampler_t sampler = + CLK_NORMALIZED_COORDS_FALSE + | CLK_ADDRESS_CLAMP_TO_EDGE + | CLK_FILTER_NEAREST; + +__constant int FILTER_SIZE = 10; + +float FilterValue (__constant const float* filterWeights, + const int x, const int y) +{ + return filterWeights[(x+FILTER_SIZE) + (y+FILTER_SIZE)*(FILTER_SIZE*2 + 1)]; +} + +__kernel void Filter ( + __read_only image2d_t input, + __constant float* filterWeights, + __write_only image2d_t output) +{ + const int2 pos = {get_global_id(0), get_global_id(1)}; + + float4 sum = (float4)(0.0f); + for(int y = -FILTER_SIZE; y <= FILTER_SIZE; y++) { + for(int x = -FILTER_SIZE; x <= FILTER_SIZE; x++) { + sum += FilterValue(filterWeights, x, y) + * read_imagef(input, sampler, pos + (int2)(x,y)); + } + } + + write_imagef (output, (int2)(pos.x, pos.y), sum); +} + + diff --git a/notes/opencl_error_codes.txt b/notes/opencl_error_codes.txt index 61ead52..39bad3b 100644 --- a/notes/opencl_error_codes.txt +++ b/notes/opencl_error_codes.txt @@ -1,47 +1,47 @@ -CL_SUCCESS 0 -CL_DEVICE_NOT_FOUND -1 -CL_DEVICE_NOT_AVAILABLE -2 -CL_COMPILER_NOT_AVAILABLE -3 -CL_MEM_OBJECT_ALLOCATION_FAILURE -4 -CL_OUT_OF_RESOURCES -5 -CL_OUT_OF_HOST_MEMORY -6 -CL_PROFILING_INFO_NOT_AVAILABLE -7 -CL_MEM_COPY_OVERLAP -8 -CL_IMAGE_FORMAT_MISMATCH -9 -CL_IMAGE_FORMAT_NOT_SUPPORTED -10 -CL_BUILD_PROGRAM_FAILURE -11 -CL_MAP_FAILURE -12 - -CL_INVALID_VALUE -30 -CL_INVALID_DEVICE_TYPE -31 -CL_INVALID_PLATFORM -32 -CL_INVALID_DEVICE -33 -CL_INVALID_CONTEXT -34 -CL_INVALID_QUEUE_PROPERTIES -35 -CL_INVALID_COMMAND_QUEUE -36 -CL_INVALID_HOST_PTR -37 -CL_INVALID_MEM_OBJECT -38 -CL_INVALID_IMAGE_FORMAT_DESCRIPTOR -39 -CL_INVALID_IMAGE_SIZE -40 -CL_INVALID_SAMPLER -41 -CL_INVALID_BINARY -42 -CL_INVALID_BUILD_OPTIONS -43 -CL_INVALID_PROGRAM -44 -CL_INVALID_PROGRAM_EXECUTABLE -45 -CL_INVALID_KERNEL_NAME -46 -CL_INVALID_KERNEL_DEFINITION -47 -CL_INVALID_KERNEL -48 -CL_INVALID_ARG_INDEX -49 -CL_INVALID_ARG_VALUE -50 -CL_INVALID_ARG_SIZE -51 -CL_INVALID_KERNEL_ARGS -52 -CL_INVALID_WORK_DIMENSION -53 -CL_INVALID_WORK_GROUP_SIZE -54 -CL_INVALID_WORK_ITEM_SIZE -55 -CL_INVALID_GLOBAL_OFFSET -56 -CL_INVALID_EVENT_WAIT_LIST -57 -CL_INVALID_EVENT -58 -CL_INVALID_OPERATION -59 -CL_INVALID_GL_OBJECT -60 -CL_INVALID_BUFFER_SIZE -61 -CL_INVALID_MIP_LEVEL -62 +CL_SUCCESS 0 +CL_DEVICE_NOT_FOUND -1 +CL_DEVICE_NOT_AVAILABLE -2 +CL_COMPILER_NOT_AVAILABLE -3 +CL_MEM_OBJECT_ALLOCATION_FAILURE -4 +CL_OUT_OF_RESOURCES -5 +CL_OUT_OF_HOST_MEMORY -6 +CL_PROFILING_INFO_NOT_AVAILABLE -7 +CL_MEM_COPY_OVERLAP -8 +CL_IMAGE_FORMAT_MISMATCH -9 +CL_IMAGE_FORMAT_NOT_SUPPORTED -10 +CL_BUILD_PROGRAM_FAILURE -11 +CL_MAP_FAILURE -12 + +CL_INVALID_VALUE -30 +CL_INVALID_DEVICE_TYPE -31 +CL_INVALID_PLATFORM -32 +CL_INVALID_DEVICE -33 +CL_INVALID_CONTEXT -34 +CL_INVALID_QUEUE_PROPERTIES -35 +CL_INVALID_COMMAND_QUEUE -36 +CL_INVALID_HOST_PTR -37 +CL_INVALID_MEM_OBJECT -38 +CL_INVALID_IMAGE_FORMAT_DESCRIPTOR -39 +CL_INVALID_IMAGE_SIZE -40 +CL_INVALID_SAMPLER -41 +CL_INVALID_BINARY -42 +CL_INVALID_BUILD_OPTIONS -43 +CL_INVALID_PROGRAM -44 +CL_INVALID_PROGRAM_EXECUTABLE -45 +CL_INVALID_KERNEL_NAME -46 +CL_INVALID_KERNEL_DEFINITION -47 +CL_INVALID_KERNEL -48 +CL_INVALID_ARG_INDEX -49 +CL_INVALID_ARG_VALUE -50 +CL_INVALID_ARG_SIZE -51 +CL_INVALID_KERNEL_ARGS -52 +CL_INVALID_WORK_DIMENSION -53 +CL_INVALID_WORK_GROUP_SIZE -54 +CL_INVALID_WORK_ITEM_SIZE -55 +CL_INVALID_GLOBAL_OFFSET -56 +CL_INVALID_EVENT_WAIT_LIST -57 +CL_INVALID_EVENT -58 +CL_INVALID_OPERATION -59 +CL_INVALID_GL_OBJECT -60 +CL_INVALID_BUFFER_SIZE -61 +CL_INVALID_MIP_LEVEL -62 diff --git a/src/Curses.cpp b/src/Curses.cpp index 9a3b1a8..0528376 100644 --- a/src/Curses.cpp +++ b/src/Curses.cpp @@ -1,187 +1,187 @@ -#pragma once -#include "Curses.h" -#include -#include - -Curses::Curses(sf::Vector2i tile_size_, sf::Vector2i grid_dimensions_) : - window(sf::VideoMode(tile_size_.x * grid_dimensions_.x, tile_size_.y * grid_dimensions_.y), "SimpleFML Curses"), - grid_dimensions(grid_dimensions_), - tile_pixel_dimensions(tile_size_){ - - font.loadFromFile("unifont.ttf"); - - for (int y = 0; y < grid_dimensions_.y; y++) { - for (int x = 0 ; x < grid_dimensions_.x; x++) { - tiles.emplace_back(Tile(sf::Vector2i(x, y))); - - // L'\u0020' = space char - } - } - - - // Set screen values to increasing unicode chars - /*wchar_t char_ind = L'\u0041'; - for (int i = 0; i < tiles.size(); i++) { - tiles.at(i).push_clear(char_ind++, sf::Color::White, sf::Color::Black); - }*/ -} - -Curses::~Curses() { -} - -void Curses::Update(double delta_time_) { - - for (Tile &tile : tiles) { - tile.inc_index(); - } - - sf::Event event; - while (window.pollEvent(event)) { - if (event.type == sf::Event::Closed) - window.close(); - } -} - -void Curses::Render() { - window.clear(); - - sf::Texture font_texture = font.getTexture(tile_pixel_dimensions.x); - - // Draw text and backfills - sf::VertexArray backfill_v_arr(sf::Quads, grid_dimensions.x * grid_dimensions.y * 4); - sf::VertexArray font_v_arr(sf::Quads, grid_dimensions.x * grid_dimensions.y * 4); - - int tile_index = 0; - - for (int i = 0; i < backfill_v_arr.getVertexCount(); i += 4) { - - Tile* tile = &tiles.at(tile_index); - sf::Glyph glyph = font.getGlyph(tile->current_unicode_value(), tile_pixel_dimensions.x, false); - - // Backfill the tile with the specified backfill color - backfill_v_arr[i + 0].position = sf::Vector2f(tile->getPosition().x * tile_pixel_dimensions.x, tile->getPosition().y * tile_pixel_dimensions.y); - backfill_v_arr[i + 1].position = sf::Vector2f(tile->getPosition().x * tile_pixel_dimensions.x + tile_pixel_dimensions.x, tile->getPosition().y * tile_pixel_dimensions.y); - backfill_v_arr[i + 2].position = sf::Vector2f(tile->getPosition().x * tile_pixel_dimensions.x + tile_pixel_dimensions.x, tile->getPosition().y * tile_pixel_dimensions.y + tile_pixel_dimensions.y); - backfill_v_arr[i + 3].position = sf::Vector2f(tile->getPosition().x * tile_pixel_dimensions.x, tile->getPosition().y * tile_pixel_dimensions.y + tile_pixel_dimensions.y); - - backfill_v_arr[i + 0].color = tile->current_backfill_color(); - backfill_v_arr[i + 1].color = tile->current_backfill_color(); - backfill_v_arr[i + 2].color = tile->current_backfill_color(); - backfill_v_arr[i + 3].color = tile->current_backfill_color(); - - // Draw the font with the correct size, texture location, window location, and color - font_v_arr[i + 0].position = sf::Vector2f(tile->getPosition().x * tile_pixel_dimensions.x, tile->getPosition().y * tile_pixel_dimensions.y); - font_v_arr[i + 1].position = sf::Vector2f(tile->getPosition().x * tile_pixel_dimensions.x + glyph.textureRect.width, tile->getPosition().y * tile_pixel_dimensions.y); - font_v_arr[i + 2].position = sf::Vector2f(tile->getPosition().x * tile_pixel_dimensions.x + glyph.textureRect.width, tile->getPosition().y * tile_pixel_dimensions.y + glyph.textureRect.height); - font_v_arr[i + 3].position = sf::Vector2f(tile->getPosition().x * tile_pixel_dimensions.x, tile->getPosition().y * tile_pixel_dimensions.y + glyph.textureRect.height); - - // Make the letter appear in the center of the tile by applying an offset - sf::Vector2f position_offset = sf::Vector2f(tile_pixel_dimensions.x / 4, tile_pixel_dimensions.y / 4); - font_v_arr[i + 0].position += position_offset; - font_v_arr[i + 1].position += position_offset; - font_v_arr[i + 2].position += position_offset; - font_v_arr[i + 3].position += position_offset; - - font_v_arr[i + 0].texCoords = sf::Vector2f(glyph.textureRect.left, glyph.textureRect.top); - font_v_arr[i + 1].texCoords = sf::Vector2f(glyph.textureRect.width + glyph.textureRect.left, glyph.textureRect.top); - font_v_arr[i + 2].texCoords = sf::Vector2f(glyph.textureRect.width + glyph.textureRect.left, glyph.textureRect.top + glyph.textureRect.height); - font_v_arr[i + 3].texCoords = sf::Vector2f(glyph.textureRect.left, glyph.textureRect.top + glyph.textureRect.height); - - font_v_arr[i + 0].color = tile->current_font_color(); - font_v_arr[i + 1].color = tile->current_font_color(); - font_v_arr[i + 2].color = tile->current_font_color(); - font_v_arr[i + 3].color = tile->current_font_color(); - - tile_index++; - } - - window.draw(backfill_v_arr); - window.draw(font_v_arr, &font_texture); - - window.display(); -} - -void Curses::setTile(Tile tile_) { - tiles.at(multi_to_linear(tile_.getPosition())) = tile_; -} - -void Curses::setTiles(std::vector tiles_) { - for (Tile tile: tiles_) { - tiles.at(multi_to_linear(tile.getPosition())) = tile; - } -} - -void Curses::Clear() { - for (Tile &tile : tiles) { - tile.clear(); - } -} - -Curses::Tile* Curses::getTile(sf::Vector2i position_) { - return &tiles.at(multi_to_linear(position_)); -} - -std::vector Curses::getTiles(sf::Vector2i start_, sf::Vector2i end_) { - std::vector ret; - for (int i = multi_to_linear(start_); i < multi_to_linear(end_); i++) { - ret.push_back(&tiles.at(i)); - } - return ret; -} - -void Curses::setScroll(int ratio_, sf::Vector2i start_, std::list scroll_text_) { - // Scrolling and it's scroll ratio is faux implemented by - // essentially stacking values and repeating them to slow the scroll down - - - - for (int i = 0; i < scroll_text_.size(); i++) { - append_slots(start_, scroll_text_); - scroll_text_.push_back(scroll_text_.front()); - scroll_text_.pop_front(); - } - - -} - - - -void Curses::set_tile_ratio(int ratio_, sf::Vector2i tile_position_) { - getTile(tile_position_)->set_ratio(ratio_); -} - -void Curses::append_slots(sf::Vector2i start_, std::list values_) -{ - std::vector tiles = getTiles(start_, - sf::Vector2i((values_.size() + start_.x) % grid_dimensions.x, - (values_.size() + start_.x) / grid_dimensions.x + start_.y)); - - if (tiles.size() != values_.size()) { - std::cout << "Values did not match up to slots when appending\n"; - return; - } - - std::list::iterator beg_slot_it = values_.begin(); - std::list::iterator end_slot_it = values_.end(); - std::list::iterator slot_it; - - std::vector::iterator beg_tile_it = tiles.begin(); - std::vector::iterator end_tile_it = tiles.end(); - std::vector::iterator tile_it; - - for (slot_it = beg_slot_it, tile_it = beg_tile_it; - (slot_it != end_slot_it) && (tile_it != end_tile_it); - ++slot_it, ++tile_it) { - - Tile* t = *tile_it; - t->push_back(*slot_it); - - } -} - -sf::Vector2i Curses::linear_to_multi(int position_) const { - return sf::Vector2i(position_ % grid_dimensions.x, position_ / grid_dimensions.x); -} -int Curses::multi_to_linear(sf::Vector2i position_) const { - return position_.y * grid_dimensions.x + position_.x; -} +#pragma once +#include "Curses.h" +#include +#include + +Curses::Curses(sf::Vector2i tile_size_, sf::Vector2i grid_dimensions_) : + window(sf::VideoMode(tile_size_.x * grid_dimensions_.x, tile_size_.y * grid_dimensions_.y), "SimpleFML Curses"), + grid_dimensions(grid_dimensions_), + tile_pixel_dimensions(tile_size_){ + + font.loadFromFile("unifont.ttf"); + + for (int y = 0; y < grid_dimensions_.y; y++) { + for (int x = 0 ; x < grid_dimensions_.x; x++) { + tiles.emplace_back(Tile(sf::Vector2i(x, y))); + + // L'\u0020' = space char + } + } + + + // Set screen values to increasing unicode chars + /*wchar_t char_ind = L'\u0041'; + for (int i = 0; i < tiles.size(); i++) { + tiles.at(i).push_clear(char_ind++, sf::Color::White, sf::Color::Black); + }*/ +} + +Curses::~Curses() { +} + +void Curses::Update(double delta_time_) { + + for (Tile &tile : tiles) { + tile.inc_index(); + } + + sf::Event event; + while (window.pollEvent(event)) { + if (event.type == sf::Event::Closed) + window.close(); + } +} + +void Curses::Render() { + window.clear(); + + sf::Texture font_texture = font.getTexture(tile_pixel_dimensions.x); + + // Draw text and backfills + sf::VertexArray backfill_v_arr(sf::Quads, grid_dimensions.x * grid_dimensions.y * 4); + sf::VertexArray font_v_arr(sf::Quads, grid_dimensions.x * grid_dimensions.y * 4); + + int tile_index = 0; + + for (int i = 0; i < backfill_v_arr.getVertexCount(); i += 4) { + + Tile* tile = &tiles.at(tile_index); + sf::Glyph glyph = font.getGlyph(tile->current_unicode_value(), tile_pixel_dimensions.x, false); + + // Backfill the tile with the specified backfill color + backfill_v_arr[i + 0].position = sf::Vector2f(tile->getPosition().x * tile_pixel_dimensions.x, tile->getPosition().y * tile_pixel_dimensions.y); + backfill_v_arr[i + 1].position = sf::Vector2f(tile->getPosition().x * tile_pixel_dimensions.x + tile_pixel_dimensions.x, tile->getPosition().y * tile_pixel_dimensions.y); + backfill_v_arr[i + 2].position = sf::Vector2f(tile->getPosition().x * tile_pixel_dimensions.x + tile_pixel_dimensions.x, tile->getPosition().y * tile_pixel_dimensions.y + tile_pixel_dimensions.y); + backfill_v_arr[i + 3].position = sf::Vector2f(tile->getPosition().x * tile_pixel_dimensions.x, tile->getPosition().y * tile_pixel_dimensions.y + tile_pixel_dimensions.y); + + backfill_v_arr[i + 0].color = tile->current_backfill_color(); + backfill_v_arr[i + 1].color = tile->current_backfill_color(); + backfill_v_arr[i + 2].color = tile->current_backfill_color(); + backfill_v_arr[i + 3].color = tile->current_backfill_color(); + + // Draw the font with the correct size, texture location, window location, and color + font_v_arr[i + 0].position = sf::Vector2f(tile->getPosition().x * tile_pixel_dimensions.x, tile->getPosition().y * tile_pixel_dimensions.y); + font_v_arr[i + 1].position = sf::Vector2f(tile->getPosition().x * tile_pixel_dimensions.x + glyph.textureRect.width, tile->getPosition().y * tile_pixel_dimensions.y); + font_v_arr[i + 2].position = sf::Vector2f(tile->getPosition().x * tile_pixel_dimensions.x + glyph.textureRect.width, tile->getPosition().y * tile_pixel_dimensions.y + glyph.textureRect.height); + font_v_arr[i + 3].position = sf::Vector2f(tile->getPosition().x * tile_pixel_dimensions.x, tile->getPosition().y * tile_pixel_dimensions.y + glyph.textureRect.height); + + // Make the letter appear in the center of the tile by applying an offset + sf::Vector2f position_offset = sf::Vector2f(tile_pixel_dimensions.x / 4, tile_pixel_dimensions.y / 4); + font_v_arr[i + 0].position += position_offset; + font_v_arr[i + 1].position += position_offset; + font_v_arr[i + 2].position += position_offset; + font_v_arr[i + 3].position += position_offset; + + font_v_arr[i + 0].texCoords = sf::Vector2f(glyph.textureRect.left, glyph.textureRect.top); + font_v_arr[i + 1].texCoords = sf::Vector2f(glyph.textureRect.width + glyph.textureRect.left, glyph.textureRect.top); + font_v_arr[i + 2].texCoords = sf::Vector2f(glyph.textureRect.width + glyph.textureRect.left, glyph.textureRect.top + glyph.textureRect.height); + font_v_arr[i + 3].texCoords = sf::Vector2f(glyph.textureRect.left, glyph.textureRect.top + glyph.textureRect.height); + + font_v_arr[i + 0].color = tile->current_font_color(); + font_v_arr[i + 1].color = tile->current_font_color(); + font_v_arr[i + 2].color = tile->current_font_color(); + font_v_arr[i + 3].color = tile->current_font_color(); + + tile_index++; + } + + window.draw(backfill_v_arr); + window.draw(font_v_arr, &font_texture); + + window.display(); +} + +void Curses::setTile(Tile tile_) { + tiles.at(multi_to_linear(tile_.getPosition())) = tile_; +} + +void Curses::setTiles(std::vector tiles_) { + for (Tile tile: tiles_) { + tiles.at(multi_to_linear(tile.getPosition())) = tile; + } +} + +void Curses::Clear() { + for (Tile &tile : tiles) { + tile.clear(); + } +} + +Curses::Tile* Curses::getTile(sf::Vector2i position_) { + return &tiles.at(multi_to_linear(position_)); +} + +std::vector Curses::getTiles(sf::Vector2i start_, sf::Vector2i end_) { + std::vector ret; + for (int i = multi_to_linear(start_); i < multi_to_linear(end_); i++) { + ret.push_back(&tiles.at(i)); + } + return ret; +} + +void Curses::setScroll(int ratio_, sf::Vector2i start_, std::list scroll_text_) { + // Scrolling and it's scroll ratio is faux implemented by + // essentially stacking values and repeating them to slow the scroll down + + + + for (int i = 0; i < scroll_text_.size(); i++) { + append_slots(start_, scroll_text_); + scroll_text_.push_back(scroll_text_.front()); + scroll_text_.pop_front(); + } + + +} + + + +void Curses::set_tile_ratio(int ratio_, sf::Vector2i tile_position_) { + getTile(tile_position_)->set_ratio(ratio_); +} + +void Curses::append_slots(sf::Vector2i start_, std::list values_) +{ + std::vector tiles = getTiles(start_, + sf::Vector2i((values_.size() + start_.x) % grid_dimensions.x, + (values_.size() + start_.x) / grid_dimensions.x + start_.y)); + + if (tiles.size() != values_.size()) { + std::cout << "Values did not match up to slots when appending\n"; + return; + } + + std::list::iterator beg_slot_it = values_.begin(); + std::list::iterator end_slot_it = values_.end(); + std::list::iterator slot_it; + + std::vector::iterator beg_tile_it = tiles.begin(); + std::vector::iterator end_tile_it = tiles.end(); + std::vector::iterator tile_it; + + for (slot_it = beg_slot_it, tile_it = beg_tile_it; + (slot_it != end_slot_it) && (tile_it != end_tile_it); + ++slot_it, ++tile_it) { + + Tile* t = *tile_it; + t->push_back(*slot_it); + + } +} + +sf::Vector2i Curses::linear_to_multi(int position_) const { + return sf::Vector2i(position_ % grid_dimensions.x, position_ / grid_dimensions.x); +} +int Curses::multi_to_linear(sf::Vector2i position_) const { + return position_.y * grid_dimensions.x + position_.x; +} diff --git a/src/Map.cpp b/src/Map.cpp index cda7e77..3d478cf 100644 --- a/src/Map.cpp +++ b/src/Map.cpp @@ -1,73 +1,73 @@ -#pragma once -#include "Map.h" -#include -#include -#include -#include "util.hpp" - -sf::Vector3i Map::getDimensions() { - return dimensions; -} - -void Map::moveLight(sf::Vector2f in) { - - sf::Vector3f light_spherical = CartToSphere(global_light); - - light_spherical.y += in.y; - light_spherical.x += in.x; - - global_light = SphereToCart(light_spherical); - - return; - -} - -//void Map::GenerateFloor(){ -//} - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +#pragma once +#include "Map.h" +#include +#include +#include +#include "util.hpp" + +sf::Vector3i Map::getDimensions() { + return dimensions; +} + +void Map::moveLight(sf::Vector2f in) { + + sf::Vector3f light_spherical = CartToSphere(global_light); + + light_spherical.y += in.y; + light_spherical.x += in.x; + + global_light = SphereToCart(light_spherical); + + return; + +} + +//void Map::GenerateFloor(){ +//} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/Ray.cpp b/src/Ray.cpp index b822789..508dda3 100644 --- a/src/Ray.cpp +++ b/src/Ray.cpp @@ -1,159 +1,159 @@ -#pragma once - -#include -#include -#include "Map.h" -#include -#include "util.hpp" - -Ray::Ray( - Map *map, - sf::Vector2 resolution, - sf::Vector2 pixel, - sf::Vector3 camera_position, - sf::Vector3 ray_direction) { - - this->pixel = pixel; - this->map = map; - origin = camera_position; - direction = ray_direction; - dimensions = map->getDimensions(); -} - -sf::Color Ray::Cast() { - - // Setup the voxel step based on what direction the ray is pointing - sf::Vector3 voxel_step(1, 1, 1); - voxel_step.x *= (direction.x > 0) - (direction.x < 0); - voxel_step.y *= (direction.y > 0) - (direction.y < 0); - voxel_step.z *= (direction.z > 0) - (direction.z < 0); - - // Setup the voxel coords from the camera origin - voxel = sf::Vector3( - floorf(origin.x), - floorf(origin.y), - floorf(origin.z) - ); - - // Delta T is the units a ray must travel along an axis in order to - // traverse an integer split - delta_t = sf::Vector3( - fabsf(1.0f / direction.x), - fabsf(1.0f / direction.y), - fabsf(1.0f / direction.z) - ); - - // Intersection T is the collection of the next intersection points - // for all 3 axis XYZ. - intersection_t = sf::Vector3( - delta_t.x, - delta_t.y, - delta_t.z - ); - - - int dist = 0; - int face = -1; - // X:0, Y:1, Z:2 - - // Andrew Woo's raycasting algo - do { - if ((intersection_t.x) < (intersection_t.y)) { - if ((intersection_t.x) < (intersection_t.z)) { - - face = 0; - voxel.x += voxel_step.x; - intersection_t.x = intersection_t.x + delta_t.x; - } else { - - face = 2; - voxel.z += voxel_step.z; - intersection_t.z = intersection_t.z + delta_t.z; - } - } else { - if ((intersection_t.y) < (intersection_t.z)) { - - face = 1; - voxel.y += voxel_step.y; - intersection_t.y = intersection_t.y + delta_t.y; - } else { - - face = 2; - voxel.z += voxel_step.z; - intersection_t.z = intersection_t.z + delta_t.z; - } - } - - // If the ray went out of bounds - if (voxel.z >= dimensions.z) { - return sf::Color(172, 245, 251, 200); - } - if (voxel.x >= dimensions.x) { - return sf::Color(172, 245, 251, 200); - } - if (voxel.y >= dimensions.x) { - return sf::Color(172, 245, 251, 200); - } - - if (voxel.x < 0) { - return sf::Color(172, 245, 251, 200); - } - if (voxel.y < 0) { - return sf::Color(172, 245, 251, 200); - } - if (voxel.z < 0) { - return sf::Color(172, 245, 251, 200); - } - - // If we hit a voxel - int index = voxel.x + dimensions.x * (voxel.y + dimensions.z * voxel.z); - int voxel_data = map->list[index]; - - float alpha = 0; - if (face == 0) { - - alpha = AngleBetweenVectors(sf::Vector3f(1, 0, 0), map->global_light); - alpha = fmod(alpha, 0.785) * 2; - - } else if (face == 1) { - - alpha = AngleBetweenVectors(sf::Vector3f(0, 1, 0), map->global_light); - alpha = fmod(alpha, 0.785) * 2; - - } else if (face == 2){ - - //alpha = 1.57 / 2; - alpha = AngleBetweenVectors(sf::Vector3f(0, 0, 1), map->global_light); - alpha = fmod(alpha, 0.785) * 2; - } - - alpha *= 162; - - switch (voxel_data) { - case 1: - // AngleBew0 - 1.57 * 162 = 0 - 255 - - return sf::Color(255, 0, 0, alpha); - case 2: - return sf::Color(255, 10, 0, alpha); - case 3: - return sf::Color(255, 0, 255, alpha); - case 4: - return sf::Color(80, 0, 150, alpha); - case 5: - return sf::Color(255, 120, 255, alpha); - case 6: - return sf::Color(150, 80, 220, alpha); - } - - dist++; - - - } while (dist < 200); - - // Ray timeout color - return sf::Color::Cyan; -} - - - +#pragma once + +#include +#include +#include "Map.h" +#include +#include "util.hpp" + +Ray::Ray( + Map *map, + sf::Vector2 resolution, + sf::Vector2 pixel, + sf::Vector3 camera_position, + sf::Vector3 ray_direction) { + + this->pixel = pixel; + this->map = map; + origin = camera_position; + direction = ray_direction; + dimensions = map->getDimensions(); +} + +sf::Color Ray::Cast() { + + // Setup the voxel step based on what direction the ray is pointing + sf::Vector3 voxel_step(1, 1, 1); + voxel_step.x *= (direction.x > 0) - (direction.x < 0); + voxel_step.y *= (direction.y > 0) - (direction.y < 0); + voxel_step.z *= (direction.z > 0) - (direction.z < 0); + + // Setup the voxel coords from the camera origin + voxel = sf::Vector3( + floorf(origin.x), + floorf(origin.y), + floorf(origin.z) + ); + + // Delta T is the units a ray must travel along an axis in order to + // traverse an integer split + delta_t = sf::Vector3( + fabsf(1.0f / direction.x), + fabsf(1.0f / direction.y), + fabsf(1.0f / direction.z) + ); + + // Intersection T is the collection of the next intersection points + // for all 3 axis XYZ. + intersection_t = sf::Vector3( + delta_t.x, + delta_t.y, + delta_t.z + ); + + + int dist = 0; + int face = -1; + // X:0, Y:1, Z:2 + + // Andrew Woo's raycasting algo + do { + if ((intersection_t.x) < (intersection_t.y)) { + if ((intersection_t.x) < (intersection_t.z)) { + + face = 0; + voxel.x += voxel_step.x; + intersection_t.x = intersection_t.x + delta_t.x; + } else { + + face = 2; + voxel.z += voxel_step.z; + intersection_t.z = intersection_t.z + delta_t.z; + } + } else { + if ((intersection_t.y) < (intersection_t.z)) { + + face = 1; + voxel.y += voxel_step.y; + intersection_t.y = intersection_t.y + delta_t.y; + } else { + + face = 2; + voxel.z += voxel_step.z; + intersection_t.z = intersection_t.z + delta_t.z; + } + } + + // If the ray went out of bounds + if (voxel.z >= dimensions.z) { + return sf::Color(172, 245, 251, 200); + } + if (voxel.x >= dimensions.x) { + return sf::Color(172, 245, 251, 200); + } + if (voxel.y >= dimensions.x) { + return sf::Color(172, 245, 251, 200); + } + + if (voxel.x < 0) { + return sf::Color(172, 245, 251, 200); + } + if (voxel.y < 0) { + return sf::Color(172, 245, 251, 200); + } + if (voxel.z < 0) { + return sf::Color(172, 245, 251, 200); + } + + // If we hit a voxel + int index = voxel.x + dimensions.x * (voxel.y + dimensions.z * voxel.z); + int voxel_data = map->list[index]; + + float alpha = 0; + if (face == 0) { + + alpha = AngleBetweenVectors(sf::Vector3f(1, 0, 0), map->global_light); + alpha = fmod(alpha, 0.785) * 2; + + } else if (face == 1) { + + alpha = AngleBetweenVectors(sf::Vector3f(0, 1, 0), map->global_light); + alpha = fmod(alpha, 0.785) * 2; + + } else if (face == 2){ + + //alpha = 1.57 / 2; + alpha = AngleBetweenVectors(sf::Vector3f(0, 0, 1), map->global_light); + alpha = fmod(alpha, 0.785) * 2; + } + + alpha *= 162; + + switch (voxel_data) { + case 1: + // AngleBew0 - 1.57 * 162 = 0 - 255 + + return sf::Color(255, 0, 0, alpha); + case 2: + return sf::Color(255, 10, 0, alpha); + case 3: + return sf::Color(255, 0, 255, alpha); + case 4: + return sf::Color(80, 0, 150, alpha); + case 5: + return sf::Color(255, 120, 255, alpha); + case 6: + return sf::Color(150, 80, 220, alpha); + } + + dist++; + + + } while (dist < 200); + + // Ray timeout color + return sf::Color::Cyan; +} + + + diff --git a/src/RayCaster.cpp b/src/RayCaster.cpp index ec3b40a..56e6c1e 100644 --- a/src/RayCaster.cpp +++ b/src/RayCaster.cpp @@ -1,110 +1,110 @@ -#include "RayCaster.h" -#include -#include - - -RayCaster::RayCaster( - Map *map, - sf::Vector3 map_dimensions, - sf::Vector2 viewport_resolution ) { - - // Override values - //this.map_dimensions = new Vector3 (50, 50, 50); - //this.resolution = new Vector2 (200, 200); - //this.camera_direction = new Vector3 (1f, 0f, .8f); - //this.camera_position = new Vector3 (1, 10, 10); - - this->map_dimensions = map_dimensions; - this->map = map; - - resolution = viewport_resolution; - image = new sf::Color[resolution.x * resolution.y]; - - - // Calculate the view plane vectors - // Because casting to individual pixels causes barrel distortion, - // Get the radian increments - // Set the camera origin - // Rotate the ray by the specified pixel * increment - - double y_increment_radians = DegreesToRadians(50.0 / resolution.y); - double x_increment_radians = DegreesToRadians(80.0 / resolution.x); - - view_plane_vectors = new sf::Vector3f[resolution.x * resolution.y]; - for (int y = -resolution.y / 2 ; y < resolution.y / 2; y++) { - for (int x = -resolution.x / 2; x < resolution.x / 2; x++) { - - // The base ray direction to slew from - sf::Vector3f ray(1, 0, 0); - - // Y axis, pitch - ray = sf::Vector3f( - ray.z * sin(y_increment_radians * y) + ray.x * cos(y_increment_radians * y), - ray.y, - ray.z * cos(y_increment_radians * y) - ray.x * sin(y_increment_radians * y) - ); - - // Z axis, yaw - ray = sf::Vector3f( - ray.x * cos(x_increment_radians * x) - ray.y * sin(x_increment_radians * x), - ray.x * sin(x_increment_radians * x) + ray.y * cos(x_increment_radians * x), - ray.z - ); - - int index = (x + resolution.x / 2) + resolution.x * (y + resolution.y / 2); - view_plane_vectors[index] = Normalize(ray); - } - } -} - -RayCaster::~RayCaster() { - delete image; - delete view_plane_vectors; -} - -sf::Color* RayCaster::CastRays(sf::Vector3 camera_direction, sf::Vector3 camera_position) { - - // Setup the camera for this cast - this->camera_direction = camera_direction; - camera_direction_cartesian = Normalize(SphereToCart(camera_direction)); - this->camera_position = camera_position; - - // Start the loop at the top left, scan right and work down - for (int y = 0; y < resolution.y; y++) { - for (int x = 0; x < resolution.x; x++) { - - // Get the ray at the base direction - sf::Vector3f ray = view_plane_vectors[x + resolution.x * y]; - - // Rotate it to the correct pitch and yaw - - // Y axis, pitch - ray = sf::Vector3f( - ray.z * sin(camera_direction.y) + ray.x * cos(camera_direction.y), - ray.y, - ray.z * cos(camera_direction.y) - ray.x * sin(camera_direction.y) - ); - - // Z axis, yaw - ray = sf::Vector3f( - ray.x * cos(camera_direction.z) - ray.y * sin(camera_direction.z), - ray.x * sin(camera_direction.z) + ray.y * cos(camera_direction.z), - ray.z - ); - - - // Setup the ray - Ray r(map, resolution, sf::Vector2i(x, y), camera_position, ray); - - // Cast it and assign its return value - image[x + resolution.x * y] = r.Cast(); - } - } - - return image; -} - -void RayCaster::moveCamera(sf::Vector2f v) { - camera_direction.y += v.x; - camera_direction.z += v.y; -} +#include "RayCaster.h" +#include +#include + + +RayCaster::RayCaster( + Map *map, + sf::Vector3 map_dimensions, + sf::Vector2 viewport_resolution ) { + + // Override values + //this.map_dimensions = new Vector3 (50, 50, 50); + //this.resolution = new Vector2 (200, 200); + //this.camera_direction = new Vector3 (1f, 0f, .8f); + //this.camera_position = new Vector3 (1, 10, 10); + + this->map_dimensions = map_dimensions; + this->map = map; + + resolution = viewport_resolution; + image = new sf::Color[resolution.x * resolution.y]; + + + // Calculate the view plane vectors + // Because casting to individual pixels causes barrel distortion, + // Get the radian increments + // Set the camera origin + // Rotate the ray by the specified pixel * increment + + double y_increment_radians = DegreesToRadians(50.0 / resolution.y); + double x_increment_radians = DegreesToRadians(80.0 / resolution.x); + + view_plane_vectors = new sf::Vector3f[resolution.x * resolution.y]; + for (int y = -resolution.y / 2 ; y < resolution.y / 2; y++) { + for (int x = -resolution.x / 2; x < resolution.x / 2; x++) { + + // The base ray direction to slew from + sf::Vector3f ray(1, 0, 0); + + // Y axis, pitch + ray = sf::Vector3f( + ray.z * sin(y_increment_radians * y) + ray.x * cos(y_increment_radians * y), + ray.y, + ray.z * cos(y_increment_radians * y) - ray.x * sin(y_increment_radians * y) + ); + + // Z axis, yaw + ray = sf::Vector3f( + ray.x * cos(x_increment_radians * x) - ray.y * sin(x_increment_radians * x), + ray.x * sin(x_increment_radians * x) + ray.y * cos(x_increment_radians * x), + ray.z + ); + + int index = (x + resolution.x / 2) + resolution.x * (y + resolution.y / 2); + view_plane_vectors[index] = Normalize(ray); + } + } +} + +RayCaster::~RayCaster() { + delete image; + delete view_plane_vectors; +} + +sf::Color* RayCaster::CastRays(sf::Vector3 camera_direction, sf::Vector3 camera_position) { + + // Setup the camera for this cast + this->camera_direction = camera_direction; + camera_direction_cartesian = Normalize(SphereToCart(camera_direction)); + this->camera_position = camera_position; + + // Start the loop at the top left, scan right and work down + for (int y = 0; y < resolution.y; y++) { + for (int x = 0; x < resolution.x; x++) { + + // Get the ray at the base direction + sf::Vector3f ray = view_plane_vectors[x + resolution.x * y]; + + // Rotate it to the correct pitch and yaw + + // Y axis, pitch + ray = sf::Vector3f( + ray.z * sin(camera_direction.y) + ray.x * cos(camera_direction.y), + ray.y, + ray.z * cos(camera_direction.y) - ray.x * sin(camera_direction.y) + ); + + // Z axis, yaw + ray = sf::Vector3f( + ray.x * cos(camera_direction.z) - ray.y * sin(camera_direction.z), + ray.x * sin(camera_direction.z) + ray.y * cos(camera_direction.z), + ray.z + ); + + + // Setup the ray + Ray r(map, resolution, sf::Vector2i(x, y), camera_position, ray); + + // Cast it and assign its return value + image[x + resolution.x * y] = r.Cast(); + } + } + + return image; +} + +void RayCaster::moveCamera(sf::Vector2f v) { + camera_direction.y += v.x; + camera_direction.z += v.y; +} diff --git a/src/TestPlatform.cpp b/src/TestPlatform.cpp index de24a68..51e978e 100644 --- a/src/TestPlatform.cpp +++ b/src/TestPlatform.cpp @@ -1,95 +1,121 @@ -#ifdef linux - -#elif defined _WIN32 - -#elif defined TARGET_OS_MAC -# include -# include -# include -#endif - - -int IsExtensionSupported( - const char* support_str, - const char* ext_string, - size_t ext_buffer_size) { - - size_t offset = 0; - - const char* space_substr = strnstr(ext_string + offset, " ", ext_buffer_size - offset); - - size_t space_pos = space_substr ? space_substr - ext_string : 0; - - while (space_pos < ext_buffer_size) { - - if( strncmp(support_str, ext_string + offset, space_pos) == 0 ) { - // Device supports requested extension! - printf("Info: Found extension support ‘%s’!\n", support_str); - return 1; - } - - // Keep searching -- skip to next token string - offset = space_pos + 1; - space_substr = strnstr(ext_string + offset, " ", ext_buffer_size - offset); - space_pos = space_substr ? space_substr - ext_string : 0; - } - - printf("Warning: Extension not supported ‘%s’!\n", support_str); - return 0; -} - -int test_for_gl_cl_sharing() { - - - int err = 0; -#if defined (__APPLE__) || defined(MACOSX) - static const char *CL_GL_SHARING_EXT = "cl_APPLE_gl_sharing"; -#else - static const char* CL_GL_SHARING_EXT = "cl_khr_gl_sharing"; -#endif - - cl_uint num_devices, i; - clGetDeviceIDs(NULL, CL_DEVICE_TYPE_ALL, 0, NULL, &num_devices); - - cl_device_id *devices = (cl_device_id *) calloc(sizeof(cl_device_id), num_devices); - clGetDeviceIDs(NULL, CL_DEVICE_TYPE_ALL, num_devices, devices, NULL); - - // Get string containing supported device extensions - size_t ext_size = 1024; - char *ext_string = (char *) malloc(ext_size); - err = clGetDeviceInfo(devices[0], CL_DEVICE_EXTENSIONS, ext_size, ext_string, &ext_size); - - free(devices); - - // Search for GL support in extension string (space delimited) - int supported = IsExtensionSupported(CL_GL_SHARING_EXT, ext_string, ext_size); - if (supported) { - // Device supports context sharing with OpenGL - printf("Found GL Sharing Support!\n"); - return 1; - } - return -1; -} - - -int query_platform_devices() { - // From stackoverflow, gets and lists the compute devices - cl_uint num_devices, i; - clGetDeviceIDs(NULL, CL_DEVICE_TYPE_ALL, 0, NULL, &num_devices); - - cl_device_id *devices = (cl_device_id *) calloc(sizeof(cl_device_id), num_devices); - clGetDeviceIDs(NULL, CL_DEVICE_TYPE_ALL, num_devices, devices, NULL); - - char buf[128]; - for (i = 0; i < num_devices; i++) { - clGetDeviceInfo(devices[i], CL_DEVICE_NAME, 128, buf, NULL); - fprintf(stdout, "Device %s supports ", buf); - - clGetDeviceInfo(devices[i], CL_DEVICE_VERSION, 128, buf, NULL); - fprintf(stdout, "%s\n", buf); - } - - free(devices); - - return 1; +#pragma once +#include +#include +#include +#include +#include + +#ifdef linux + +#elif defined _WIN32 + +#elif defined TARGET_OS_MAC +# include +# include +# include +#endif + + + +#ifdef TARGET_OS_MAC +int IsExtensionSupported( + const char* support_str, + const char* ext_string, + size_t ext_buffer_size) { + + size_t offset = 0; + + const char* space_substr = strnstr(ext_string + offset, " ", ext_buffer_size - offset); + + size_t space_pos = space_substr ? space_substr - ext_string : 0; + + while (space_pos < ext_buffer_size) { + + if( strncmp(support_str, ext_string + offset, space_pos) == 0 ) { + // Device supports requested extension! + printf("Info: Found extension support ‘%s’!\n", support_str); + return 1; + } + + // Keep searching -- skip to next token string + offset = space_pos + 1; + space_substr = strnstr(ext_string + offset, " ", ext_buffer_size - offset); + space_pos = space_substr ? space_substr - ext_string : 0; + } + + std::cout << "Warning: Extension not supported " << support_str << std::endl; + return 0; +} +#endif + +inline int test_for_gl_cl_sharing() { + + + int err = 0; + +#if defined (__APPLE__) || defined(MACOSX) + static const char *CL_GL_SHARING_EXT = "cl_APPLE_gl_sharing"; +#else + static const char* CL_GL_SHARING_EXT = "cl_khr_gl_sharing"; +#endif + + cl_uint num_devices; + clGetDeviceIDs(NULL, CL_DEVICE_TYPE_ALL, 0, NULL, &num_devices); + + cl_device_id *devices = (cl_device_id *) calloc(sizeof(cl_device_id), num_devices); + clGetDeviceIDs(NULL, CL_DEVICE_TYPE_ALL, num_devices, devices, NULL); + + // Get string containing supported device extensions + size_t ext_size = 1024; + char *ext_string = (char *) malloc(ext_size); + err = clGetDeviceInfo(devices[0], CL_DEVICE_EXTENSIONS, ext_size, ext_string, &ext_size); + + free(devices); + + // Search for GL support in extension string (space delimited) + //int supported = IsExtensionSupported(CL_GL_SHARING_EXT, ext_string, ext_size); + int supported = 0; + if (supported) { + // Device supports context sharing with OpenGL + printf("Found GL Sharing Support!\n"); + return 1; + } + return -1; +} + + +inline int query_platform_devices() { + + int error = 0; + // Get the number of platforms + cl_uint platform_count = 0; + clGetPlatformIDs(0, nullptr, &platform_count); + + // Fetch the platforms + std::vector platformIds(platform_count); + clGetPlatformIDs(platform_count, platformIds.data(), nullptr); + + + for (unsigned int q = 0; q < platform_count; q++) { + + // From stackoverflow, gets and lists the compute devices + cl_uint num_devices, i; + error = clGetDeviceIDs(platformIds[q], CL_DEVICE_TYPE_ALL, 0, NULL, &num_devices); + + cl_device_id *devices = (cl_device_id *)calloc(sizeof(cl_device_id), num_devices); + error = clGetDeviceIDs(platformIds[q], CL_DEVICE_TYPE_ALL, num_devices, devices, NULL); + + char buf[128]; + for (i = 0; i < num_devices; i++) { + clGetDeviceInfo(devices[i], CL_DEVICE_NAME, 128, buf, NULL); + fprintf(stdout, "Device %s supports ", buf); + + clGetDeviceInfo(devices[i], CL_DEVICE_VERSION, 128, buf, NULL); + fprintf(stdout, "%s\n", buf); + } + + free(devices); + } + + return 1; } \ No newline at end of file diff --git a/src/Vector3.cpp b/src/Vector3.cpp index 056d8f1..3dfcd90 100644 --- a/src/Vector3.cpp +++ b/src/Vector3.cpp @@ -1,5 +1,5 @@ -// -// Created by Mitchell Hansen on 8/7/16. -// - -#include "Vector3.h" +// +// Created by Mitchell Hansen on 8/7/16. +// + +#include "Vector3.h" diff --git a/src/main.cpp b/src/main.cpp index 9b871c7..72882b7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,415 +1,464 @@ -#include -#include -#include -#include -#include -#include - -#ifdef linux -#include -#include - -#elif defined _WIN32 -#include -#include -#include - -#elif defined TARGET_OS_MAC -# include -# include - -#endif - -#include "Map.h" -#include "Curses.h" -#include "util.hpp" -#include "RayCaster.h" - - - -const int WINDOW_X = 150; -const int WINDOW_Y = 150; - -std::string read_file(std::string file_name){ - std::ifstream input_file(file_name); - - if (!input_file.is_open()){ - std::cout << file_name << " could not be opened" << std::endl; - return nullptr; - } - - std::stringstream buf; - buf << input_file.rdbuf(); - return buf.str(); -} - - -int main(){ - char buffer[256]; - char *val = getcwd(buffer, sizeof(buffer)); - if (val) { - std::cout << buffer << std::endl; - } - // ===================================================================== // - // ==== Opencl setup - - int error = 0; - - // Get the number of platforms - cl_uint platformIdCount = 0; - clGetPlatformIDs(0, nullptr, &platformIdCount); - - // Fetch the platforms - std::vector platformIds (platformIdCount); - clGetPlatformIDs(platformIdCount, platformIds.data(), nullptr); - - - // get the number of devices, fetch them, choose the first one - cl_uint deviceIdCount = 0; - std::vector deviceIds; - // Try to get a GPU first - error = clGetDeviceIDs (platformIds [0], CL_DEVICE_TYPE_GPU, 0, nullptr, - &deviceIdCount); - - if (deviceIdCount == 0) { - std::cout << "couldn't aquire a GPU, falling back to CPU" << std::endl; - error = clGetDeviceIDs(platformIds[0], CL_DEVICE_TYPE_CPU, 0, nullptr, &deviceIdCount); - deviceIds.resize(deviceIdCount); - error = clGetDeviceIDs(platformIds[0], CL_DEVICE_TYPE_CPU, deviceIdCount, deviceIds.data(), NULL); - } else { - std::cout << "aquired GPU cl target" << std::endl; - deviceIds.resize(deviceIdCount); - clGetDeviceIDs (platformIds[0], CL_DEVICE_TYPE_GPU, deviceIdCount, deviceIds.data (), nullptr); - } - - if (error != 0){ - std::cout << "Err: clGetDeviceIDs returned: " << error << std::endl; - return error; - } - - // Hurray for standards! - // Setup the context properties to grab the current GL context - #ifdef linux - cl_context_properties context_properties[] = { - CL_GL_CONTEXT_KHR, (cl_context_properties) glXGetCurrentContext(), - CL_GLX_DISPLAY_KHR, (cl_context_properties) glXGetCurrentDisplay(), - CL_CONTEXT_PLATFORM, (cl_context_properties) platform, - 0 - }; - - #elif defined _WIN32 - cl_context_properties context_properties[] = { - CL_GL_CONTEXT_KHR, (cl_context_properties) wglGetCurrentContext(), - CL_WGL_HDC_KHR, (cl_context_properties) wglGetCurrentDC(), - CL_CONTEXT_PLATFORM, (cl_context_properties) platform, - 0 - }; - - #elif defined TARGET_OS_MAC - CGLContextObj glContext = CGLGetCurrentContext(); - CGLShareGroupObj shareGroup = CGLGetShareGroup(glContext); - cl_context_properties context_properties[] = { - CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE, - (cl_context_properties)shareGroup, - 0 - }; - - #endif - - // Create our shared context - auto context = clCreateContext( - context_properties, - deviceIdCount, - deviceIds.data(), - nullptr, nullptr, - &error - ); - - if (error != 0){ - std::cout << "Err: clCreateContext returned: " << error << std::endl; - return error; - } - - // And the cl command queue - auto commandQueue = clCreateCommandQueue(context, deviceIds[0], 0, &error); - - if (error != 0){ - std::cout << "Err: clCreateCommandQueue returned: " << error << std::endl; - return error; - } - - // At this point the shared GL/CL context is up and running - // ====================================================================== // - // ========== Kernel setup & compilation - - // Load in the kernel, and c stringify it - std::string kernel_source; - kernel_source = read_file("../kernels/kernel.txt"); - const char* kernel_source_c_str = kernel_source.c_str(); - size_t kernel_source_size = strlen(kernel_source_c_str); - - - // Load the source into CL's data structure - cl_program kernel_program = clCreateProgramWithSource( - context, 1, - &kernel_source_c_str, - &kernel_source_size, &error - ); - - if (error != 0){ - std::cout << "Err: clCreateProgramWithSource returned: " << error << std::endl; - return error; - } - - - // Try and build the program - error = clBuildProgram(kernel_program, 1, &deviceIds[0], NULL, NULL, NULL); - - // Check to see if it errored out - if (error == CL_BUILD_PROGRAM_FAILURE){ - - // Get the size of the queued log - size_t log_size; - clGetProgramBuildInfo(kernel_program, deviceIds[0], CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size); - char *log = new char[log_size]; - - // Grab the log - clGetProgramBuildInfo(kernel_program, deviceIds[0], CL_PROGRAM_BUILD_LOG, log_size, log, NULL); - - - std::cout << "Err: clBuildProgram returned: " << error << std::endl; - std::cout << log << std::endl; - return error; - } - - // Done initializing the kernel - cl_kernel finished_kernel = clCreateKernel(kernel_program, "kernel_name", &error); - - if (error != 0){ - std::cout << "Err: clCreateKernel returned: " << error << std::endl; - return error; - } - -}; - - - - - - -float elap_time(){ - static std::chrono::time_point start; - static bool started = false; - - if (!started){ - start = std::chrono::system_clock::now(); - started = true; - } - - std::chrono::time_point now = std::chrono::system_clock::now(); - std::chrono::duration elapsed_time = now - start; - return elapsed_time.count(); -} - -sf::Sprite window_sprite; -sf::Texture window_texture; - -// Y: -1.57 is straight up -// Y: 1.57 is straight down - -void test_ray_reflection(){ - - sf::Vector3f r(0.588, -0.78, -0.196); - sf::Vector3f i(0, 0.928, 0.37); - - // is this needed? free spin but bounded 0 < z < pi - if (i.z > PI) - i.z -= PI; - else if (i.z < 0) - i.z += PI; - - std::cout << AngleBetweenVectors(r, i); - - - - return; -} - -int main0() { - - // Initialize the render window - Curses curse(sf::Vector2i(5, 5), sf::Vector2i(WINDOW_X, WINDOW_Y)); - sf::RenderWindow window(sf::VideoMode(WINDOW_X, WINDOW_Y), "SFML"); - - - - // The step size in milliseconds between calls to Update() - // Lets set it to 16.6 milliseonds (60FPS) - float step_size = 0.0166f; - - // Timekeeping values for the loop - double frame_time = 0.0, - elapsed_time = 0.0, - delta_time = 0.0, - accumulator_time = 0.0, - current_time = 0.0; - - fps_counter fps; - - // ============================= RAYCASTER SETUP ================================== - - // Setup the sprite and texture - window_texture.create(WINDOW_X, WINDOW_Y); - window_sprite.setPosition(0, 0); - - // State values - sf::Vector3i map_dim(100, 100, 100); - sf::Vector2i view_res(WINDOW_X, WINDOW_Y); - sf::Vector3f cam_dir(1.0f, 0.0f, 1.57f); - sf::Vector3f cam_pos(50, 50, 50); - sf::Vector3f cam_vec(0, 0, 0); - Map* map = new Map(map_dim); - RayCaster ray_caster(map, map_dim, view_res); - - - // =============================================================================== - - // Mouse capture - sf::Vector2i deltas; - sf::Vector2i fixed(window.getSize()); - bool mouse_enabled = true; - - while (window.isOpen()) { - - // Poll for events from the user - sf::Event event; - while (window.pollEvent(event)) { - - // If the user tries to exit the application via the GUI - if (event.type == sf::Event::Closed) - window.close(); - } - - cam_vec.x = 0; - cam_vec.y = 0; - cam_vec.z = 0; - - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Q)) { - cam_vec.z = 1; - } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::E)) { - cam_vec.z = -1; - } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) { - cam_vec.y = 1; - } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) { - cam_vec.y = -1; - } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) { - cam_vec.x = 1; - } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) { - cam_vec.x = -1; - } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { - cam_dir.z = -0.1f; - } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) { - cam_vec.z = +0.1f; - } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) { - cam_vec.y = +0.1f; - } - if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) { - cam_vec.y = -0.1f; - } - - deltas = fixed - sf::Mouse::getPosition(); - if (deltas != sf::Vector2i(0, 0) && mouse_enabled == true) { - - // Mouse movement - sf::Mouse::setPosition(fixed); - cam_dir.y -= deltas.y / 300.0f; - cam_dir.z -= deltas.x / 300.0f; - } - - cam_pos.x += cam_vec.x / 1.0; - cam_pos.y += cam_vec.y / 1.0; - cam_pos.z += cam_vec.z / 1.0; - -// if (cam_vec.x > 0.0f) -// cam_vec.x -= 0.1; -// else if (cam_vec.x < 0.0f) -// cam_vec.x += 0.1; -// -// if (cam_vec.y > 0.0f) -// cam_vec.y -= 0.1; -// else if (cam_vec.y < 0.0f) -// cam_vec.y += 0.1; -// -// if (cam_vec.z > 0.0f) -// cam_vec.z -= 0.1; -// else if (cam_vec.z < 0.0f) -// cam_vec.z += 0.1; - - std::cout << cam_vec.x << " : " << cam_vec.y << " : " << cam_vec.z << std::endl; - - - // Time keeping - elapsed_time = elap_time(); - delta_time = elapsed_time - current_time; - current_time = elapsed_time; - if (delta_time > 0.2f) - delta_time = 0.2f; - accumulator_time += delta_time; - while ((accumulator_time - step_size) >= step_size) { - accumulator_time -= step_size; - - - // Update cycle - curse.Update(delta_time); - - - } - - // Fps cycle - // map->moveLight(sf::Vector2f(0.3, 0)); - - window.clear(sf::Color::Black); - - // Cast the rays and get the image - sf::Color* pixel_colors = ray_caster.CastRays(cam_dir, cam_pos); - - for (int i = 0; i < WINDOW_X * WINDOW_Y; i++) { - - Curses::Tile t(sf::Vector2i(i % WINDOW_X, i / WINDOW_X)); - Curses::Slot s(L'\u0045', pixel_colors[i], sf::Color::Black); - t.push_back(s); - curse.setTile(t); - - } - - - // Cast it to an array of Uint8's - auto out = (sf::Uint8*)pixel_colors; - - window_texture.update(out); - window_sprite.setTexture(window_texture); - window.draw(window_sprite); - - - curse.Render(); - - // Give the frame counter the frame time and draw the average frame time - fps.frame(delta_time); - fps.draw(&window); - - window.display(); - - - - } - return 0; - -} +#include +#include +#include +#include +#include +#include + +#ifdef linux +#include +#include + +#elif defined _WIN32 +#include +#include +#include +#include + +#elif defined TARGET_OS_MAC +# include +# include + +#endif +#include "TestPlatform.cpp" +#include "Map.h" +#include "Curses.h" +#include "util.hpp" +#include "RayCaster.h" + + +const int WINDOW_X = 150; +const int WINDOW_Y = 150; + +std::string read_file(std::string file_name){ + std::ifstream input_file(file_name); + + if (!input_file.is_open()){ + std::cout << file_name << " could not be opened" << std::endl; + return nullptr; + } + + std::stringstream buf; + buf << input_file.rdbuf(); + return buf.str(); +} + + +int main(){ + + int error = 0; + + // ===================================================================== // + // ==== Opencl setup + + // Get the number of platforms + cl_uint platform_count = 0; + clGetPlatformIDs(0, nullptr, &platform_count); + + // Fetch the platforms + std::vector platformIds(platform_count); + clGetPlatformIDs(platform_count, platformIds.data(), nullptr); + + + + // Print out this machines info + + std::cout << "============ Hardware info =================" << std::endl; + + for (unsigned int i = 0; i < platform_count; i++) { + + std::cout << "--Platform: " << i << std::endl; + + char platform[128]; + char version[128]; + + clGetPlatformInfo(platformIds[i], CL_PLATFORM_NAME, 128, platform, NULL); + clGetPlatformInfo(platformIds[i], CL_PLATFORM_VERSION, 128, version, NULL); + + std::cout << platform << "\n"; + std::cout << version << "\n\n"; + + // get the number of devices, fetch them, choose the first one + cl_uint deviceIdCount = 0; + error = clGetDeviceIDs(platformIds[i], CL_DEVICE_TYPE_ALL, 0, nullptr, &deviceIdCount); + + std::vector deviceIds(deviceIdCount); + + for (int q = 0; q < deviceIdCount; q++) { + + std::cout << "++++Device " << q << std::endl; + error = clGetDeviceIDs(platformIds[i], CL_DEVICE_TYPE_ALL, deviceIdCount, deviceIds.data(), NULL); + + clGetDeviceInfo(deviceIds[q], CL_DEVICE_NAME, 128, platform, NULL); + clGetDeviceInfo(deviceIds[q], CL_DEVICE_VERSION, 128, version, NULL); + + std::cout << platform << "\n"; + std::cout << version << "\n\n"; + + } + } + + std::cout << "============================================" << std::endl; + + cl_uint deviceIdCount = 0; + std::vector deviceIds; + + // Try to get a GPU first + error = clGetDeviceIDs(platformIds[1], CL_DEVICE_TYPE_GPU, 0, nullptr, + &deviceIdCount); + + + if (deviceIdCount == 0) { + std::cout << "couldn't acquire a GPU, falling back to CPU" << std::endl; + error = clGetDeviceIDs(platformIds[1], CL_DEVICE_TYPE_CPU, 0, nullptr, &deviceIdCount); + deviceIds.resize(deviceIdCount); + error = clGetDeviceIDs(platformIds[1], CL_DEVICE_TYPE_CPU, deviceIdCount, deviceIds.data(), NULL); + } else { + std::cout << "acquired GPU cl target" << std::endl; + deviceIds.resize(deviceIdCount); + clGetDeviceIDs (platformIds[1], CL_DEVICE_TYPE_GPU, deviceIdCount, deviceIds.data (), nullptr); + } + + + + if (error != 0){ + std::cout << "Err: clGetDeviceIDs returned: " << error << std::endl; + return error; + } + + // Hurray for standards! + // Setup the context properties to grab the current GL context + #ifdef linux + cl_context_properties context_properties[] = { + CL_GL_CONTEXT_KHR, (cl_context_properties) glXGetCurrentContext(), + CL_GLX_DISPLAY_KHR, (cl_context_properties) glXGetCurrentDisplay(), + CL_CONTEXT_PLATFORM, (cl_context_properties) platform, + 0 + }; + + #elif defined _WIN32 + //cl_context_properties context_properties[] = { + // CL_CONTEXT_PLATFORM, (cl_context_properties) platformIds[0], + // CL_GL_CONTEXT_KHR, (cl_context_properties) wglGetCurrentContext(), + // CL_WGL_HDC_KHR, (cl_context_properties) wglGetCurrentDC(), + // 0 + //}; + HGLRC hGLRC = wglGetCurrentContext(); + HDC hDC = wglGetCurrentDC(); + cl_context_properties context_properties[] = { CL_CONTEXT_PLATFORM, (cl_context_properties)platformIds[1], CL_GL_CONTEXT_KHR, (cl_context_properties)hGLRC, CL_WGL_HDC_KHR, (cl_context_properties)hDC, 0 }; + + + #elif defined TARGET_OS_MAC + CGLContextObj glContext = CGLGetCurrentContext(); + CGLShareGroupObj shareGroup = CGLGetShareGroup(glContext); + cl_context_properties context_properties[] = { + CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE, + (cl_context_properties)shareGroup, + 0 + }; + + #endif + + // Create our shared context + auto context = clCreateContext( + context_properties, + 1, + &deviceIds[0], + nullptr, nullptr, + &error + ); + + + //cl_device_id devices[32]; + //size_t size; + //clGetGLContextInfoKHR(context_properties, CL_DEVICES_FOR_GL_CONTEXT_KHR, + // 32 * sizeof(cl_device_id), devices, &size); + + if (error != 0){ + std::cout << "Err: clCreateContext returned: " << error << std::endl; + return error; + } + + // And the cl command queue + auto commandQueue = clCreateCommandQueue(context, deviceIds[0], 0, &error); + + if (error != 0){ + std::cout << "Err: clCreateCommandQueue returned: " << error << std::endl; + return error; + } + + // At this point the shared GL/CL context is up and running + // ====================================================================== // + // ========== Kernel setup & compilation + + // Load in the kernel, and c stringify it + std::string kernel_source; + kernel_source = read_file("../kernels/kernel.txt"); + const char* kernel_source_c_str = kernel_source.c_str(); + size_t kernel_source_size = strlen(kernel_source_c_str); + + + // Load the source into CL's data structure + cl_program kernel_program = clCreateProgramWithSource( + context, 1, + &kernel_source_c_str, + &kernel_source_size, &error + ); + + if (error != 0){ + std::cout << "Err: clCreateProgramWithSource returned: " << error << std::endl; + return error; + } + + + // Try and build the program + error = clBuildProgram(kernel_program, 1, &deviceIds[0], NULL, NULL, NULL); + + // Check to see if it errored out + if (error == CL_BUILD_PROGRAM_FAILURE){ + + // Get the size of the queued log + size_t log_size; + clGetProgramBuildInfo(kernel_program, deviceIds[0], CL_PROGRAM_BUILD_LOG, 0, NULL, &log_size); + char *log = new char[log_size]; + + // Grab the log + clGetProgramBuildInfo(kernel_program, deviceIds[0], CL_PROGRAM_BUILD_LOG, log_size, log, NULL); + + + std::cout << "Err: clBuildProgram returned: " << error << std::endl; + std::cout << log << std::endl; + return error; + } + + // Done initializing the kernel + cl_kernel finished_kernel = clCreateKernel(kernel_program, "kernel_name", &error); + + if (error != 0){ + std::cout << "Err: clCreateKernel returned: " << error << std::endl; + return error; + } + +}; + + + + + + +float elap_time(){ + static std::chrono::time_point start; + static bool started = false; + + if (!started){ + start = std::chrono::system_clock::now(); + started = true; + } + + std::chrono::time_point now = std::chrono::system_clock::now(); + std::chrono::duration elapsed_time = now - start; + return elapsed_time.count(); +} + +sf::Sprite window_sprite; +sf::Texture window_texture; + +// Y: -1.57 is straight up +// Y: 1.57 is straight down + +void test_ray_reflection(){ + + sf::Vector3f r(0.588, -0.78, -0.196); + sf::Vector3f i(0, 0.928, 0.37); + + // is this needed? free spin but bounded 0 < z < pi + if (i.z > PI) + i.z -= PI; + else if (i.z < 0) + i.z += PI; + + std::cout << AngleBetweenVectors(r, i); + + + + return; +} + +int main0() { + + // Initialize the render window + Curses curse(sf::Vector2i(5, 5), sf::Vector2i(WINDOW_X, WINDOW_Y)); + sf::RenderWindow window(sf::VideoMode(WINDOW_X, WINDOW_Y), "SFML"); + + + + // The step size in milliseconds between calls to Update() + // Lets set it to 16.6 milliseonds (60FPS) + float step_size = 0.0166f; + + // Timekeeping values for the loop + double frame_time = 0.0, + elapsed_time = 0.0, + delta_time = 0.0, + accumulator_time = 0.0, + current_time = 0.0; + + fps_counter fps; + + // ============================= RAYCASTER SETUP ================================== + + // Setup the sprite and texture + window_texture.create(WINDOW_X, WINDOW_Y); + window_sprite.setPosition(0, 0); + + // State values + sf::Vector3i map_dim(100, 100, 100); + sf::Vector2i view_res(WINDOW_X, WINDOW_Y); + sf::Vector3f cam_dir(1.0f, 0.0f, 1.57f); + sf::Vector3f cam_pos(50, 50, 50); + sf::Vector3f cam_vec(0, 0, 0); + Map* map = new Map(map_dim); + RayCaster ray_caster(map, map_dim, view_res); + + + // =============================================================================== + + // Mouse capture + sf::Vector2i deltas; + sf::Vector2i fixed(window.getSize()); + bool mouse_enabled = true; + + while (window.isOpen()) { + + // Poll for events from the user + sf::Event event; + while (window.pollEvent(event)) { + + // If the user tries to exit the application via the GUI + if (event.type == sf::Event::Closed) + window.close(); + } + + cam_vec.x = 0; + cam_vec.y = 0; + cam_vec.z = 0; + + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Q)) { + cam_vec.z = 1; + } + if (sf::Keyboard::isKeyPressed(sf::Keyboard::E)) { + cam_vec.z = -1; + } + if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) { + cam_vec.y = 1; + } + if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) { + cam_vec.y = -1; + } + if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) { + cam_vec.x = 1; + } + if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) { + cam_vec.x = -1; + } + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { + cam_dir.z = -0.1f; + } + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) { + cam_vec.z = +0.1f; + } + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) { + cam_vec.y = +0.1f; + } + if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) { + cam_vec.y = -0.1f; + } + + deltas = fixed - sf::Mouse::getPosition(); + if (deltas != sf::Vector2i(0, 0) && mouse_enabled == true) { + + // Mouse movement + sf::Mouse::setPosition(fixed); + cam_dir.y -= deltas.y / 300.0f; + cam_dir.z -= deltas.x / 300.0f; + } + + cam_pos.x += cam_vec.x / 1.0; + cam_pos.y += cam_vec.y / 1.0; + cam_pos.z += cam_vec.z / 1.0; + +// if (cam_vec.x > 0.0f) +// cam_vec.x -= 0.1; +// else if (cam_vec.x < 0.0f) +// cam_vec.x += 0.1; +// +// if (cam_vec.y > 0.0f) +// cam_vec.y -= 0.1; +// else if (cam_vec.y < 0.0f) +// cam_vec.y += 0.1; +// +// if (cam_vec.z > 0.0f) +// cam_vec.z -= 0.1; +// else if (cam_vec.z < 0.0f) +// cam_vec.z += 0.1; + + std::cout << cam_vec.x << " : " << cam_vec.y << " : " << cam_vec.z << std::endl; + + + // Time keeping + elapsed_time = elap_time(); + delta_time = elapsed_time - current_time; + current_time = elapsed_time; + if (delta_time > 0.2f) + delta_time = 0.2f; + accumulator_time += delta_time; + while ((accumulator_time - step_size) >= step_size) { + accumulator_time -= step_size; + + + // Update cycle + curse.Update(delta_time); + + + } + + // Fps cycle + // map->moveLight(sf::Vector2f(0.3, 0)); + + window.clear(sf::Color::Black); + + // Cast the rays and get the image + sf::Color* pixel_colors = ray_caster.CastRays(cam_dir, cam_pos); + + for (int i = 0; i < WINDOW_X * WINDOW_Y; i++) { + + Curses::Tile t(sf::Vector2i(i % WINDOW_X, i / WINDOW_X)); + Curses::Slot s(L'\u0045', pixel_colors[i], sf::Color::Black); + t.push_back(s); + curse.setTile(t); + + } + + + // Cast it to an array of Uint8's + auto out = (sf::Uint8*)pixel_colors; + + window_texture.update(out); + window_sprite.setTexture(window_texture); + window.draw(window_sprite); + + + curse.Render(); + + // Give the frame counter the frame time and draw the average frame time + fps.frame(delta_time); + fps.draw(&window); + + window.display(); + + + + } + return 0; + +}