source: trunk/src/tileset.cpp @ 2609

Last change on this file since 2609 was 2609, checked in by sam, 8 years ago

tileset: refactor the tile generation code.

  • Property svn:keywords set to Id
File size: 6.7 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://www.wtfpl.net/ for more details.
9//
10
11#if defined HAVE_CONFIG_H
12#   include "config.h"
13#endif
14
15#include <cstdlib>
16#include <cstdio>
17#include <cstring>
18
19#if defined WIN32 && !defined _XBOX
20#   define WIN32_LEAN_AND_MEAN
21#   include <windows.h>
22#   if defined USE_D3D9
23#       define FAR
24#       define NEAR
25#       include <d3d9.h>
26#   endif
27#endif
28
29#include "core.h"
30#include "lolgl.h"
31
32using namespace std;
33
34#if defined USE_D3D9
35extern IDirect3DDevice9 *g_d3ddevice;
36#elif defined _XBOX
37extern D3DDevice *g_d3ddevice;
38#endif
39
40namespace lol
41{
42
43/*
44 * TileSet implementation class
45 */
46
47class TileSetData
48{
49    friend class TileSet;
50
51private:
52    String m_name;
53
54    /* Pixels, then texture coordinates */
55    Array<ibox2, box2> m_tiles;
56    ivec2 m_tile_size, m_image_size, m_texture_size;
57
58    Image *m_image;
59    Texture *m_texture;
60};
61
62/*
63 * Public TileSet class
64 */
65
66TileSet::TileSet(char const *path)
67  : m_data(new TileSetData())
68{
69    Init(path);
70
71    m_drawgroup = DRAWGROUP_BEFORE;
72}
73
74TileSet::TileSet(char const *path, ivec2 size, ivec2 count)
75  : m_data(new TileSetData())
76{
77    Init(path);
78
79    /* If count is valid, fix size; otherwise, fix count. */
80    if (count.x > 0 && count.y > 0)
81    {
82        size = m_data->m_image_size / count;
83    }
84    else
85    {
86        if (size.x <= 0 || size.y <= 0)
87            size = ivec2(32, 32);
88        count = ivec2(max(1, m_data->m_image_size.x / size.x),
89                      max(1, m_data->m_image_size.y / size.y));
90    }
91
92    for (int j = 0; j < count.y; ++j)
93    for (int i = 0; i < count.x; ++i)
94    {
95        AddTile(ibox2(size * ivec2(i, j),
96                      size * ivec2(i + 1, j + 1)));
97    }
98
99    m_drawgroup = DRAWGROUP_BEFORE;
100}
101
102void TileSet::Init(char const *path)
103{
104    m_data->m_name = String("<tileset> ") + path;
105
106    m_data->m_texture = 0;
107    m_data->m_image = Image::Create(path);
108    m_data->m_image_size = m_data->m_image->GetSize();
109    m_data->m_texture_size = ivec2(PotUp(m_data->m_image_size.x),
110                                   PotUp(m_data->m_image_size.y));
111}
112
113int TileSet::AddTile(ibox2 rect)
114{
115    m_data->m_tiles.Push(rect,
116                         box2((vec2)rect.A / (vec2)m_data->m_texture_size,
117                              (vec2)rect.B / (vec2)m_data->m_texture_size));
118    return m_data->m_tiles.Count() - 1;
119}
120
121TileSet::~TileSet()
122{
123    delete m_data;
124}
125
126void TileSet::TickDraw(float seconds)
127{
128    Entity::TickDraw(seconds);
129
130    if (IsDestroying())
131    {
132        if (m_data->m_image)
133        {
134            Image::Destroy(m_data->m_image);
135            m_data->m_image = nullptr;
136        }
137        else
138        {
139            delete m_data->m_texture;
140            m_data->m_texture = nullptr;
141        }
142    }
143    else if (m_data->m_image)
144    {
145        int planes;
146        PixelFormat format = m_data->m_image->GetFormat();
147
148        switch (format)
149        {
150        case PixelFormat::RGB_8:
151            planes = 3;
152            break;
153        case PixelFormat::RGBA_8:
154        case PixelFormat::ARGB_8:
155        case PixelFormat::ABGR_8:
156        default:
157            planes = 4;
158            break;
159        }
160
161        int w = m_data->m_texture_size.x;
162        int h = m_data->m_texture_size.y;
163
164        uint8_t *pixels = m_data->m_image->GetData();
165        if (w != m_data->m_image_size.x || h != m_data->m_image_size.y)
166        {
167            uint8_t *tmp = new uint8_t[planes * w * h];
168            for (int line = 0; line < m_data->m_image_size.y; line++)
169                memcpy(tmp + planes * w * line,
170                       pixels + planes * m_data->m_image_size.x * line,
171                       planes * m_data->m_image_size.x);
172            pixels = tmp;
173        }
174
175        m_data->m_texture = new Texture(ivec2(w, h), format);
176        m_data->m_texture->SetData(pixels);
177
178        if (pixels != m_data->m_image->GetData())
179            delete[] pixels;
180        Image::Destroy(m_data->m_image);
181        m_data->m_image = nullptr;
182    }
183}
184
185char const *TileSet::GetName()
186{
187    return m_data->m_name.C();
188}
189
190int TileSet::GetTileCount() const
191{
192    return m_data->m_tiles.Count();
193}
194
195ivec2 TileSet::GetTileSize(int tileid) const
196{
197    (void)tileid;
198
199    ibox2 const &box = m_data->m_tiles[tileid].m1;
200    return box.B - box.A;
201}
202
203ivec2 TileSet::GetTextureSize() const
204{
205    return m_data->m_texture_size;
206}
207
208ShaderTexture TileSet::GetTexture() const
209{
210    return m_data->m_texture->GetTexture();
211}
212
213void TileSet::Bind()
214{
215    if (!m_data->m_image && m_data->m_texture)
216        m_data->m_texture->Bind();
217}
218
219void TileSet::Unbind()
220{
221    ;
222}
223
224void TileSet::BlitTile(uint32_t id, vec3 pos, int o, vec2 scale,
225                       float *vertex, float *texture)
226{
227    ibox2 pixels = m_data->m_tiles[id].m1;
228    box2 texels = m_data->m_tiles[id].m2;
229    float dtx = texels.B.x - texels.A.x;
230    float dty = texels.B.y - texels.A.y;
231    float tx = texels.A.x;
232    float ty = texels.A.y;
233
234    int dx = (pixels.B.x - pixels.A.x) * scale.x;
235    int dy = o ? 0 : (pixels.B.y - pixels.A.y) * scale.y;
236    int dz = o ? (pixels.B.y - pixels.A.y) * scale.y : 0;
237
238    /* If scaling is negative, switch triangle winding */
239    if (scale.x * scale.y < 0.0f)
240    {
241        pos.x += dx;
242        dx = -dx;
243
244        tx += dtx;
245        dtx = -dtx;
246    }
247
248#if 1
249    /* HACK: tweak UV values */
250    tx += (1.f / 128.f) * dtx;
251    ty += (1.f / 128.f) * dty;
252    dtx *= 126.f / 128.f;
253    dty *= 126.f / 128.f;
254#endif
255
256    if (!m_data->m_image && m_data->m_texture)
257    {
258        float tmp[10];
259
260        *vertex++ = pos.x + dx;
261        *vertex++ = pos.y + dy;
262        *vertex++ = pos.z + dz;
263        *texture++ = tx + dtx;
264        *texture++ = ty;
265
266        *vertex++ = tmp[0] = pos.x;
267        *vertex++ = tmp[1] = pos.y + dy;
268        *vertex++ = tmp[2] = pos.z + dz;
269        *texture++ = tmp[3] = tx;
270        *texture++ = tmp[4] = ty;
271
272        *vertex++ = tmp[5] = pos.x + dx;
273        *vertex++ = tmp[6] = pos.y;
274        *vertex++ = tmp[7] = pos.z;
275        *texture++ = tmp[8] = tx + dtx;
276        *texture++ = tmp[9] = ty + dty;
277
278        *vertex++ = tmp[5];
279        *vertex++ = tmp[6];
280        *vertex++ = tmp[7];
281        *texture++ = tmp[8];
282        *texture++ = tmp[9];
283
284        *vertex++ = tmp[0];
285        *vertex++ = tmp[1];
286        *vertex++ = tmp[2];
287        *texture++ = tmp[3];
288        *texture++ = tmp[4];
289
290        *vertex++ = pos.x;
291        *vertex++ = pos.y;
292        *vertex++ = pos.z;
293        *texture++ = tx;
294        *texture++ = ty + dty;
295    }
296    else
297    {
298        memset(vertex, 0, 3 * sizeof(float));
299        memset(texture, 0, 2 * sizeof(float));
300    }
301}
302
303} /* namespace lol */
304
Note: See TracBrowser for help on using the repository browser.