summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--CMakeLists.txt7
-rw-r--r--cmake/FindSDL2.cmake254
-rw-r--r--cmake/FindSDL2_Image.cmake98
-rw-r--r--src/consts.h7
-rw-r--r--src/main.cpp25
-rw-r--r--src/renderer.cpp216
-rw-r--r--src/renderer.h126
7 files changed, 732 insertions, 1 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 52d0ce5..55237fa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt
@@ -3,15 +3,21 @@ project (tanetane)
3 3
4set(CMAKE_BUILD_TYPE Debug) 4set(CMAKE_BUILD_TYPE Debug)
5 5
6# Set directory to look for package helpers.
7set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${tanetane_SOURCE_DIR}/cmake")
8
6# Get dependencies. 9# Get dependencies.
7find_package(SDL2 REQUIRED) 10find_package(SDL2 REQUIRED)
11find_package(SDL2_Image REQUIRED)
8 12
9set(ALL_LIBS 13set(ALL_LIBS
10 ${SDL2_LIBRARY} 14 ${SDL2_LIBRARY}
15 ${SDL2_IMAGE_LIBRARIES}
11) 16)
12 17
13include_directories( 18include_directories(
14 ${SDL2_INCLUDE_DIR} 19 ${SDL2_INCLUDE_DIR}
20 ${SDL2_IMAGE_INCLUDE_DIRS}
15 src 21 src
16) 22)
17 23
@@ -21,6 +27,7 @@ link_directories(
21 27
22add_executable(tanetane 28add_executable(tanetane
23 src/main.cpp 29 src/main.cpp
30 src/renderer.cpp
24) 31)
25 32
26set_property(TARGET tanetane PROPERTY CXX_STANDARD 17) 33set_property(TARGET tanetane PROPERTY CXX_STANDARD 17)
diff --git a/cmake/FindSDL2.cmake b/cmake/FindSDL2.cmake new file mode 100644 index 0000000..f9ebcf3 --- /dev/null +++ b/cmake/FindSDL2.cmake
@@ -0,0 +1,254 @@
1# Locate SDL2 library
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 SDL2_main 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 SDL2main.h and SDL2main.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# $SDL2 is an environment variable that would
31# correspond to the ./configure --prefix=$SDL2
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" SDL2 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" SDL2 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# Ported by Johnny Patterson. This is a literal port for SDL2 of the FindSDL.cmake
56# module with the minor edit of changing "SDL" to "SDL2" where necessary. This
57# was not created for redistribution, and exists temporarily pending official
58# SDL2 CMake modules.
59#
60# Note that on windows this will only search for the 32bit libraries, to search
61# for 64bit change x86/i686-w64 to x64/x86_64-w64
62
63#=============================================================================
64# Copyright 2003-2009 Kitware, Inc.
65#
66# CMake - Cross Platform Makefile Generator
67# Copyright 2000-2014 Kitware, Inc.
68# Copyright 2000-2011 Insight Software Consortium
69# All rights reserved.
70#
71# Redistribution and use in source and binary forms, with or without
72# modification, are permitted provided that the following conditions
73# are met:
74#
75# * Redistributions of source code must retain the above copyright
76# notice, this list of conditions and the following disclaimer.
77#
78# * Redistributions in binary form must reproduce the above copyright
79# notice, this list of conditions and the following disclaimer in the
80# documentation and/or other materials provided with the distribution.
81#
82# * Neither the names of Kitware, Inc., the Insight Software Consortium,
83# nor the names of their contributors may be used to endorse or promote
84# products derived from this software without specific prior written
85# permission.
86#
87# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
88# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
89# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
90# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
91# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
92# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
93# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
94# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
95# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
96# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
97# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
98#
99# This software is distributed WITHOUT ANY WARRANTY; without even the
100# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
101# See the License for more information.
102#=============================================================================
103# (To distribute this file outside of CMake, substitute the full
104# License text for the above reference.)
105
106FIND_PATH(SDL2_INCLUDE_DIR SDL.h
107 HINTS
108 ${SDL2}
109 $ENV{SDL2}
110 PATH_SUFFIXES include/SDL2 include SDL2
111 i686-w64-mingw32/include/SDL2
112 x86_64-w64-mingw32/include/SDL2
113 PATHS
114 ~/Library/Frameworks
115 /Library/Frameworks
116 /usr/local/include/SDL2
117 /usr/include/SDL2
118 /sw # Fink
119 /opt/local # DarwinPorts
120 /opt/csw # Blastwave
121 /opt
122)
123
124# Lookup the 64 bit libs on x64
125IF(CMAKE_SIZEOF_VOID_P EQUAL 8)
126 FIND_LIBRARY(SDL2_LIBRARY_TEMP SDL2
127 HINTS
128 ${SDL2}
129 $ENV{SDL2}
130 PATH_SUFFIXES lib64 lib
131 lib/x64
132 x86_64-w64-mingw32/lib
133 PATHS
134 /sw
135 /opt/local
136 /opt/csw
137 /opt
138 )
139# On 32bit build find the 32bit libs
140ELSE(CMAKE_SIZEOF_VOID_P EQUAL 8)
141 FIND_LIBRARY(SDL2_LIBRARY_TEMP SDL2
142 HINTS
143 ${SDL2}
144 $ENV{SDL2}
145 PATH_SUFFIXES lib
146 lib/x86
147 i686-w64-mingw32/lib
148 PATHS
149 /sw
150 /opt/local
151 /opt/csw
152 /opt
153 )
154ENDIF(CMAKE_SIZEOF_VOID_P EQUAL 8)
155
156IF(NOT SDL2_BUILDING_LIBRARY)
157 IF(NOT ${SDL2_INCLUDE_DIR} MATCHES ".framework")
158 # Non-OS X framework versions expect you to also dynamically link to
159 # SDL2main. This is mainly for Windows and OS X. Other (Unix) platforms
160 # seem to provide SDL2main for compatibility even though they don't
161 # necessarily need it.
162 # Lookup the 64 bit libs on x64
163 IF(CMAKE_SIZEOF_VOID_P EQUAL 8)
164 FIND_LIBRARY(SDL2MAIN_LIBRARY
165 NAMES SDL2main
166 HINTS
167 ${SDL2}
168 $ENV{SDL2}
169 PATH_SUFFIXES lib64 lib
170 lib/x64
171 x86_64-w64-mingw32/lib
172 PATHS
173 /sw
174 /opt/local
175 /opt/csw
176 /opt
177 )
178 # On 32bit build find the 32bit libs
179 ELSE(CMAKE_SIZEOF_VOID_P EQUAL 8)
180 FIND_LIBRARY(SDL2MAIN_LIBRARY
181 NAMES SDL2main
182 HINTS
183 ${SDL2}
184 $ENV{SDL2}
185 PATH_SUFFIXES lib
186 lib/x86
187 i686-w64-mingw32/lib
188 PATHS
189 /sw
190 /opt/local
191 /opt/csw
192 /opt
193 )
194 ENDIF(CMAKE_SIZEOF_VOID_P EQUAL 8)
195 ENDIF(NOT ${SDL2_INCLUDE_DIR} MATCHES ".framework")
196ENDIF(NOT SDL2_BUILDING_LIBRARY)
197
198# SDL2 may require threads on your system.
199# The Apple build may not need an explicit flag because one of the
200# frameworks may already provide it.
201# But for non-OSX systems, I will use the CMake Threads package.
202IF(NOT APPLE)
203 FIND_PACKAGE(Threads)
204ENDIF(NOT APPLE)
205
206# MinGW needs an additional library, mwindows
207# It's total link flags should look like -lmingw32 -lSDL2main -lSDL2 -lmwindows
208# (Actually on second look, I think it only needs one of the m* libraries.)
209IF(MINGW)
210 SET(MINGW32_LIBRARY mingw32 CACHE STRING "mwindows for MinGW")
211ENDIF(MINGW)
212
213SET(SDL2_FOUND "NO")
214 IF(SDL2_LIBRARY_TEMP)
215 # For SDL2main
216 IF(NOT SDL2_BUILDING_LIBRARY)
217 IF(SDL2MAIN_LIBRARY)
218 SET(SDL2_LIBRARY_TEMP ${SDL2MAIN_LIBRARY} ${SDL2_LIBRARY_TEMP})
219 ENDIF(SDL2MAIN_LIBRARY)
220 ENDIF(NOT SDL2_BUILDING_LIBRARY)
221
222 # For OS X, SDL2 uses Cocoa as a backend so it must link to Cocoa.
223 # CMake doesn't display the -framework Cocoa string in the UI even
224 # though it actually is there if I modify a pre-used variable.
225 # I think it has something to do with the CACHE STRING.
226 # So I use a temporary variable until the end so I can set the
227 # "real" variable in one-shot.
228 IF(APPLE)
229 SET(SDL2_LIBRARY_TEMP ${SDL2_LIBRARY_TEMP} "-framework Cocoa")
230 ENDIF(APPLE)
231
232 # For threads, as mentioned Apple doesn't need this.
233 # In fact, there seems to be a problem if I used the Threads package
234 # and try using this line, so I'm just skipping it entirely for OS X.
235 IF(NOT APPLE)
236 SET(SDL2_LIBRARY_TEMP ${SDL2_LIBRARY_TEMP} ${CMAKE_THREAD_LIBS_INIT})
237 ENDIF(NOT APPLE)
238
239 # For MinGW library
240 IF(MINGW)
241 SET(SDL2_LIBRARY_TEMP ${MINGW32_LIBRARY} ${SDL2_LIBRARY_TEMP})
242 ENDIF(MINGW)
243
244 # Set the final string here so the GUI reflects the final state.
245 SET(SDL2_LIBRARY ${SDL2_LIBRARY_TEMP} CACHE STRING "Where the SDL2 Library can be found")
246 # Set the temp variable to INTERNAL so it is not seen in the CMake GUI
247 SET(SDL2_LIBRARY_TEMP "${SDL2_LIBRARY_TEMP}" CACHE INTERNAL "")
248
249 SET(SDL2_FOUND "YES")
250ENDIF(SDL2_LIBRARY_TEMP)
251
252INCLUDE(FindPackageHandleStandardArgs)
253
254FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL2 REQUIRED_VARS SDL2_LIBRARY SDL2_INCLUDE_DIR)
diff --git a/cmake/FindSDL2_Image.cmake b/cmake/FindSDL2_Image.cmake new file mode 100644 index 0000000..de842ca --- /dev/null +++ b/cmake/FindSDL2_Image.cmake
@@ -0,0 +1,98 @@
1# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
2# file Copyright.txt or https://cmake.org/licensing for details.
3
4#.rst:
5# FindSDL2_image
6# -------------
7#
8# Locate SDL2_image library
9#
10# This module defines:
11#
12# ::
13#
14# SDL2_IMAGE_LIBRARIES, the name of the library to link against
15# SDL2_IMAGE_INCLUDE_DIRS, where to find the headers
16# SDL2_IMAGE_FOUND, if false, do not try to link against
17# SDL2_IMAGE_VERSION_STRING - human-readable string containing the
18# version of SDL_image
19#
20#
21#
22# For backward compatibility the following variables are also set:
23#
24# ::
25#
26# SDL2IMAGE_LIBRARY (same value as SDL_IMAGE_LIBRARIES)
27# SDL2IMAGE_INCLUDE_DIR (same value as SDL_IMAGE_INCLUDE_DIRS)
28# SDL2IMAGE_FOUND (same value as SDL_IMAGE_FOUND)
29#
30#
31#
32# $SDL2DIR is an environment variable that would correspond to the
33# ./configure --prefix=$SDL2DIR used in building SDL.
34#
35# Created by Eric Wing. This was influenced by the FindSDL.cmake
36# module, but with modifications to recognize OS X frameworks and
37# additional Unix paths (FreeBSD, etc).
38
39if(NOT SDL2_IMAGE_INCLUDE_DIR AND SDL2IMAGE_INCLUDE_DIR)
40 set(SDL2_IMAGE_INCLUDE_DIR ${SDL2IMAGE_INCLUDE_DIR} CACHE PATH "directory cache entry initialized from old variable name")
41endif()
42find_path(SDL2_IMAGE_INCLUDE_DIR SDL_image.h
43 HINTS
44 ENV SDL2IMAGEDIR
45 ENV SDL2DIR
46 ${SDL2_DIR}
47 PATH_SUFFIXES SDL2
48 # path suffixes to search inside ENV{SDL2DIR}
49 include/SDL2 include
50)
51
52if(CMAKE_SIZEOF_VOID_P EQUAL 8)
53 set(VC_LIB_PATH_SUFFIX lib/x64)
54else()
55 set(VC_LIB_PATH_SUFFIX lib/x86)
56endif()
57
58if(NOT SDL2_IMAGE_LIBRARY AND SDL2IMAGE_LIBRARY)
59 set(SDL2_IMAGE_LIBRARY ${SDL2IMAGE_LIBRARY} CACHE FILEPATH "file cache entry initialized from old variable name")
60endif()
61find_library(SDL2_IMAGE_LIBRARY
62 NAMES SDL2_image
63 HINTS
64 ENV SDL2IMAGEDIR
65 ENV SDL2DIR
66 ${SDL2_DIR}
67 PATH_SUFFIXES lib ${VC_LIB_PATH_SUFFIX}
68)
69
70if(SDL2_IMAGE_INCLUDE_DIR AND EXISTS "${SDL2_IMAGE_INCLUDE_DIR}/SDL_image.h")
71 file(STRINGS "${SDL2_IMAGE_INCLUDE_DIR}/SDL_image.h" SDL2_IMAGE_VERSION_MAJOR_LINE REGEX "^#define[ \t]+SDL_IMAGE_MAJOR_VERSION[ \t]+[0-9]+$")
72 file(STRINGS "${SDL2_IMAGE_INCLUDE_DIR}/SDL_image.h" SDL2_IMAGE_VERSION_MINOR_LINE REGEX "^#define[ \t]+SDL_IMAGE_MINOR_VERSION[ \t]+[0-9]+$")
73 file(STRINGS "${SDL2_IMAGE_INCLUDE_DIR}/SDL_image.h" SDL2_IMAGE_VERSION_PATCH_LINE REGEX "^#define[ \t]+SDL_IMAGE_PATCHLEVEL[ \t]+[0-9]+$")
74 string(REGEX REPLACE "^#define[ \t]+SDL_IMAGE_MAJOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL2_IMAGE_VERSION_MAJOR "${SDL_IMAGE_VERSION_MAJOR_LINE}")
75 string(REGEX REPLACE "^#define[ \t]+SDL_IMAGE_MINOR_VERSION[ \t]+([0-9]+)$" "\\1" SDL2_IMAGE_VERSION_MINOR "${SDL_IMAGE_VERSION_MINOR_LINE}")
76 string(REGEX REPLACE "^#define[ \t]+SDL_IMAGE_PATCHLEVEL[ \t]+([0-9]+)$" "\\1" SDL2_IMAGE_VERSION_PATCH "${SDL_IMAGE_VERSION_PATCH_LINE}")
77 set(SDL2_IMAGE_VERSION_STRING ${SDL2_IMAGE_VERSION_MAJOR}.${SDL2_IMAGE_VERSION_MINOR}.${SDL2_IMAGE_VERSION_PATCH})
78 unset(SDL2_IMAGE_VERSION_MAJOR_LINE)
79 unset(SDL2_IMAGE_VERSION_MINOR_LINE)
80 unset(SDL2_IMAGE_VERSION_PATCH_LINE)
81 unset(SDL2_IMAGE_VERSION_MAJOR)
82 unset(SDL2_IMAGE_VERSION_MINOR)
83 unset(SDL2_IMAGE_VERSION_PATCH)
84endif()
85
86set(SDL2_IMAGE_LIBRARIES ${SDL2_IMAGE_LIBRARY})
87set(SDL2_IMAGE_INCLUDE_DIRS ${SDL2_IMAGE_INCLUDE_DIR})
88
89FIND_PACKAGE_HANDLE_STANDARD_ARGS(SDL2_image
90 REQUIRED_VARS SDL2_IMAGE_LIBRARIES SDL2_IMAGE_INCLUDE_DIRS
91 VERSION_VAR SDL2_IMAGE_VERSION_STRING)
92
93# for backward compatibility
94set(SDL2IMAGE_LIBRARY ${SDL2_IMAGE_LIBRARIES})
95set(SDL2IMAGE_INCLUDE_DIR ${SDL2_IMAGE_INCLUDE_DIRS})
96set(SDL2IMAGE_FOUND ${SDL2_IMAGE_FOUND})
97
98mark_as_advanced(SDL2_IMAGE_LIBRARY SDL2_IMAGE_INCLUDE_DIR) \ No newline at end of file
diff --git a/src/consts.h b/src/consts.h new file mode 100644 index 0000000..2088f45 --- /dev/null +++ b/src/consts.h
@@ -0,0 +1,7 @@
1#ifndef CONSTS_H_9561E49C
2#define CONSTS_H_9561E49C
3
4const int GAME_WIDTH = 640;
5const int GAME_HEIGHT = 480;
6
7#endif /* end of include guard: CONSTS_H_9561E49C */
diff --git a/src/main.cpp b/src/main.cpp index 22bdf92..2da7d9a 100644 --- a/src/main.cpp +++ b/src/main.cpp
@@ -1,6 +1,29 @@
1#include <iostream> 1#include <iostream>
2#include "renderer.h"
3
4void loop(Renderer& renderer) {
5 for (;;) {
6 SDL_Event e;
7 while (SDL_PollEvent(&e))
8 {
9 if (e.type == SDL_QUIT)
10 {
11 return;
12 }
13 }
14 }
15}
2 16
3int main(int, char**) { 17int main(int, char**) {
4 std::cout << "hi" << std::endl; 18 try
19 {
20 Renderer renderer;
21
22 loop(renderer);
23 } catch (const sdl_error& ex)
24 {
25 std::cout << "SDL error (" << ex.what() << ")" << std::endl;
26 }
27
5 return 0; 28 return 0;
6} 29}
diff --git a/src/renderer.cpp b/src/renderer.cpp new file mode 100644 index 0000000..e29d8cd --- /dev/null +++ b/src/renderer.cpp
@@ -0,0 +1,216 @@
1#include "renderer.h"
2#include "consts.h"
3
4Renderer::Renderer()
5{
6 win_ = window_ptr(
7 SDL_CreateWindow(
8 "Tanetane",
9 100,
10 100,
11 GAME_WIDTH,
12 GAME_HEIGHT,
13 SDL_WINDOW_SHOWN));
14
15 if (!win_)
16 {
17 throw sdl_error();
18 }
19
20 ren_ = renderer_ptr(
21 SDL_CreateRenderer(
22 win_.get(),
23 -1,
24 SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC));
25
26 if (!ren_)
27 {
28 throw sdl_error();
29 }
30}
31
32/*void Renderer::render()
33{
34 texture_ptr canvas(
35 SDL_CreateTexture(
36 ren_.get(),
37 SDL_PIXELFORMAT_RGBA8888,
38 SDL_TEXTUREACCESS_TARGET,
39 TILE_WIDTH * game.map.getWidth(),
40 TILE_HEIGHT * game.map.getHeight()));
41
42 if (!canvas)
43 {
44 throw sdl_error();
45 }
46
47 SDL_SetRenderTarget(ren_.get(), canvas.get());
48 SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_NONE);
49 SDL_SetRenderDrawColor(ren_.get(), rand() % 255, rand() % 255, rand() % 255, 255);
50 SDL_RenderClear(ren_.get());
51
52 for (int y = game.map.getTop(); y < game.map.getBottom(); y++)
53 {
54 for (int x = game.map.getLeft(); x < game.map.getRight(); x++)
55 {
56 bool draw = true;
57
58 if ((game.player_x == x && game.player_y == y) && game.renderPlayer)
59 {
60 SDL_SetRenderDrawColor(ren_.get(), 255, 255, 0, 255);
61 } else if (!game.map.at(x,y).lit)
62 {
63 if (drawDark)
64 {
65 SDL_SetRenderDrawColor(ren_.get(), 40, 40, 40, 255);
66 } else {
67 draw = false;
68 }
69 } else {
70 int alpha = 255;
71
72 switch (game.map.at(x,y).tile)
73 {
74 case Tile::Floor:
75 {
76 SDL_SetRenderDrawColor(ren_.get(), 210, 210, 210, alpha);
77 break;
78 }
79
80 case Tile::Wall:
81 {
82 SDL_SetRenderDrawColor(ren_.get(), 100, 100, 100, alpha);
83 break;
84 }
85
86 case Tile::Dust:
87 {
88 SDL_SetRenderDrawColor(ren_.get(), 128, 40, 255, alpha);
89 break;
90 }
91
92 case Tile::Lamp:
93 {
94 SDL_SetRenderDrawColor(ren_.get(), 0, 255, 255, alpha);
95 break;
96 }
97 }
98 }
99
100 if (draw)
101 {
102 SDL_Rect rect {
103 game.map.getTrueX(x) * TILE_WIDTH,
104 game.map.getTrueY(y) * TILE_HEIGHT,
105 TILE_WIDTH,
106 TILE_HEIGHT};
107
108 SDL_RenderFillRect(ren_.get(), &rect);
109 }
110 }
111 }
112
113 texture_ptr mask(
114 SDL_CreateTexture(
115 ren_.get(),
116 SDL_PIXELFORMAT_RGBA8888,
117 SDL_TEXTUREACCESS_TARGET,
118 TILE_WIDTH * game.map.getWidth(),
119 TILE_HEIGHT * game.map.getHeight()));
120
121 if (!mask)
122 {
123 throw sdl_error();
124 }
125
126 SDL_SetRenderTarget(ren_.get(), mask.get());
127 SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 0);
128 SDL_RenderClear(ren_.get());
129
130 for (int y = game.map.getTop(); y < game.map.getBottom(); y++)
131 {
132 for (int x = game.map.getLeft(); x < game.map.getRight(); x++)
133 {
134 if (game.map.at(x,y).lightType != Source::None)
135 {
136 texture_ptr sourceMask(
137 SDL_CreateTexture(
138 ren_.get(),
139 SDL_PIXELFORMAT_RGBA8888,
140 SDL_TEXTUREACCESS_TARGET,
141 TILE_WIDTH * game.map.getWidth(),
142 TILE_HEIGHT * game.map.getHeight()));
143
144 if (!sourceMask)
145 {
146 throw sdl_error();
147 }
148
149 SDL_SetRenderTarget(ren_.get(), sourceMask.get());
150 SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_NONE);
151 SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 0);
152 SDL_RenderClear(ren_.get());
153
154 int fadeX = game.map.getTrueX(x) - game.map.at(x,y).lightRadius;
155 int fadeY = game.map.getTrueY(y) - game.map.at(x,y).lightRadius;
156 int fadeRight = game.map.getTrueX(x) + game.map.at(x,y).lightRadius;
157 int fadeBottom = game.map.getTrueY(y) + game.map.at(x,y).lightRadius;
158
159 SDL_Rect fadeRect {
160 fadeX * TILE_WIDTH,
161 fadeY * TILE_HEIGHT,
162 (game.map.at(x,y).lightRadius * 2 + 1) * TILE_WIDTH,
163 (game.map.at(x,y).lightRadius * 2 + 1) * TILE_HEIGHT};
164
165 if (game.map.at(x,y).lightType == Source::Lamp)
166 {
167 SDL_SetTextureBlendMode(lampFade_.get(), SDL_BLENDMODE_NONE);
168 SDL_RenderCopy(ren_.get(), lampFade_.get(), nullptr, &fadeRect);
169 } else if (game.map.at(x,y).lightType == Source::Player) {
170 SDL_SetTextureBlendMode(playerFade_.get(), SDL_BLENDMODE_NONE);
171 SDL_RenderCopy(ren_.get(), playerFade_.get(), nullptr, &fadeRect);
172 } else if (game.map.at(x,y).lightType == Source::Dust) {
173 SDL_SetTextureBlendMode(dustFade_.get(), SDL_BLENDMODE_NONE);
174 SDL_RenderCopy(ren_.get(), dustFade_.get(), nullptr, &fadeRect);
175 }
176
177 SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 0);
178
179 for (int sy = fadeY; sy < fadeBottom; sy++)
180 {
181 for (int sx = fadeX; sx < fadeRight; sx++)
182 {
183 if (!game.map.at(x,y).litTiles.count({sx, sy}))
184 {
185 SDL_Rect rect {
186 game.map.getTrueX(sx) * TILE_WIDTH,
187 game.map.getTrueY(sy) * TILE_HEIGHT,
188 TILE_WIDTH,
189 TILE_HEIGHT};
190
191 SDL_RenderFillRect(ren_.get(), &rect);
192 }
193 }
194 }
195
196 SDL_SetRenderTarget(ren_.get(), mask.get());
197 SDL_SetTextureBlendMode(sourceMask.get(), SDL_BLENDMODE_ADD);
198 SDL_RenderCopy(ren_.get(), sourceMask.get(), nullptr, nullptr);
199 }
200 }
201 }
202
203 SDL_SetRenderTarget(ren_.get(), canvas.get());
204 SDL_SetTextureBlendMode(mask.get(), SDL_BLENDMODE_MOD);
205 SDL_RenderCopy(ren_.get(), mask.get(), nullptr, nullptr);
206
207 SDL_SetRenderTarget(ren_.get(), nullptr);
208
209 SDL_Rect zoomRect;
210
211 std::tie(zoomRect.x, zoomRect.y, zoomRect.w, zoomRect.h) =
212 calculateZoomRect(game);
213
214 SDL_RenderCopy(ren_.get(), canvas.get(), &zoomRect, nullptr);
215 SDL_RenderPresent(ren_.get());
216}*/
diff --git a/src/renderer.h b/src/renderer.h new file mode 100644 index 0000000..78856ed --- /dev/null +++ b/src/renderer.h
@@ -0,0 +1,126 @@
1#ifndef RENDERER_H_6A58EC30
2#define RENDERER_H_6A58EC30
3
4#include <SDL.h>
5#include <SDL_image.h>
6#include <stdexcept>
7#include <memory>
8
9class Game;
10
11class sdl_error : public std::logic_error {
12public:
13
14 sdl_error() : std::logic_error(SDL_GetError())
15 {
16 }
17};
18
19class img_error : public std::logic_error {
20public:
21
22 img_error() : std::logic_error(IMG_GetError())
23 {
24 }
25};
26
27class sdl_wrapper {
28public:
29
30 sdl_wrapper()
31 {
32 if (SDL_Init(SDL_INIT_VIDEO) != 0)
33 {
34 sdl_error ex;
35 SDL_Quit();
36
37 throw ex;
38 }
39 }
40
41 ~sdl_wrapper()
42 {
43 SDL_Quit();
44 }
45};
46
47class img_wrapper {
48public:
49
50 img_wrapper()
51 {
52 if (IMG_Init(IMG_INIT_PNG) != IMG_INIT_PNG)
53 {
54 img_error ex;
55 IMG_Quit();
56
57 throw ex;
58 }
59 }
60
61 ~img_wrapper()
62 {
63 IMG_Quit();
64 }
65};
66
67class window_deleter {
68public:
69
70 void operator()(SDL_Window* ptr)
71 {
72 SDL_DestroyWindow(ptr);
73 }
74};
75
76using window_ptr = std::unique_ptr<SDL_Window, window_deleter>;
77
78class renderer_deleter {
79public:
80
81 void operator()(SDL_Renderer* ptr)
82 {
83 SDL_DestroyRenderer(ptr);
84 }
85};
86
87using renderer_ptr = std::unique_ptr<SDL_Renderer, renderer_deleter>;
88
89class surface_deleter {
90public:
91
92 void operator()(SDL_Surface* ptr)
93 {
94 SDL_FreeSurface(ptr);
95 }
96};
97
98using surface_ptr = std::unique_ptr<SDL_Surface, surface_deleter>;
99
100class texture_deleter {
101public:
102
103 void operator()(SDL_Texture* ptr)
104 {
105 SDL_DestroyTexture(ptr);
106 }
107};
108
109using texture_ptr = std::unique_ptr<SDL_Texture, texture_deleter>;
110
111class Renderer {
112public:
113
114 Renderer();
115
116 //void render();
117
118private:
119
120 sdl_wrapper sdl_;
121 img_wrapper img_;
122 window_ptr win_;
123 renderer_ptr ren_;
124};
125
126#endif /* end of include guard: RENDERER_H_6A58EC30 */