GameKit  0.0.1a
C++ gamedev tools
TilemapRenderer.cpp
Go to the documentation of this file.
1 /*
2  * =====================================================================================
3  *
4  * Filename: TilemapRenderer.cpp
5  *
6  * Description:
7  *
8  * Created: 15/02/2019 19:21:11
9  *
10  * Author: Quentin Bazin, <quent42340@gmail.com>
11  *
12  * =====================================================================================
13  */
14 #include "gk/gl/GLCheck.hpp"
15 #include "gk/gl/Shader.hpp"
16 #include "gk/gl/Vertex.hpp"
18 #include "gk/graphics/Tilemap.hpp"
19 
20 namespace gk {
21 
22 void TilemapRenderer::init(Tilemap *map, u16 mapWidth, u16 mapHeight, u8 mapLayers) {
23  m_map = map;
24 
26  m_vbo.setData(sizeof(gk::Vertex) * mapWidth * mapHeight * mapLayers * 6, nullptr, GL_DYNAMIC_DRAW);
27  gk::VertexBuffer::bind(nullptr);
28 }
29 
30 void TilemapRenderer::updateTile(u8 layer, u16 tileX, u16 tileY, u16 id, Tilemap &map) {
31  if (id == 0) return;
32 
34 
35  float tileWidth = map.tileset().tileWidth();
36  float tileHeight = map.tileset().tileHeight();
37 
38  float x = tileX * tileWidth;
39  float y = tileY * tileHeight;
40 
41  float texTileX = id % u16(map.tileset().width() / tileWidth) * tileWidth / map.tileset().width();
42  float texTileY = id / u16(map.tileset().width() / tileWidth) * tileHeight / map.tileset().height();
43 
44  float texTileWidth = tileWidth / map.tileset().width();
45  float texTileHeight = tileHeight / map.tileset().height();
46 
47  gk::Vertex vertices[] = {
48  {{x , y , 0, 1}, {texTileX , texTileY }, {1.0f, 1.0f, 1.0f, 1.0f}},
49  {{x + tileWidth, y , 0, 1}, {texTileX + texTileWidth, texTileY }, {1.0f, 1.0f, 1.0f, 1.0f}},
50  {{x + tileWidth, y + tileHeight, 0, 1}, {texTileX + texTileWidth, texTileY + texTileHeight}, {1.0f, 1.0f, 1.0f, 1.0f}},
51  {{x , y , 0, 1}, {texTileX , texTileY }, {1.0f, 1.0f, 1.0f, 1.0f}},
52  {{x + tileWidth, y + tileHeight, 0, 1}, {texTileX + texTileWidth, texTileY + texTileHeight}, {1.0f, 1.0f, 1.0f, 1.0f}},
53  {{x , y + tileHeight, 0, 1}, {texTileX , texTileY + texTileHeight}, {1.0f, 1.0f, 1.0f, 1.0f}}
54  };
55 
56  m_vbo.updateData(sizeof(vertices) * (tileX + tileY * map.width() + layer * map.width() * map.height()), sizeof(vertices), vertices);
57 
58  gk::VertexBuffer::bind(nullptr);
59 }
60 
62  if (!m_map) return;
63 
64  states.texture = &m_map->tileset();
66 
67  glCheck(glDisable(GL_CULL_FACE));
68  glCheck(glDisable(GL_DEPTH_TEST));
69 
70  for (u8 i = 0 ; i < m_map->layerCount() ; ++i) {
71  target.draw(m_vbo, GL_TRIANGLES,
72  (6 * m_map->width() * m_map->height()) * (m_map->layerCount() - 1 - i),
73  6 * m_map->width() * m_map->height(), states);
74  }
75 }
76 
77 } // namespace gk
78 
void updateTile(u8 layer, u16 tileX, u16 tileY, u16 id, Tilemap &map)
const Texture * texture
void updateData(GLintptr offset, GLsizeiptr size, const GLvoid *data) const
u16 width() const
Definition: Tilemap.hpp:39
unsigned short u16
Definition: IntTypes.hpp:22
Tileset & tileset()
Definition: Tilemap.hpp:44
unsigned char u8
Definition: IntTypes.hpp:21
void draw(const IDrawable &drawable, const RenderStates &states=RenderStates::Default)
void draw(gk::RenderTarget &target, gk::RenderStates states) const override
Draw the object to a render target.
gk::VertexBuffer m_vbo
void init(Tilemap *map, u16 mapWidth, u16 mapHeight, u8 mapLayers)
static void bind(const VertexBuffer *vertexBuffer)
u16 height() const
Return the height of the texture.
Definition: Texture.hpp:124
u16 tileHeight() const
Definition: Tileset.hpp:59
u16 width() const
Return the width of the texture.
Definition: Texture.hpp:116
#define glCheck(expr)
Definition: GLCheck.hpp:18
u16 height() const
Definition: Tilemap.hpp:40
u8 layerCount() const
Definition: Tilemap.hpp:42
void setData(GLsizeiptr size, const GLvoid *data, GLenum usage) const
u16 tileWidth() const
Definition: Tileset.hpp:58