Skip to content

Commit a4787f5

Browse files
committed
add loading tile set textures to opengl example
1 parent 75b1f3f commit a4787f5

9 files changed

Lines changed: 95 additions & 17 deletions

File tree

OpenGLExample/src/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
set(PROJECT_SRC
22
${PROJECT_DIR}/Game.cpp
3-
${PROJECT_DIR}/GLExtensions.cpp
43
${PROJECT_DIR}/main.cpp
5-
${PROJECT_DIR}/MapLayer.cpp)
4+
${PROJECT_DIR}/MapLayer.cpp
5+
${PROJECT_DIR}/OpenGL.cpp)

OpenGLExample/src/Game.cpp

Lines changed: 72 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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

137146
void 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

159184
void 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

OpenGLExample/src/Game.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,10 @@ class Game final
5656
glm::mat4 m_projectionMatrix;
5757

5858
unsigned m_shader;
59-
void initGLStuff();
59+
std::vector<unsigned> m_tileTextures;
60+
void initGLStuff(const tmx::Map&);
6061
void loadShader();
62+
void loadTexture(const std::string&);
6163

6264
};
6365

OpenGLExample/src/MapLayer.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,14 @@ source distribution.
2626
*********************************************************************/
2727

2828
#include <MapLayer.hpp>
29-
#include <GLExtensions.hpp>
29+
#include <OpenGL.hpp>
3030
#include <GLCheck.hpp>
3131

3232
#include <tmxlite/Map.hpp>
3333

34-
MapLayer::MapLayer(const tmx::Map& map, int layer)
35-
: m_vbo(0)
34+
MapLayer::MapLayer(const tmx::Map& map, int layer, const std::vector<unsigned>& textures)
35+
: m_tilesetTextures (textures),
36+
m_vbo (0)
3637
{
3738
createResources(map);
3839
}
@@ -49,6 +50,9 @@ MapLayer::~MapLayer()
4950
//public
5051
void ::MapLayer::draw()
5152
{
53+
glCheck(glActiveTexture(GL_TEXTURE0));
54+
glCheck(glBindTexture(GL_TEXTURE_2D, m_tilesetTextures[0]));
55+
5256
glCheck(glEnableVertexAttribArray(0));
5357
glCheck(glEnableVertexAttribArray(1));
5458
glCheck(glBindBuffer(GL_ARRAY_BUFFER, m_vbo));

OpenGLExample/src/MapLayer.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ source distribution.
2828
#ifndef MAPLAYER_HPP_
2929
#define MAPLAYER_HPP_
3030

31+
#include <vector>
32+
3133
namespace tmx
3234
{
3335
class Map;
@@ -36,7 +38,7 @@ namespace tmx
3638
class MapLayer final
3739
{
3840
public:
39-
MapLayer(const tmx::Map&, int);
41+
MapLayer(const tmx::Map&, int, const std::vector<unsigned>&);
4042
~MapLayer();
4143

4244
MapLayer(const MapLayer&) = delete;
@@ -46,6 +48,8 @@ class MapLayer final
4648

4749
private:
4850

51+
const std::vector<unsigned>& m_tilesetTextures;
52+
4953
unsigned m_vbo;
5054
void createResources(const tmx::Map&);
5155
};
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include <stdlib.h>
22
#include <string.h>
33
#include <stddef.h>
4-
#include <GLExtensions.hpp>
4+
#include <OpenGL.hpp>
55

66
#if defined(__APPLE__)
77
#include <dlfcn.h>

OpenGLExample/src/Shader.inl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ static const std::string fragmentShader = R"(
6464
6565
void main()
6666
{
67-
colour = vec4(1.0, 0.0, 0.0, 1.0);
67+
colour = texture(u_tileMap, v_texCoord);
6868
})";
6969

7070
#endif //SHADER_INL_

OpenGLExample/src/main.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ source distribution.
2828
#include <Game.hpp>
2929

3030
#include <SDL2/SDL.h>
31+
#include <SDL2/SDL_image.h>
3132

3233
#include <iostream>
3334

@@ -71,11 +72,14 @@ int main()
7172
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &value);
7273
std::cout << "." << value << std::endl;
7374

75+
IMG_Init(IMG_INIT_JPG | IMG_INIT_PNG);
76+
7477
Game game;
7578
game.run(window);
7679
}
7780
}
7881

82+
IMG_Quit();
7983
SDL_GL_DeleteContext(context);
8084
SDL_DestroyWindow(window);
8185
SDL_Quit();

0 commit comments

Comments
 (0)