source: trunk/src/tileset.cpp @ 686

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

Put everything in the "lol" namespace. Better late than never.

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