Skip to content

Commit 7ff665b

Browse files
committed
add basic CMake file for SDL2 example
1 parent 8cb41f7 commit 7ff665b

3 files changed

Lines changed: 230 additions & 0 deletions

File tree

SDL2Example/CMakeLists.txt

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
cmake_minimum_required(VERSION 2.8.12)
2+
3+
project(SFML_example)
4+
SET(PROJECT_NAME SDL2_example)
5+
6+
if(NOT CMAKE_BUILD_TYPE)
7+
SET(CMAKE_BUILD_TYPE Debug CACHE STRING "Choose the type of build (Debug or Release)" FORCE)
8+
endif()
9+
10+
SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/modules/")
11+
SET(PROJECT_STATIC_RUNTIME FALSE CACHE BOOL "Use statically linked standard/runtime libraries?")
12+
#SET(PROJECT_STATIC_TMX FALSE CACHE BOOL "Use statically linked tmxlite library?")
13+
14+
if(CMAKE_COMPILER_IS_GNUCXX OR APPLE)
15+
if(PROJECT_STATIC_RUNTIME)
16+
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=c++14 -static")
17+
else()
18+
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=c++14")
19+
endif()
20+
endif()
21+
22+
SET (CMAKE_CXX_FLAGS_DEBUG "-g -D_DEBUG_")
23+
SET (CMAKE_CXX_FLAGS_RELEASE "-O4 -DNDEBUG")
24+
25+
find_package(tmxlite REQUIRED)
26+
find_package(SDL2 REQUIRED)
27+
28+
include_directories(
29+
${TMXLITE_INCLUDE_DIR}
30+
${SDL2_INCLUDE_DIR})
31+
32+
set(PROJECT_SRC
33+
src/main.cpp
34+
src/MapLayer.cpp
35+
src/Texture.cpp)
36+
37+
if(WIN32)
38+
add_executable(${PROJECT_NAME} WIN32 ${PROJECT_SRC})
39+
else()
40+
add_executable(${PROJECT_NAME} ${PROJECT_SRC})
41+
endif()
42+
43+
target_link_libraries(${PROJECT_NAME}
44+
${TMXLITE_LIBRARIES}
45+
${SDL2_LIBRARY})
46+
47+
#install executable
48+
install(TARGETS ${PROJECT_NAME}
49+
RUNTIME DESTINATION .)
50+
51+
#install game data
52+
install(DIRECTORY assets
53+
DESTINATION .)
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
2+
# This module defines
3+
# SDL2_LIBRARY, the name of the library to link against
4+
# SDL2_FOUND, if false, do not try to link to SDL2
5+
# SDL2_INCLUDE_DIR, where to find SDL.h
6+
#
7+
# This module responds to the the flag:
8+
# SDL2_BUILDING_LIBRARY
9+
# If this is defined, then no SDL2main will be linked in because
10+
# only applications need main().
11+
# Otherwise, it is assumed you are building an application and this
12+
# module will attempt to locate and set the the proper link flags
13+
# as part of the returned SDL2_LIBRARY variable.
14+
#
15+
# Don't forget to include SDLmain.h and SDLmain.m your project for the
16+
# OS X framework based version. (Other versions link to -lSDL2main which
17+
# this module will try to find on your behalf.) Also for OS X, this
18+
# module will automatically add the -framework Cocoa on your behalf.
19+
#
20+
#
21+
# Additional Note: If you see an empty SDL2_LIBRARY_TEMP in your configuration
22+
# and no SDL2_LIBRARY, it means CMake did not find your SDL2 library
23+
# (SDL2.dll, libsdl2.so, SDL2.framework, etc).
24+
# Set SDL2_LIBRARY_TEMP to point to your SDL2 library, and configure again.
25+
# Similarly, if you see an empty SDL2MAIN_LIBRARY, you should set this value
26+
# as appropriate. These values are used to generate the final SDL2_LIBRARY
27+
# variable, but when these values are unset, SDL2_LIBRARY does not get created.
28+
#
29+
#
30+
# $SDL2DIR is an environment variable that would
31+
# correspond to the ./configure --prefix=$SDL2DIR
32+
# used in building SDL2.
33+
# l.e.galup 9-20-02
34+
#
35+
# Modified by Eric Wing.
36+
# Added code to assist with automated building by using environmental variables
37+
# and providing a more controlled/consistent search behavior.
38+
# Added new modifications to recognize OS X frameworks and
39+
# additional Unix paths (FreeBSD, etc).
40+
# Also corrected the header search path to follow "proper" SDL guidelines.
41+
# Added a search for SDL2main which is needed by some platforms.
42+
# Added a search for threads which is needed by some platforms.
43+
# Added needed compile switches for MinGW.
44+
#
45+
# On OSX, this will prefer the Framework version (if found) over others.
46+
# People will have to manually change the cache values of
47+
# SDL2_LIBRARY to override this selection or set the CMake environment
48+
# CMAKE_INCLUDE_PATH to modify the search paths.
49+
#
50+
# Note that the header path has changed from SDL2/SDL.h to just SDL.h
51+
# This needed to change because "proper" SDL convention
52+
# is #include "SDL.h", not <SDL2/SDL.h>. This is done for portability
53+
# reasons because not all systems place things in SDL2/ (see FreeBSD).
54+
55+
#=============================================================================
56+
# Copyright 2003-2009 Kitware, Inc.
57+
#
58+
# Distributed under the OSI-approved BSD License (the "License");
59+
# see accompanying file Copyright.txt for details.
60+
#
61+
# This software is distributed WITHOUT ANY WARRANTY; without even the
62+
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
63+
# See the License for more information.
64+
#=============================================================================
65+
# (To distribute this file outside of CMake, substitute the full
66+
# License text for the above reference.)
67+
68+
message("<FindSDL2.cmake>")
69+
70+
SET(SDL2_SEARCH_PATHS
71+
~/Library/Frameworks
72+
/Library/Frameworks
73+
/usr/local
74+
/usr
75+
/sw # Fink
76+
/opt/local # DarwinPorts
77+
/opt/csw # Blastwave
78+
/opt
79+
${SDL2_PATH}
80+
)
81+
82+
FIND_PATH(SDL2_INCLUDE_DIR SDL.h
83+
HINTS
84+
$ENV{SDL2DIR}
85+
PATH_SUFFIXES include/SDL2 include
86+
PATHS ${SDL2_SEARCH_PATHS}
87+
)
88+
89+
FIND_LIBRARY(SDL2_LIBRARY_TEMP
90+
NAMES SDL2
91+
HINTS
92+
$ENV{SDL2DIR}
93+
PATH_SUFFIXES lib64 lib lib/x64 lib/x86
94+
PATHS ${SDL2_SEARCH_PATHS}
95+
)
96+
97+
IF(NOT SDL2_BUILDING_LIBRARY)
98+
IF(NOT ${SDL2_INCLUDE_DIR} MATCHES ".framework")
99+
# Non-OS X framework versions expect you to also dynamically link to
100+
# SDL2main. This is mainly for Windows and OS X. Other (Unix) platforms
101+
# seem to provide SDL2main for compatibility even though they don't
102+
# necessarily need it.
103+
FIND_LIBRARY(SDL2MAIN_LIBRARY
104+
NAMES SDL2main
105+
HINTS
106+
$ENV{SDL2DIR}
107+
PATH_SUFFIXES lib64 lib lib/x64 lib/x86
108+
PATHS ${SDL2_SEARCH_PATHS}
109+
)
110+
ENDIF(NOT ${SDL2_INCLUDE_DIR} MATCHES ".framework")
111+
ENDIF(NOT SDL2_BUILDING_LIBRARY)
112+
113+
# SDL2 may require threads on your system.
114+
# The Apple build may not need an explicit flag because one of the
115+
# frameworks may already provide it.
116+
# But for non-OSX systems, I will use the CMake Threads package.
117+
IF(NOT APPLE)
118+
FIND_PACKAGE(Threads)
119+
ENDIF(NOT APPLE)
120+
121+
# MinGW needs an additional link flag, -mwindows
122+
# It's total link flags should look like -lmingw32 -lSDL2main -lSDL2 -mwindows
123+
IF(MINGW)
124+
SET(MINGW32_LIBRARY mingw32 "-mwindows" CACHE STRING "mwindows for MinGW")
125+
ENDIF(MINGW)
126+
127+
IF(SDL2_LIBRARY_TEMP)
128+
# For SDL2main
129+
IF(NOT SDL2_BUILDING_LIBRARY)
130+
IF(SDL2MAIN_LIBRARY)
131+
SET(SDL2_LIBRARY_TEMP ${SDL2MAIN_LIBRARY} ${SDL2_LIBRARY_TEMP})
132+
ENDIF(SDL2MAIN_LIBRARY)
133+
ENDIF(NOT SDL2_BUILDING_LIBRARY)
134+
135+
# For OS X, SDL2 uses Cocoa as a backend so it must link to Cocoa.
136+
# CMake doesn't display the -framework Cocoa string in the UI even
137+
# though it actually is there if I modify a pre-used variable.
138+
# I think it has something to do with the CACHE STRING.
139+
# So I use a temporary variable until the end so I can set the
140+
# "real" variable in one-shot.
141+
IF(APPLE)
142+
SET(SDL2_LIBRARY_TEMP ${SDL2_LIBRARY_TEMP} "-framework Cocoa")
143+
ENDIF(APPLE)
144+
145+
# For threads, as mentioned Apple doesn't need this.
146+
# In fact, there seems to be a problem if I used the Threads package
147+
# and try using this line, so I'm just skipping it entirely for OS X.
148+
IF(NOT APPLE)
149+
SET(SDL2_LIBRARY_TEMP ${SDL2_LIBRARY_TEMP} ${CMAKE_THREAD_LIBS_INIT})
150+
ENDIF(NOT APPLE)
151+
152+
# For MinGW library
153+
IF(MINGW)
154+
SET(SDL2_LIBRARY_TEMP ${MINGW32_LIBRARY} ${SDL2_LIBRARY_TEMP})
155+
ENDIF(MINGW)
156+
157+
# Set the final string here so the GUI reflects the final state.
158+
SET(SDL2_LIBRARY ${SDL2_LIBRARY_TEMP} CACHE STRING "Where the SDL2 Library can be found")
159+
# Set the temp variable to INTERNAL so it is not seen in the CMake GUI
160+
SET(SDL2_LIBRARY_TEMP "${SDL2_LIBRARY_TEMP}" CACHE INTERNAL "")
161+
ENDIF(SDL2_LIBRARY_TEMP)
162+
163+
message("</FindSDL2.cmake>")
164+
165+
INCLUDE(FindPackageHandleStandardArgs)
166+
167+
FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL2 REQUIRED_VARS SDL2_LIBRARY SDL2_INCLUDE_DIR)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
include(FindPackageHandleStandardArgs)
2+
3+
# Search for the header file
4+
find_path(TMXLITE_INCLUDE_DIR NAMES tmxlite/Config.hpp PATH_SUFFIXES include)
5+
6+
# Search for the library
7+
find_library(TMXLITE_LIBRARIES NAMES tmxlite PATH_SUFFIXES lib)
8+
9+
# Did we find everything we need?
10+
FIND_PACKAGE_HANDLE_STANDARD_ARGS(tmxlite DEFAULT_MSG TMXLITE_LIBRARIES TMXLITE_INCLUDE_DIR)

0 commit comments

Comments
 (0)