source: trunk/src/tileset.cpp @ 689

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

Use the new Image class instead of SDL_image in the TileSet class.

  • Property svn:keywords set to Id
File size: 5.3 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2011 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#ifdef WIN32
21#   define WIN32_LEAN_AND_MEAN
22#   include <windows.h>
23#endif
24
25#include "core.h"
26#include "lolgl.h"
27
28namespace lol
29{
30
31/*
32 * TileSet implementation class
33 */
34
35class TileSetData
36{
37    friend class TileSet;
38
39private:
40    char *name, *path;
41    int *tiles, ntiles;
42    vec2i size, isize, count;
43    float dilate, tx, ty;
44
45    Image *img;
46    GLuint texture;
47};
48
49/*
50 * Public TileSet class
51 */
52
53TileSet::TileSet(char const *path, vec2i size, vec2i count, float dilate)
54  : data(new TileSetData())
55{
56    data->name = (char *)malloc(10 + strlen(path) + 1);
57    data->path = data->name + 10;
58    sprintf(data->name, "<tileset> %s", path);
59
60    data->tiles = NULL;
61    data->texture = 0;
62    data->img = new Image(path);
63    data->isize = data->img->GetSize();
64
65    if (count.i > 0 && count.j > 0)
66    {
67        data->count = count;
68        data->size = data->isize / count;
69    }
70    else
71    {
72        if (size.x <= 0 || size.y <= 0)
73            size = 32;
74        data->count.i = data->isize.x > size.i ? data->isize.x / size.i : 1;
75        data->count.j = data->isize.y > size.j ? data->isize.y / size.j : 1;
76        data->size = size;
77    }
78
79    data->tx = (float)data->size.x / PotUp(data->isize.x);
80    data->ty = (float)data->size.y / PotUp(data->isize.y);
81
82    data->dilate = dilate;
83    data->ntiles = data->count.i * data->count.j;
84
85    drawgroup = DRAWGROUP_BEFORE;
86}
87
88TileSet::~TileSet()
89{
90    free(data->tiles);
91    free(data->name);
92    delete data;
93}
94
95void TileSet::TickDraw(float deltams)
96{
97    Entity::TickDraw(deltams);
98
99    if (IsDestroying())
100    {
101        if (data->img)
102            delete data->img;
103        else
104            glDeleteTextures(1, &data->texture);
105    }
106    else if (data->img)
107    {
108        GLuint format;
109        int planes;
110
111        switch (data->img->GetFormat())
112        {
113        case Image::FORMAT_RGB:
114           format = GL_RGB;
115           planes = 3;
116           break;
117        case Image::FORMAT_RGBA:
118        default:
119           format = GL_RGBA;
120           planes = 4;
121           break;
122        }
123
124        int w = PotUp(data->isize.x);
125        int h = PotUp(data->isize.y);
126
127        uint8_t *pixels = (uint8_t *)data->img->GetData();
128        if (w != data->isize.x || h != data->isize.y)
129        {
130            uint8_t *tmp = (uint8_t *)malloc(planes * w * h);
131            for (int line = 0; line < data->isize.y; line++)
132                memcpy(tmp + planes * w * line,
133                       pixels + planes * data->isize.x * line,
134                       planes * data->isize.x);
135            pixels = tmp;
136        }
137
138        glGenTextures(1, &data->texture);
139        glBindTexture(GL_TEXTURE_2D, data->texture);
140
141        glTexImage2D(GL_TEXTURE_2D, 0, planes, w, h, 0,
142                     format, GL_UNSIGNED_BYTE, pixels);
143
144        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
145        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
146
147        if (pixels != data->img->GetData())
148            free(pixels);
149        delete data->img;
150        data->img = NULL;
151    }
152}
153
154char const *TileSet::GetName()
155{
156    return data->name;
157}
158
159vec2i TileSet::GetSize() const
160{
161    return data->size;
162}
163
164vec2i TileSet::GetCount() const
165{
166    return data->count;
167}
168
169void TileSet::Bind()
170{
171    if (!data->img && data->texture)
172        glBindTexture(GL_TEXTURE_2D, data->texture);
173}
174
175void TileSet::BlitTile(uint32_t id, int x, int y, int z, int o,
176                       float *vertex, float *texture)
177{
178    float tx = data->tx * ((id & 0xffff) % data->count.i);
179    float ty = data->ty * ((id & 0xffff) / data->count.i);
180    float dilate = data->dilate;
181
182    int dx = data->size.x;
183    int dy = o ? 0 : data->size.y;
184    int dz = o ? data->size.y : 0;
185
186    if (!data->img && data->texture)
187    {
188        float tmp[10];
189
190        *vertex++ = tmp[0] = x;
191        *vertex++ = tmp[1] = dilate * (y + dy);
192        *vertex++ = tmp[2] = dilate * (z + dz);
193        *texture++ = tmp[3] = tx;
194        *texture++ = tmp[4] = ty;
195
196        *vertex++ = x + dx;
197        *vertex++ = dilate * (y + dy);
198        *vertex++ = dilate * (z + dz);
199        *texture++ = tx + data->tx;
200        *texture++ = ty;
201
202        *vertex++ = tmp[5] = x + dx;
203        *vertex++ = tmp[6] = dilate * y;
204        *vertex++ = tmp[7] = dilate * z;
205        *texture++ = tmp[8] = tx + data->tx;
206        *texture++ = tmp[9] = ty + data->ty;
207
208        *vertex++ = tmp[0];
209        *vertex++ = tmp[1];
210        *vertex++ = tmp[2];
211        *texture++ = tmp[3];
212        *texture++ = tmp[4];
213
214        *vertex++ = tmp[5];
215        *vertex++ = tmp[6];
216        *vertex++ = tmp[7];
217        *texture++ = tmp[8];
218        *texture++ = tmp[9];
219
220        *vertex++ = x;
221        *vertex++ = dilate * y;
222        *vertex++ = dilate * z;
223        *texture++ = tx;
224        *texture++ = ty + data->ty;
225    }
226    else
227    {
228        memset(vertex, 0, 3 * sizeof(float));
229        memset(texture, 0, 2 * sizeof(float));
230    }
231}
232
233} /* namespace lol */
234
Note: See TracBrowser for help on using the repository browser.