source: trunk/src/tileset.cpp @ 866

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

core: more vec?i -> ?veci renames.

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