source: trunk/src/tileset.cpp @ 419

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

Name tilers, samples and pieces for easier debugging.

  • Property svn:keywords set to Id
File size: 4.2 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;
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 = (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 !FINAL_RELEASE
74        fprintf(stderr, "ERROR: could not load %s\n", path);
75#endif
76        SDL_Quit();
77        exit(1);
78    }
79
80    if (w <= 0)
81        w = 32;
82    if (h <= 0)
83        h = 32;
84
85    data->w = w;
86    data->h = h;
87    data->dilate = dilate;
88    data->nw = data->img->w > w ? data->img->w / w : 1;
89    data->nh = data->img->h > h ? data->img->h / h : 1;
90    data->ntiles = data->nw * data->nh;
91    data->tx = (float)w / PotUp(data->img->w);
92    data->ty = (float)h / 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
155void TileSet::BlitTile(uint32_t id, int x, int y, int z, int o)
156{
157    float tx = data->tx * ((id & 0xffff) % data->nw);
158    float ty = data->ty * ((id & 0xffff) / data->nw);
159    float dilate = data->dilate;
160
161    int dx = data->w;
162    int dy = o ? 0 : data->h;
163    int dz = o ? data->h : 0;
164
165    if (!data->img)
166    {
167        glBindTexture(GL_TEXTURE_2D, data->texture);
168        glBegin(GL_QUADS);
169            glTexCoord2f(tx, ty);
170            glVertex3f(x, dilate * (y + dy), dilate * (z + dz));
171            glTexCoord2f(tx + data->tx, ty);
172            glVertex3f(x + dx, dilate * (y + dy), dilate * (z + dz));
173            glTexCoord2f(tx + data->tx, ty + data->ty);
174            glVertex3f(x + dx, dilate * y, dilate * z);
175            glTexCoord2f(tx, ty + data->ty);
176            glVertex3f(x, dilate * y, dilate * z);
177        glEnd();
178    }
179}
180
Note: See TracBrowser for help on using the repository browser.