source: trunk/src/tileset.cpp @ 335

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

Fix a crash caused by potential code/data discrepancies.

  • Property svn:keywords set to Id
File size: 4.1 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;
44    int *tiles;
45    int w, h, nw, nh, ntiles;
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, int w, int h, float dilate)
57  : data(new TileSetData())
58{
59    data->name = strdup(path);
60    data->tiles = NULL;
61    data->img = NULL;
62    data->texture = 0;
63
64    for (char const *name = path; *name; name++)
65        if ((data->img = IMG_Load(name)))
66            break;
67
68    if (!data->img)
69    {
70#if !FINAL_RELEASE
71        fprintf(stderr, "ERROR: could not load %s\n", path);
72#endif
73        SDL_Quit();
74        exit(1);
75    }
76
77    if (w <= 0)
78        w = 32;
79    if (h <= 0)
80        h = 32;
81
82    data->w = w;
83    data->h = h;
84    data->dilate = dilate;
85    data->nw = data->img->w > w ? data->img->w / w : 1;
86    data->nh = data->img->h > h ? data->img->h / h : 1;
87    data->ntiles = data->nw * data->nh;
88    data->tx = (float)w / PotUp(data->img->w);
89    data->ty = (float)h / PotUp(data->img->h);
90
91    drawgroup = DRAWGROUP_BEFORE;
92}
93
94TileSet::~TileSet()
95{
96    free(data->tiles);
97    free(data->name);
98    delete data;
99}
100
101void TileSet::TickDraw(float deltams)
102{
103    Entity::TickDraw(deltams);
104
105    if (IsDestroying())
106    {
107        if (data->img)
108            SDL_FreeSurface(data->img);
109        else
110            glDeleteTextures(1, &data->texture);
111    }
112    else if (data->img)
113    {
114        GLuint format = data->img->format->Amask ? GL_RGBA : GL_RGB;
115        int planes = data->img->format->Amask ? 4 : 3;
116
117        int w = PotUp(data->img->w);
118        int h = PotUp(data->img->h);
119
120        uint8_t *pixels = (uint8_t *)data->img->pixels;
121        if (w != data->img->w || h != data->img->h)
122        {
123            uint8_t *tmp = (uint8_t *)malloc(planes * w * h);
124            for (int line = 0; line < data->img->h; line++)
125                memcpy(tmp + planes * w * line,
126                       pixels + planes * data->img->w * line,
127                       planes * data->img->w);
128            pixels = tmp;
129        }
130
131        glGenTextures(1, &data->texture);
132        glBindTexture(GL_TEXTURE_2D, data->texture);
133
134        glTexImage2D(GL_TEXTURE_2D, 0, planes, w, h, 0,
135                     format, GL_UNSIGNED_BYTE, pixels);
136
137        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
138        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
139
140        if (pixels != data->img->pixels)
141            free(pixels);
142        SDL_FreeSurface(data->img);
143        data->img = NULL;
144    }
145}
146
147char const *TileSet::GetName()
148{
149    return data->name;
150}
151
152void TileSet::BlitTile(uint32_t id, int x, int y, int z, int o)
153{
154    float tx = data->tx * ((id & 0xffff) % data->nw);
155    float ty = data->ty * ((id & 0xffff) / data->nw);
156    float dilate = data->dilate;
157
158    int dx = data->w;
159    int dy = o ? 0 : data->h;
160    int dz = o ? data->h : 0;
161
162    if (!data->img)
163    {
164        glBindTexture(GL_TEXTURE_2D, data->texture);
165        glBegin(GL_QUADS);
166            glTexCoord2f(tx, ty);
167            glVertex3f(x, dilate * (y + dy), dilate * (z + dz));
168            glTexCoord2f(tx + data->tx, ty);
169            glVertex3f(x + dx, dilate * (y + dy), dilate * (z + dz));
170            glTexCoord2f(tx + data->tx, ty + data->ty);
171            glVertex3f(x + dx, dilate * y, dilate * z);
172            glTexCoord2f(tx, ty + data->ty);
173            glVertex3f(x, dilate * y, dilate * z);
174        glEnd();
175    }
176}
177
Note: See TracBrowser for help on using the repository browser.