source: trunk/src/tileset.cpp @ 1233

Last change on this file since 1233 was 1233, checked in by sam, 10 years ago

gpu: add an Unbind() method for textures for clean up. Unfortunately
the Scene code still breaks Direct3D rendering.

  • Property svn:keywords set to Id
File size: 7.2 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 <cmath>
18#include <cstring>
19
20#if defined WIN32 && !defined _XBOX
21#   define WIN32_LEAN_AND_MEAN
22#   include <windows.h>
23#   if defined USE_D3D9
24#       define FAR
25#       define NEAR
26#       include <d3d9.h>
27#   endif
28#endif
29
30#include "core.h"
31#include "lolgl.h"
32
33using namespace std;
34
35#if defined USE_D3D9
36extern IDirect3DDevice9 *g_d3ddevice;
37#elif defined _XBOX
38extern D3DDevice *g_d3ddevice;
39#endif
40
41namespace lol
42{
43
44/*
45 * TileSet implementation class
46 */
47
48class TileSetData
49{
50    friend class TileSet;
51
52private:
53    char *name, *path;
54    int *tiles, ntiles;
55    ivec2 size, isize, count;
56    vec2 scale;
57    float tx, ty;
58
59    Image *img;
60#if defined USE_D3D9
61    IDirect3DTexture9 *m_tex;
62#elif defined _XBOX
63    D3DTexture *m_tex;
64#else
65    GLuint m_tex;
66#endif
67};
68
69/*
70 * Public TileSet class
71 */
72
73TileSet::TileSet(char const *path, ivec2 size, ivec2 count)
74  : data(new TileSetData())
75{
76    data->name = (char *)malloc(10 + strlen(path) + 1);
77    data->path = data->name + 10;
78    sprintf(data->name, "<tileset> %s", path);
79
80    data->tiles = NULL;
81    data->m_tex = 0;
82    data->img = new Image(path);
83    data->isize = data->img->GetSize();
84
85    if (count.x > 0 && count.y > 0)
86    {
87        data->count = count;
88        data->size = data->isize / count;
89    }
90    else
91    {
92        if (size.x <= 0 || size.y <= 0)
93            size = ivec2(32, 32);
94        data->count.x = data->isize.x > size.x ? data->isize.x / size.x : 1;
95        data->count.y = data->isize.y > size.y ? data->isize.y / size.y : 1;
96        data->size = size;
97    }
98
99    data->tx = (float)data->size.x / PotUp(data->isize.x);
100    data->ty = (float)data->size.y / PotUp(data->isize.y);
101
102    data->ntiles = data->count.x * data->count.y;
103
104    m_drawgroup = DRAWGROUP_BEFORE;
105}
106
107TileSet::~TileSet()
108{
109    free(data->tiles);
110    free(data->name);
111    delete data;
112}
113
114void TileSet::TickDraw(float deltams)
115{
116    Entity::TickDraw(deltams);
117
118    if (IsDestroying())
119    {
120        if (data->img)
121            delete data->img;
122        else
123#if defined USE_D3D9 || defined _XBOX
124            /* FIXME: is it really the correct call? */
125            data->m_tex->Release();
126#else
127            glDeleteTextures(1, &data->m_tex);
128#endif
129    }
130    else if (data->img)
131    {
132#if defined USE_D3D9 || defined _XBOX
133        D3DFORMAT format;
134#else
135        GLuint format;
136#endif
137        int planes;
138
139        switch (data->img->GetFormat())
140        {
141        case Image::FORMAT_RGB:
142#if defined USE_D3D9
143           format = D3DFMT_R8G8B8;
144#elif defined _XBOX
145           format = D3DFMT_LIN_R8G8B8; /* FIXME */
146#else
147           format = GL_RGB;
148#endif
149           planes = 3;
150           break;
151        case Image::FORMAT_RGBA:
152        default:
153#if defined USE_D3D9
154           format = D3DFMT_A8R8G8B8;
155#elif defined _XBOX
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#   if defined USE_D3D9
181        g_d3ddevice->CreateTexture(w, h, 1, D3DUSAGE_DYNAMIC, format,
182                                   D3DPOOL_SYSTEMMEM, &data->m_tex, NULL);
183        data->m_tex->LockRect(0, &rect, NULL, D3DLOCK_DISCARD | D3DLOCK_NOOVERWRITE);
184#   elif defined _XBOX
185        /* By default the X360 will swizzle the texture. Ask for linear. */
186        g_d3ddevice->CreateTexture(w, h, 1, D3DUSAGE_WRITEONLY, format,
187                                   D3DPOOL_DEFAULT, &data->m_tex, NULL);
188        data->m_tex->LockRect(0, &rect, NULL, D3DLOCK_NOOVERWRITE);
189#   endif
190        for (int j = 0; j < h; j++)
191            memcpy((uint8_t *)rect.pBits + j * rect.Pitch, pixels + w * j * 4, w * 4);
192        data->m_tex->UnlockRect(0);
193#else
194        glGenTextures(1, &data->m_tex);
195        glEnable(GL_TEXTURE_2D);
196        glBindTexture(GL_TEXTURE_2D, data->m_tex);
197
198        glTexImage2D(GL_TEXTURE_2D, 0, format, w, h, 0,
199                     format, GL_UNSIGNED_BYTE, pixels);
200
201        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
202        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
203#endif
204
205        if (pixels != data->img->GetData())
206            free(pixels);
207        delete data->img;
208        data->img = NULL;
209    }
210}
211
212char const *TileSet::GetName()
213{
214    return data->name;
215}
216
217ivec2 TileSet::GetCount() const
218{
219    return data->count;
220}
221
222ivec2 TileSet::GetSize(int tileid) const
223{
224    return data->size;
225}
226
227void TileSet::Bind()
228{
229    if (!data->img && data->m_tex)
230    {
231#if defined USE_D3D9 || defined _XBOX
232        g_d3ddevice->SetTexture(0, data->m_tex);
233#else
234        glActiveTexture(GL_TEXTURE0);
235        glBindTexture(GL_TEXTURE_2D, data->m_tex);
236#endif
237    }
238}
239
240void TileSet::Unbind()
241{
242#if defined USE_D3D9 || defined _XBOX
243    g_d3ddevice->SetTexture(0, NULL);
244#else
245    glActiveTexture(GL_TEXTURE0);
246    glBindTexture(GL_TEXTURE_2D, 0);
247#endif
248}
249
250void TileSet::BlitTile(uint32_t id, vec3 pos, int o, vec2 scale,
251                       float *vertex, float *texture)
252{
253    float tx = data->tx * ((id & 0xffff) % data->count.x);
254    float ty = data->ty * ((id & 0xffff) / data->count.x);
255
256    int dx = data->size.x * scale.x;
257    int dy = o ? 0 : data->size.y * scale.y;
258    int dz = o ? data->size.y * scale.y : 0;
259
260    if (!data->img && data->m_tex)
261    {
262        float tmp[10];
263
264        *vertex++ = tmp[0] = pos.x;
265        *vertex++ = tmp[1] = pos.y + dy;
266        *vertex++ = tmp[2] = pos.z + dz;
267        *texture++ = tmp[3] = tx;
268        *texture++ = tmp[4] = ty;
269
270        *vertex++ = pos.x + dx;
271        *vertex++ = pos.y + dy;
272        *vertex++ = pos.z + dz;
273        *texture++ = tx + data->tx;
274        *texture++ = ty;
275
276        *vertex++ = tmp[5] = pos.x + dx;
277        *vertex++ = tmp[6] = pos.y;
278        *vertex++ = tmp[7] = pos.z;
279        *texture++ = tmp[8] = tx + data->tx;
280        *texture++ = tmp[9] = ty + data->ty;
281
282        *vertex++ = tmp[0];
283        *vertex++ = tmp[1];
284        *vertex++ = tmp[2];
285        *texture++ = tmp[3];
286        *texture++ = tmp[4];
287
288        *vertex++ = tmp[5];
289        *vertex++ = tmp[6];
290        *vertex++ = tmp[7];
291        *texture++ = tmp[8];
292        *texture++ = tmp[9];
293
294        *vertex++ = pos.x;
295        *vertex++ = pos.y;
296        *vertex++ = pos.z;
297        *texture++ = tx;
298        *texture++ = ty + data->ty;
299    }
300    else
301    {
302        memset(vertex, 0, 3 * sizeof(float));
303        memset(texture, 0, 2 * sizeof(float));
304    }
305}
306
307} /* namespace lol */
308
Note: See TracBrowser for help on using the repository browser.