source: trunk/src/tileset.cpp @ 1241

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

gpu: fix a great lot of Direct3D problems, spotted using PIX.

  • 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 <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_A8R8G8B8; /* 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            /* By default the X360 will swizzle the texture. Ask for linear. */
157           format = D3DFMT_LIN_A8R8G8B8;
158#else
159           format = GL_RGBA;
160#endif
161           planes = 4;
162           break;
163        }
164
165        int w = PotUp(data->isize.x);
166        int h = PotUp(data->isize.y);
167
168        uint8_t *pixels = (uint8_t *)data->img->GetData();
169        if (w != data->isize.x || h != data->isize.y)
170        {
171            uint8_t *tmp = (uint8_t *)malloc(planes * w * h);
172            for (int line = 0; line < data->isize.y; line++)
173                memcpy(tmp + planes * w * line,
174                       pixels + planes * data->isize.x * line,
175                       planes * data->isize.x);
176            pixels = tmp;
177        }
178
179#if defined USE_D3D9 || defined _XBOX
180        D3DLOCKED_RECT rect;
181        HRESULT hr;
182#   if defined USE_D3D9
183        hr = g_d3ddevice->CreateTexture(w, h, 1, D3DUSAGE_DYNAMIC, format,
184                                        D3DPOOL_DEFAULT, &data->m_tex, NULL);
185#   elif defined _XBOX
186        hr = g_d3ddevice->CreateTexture(w, h, 1, D3DUSAGE_WRITEONLY, format,
187                                        D3DPOOL_DEFAULT, &data->m_tex, NULL);
188#   endif
189        if (FAILED(hr))
190            Abort();
191#   if defined USE_D3D9
192        hr = data->m_tex->LockRect(0, &rect, NULL, D3DLOCK_DISCARD);
193#   else
194        hr = data->m_tex->LockRect(0, &rect, NULL, 0);
195#   endif
196        if (FAILED(hr))
197            Abort();
198        for (int j = 0; j < h; j++)
199            memcpy((uint8_t *)rect.pBits + j * rect.Pitch, pixels + w * j * 4, w * 4);
200        hr = data->m_tex->UnlockRect(0);
201        if (FAILED(hr))
202            Abort();
203#else
204        glGenTextures(1, &data->m_tex);
205        glEnable(GL_TEXTURE_2D);
206        glBindTexture(GL_TEXTURE_2D, data->m_tex);
207
208        glTexImage2D(GL_TEXTURE_2D, 0, format, w, h, 0,
209                     format, GL_UNSIGNED_BYTE, pixels);
210
211        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
212        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
213#endif
214
215        if (pixels != data->img->GetData())
216            free(pixels);
217        delete data->img;
218        data->img = NULL;
219    }
220}
221
222char const *TileSet::GetName()
223{
224    return data->name;
225}
226
227ivec2 TileSet::GetCount() const
228{
229    return data->count;
230}
231
232ivec2 TileSet::GetSize(int tileid) const
233{
234    return data->size;
235}
236
237void TileSet::Bind()
238{
239    if (!data->img && data->m_tex)
240    {
241#if defined USE_D3D9 || defined _XBOX
242        HRESULT hr = g_d3ddevice->SetTexture(0, data->m_tex);
243        if (FAILED(hr))
244            Abort();
245#else
246        glActiveTexture(GL_TEXTURE0);
247        glBindTexture(GL_TEXTURE_2D, data->m_tex);
248#endif
249    }
250}
251
252void TileSet::Unbind()
253{
254    if (!data->img && data->m_tex)
255    {
256#if defined USE_D3D9 || defined _XBOX
257        HRESULT hr = g_d3ddevice->SetTexture(0, NULL);
258        if (FAILED(hr))
259            Abort();
260#else
261        glActiveTexture(GL_TEXTURE0);
262        glBindTexture(GL_TEXTURE_2D, 0);
263#endif
264    }
265}
266
267void TileSet::BlitTile(uint32_t id, vec3 pos, int o, vec2 scale,
268                       float *vertex, float *texture)
269{
270    float tx = data->tx * ((id & 0xffff) % data->count.x);
271    float ty = data->ty * ((id & 0xffff) / data->count.x);
272
273    int dx = data->size.x * scale.x;
274    int dy = o ? 0 : data->size.y * scale.y;
275    int dz = o ? data->size.y * scale.y : 0;
276
277    if (!data->img && data->m_tex)
278    {
279        float tmp[10];
280
281        *vertex++ = tmp[0] = pos.x;
282        *vertex++ = tmp[1] = pos.y + dy;
283        *vertex++ = tmp[2] = pos.z + dz;
284        *texture++ = tmp[3] = tx;
285        *texture++ = tmp[4] = ty;
286
287        *vertex++ = pos.x + dx;
288        *vertex++ = pos.y + dy;
289        *vertex++ = pos.z + dz;
290        *texture++ = tx + data->tx;
291        *texture++ = ty;
292
293        *vertex++ = tmp[5] = pos.x + dx;
294        *vertex++ = tmp[6] = pos.y;
295        *vertex++ = tmp[7] = pos.z;
296        *texture++ = tmp[8] = tx + data->tx;
297        *texture++ = tmp[9] = ty + data->ty;
298
299        *vertex++ = tmp[0];
300        *vertex++ = tmp[1];
301        *vertex++ = tmp[2];
302        *texture++ = tmp[3];
303        *texture++ = tmp[4];
304
305        *vertex++ = tmp[5];
306        *vertex++ = tmp[6];
307        *vertex++ = tmp[7];
308        *texture++ = tmp[8];
309        *texture++ = tmp[9];
310
311        *vertex++ = pos.x;
312        *vertex++ = pos.y;
313        *vertex++ = pos.z;
314        *texture++ = tx;
315        *texture++ = ty + data->ty;
316    }
317    else
318    {
319        memset(vertex, 0, 3 * sizeof(float));
320        memset(texture, 0, 2 * sizeof(float));
321    }
322}
323
324} /* namespace lol */
325
Note: See TracBrowser for help on using the repository browser.