@@ -26,13 +26,15 @@ source distribution.
2626*********************************************************************/
2727
2828#include < Game.hpp>
29- #include < GLExtensions .hpp>
29+ #include < OpenGL .hpp>
3030#include < GLCheck.hpp>
3131
3232#include < glm/gtc/matrix_transform.hpp>
3333
3434#include < tmxlite/Map.hpp>
3535
36+ #include < SDL2/SDL_image.h>
37+
3638#include < cassert>
3739#include < array>
3840
@@ -56,6 +58,10 @@ Game::~Game()
5658 {
5759 glCheck (glDeleteProgram (m_shader));
5860 }
61+ for (const auto & t : m_tileTextures)
62+ {
63+ glCheck (glDeleteTextures (1 , &t));
64+ }
5965}
6066
6167// public
@@ -125,6 +131,9 @@ void Game::draw(SDL_Window* window)
125131 glCheck (glClear (GL_COLOR_BUFFER_BIT ));
126132 glCheck (glUseProgram (m_shader));
127133
134+ // glCheck(glActiveTexture(GL_TEXTURE0));
135+ // glCheck(glBindTexture(GL_TEXTURE_2D, m_tileTextures[0]));
136+
128137 for (const auto & layer : m_mapLayers)
129138 {
130139 layer->draw ();
@@ -136,24 +145,40 @@ void Game::draw(SDL_Window* window)
136145
137146void Game::loadMap ()
138147{
139- // create shared resources, shader and tileset textures
140- initGLStuff ();
141-
142148 tmx::Map map;
143- map.load (" assets/demo.tmx" );
149+ map.load (" assets/demo.tmx" );
144150
151+ // create shared resources, shader and tileset textures
152+ initGLStuff (map);
153+
145154 // TODO pass reference to shader and textures to each layer
146- m_mapLayers.emplace_back (std::make_unique<MapLayer>(map, 0 ));
155+ m_mapLayers.emplace_back (std::make_unique<MapLayer>(map, 0 , m_tileTextures ));
147156}
148157
149158
150- void Game::initGLStuff ()
159+ void Game::initGLStuff (const tmx::Map& map )
151160{
152161 m_projectionMatrix = glm::ortho (0 .f , 800 .f , 600 .f , 0 .f , -0 .1f , 100 .f );
153162
154163 loadShader ();
155164 glCheck (glUseProgram (m_shader));
156165 glCheck (glUniformMatrix4fv (glGetUniformLocation (m_shader, " u_projectionMatrix" ), 1 , GL_FALSE , &m_projectionMatrix[0 ][0 ]));
166+
167+ // we'll make sure the current tile texture is active in 0, and lookup texture is active in 1
168+ glCheck (glUniform1i (glGetUniformLocation (m_shader, " u_tileMap" ), 0 ));
169+ glCheck (glUniform1i (glGetUniformLocation (m_shader, " u_lookupMap" ), 1 ));
170+
171+ const auto & tilesets = map.getTilesets ();
172+ for (const auto & ts : tilesets)
173+ {
174+ loadTexture (ts.getImagePath ());
175+ }
176+
177+
178+ glCheck (glClearColor (0 .6f , 0 .8f , 0 .92f , 1 .f ));
179+ glCheck (glEnable (GL_BLEND ));
180+ glCheck (glBlendFunc (GL_SRC_ALPHA , GL_ONE_MINUS_SRC_ALPHA ));
181+ glCheck (glBlendEquation (GL_FUNC_ADD ));
157182}
158183
159184void Game::loadShader ()
@@ -241,7 +266,46 @@ void Game::loadShader()
241266 glCheck (glDeleteShader (fragID));
242267}
243268
244-
269+ void Game::loadTexture (const std::string& path)
270+ {
271+ /* static unsigned char flunge[] =
272+ {
273+ 255, 128, 255, 128, 255, 128, 255, 128,
274+ 255, 128, 255, 128, 255, 128, 255, 128,
275+ 255, 128, 255, 128, 255, 128, 255, 128,
276+ 255, 128, 255, 128, 255, 128, 255, 128
277+ };*/
278+
279+ auto img = IMG_Load (path.c_str ());
280+ if (img)
281+ {
282+ SDL_LockSurface (img);
283+
284+ m_tileTextures.emplace_back (0 );
285+ auto & texture = m_tileTextures.back ();
286+
287+ GLint format = (img->format ->BitsPerPixel == 32 ) ? GL_RGBA : GL_RGB ;
288+
289+ glCheck (glGenTextures (1 , &texture));
290+ glCheck (glBindTexture (GL_TEXTURE_2D , texture));
291+ glCheck (glTexImage2D (GL_TEXTURE_2D , 0 , format, img->w , img->h , 0 , format, GL_UNSIGNED_BYTE , img->pixels ));
292+ // glCheck(glTexImage2D(GL_TEXTURE_2D, 0, GL_RG, 4, 4, 0, GL_RG, GL_UNSIGNED_BYTE, flunge));
293+
294+ glCheck (glTexParameteri (GL_TEXTURE_2D , GL_TEXTURE_WRAP_S , GL_REPEAT ));
295+ glCheck (glTexParameteri (GL_TEXTURE_2D , GL_TEXTURE_WRAP_T , GL_REPEAT ));
296+ glCheck (glTexParameteri (GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER , GL_NEAREST ));
297+ glCheck (glTexParameteri (GL_TEXTURE_2D , GL_TEXTURE_MAG_FILTER , GL_LINEAR ));
298+
299+ SDL_UnlockSurface (img);
300+ SDL_FreeSurface (img);
301+
302+ std::cout << " Loaded " << path << std::endl;
303+ }
304+ else
305+ {
306+ std::cout << " Failed to load " << path << std::endl;
307+ }
308+ }
245309
246310
247311
0 commit comments