source: trunk/src/tileset.cpp @ 664

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

Get rid of float3, float4, int3 etc. in favour of GLSL types.

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