source: trunk/src/tileset.cpp @ 1198

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

core: allow each blitted tile to be stretched differently.

  • Property svn:keywords set to Id
File size: 6.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#endif
24
25#include "core.h"
26#include "lolgl.h"
27
28using namespace std;
29
30namespace lol
31{
32
33/*
34 * TileSet implementation class
35 */
36
37class TileSetData
38{
39    friend class TileSet;
40
41private:
42    char *name, *path;
43    int *tiles, ntiles;
44    ivec2 size, isize, count;
45    vec2 scale;
46    float tx, ty;
47
48    Image *img;
49#if defined _XBOX
50#   define STR0(x) #x
51#   define STR(x) STR0(x)
52#   pragma message(__FILE__ "(" STR(__LINE__) "): warning: TileSet not implemented")
53#   undef STR
54#   undef STR0
55#else
56    GLuint texture;
57#endif
58};
59
60/*
61 * Public TileSet class
62 */
63
64TileSet::TileSet(char const *path, ivec2 size, ivec2 count)
65  : data(new TileSetData())
66{
67    data->name = (char *)malloc(10 + strlen(path) + 1);
68    data->path = data->name + 10;
69    sprintf(data->name, "<tileset> %s", path);
70
71    data->tiles = NULL;
72#if defined _XBOX
73#   define STR0(x) #x
74#   define STR(x) STR0(x)
75#   pragma message(__FILE__ "(" STR(__LINE__) "): warning: TileSet::TileSet() not implemented")
76#   undef STR
77#   undef STR0
78#else
79    data->texture = 0;
80#endif
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 deltams)
114{
115    Entity::TickDraw(deltams);
116
117    if (IsDestroying())
118    {
119        if (data->img)
120            delete data->img;
121#if defined _XBOX
122#   define STR0(x) #x
123#   define STR(x) STR0(x)
124#   pragma message(__FILE__ "(" STR(__LINE__) "): warning: TileSet::TickDraw() not implemented")
125#   undef STR
126#   undef STR0
127#else
128        else
129            glDeleteTextures(1, &data->texture);
130#endif
131    }
132    else if (data->img)
133    {
134#if defined _XBOX
135#   define STR0(x) #x
136#   define STR(x) STR0(x)
137#   pragma message(__FILE__ "(" STR(__LINE__) "): warning: TileSet::TickDraw() not implemented")
138#   undef STR
139#   undef STR0
140#else
141        GLuint format;
142        int planes;
143
144        switch (data->img->GetFormat())
145        {
146        case Image::FORMAT_RGB:
147           format = GL_RGB;
148           planes = 3;
149           break;
150        case Image::FORMAT_RGBA:
151        default:
152           format = GL_RGBA;
153           planes = 4;
154           break;
155        }
156
157        int w = PotUp(data->isize.x);
158        int h = PotUp(data->isize.y);
159
160        uint8_t *pixels = (uint8_t *)data->img->GetData();
161        if (w != data->isize.x || h != data->isize.y)
162        {
163            uint8_t *tmp = (uint8_t *)malloc(planes * w * h);
164            for (int line = 0; line < data->isize.y; line++)
165                memcpy(tmp + planes * w * line,
166                       pixels + planes * data->isize.x * line,
167                       planes * data->isize.x);
168            pixels = tmp;
169        }
170
171        glGenTextures(1, &data->texture);
172        glEnable(GL_TEXTURE_2D);
173        glBindTexture(GL_TEXTURE_2D, data->texture);
174
175        glTexImage2D(GL_TEXTURE_2D, 0, format, w, h, 0,
176                     format, GL_UNSIGNED_BYTE, pixels);
177
178        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
179        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
180
181        if (pixels != data->img->GetData())
182            free(pixels);
183#endif
184        delete data->img;
185        data->img = NULL;
186    }
187}
188
189char const *TileSet::GetName()
190{
191    return data->name;
192}
193
194ivec2 TileSet::GetCount() const
195{
196    return data->count;
197}
198
199ivec2 TileSet::GetSize(int tileid) const
200{
201    return data->size;
202}
203
204void TileSet::Bind()
205{
206#if defined _XBOX
207#   define STR0(x) #x
208#   define STR(x) STR0(x)
209#   pragma message(__FILE__ "(" STR(__LINE__) "): warning: TileSet::Bind() not implemented")
210#   undef STR
211#   undef STR0
212#else
213    if (!data->img && data->texture)
214    {
215        glActiveTexture(GL_TEXTURE0);
216        glBindTexture(GL_TEXTURE_2D, data->texture);
217    }
218#endif
219}
220
221void TileSet::BlitTile(uint32_t id, vec3 pos, int o, vec2 scale,
222                       float *vertex, float *texture)
223{
224    float tx = data->tx * ((id & 0xffff) % data->count.x);
225    float ty = data->ty * ((id & 0xffff) / data->count.x);
226
227    int dx = data->size.x * scale.x;
228    int dy = o ? 0 : data->size.y * scale.y;
229    int dz = o ? data->size.y * scale.y : 0;
230
231#if defined _XBOX
232#   define STR0(x) #x
233#   define STR(x) STR0(x)
234#   pragma message(__FILE__ "(" STR(__LINE__) "): warning: TileSet::TileSet() not implemented")
235#   undef STR
236#   undef STR0
237#else
238    if (!data->img && data->texture)
239    {
240        float tmp[10];
241
242        *vertex++ = tmp[0] = pos.x;
243        *vertex++ = tmp[1] = pos.y + dy;
244        *vertex++ = tmp[2] = pos.z + dz;
245        *texture++ = tmp[3] = tx;
246        *texture++ = tmp[4] = ty;
247
248        *vertex++ = pos.x + dx;
249        *vertex++ = pos.y + dy;
250        *vertex++ = pos.z + dz;
251        *texture++ = tx + data->tx;
252        *texture++ = ty;
253
254        *vertex++ = tmp[5] = pos.x + dx;
255        *vertex++ = tmp[6] = pos.y;
256        *vertex++ = tmp[7] = pos.z;
257        *texture++ = tmp[8] = tx + data->tx;
258        *texture++ = tmp[9] = ty + data->ty;
259
260        *vertex++ = tmp[0];
261        *vertex++ = tmp[1];
262        *vertex++ = tmp[2];
263        *texture++ = tmp[3];
264        *texture++ = tmp[4];
265
266        *vertex++ = tmp[5];
267        *vertex++ = tmp[6];
268        *vertex++ = tmp[7];
269        *texture++ = tmp[8];
270        *texture++ = tmp[9];
271
272        *vertex++ = pos.x;
273        *vertex++ = pos.y;
274        *vertex++ = pos.z;
275        *texture++ = tx;
276        *texture++ = ty + data->ty;
277    }
278    else
279    {
280        memset(vertex, 0, 3 * sizeof(float));
281        memset(texture, 0, 2 * sizeof(float));
282    }
283#endif
284}
285
286} /* namespace lol */
287
Note: See TracBrowser for help on using the repository browser.