source: trunk/src/tileset.cpp @ 269

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

Convert NPOT textures in code.

  • 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        SDL_Quit();
71        exit(1);
72    }
73
74    if (w <= 0)
75        w = 32;
76    if (h <= 0)
77        h = 32;
78
79    data->w = w;
80    data->h = h;
81    data->dilate = dilate;
82    /* FIXME: check for non-zero here */
83    data->nw = data->img->w / w;
84    data->nh = data->img->h / h;
85    data->ntiles = data->nw * data->nh;
86    data->tx = (float)w / PotUp(data->img->w);
87    data->ty = (float)h / PotUp(data->img->h);
88
89    drawgroup = DRAWGROUP_BEFORE;
90}
91
92TileSet::~TileSet()
93{
94    free(data->tiles);
95    free(data->name);
96    delete data;
97}
98
99void TileSet::TickDraw(float deltams)
100{
101    Entity::TickDraw(deltams);
102
103    if (destroy)
104    {
105        if (data->img)
106            SDL_FreeSurface(data->img);
107        else
108            glDeleteTextures(1, &data->texture);
109    }
110    else if (data->img)
111    {
112        GLuint format = data->img->format->Amask ? GL_RGBA : GL_RGB;
113        int planes = data->img->format->Amask ? 4 : 3;
114
115        int w = PotUp(data->img->w);
116        int h = PotUp(data->img->h);
117
118        uint8_t *pixels = (uint8_t *)data->img->pixels;
119        if (w != data->img->w || h != data->img->h)
120        {
121            uint8_t *tmp = (uint8_t *)malloc(planes * w * h);
122            for (int line = 0; line < data->img->h; line++)
123                memcpy(tmp + planes * w * line,
124                       pixels + planes * data->img->w * line,
125                       planes * data->img->w);
126            pixels = tmp;
127        }
128
129        glGenTextures(1, &data->texture);
130        glBindTexture(GL_TEXTURE_2D, data->texture);
131
132        glTexImage2D(GL_TEXTURE_2D, 0, planes, w, h, 0,
133                     format, GL_UNSIGNED_BYTE, pixels);
134
135        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
136        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
137
138        if (pixels != data->img->pixels)
139            free(pixels);
140        SDL_FreeSurface(data->img);
141        data->img = NULL;
142    }
143}
144
145char const *TileSet::GetName()
146{
147    return data->name;
148}
149
150void TileSet::BlitTile(uint32_t id, int x, int y, int z, int o)
151{
152    float tx = data->tx * ((id & 0xffff) % data->nw);
153    float ty = data->ty * ((id & 0xffff) / data->nw);
154    float dilate = data->dilate;
155
156    int off = o ? data->h : 0;
157    int dx = data->w;
158    int dy = data->h * 38 / 32; /* Magic... fix this one day */
159    int dy2 = data->h * 70 / 32;
160
161    if (!data->img)
162    {
163        glBindTexture(GL_TEXTURE_2D, data->texture);
164        glBegin(GL_QUADS);
165            glTexCoord2f(tx, ty);
166            glVertex3f(x, dilate * (y - dy - off), dilate * (z + off));
167            glTexCoord2f(tx + data->tx, ty);
168            glVertex3f(x + dx, dilate * (y - dy - off), dilate * (z + off));
169            glTexCoord2f(tx + data->tx, ty + data->ty);
170            glVertex3f(x + dx, dilate * (y - dy2), dilate * z);
171            glTexCoord2f(tx, ty + data->ty);
172            glVertex3f(x, dilate * (y - dy2), dilate * z);
173        glEnd();
174    }
175}
176
Note: See TracBrowser for help on using the repository browser.