From 6c0cab193c1f7ede6e0b72b2625e3dfbaab92a2b Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Mon, 14 Apr 2014 12:05:04 +0000 Subject: [PATCH] scene: position sprite tiles from their centre instead of their corner. --- src/scene.cpp | 10 ++++----- src/tileset.cpp | 60 +++++++++++++++++-------------------------------- src/tileset.h | 2 +- 3 files changed, 26 insertions(+), 46 deletions(-) diff --git a/src/scene.cpp b/src/scene.cpp index af88505b..7fa6e3e2 100644 --- a/src/scene.cpp +++ b/src/scene.cpp @@ -298,10 +298,10 @@ void Scene::RenderTiles() // XXX: rename to Blit() break; /* Create a vertex array object */ - VertexBuffer *vb1 = new VertexBuffer(6 * 3 * (n - i) * sizeof(float)); - float *vertex = (float *)vb1->Lock(0, 0); - VertexBuffer *vb2 = new VertexBuffer(6 * 2 * (n - i) * sizeof(float)); - float *texture = (float *)vb2->Lock(0, 0); + VertexBuffer *vb1 = new VertexBuffer(6 * (n - i) * sizeof(vec3)); + vec3 *vertex = (vec3 *)vb1->Lock(0, 0); + VertexBuffer *vb2 = new VertexBuffer(6 * (n - i) * sizeof(vec2)); + vec2 *texture = (vec2 *)vb2->Lock(0, 0); data->m_tile_bufs.Push(vb1); data->m_tile_bufs.Push(vb2); @@ -311,7 +311,7 @@ void Scene::RenderTiles() // XXX: rename to Blit() data->m_tiles[i].tileset->BlitTile(data->m_tiles[j].id, data->m_tiles[j].pos, data->m_tiles[j].o, data->m_tiles[j].scale, - vertex + 18 * (j - i), texture + 12 * (j - i)); + vertex + 6 * (j - i), texture + 6 * (j - i)); } vb1->Unlock(); diff --git a/src/tileset.cpp b/src/tileset.cpp index 9ebaa040..72b9ca53 100644 --- a/src/tileset.cpp +++ b/src/tileset.cpp @@ -197,7 +197,7 @@ void TileSet::Unbind() } void TileSet::BlitTile(uint32_t id, vec3 pos, int o, vec2 scale, - float *vertex, float *texture) + vec3 *vertex, vec2 *texture) { ibox2 pixels = m_data->m_tiles[id].m1; box2 texels = m_data->m_tiles[id].m2; @@ -228,50 +228,30 @@ void TileSet::BlitTile(uint32_t id, vec3 pos, int o, vec2 scale, dty *= 126.f / 128.f; #endif + vec3 extent_x = 0.5f * vec3(dx, 0.f, 0.f); + vec3 extent_y = 0.5f * vec3(0.f, dy, dz); + vec3 center = pos + extent_x + extent_y; + if (!m_data->m_image && m_data->m_texture) { - float tmp[10]; - - *vertex++ = pos.x + dx; - *vertex++ = pos.y + dy; - *vertex++ = pos.z + dz; - *texture++ = tx + dtx; - *texture++ = ty; - - *vertex++ = tmp[0] = pos.x; - *vertex++ = tmp[1] = pos.y + dy; - *vertex++ = tmp[2] = pos.z + dz; - *texture++ = tmp[3] = tx; - *texture++ = tmp[4] = ty; - - *vertex++ = tmp[5] = pos.x + dx; - *vertex++ = tmp[6] = pos.y; - *vertex++ = tmp[7] = pos.z; - *texture++ = tmp[8] = tx + dtx; - *texture++ = tmp[9] = ty + dty; - - *vertex++ = tmp[5]; - *vertex++ = tmp[6]; - *vertex++ = tmp[7]; - *texture++ = tmp[8]; - *texture++ = tmp[9]; - - *vertex++ = tmp[0]; - *vertex++ = tmp[1]; - *vertex++ = tmp[2]; - *texture++ = tmp[3]; - *texture++ = tmp[4]; - - *vertex++ = pos.x; - *vertex++ = pos.y; - *vertex++ = pos.z; - *texture++ = tx; - *texture++ = ty + dty; + *vertex++ = center + extent_x + extent_y; + *vertex++ = center - extent_x + extent_y; + *vertex++ = center + extent_x - extent_y; + *vertex++ = center + extent_x - extent_y; + *vertex++ = center - extent_x + extent_y; + *vertex++ = center - extent_x - extent_y; + + *texture++ = vec2(tx + dtx, ty); + *texture++ = vec2(tx, ty); + *texture++ = vec2(tx + dtx, ty + dty); + *texture++ = vec2(tx + dtx, ty + dty); + *texture++ = vec2(tx, ty); + *texture++ = vec2(tx, ty + dty); } else { - memset(vertex, 0, 3 * sizeof(float)); - memset(texture, 0, 2 * sizeof(float)); + memset(vertex, 0, 6 * sizeof(vec3)); + memset(texture, 0, 6 * sizeof(vec2)); } } diff --git a/src/tileset.h b/src/tileset.h index 0d3cef1b..67513327 100644 --- a/src/tileset.h +++ b/src/tileset.h @@ -53,7 +53,7 @@ public: void Bind(); void Unbind(); void BlitTile(uint32_t id, vec3 pos, int o, vec2 scale, - float *vertex, float *texture); + vec3 *vertex, vec2 *texture); private: void Init(char const *path);