source: trunk/src/tileset.cpp @ 673

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

Put the OpenGL header handling in <lolgl.h> for more convenience.

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