source: trunk/src/tileset.cpp @ 680

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

Create the SdlApp class to avoid code duplication. Ensure USE_SDL is
tested wherever relevant.

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