source: trunk/src/tileset.cpp @ 1687

Last change on this file since 1687 was 1513, checked in by sam, 9 years ago

core: replace usage of sin() or std::sin() with lol::sin() where appropriate.

  • Property svn:keywords set to Id
File size: 7.6 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2012 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://sam.zoy.org/projects/COPYING.WTFPL 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    char *name, *path;
53    int *tiles, ntiles;
54    ivec2 size, isize, count;
55    vec2 scale;
56    float tx, ty;
57
58    Image *img;
59#if defined USE_D3D9
60    IDirect3DTexture9 *m_tex;
61#elif defined _XBOX
62    D3DTexture *m_tex;
63#else
64    GLuint m_tex;
65#endif
66};
67
68/*
69 * Public TileSet class
70 */
71
72TileSet::TileSet(char const *path, ivec2 size, ivec2 count)
73  : data(new TileSetData())
74{
75    data->name = (char *)malloc(10 + strlen(path) + 1);
76    data->path = data->name + 10;
77    sprintf(data->name, "<tileset> %s", path);
78
79    data->tiles = NULL;
80    data->m_tex = 0;
81    data->img = new Image(path);
82    data->isize = data->img->GetSize();
83
84    if (count.x > 0 && count.y > 0)
85    {
86        data->count = count;
87        data->size = data->isize / count;
88    }
89    else
90    {
91        if (size.x <= 0 || size.y <= 0)
92            size = ivec2(32, 32);
93        data->count.x = data->isize.x > size.x ? data->isize.x / size.x : 1;
94        data->count.y = data->isize.y > size.y ? data->isize.y / size.y : 1;
95        data->size = size;
96    }
97
98    data->tx = (float)data->size.x / PotUp(data->isize.x);
99    data->ty = (float)data->size.y / PotUp(data->isize.y);
100
101    data->ntiles = data->count.x * data->count.y;
102
103    m_drawgroup = DRAWGROUP_BEFORE;
104}
105
106TileSet::~TileSet()
107{
108    free(data->tiles);
109    free(data->name);
110    delete data;
111}
112
113void TileSet::TickDraw(float seconds)
114{
115    Entity::TickDraw(seconds);
116
117    if (IsDestroying())
118    {
119        if (data->img)
120            delete data->img;
121        else
122#if defined USE_D3D9 || defined _XBOX
123            /* FIXME: is it really the correct call? */
124            data->m_tex->Release();
125#else
126            glDeleteTextures(1, &data->m_tex);
127#endif
128    }
129    else if (data->img)
130    {
131#if defined USE_D3D9 || defined _XBOX
132        D3DFORMAT format;
133#else
134        GLuint format;
135#endif
136        int planes;
137
138        switch (data->img->GetFormat())
139        {
140        case Image::FORMAT_RGB:
141#if defined USE_D3D9
142           format = D3DFMT_R8G8B8;
143#elif defined _XBOX
144           format = D3DFMT_LIN_A8R8G8B8; /* FIXME */
145#else
146           format = GL_RGB;
147#endif
148           planes = 3;
149           break;
150        case Image::FORMAT_RGBA:
151        default:
152#if defined USE_D3D9
153           format = D3DFMT_A8R8G8B8;
154#elif defined _XBOX
155            /* By default the X360 will swizzle the texture. Ask for linear. */
156           format = D3DFMT_LIN_A8R8G8B8;
157#else
158           format = GL_RGBA;
159#endif
160           planes = 4;
161           break;
162        }
163
164        int w = PotUp(data->isize.x);
165        int h = PotUp(data->isize.y);
166
167        uint8_t *pixels = (uint8_t *)data->img->GetData();
168        if (w != data->isize.x || h != data->isize.y)
169        {
170            uint8_t *tmp = (uint8_t *)malloc(planes * w * h);
171            for (int line = 0; line < data->isize.y; line++)
172                memcpy(tmp + planes * w * line,
173                       pixels + planes * data->isize.x * line,
174                       planes * data->isize.x);
175            pixels = tmp;
176        }
177
178#if defined USE_D3D9 || defined _XBOX
179        D3DLOCKED_RECT rect;
180        HRESULT hr;
181#   if defined USE_D3D9
182        hr = g_d3ddevice->CreateTexture(w, h, 1, D3DUSAGE_DYNAMIC, format,
183                                        D3DPOOL_DEFAULT, &data->m_tex, NULL);
184#   elif defined _XBOX
185        hr = g_d3ddevice->CreateTexture(w, h, 1, D3DUSAGE_WRITEONLY, format,
186                                        D3DPOOL_DEFAULT, &data->m_tex, NULL);
187#   endif
188        if (FAILED(hr))
189            Abort();
190#   if defined USE_D3D9
191        hr = data->m_tex->LockRect(0, &rect, NULL, D3DLOCK_DISCARD);
192#   else
193        hr = data->m_tex->LockRect(0, &rect, NULL, 0);
194#   endif
195        if (FAILED(hr))
196            Abort();
197        for (int j = 0; j < h; j++)
198            memcpy((uint8_t *)rect.pBits + j * rect.Pitch, pixels + w * j * 4, w * 4);
199        hr = data->m_tex->UnlockRect(0);
200        if (FAILED(hr))
201            Abort();
202#else
203        glGenTextures(1, &data->m_tex);
204        glEnable(GL_TEXTURE_2D);
205        glBindTexture(GL_TEXTURE_2D, data->m_tex);
206
207        glTexImage2D(GL_TEXTURE_2D, 0, format, w, h, 0,
208                     format, GL_UNSIGNED_BYTE, pixels);
209
210        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
211        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
212#endif
213
214        if (pixels != data->img->GetData())
215            free(pixels);
216        delete data->img;
217        data->img = NULL;
218    }
219}
220
221char const *TileSet::GetName()
222{
223    return data->name;
224}
225
226ivec2 TileSet::GetCount() const
227{
228    return data->count;
229}
230
231ivec2 TileSet::GetSize(int tileid) const
232{
233    return data->size;
234}
235
236void TileSet::Bind()
237{
238    if (!data->img && data->m_tex)
239    {
240#if defined USE_D3D9 || defined _XBOX
241        HRESULT hr = g_d3ddevice->SetTexture(0, data->m_tex);
242        if (FAILED(hr))
243            Abort();
244#else
245        glActiveTexture(GL_TEXTURE0);
246        glBindTexture(GL_TEXTURE_2D, data->m_tex);
247#endif
248    }
249}
250
251void TileSet::Unbind()
252{
253    if (!data->img && data->m_tex)
254    {
255#if defined USE_D3D9 || defined _XBOX
256        HRESULT hr = g_d3ddevice->SetTexture(0, NULL);
257        if (FAILED(hr))
258            Abort();
259#else
260        glActiveTexture(GL_TEXTURE0);
261        glBindTexture(GL_TEXTURE_2D, 0);
262#endif
263    }
264}
265
266void TileSet::BlitTile(uint32_t id, vec3 pos, int o, vec2 scale,
267                       float *vertex, float *texture)
268{
269    float tx = data->tx * ((id & 0xffff) % data->count.x);
270    float ty = data->ty * ((id & 0xffff) / data->count.x);
271
272    int dx = data->size.x * scale.x;
273    int dy = o ? 0 : data->size.y * scale.y;
274    int dz = o ? data->size.y * scale.y : 0;
275
276    if (!data->img && data->m_tex)
277    {
278        float tmp[10];
279
280        *vertex++ = pos.x + dx;
281        *vertex++ = pos.y + dy;
282        *vertex++ = pos.z + dz;
283        *texture++ = tx + data->tx;
284        *texture++ = ty;
285
286        *vertex++ = tmp[0] = pos.x;
287        *vertex++ = tmp[1] = pos.y + dy;
288        *vertex++ = tmp[2] = pos.z + dz;
289        *texture++ = tmp[3] = tx;
290        *texture++ = tmp[4] = ty;
291
292        *vertex++ = tmp[5] = pos.x + dx;
293        *vertex++ = tmp[6] = pos.y;
294        *vertex++ = tmp[7] = pos.z;
295        *texture++ = tmp[8] = tx + data->tx;
296        *texture++ = tmp[9] = ty + data->ty;
297
298        *vertex++ = tmp[5];
299        *vertex++ = tmp[6];
300        *vertex++ = tmp[7];
301        *texture++ = tmp[8];
302        *texture++ = tmp[9];
303
304        *vertex++ = tmp[0];
305        *vertex++ = tmp[1];
306        *vertex++ = tmp[2];
307        *texture++ = tmp[3];
308        *texture++ = tmp[4];
309
310        *vertex++ = pos.x;
311        *vertex++ = pos.y;
312        *vertex++ = pos.z;
313        *texture++ = tx;
314        *texture++ = ty + data->ty;
315    }
316    else
317    {
318        memset(vertex, 0, 3 * sizeof(float));
319        memset(texture, 0, 2 * sizeof(float));
320    }
321}
322
323} /* namespace lol */
324
Note: See TracBrowser for help on using the repository browser.