source: trunk/src/tileset.cpp @ 2394

Last change on this file since 2394 was 2312, checked in by sam, 7 years ago

gpu: support 1-component (luminance) textures.

  • Property svn:keywords set to Id
File size: 5.5 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    char *name, *path;
53    int *tiles, ntiles;
54    ivec2 size, isize, count;
55    vec2 scale;
56    float tx, ty;
57
58    Image *img;
59    Texture *m_texture;
60};
61
62/*
63 * Public TileSet class
64 */
65
66TileSet::TileSet(char const *path, ivec2 size, ivec2 count)
67  : data(new TileSetData())
68{
69    data->name = (char *)malloc(10 + strlen(path) + 1);
70    data->path = data->name + 10;
71    sprintf(data->name, "<tileset> %s", path);
72
73    data->tiles = NULL;
74    data->m_texture = 0;
75    data->img = new Image(path);
76    data->isize = data->img->GetSize();
77
78    if (count.x > 0 && count.y > 0)
79    {
80        data->count = count;
81        data->size = data->isize / count;
82    }
83    else
84    {
85        if (size.x <= 0 || size.y <= 0)
86            size = ivec2(32, 32);
87        data->count.x = data->isize.x > size.x ? data->isize.x / size.x : 1;
88        data->count.y = data->isize.y > size.y ? data->isize.y / size.y : 1;
89        data->size = size;
90    }
91
92    data->tx = (float)data->size.x / PotUp(data->isize.x);
93    data->ty = (float)data->size.y / PotUp(data->isize.y);
94
95    data->ntiles = data->count.x * data->count.y;
96
97    m_drawgroup = DRAWGROUP_BEFORE;
98}
99
100TileSet::~TileSet()
101{
102    free(data->tiles);
103    free(data->name);
104    delete data;
105}
106
107void TileSet::TickDraw(float seconds)
108{
109    Entity::TickDraw(seconds);
110
111    if (IsDestroying())
112    {
113        if (data->img)
114            delete data->img;
115        else
116            delete data->m_texture;
117    }
118    else if (data->img)
119    {
120        int planes;
121        PixelFormat format = PixelFormat::Unknown;
122
123        switch (data->img->GetFormat())
124        {
125        case Image::FORMAT_RGB:
126            format = PixelFormat::RGB_8;
127            planes = 3;
128            break;
129        case Image::FORMAT_RGBA:
130        default:
131            format = PixelFormat::ARGB_8;
132            planes = 4;
133            break;
134        }
135
136        int w = PotUp(data->isize.x);
137        int h = PotUp(data->isize.y);
138
139        uint8_t *pixels = (uint8_t *)data->img->GetData();
140        if (w != data->isize.x || h != data->isize.y)
141        {
142            uint8_t *tmp = (uint8_t *)malloc(planes * w * h);
143            for (int line = 0; line < data->isize.y; line++)
144                memcpy(tmp + planes * w * line,
145                       pixels + planes * data->isize.x * line,
146                       planes * data->isize.x);
147            pixels = tmp;
148        }
149
150        data->m_texture = new Texture(ivec2(w, h), format);
151        data->m_texture->SetData(pixels);
152
153        if (pixels != data->img->GetData())
154            free(pixels);
155        delete data->img;
156        data->img = NULL;
157    }
158}
159
160char const *TileSet::GetName()
161{
162    return data->name;
163}
164
165ivec2 TileSet::GetCount() const
166{
167    return data->count;
168}
169
170ivec2 TileSet::GetSize(int tileid) const
171{
172    (void)tileid;
173
174    return data->size;
175}
176
177void TileSet::Bind()
178{
179    if (!data->img && data->m_texture)
180        data->m_texture->Bind();
181}
182
183void TileSet::Unbind()
184{
185    ;
186}
187
188void TileSet::BlitTile(uint32_t id, vec3 pos, int o, vec2 scale,
189                       float *vertex, float *texture)
190{
191    float dtx = data->tx;
192    float dty = data->ty;
193    float tx = dtx * ((id & 0xffff) % data->count.x);
194    float ty = dty * ((id & 0xffff) / data->count.x);
195
196    int dx = data->size.x * scale.x;
197    int dy = o ? 0 : data->size.y * scale.y;
198    int dz = o ? data->size.y * scale.y : 0;
199
200    /* If scaling is negative, switch triangle winding */
201    if (scale.x * scale.y < 0.0f)
202    {
203        pos.x += dx;
204        dx = -dx;
205
206        tx += dtx;
207        dtx = -dtx;
208    }
209
210    if (!data->img && data->m_texture)
211    {
212        float tmp[10];
213
214        *vertex++ = pos.x + dx;
215        *vertex++ = pos.y + dy;
216        *vertex++ = pos.z + dz;
217        *texture++ = tx + dtx;
218        *texture++ = ty;
219
220        *vertex++ = tmp[0] = pos.x;
221        *vertex++ = tmp[1] = pos.y + dy;
222        *vertex++ = tmp[2] = pos.z + dz;
223        *texture++ = tmp[3] = tx;
224        *texture++ = tmp[4] = ty;
225
226        *vertex++ = tmp[5] = pos.x + dx;
227        *vertex++ = tmp[6] = pos.y;
228        *vertex++ = tmp[7] = pos.z;
229        *texture++ = tmp[8] = tx + dtx;
230        *texture++ = tmp[9] = ty + dty;
231
232        *vertex++ = tmp[5];
233        *vertex++ = tmp[6];
234        *vertex++ = tmp[7];
235        *texture++ = tmp[8];
236        *texture++ = tmp[9];
237
238        *vertex++ = tmp[0];
239        *vertex++ = tmp[1];
240        *vertex++ = tmp[2];
241        *texture++ = tmp[3];
242        *texture++ = tmp[4];
243
244        *vertex++ = pos.x;
245        *vertex++ = pos.y;
246        *vertex++ = pos.z;
247        *texture++ = tx;
248        *texture++ = ty + dty;
249    }
250    else
251    {
252        memset(vertex, 0, 3 * sizeof(float));
253        memset(texture, 0, 2 * sizeof(float));
254    }
255}
256
257} /* namespace lol */
258
Note: See TracBrowser for help on using the repository browser.