source: trunk/src/tileset.cpp @ 704

Last change on this file since 704 was 704, checked in by sam, 11 years ago

Use glClientActiveTexture() wherever it appears to make sense.

  • Property svn:keywords set to Id
File size: 5.4 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        glEnable(GL_TEXTURE_2D);
140        glBindTexture(GL_TEXTURE_2D, data->texture);
141
142        glTexImage2D(GL_TEXTURE_2D, 0, format, w, h, 0,
143                     format, GL_UNSIGNED_BYTE, pixels);
144
145        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
146        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
147
148        if (pixels != data->img->GetData())
149            free(pixels);
150        delete data->img;
151        data->img = NULL;
152    }
153}
154
155char const *TileSet::GetName()
156{
157    return data->name;
158}
159
160vec2i TileSet::GetSize() const
161{
162    return data->size;
163}
164
165vec2i TileSet::GetCount() const
166{
167    return data->count;
168}
169
170void TileSet::Bind()
171{
172    if (!data->img && data->texture)
173    {
174        glClientActiveTexture(GL_TEXTURE0);
175        glBindTexture(GL_TEXTURE_2D, data->texture);
176    }
177}
178
179void TileSet::BlitTile(uint32_t id, int x, int y, int z, int o,
180                       float *vertex, float *texture)
181{
182    float tx = data->tx * ((id & 0xffff) % data->count.i);
183    float ty = data->ty * ((id & 0xffff) / data->count.i);
184    float dilate = data->dilate;
185
186    int dx = data->size.x;
187    int dy = o ? 0 : data->size.y;
188    int dz = o ? data->size.y : 0;
189
190    if (!data->img && data->texture)
191    {
192        float tmp[10];
193
194        *vertex++ = tmp[0] = x;
195        *vertex++ = tmp[1] = dilate * (y + dy);
196        *vertex++ = tmp[2] = dilate * (z + dz);
197        *texture++ = tmp[3] = tx;
198        *texture++ = tmp[4] = ty;
199
200        *vertex++ = x + dx;
201        *vertex++ = dilate * (y + dy);
202        *vertex++ = dilate * (z + dz);
203        *texture++ = tx + data->tx;
204        *texture++ = ty;
205
206        *vertex++ = tmp[5] = x + dx;
207        *vertex++ = tmp[6] = dilate * y;
208        *vertex++ = tmp[7] = dilate * z;
209        *texture++ = tmp[8] = tx + data->tx;
210        *texture++ = tmp[9] = ty + data->ty;
211
212        *vertex++ = tmp[0];
213        *vertex++ = tmp[1];
214        *vertex++ = tmp[2];
215        *texture++ = tmp[3];
216        *texture++ = tmp[4];
217
218        *vertex++ = tmp[5];
219        *vertex++ = tmp[6];
220        *vertex++ = tmp[7];
221        *texture++ = tmp[8];
222        *texture++ = tmp[9];
223
224        *vertex++ = x;
225        *vertex++ = dilate * y;
226        *vertex++ = dilate * z;
227        *texture++ = tx;
228        *texture++ = ty + data->ty;
229    }
230    else
231    {
232        memset(vertex, 0, 3 * sizeof(float));
233        memset(texture, 0, 2 * sizeof(float));
234    }
235}
236
237} /* namespace lol */
238
Note: See TracBrowser for help on using the repository browser.