source: trunk/src/tileset.cpp @ 186

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

Support tilesets larger than 512x512 and switch the coordinates system to
(0,0) at the bottom left.

  • Property svn:keywords set to Id
File size: 2.9 KB
Line 
1//
2// Deus Hax (working title)
3// Copyright (c) 2010 Sam Hocevar <sam@hocevar.net>
4//
5
6#if defined HAVE_CONFIG_H
7#   include "config.h"
8#endif
9
10#include <cstdlib>
11#include <cmath>
12
13#ifdef WIN32
14#   define WIN32_LEAN_AND_MEAN
15#   include <windows.h>
16#endif
17#if defined __APPLE__ && defined __MACH__
18#   include <OpenGL/gl.h>
19#else
20#   define GL_GLEXT_PROTOTYPES
21#   include <GL/gl.h>
22#endif
23
24#include <SDL.h>
25#include <SDL_image.h>
26
27#include "core.h"
28
29/*
30 * TileSet implementation class
31 */
32
33class TileSetData
34{
35    friend class TileSet;
36
37private:
38    char *name;
39    int *tiles;
40    int nw, nh, ntiles;
41    float tx, ty;
42
43    SDL_Surface *img;
44    GLuint texture;
45};
46
47/*
48 * Public TileSet class
49 */
50
51TileSet::TileSet(char const *path)
52{
53    data = new TileSetData();
54    data->name = strdup(path);
55    data->tiles = NULL;
56    data->img = NULL;
57    data->texture = 0;
58
59    for (char const *name = path; *name; name++)
60        if ((data->img = IMG_Load(name)))
61            break;
62
63    if (!data->img)
64    {
65        SDL_Quit();
66        exit(1);
67    }
68
69    data->nw = data->img->w / 32;
70    data->nh = data->img->h / 32;
71    data->ntiles = data->nw * data->nh;
72    data->tx = 32.0f / data->img->w;
73    data->ty = 32.0f / data->img->h;
74}
75
76TileSet::~TileSet()
77{
78    free(data->tiles);
79    free(data->name);
80    delete data;
81}
82
83Entity::Group TileSet::GetGroup()
84{
85    return GROUP_BEFORE;
86}
87
88void TileSet::TickDraw(float deltams)
89{
90    Entity::TickDraw(deltams);
91
92    if (destroy)
93    {
94        if (data->img)
95            SDL_FreeSurface(data->img);
96        else
97            glDeleteTextures(1, &data->texture);
98    }
99    else if (data->img)
100    {
101        glGenTextures(1, &data->texture);
102        glBindTexture(GL_TEXTURE_2D, data->texture);
103
104        glTexImage2D(GL_TEXTURE_2D, 0, 4, data->img->w, data->img->h, 0,
105                     GL_RGBA, GL_UNSIGNED_BYTE, data->img->pixels);
106
107        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
108        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
109
110        SDL_FreeSurface(data->img);
111        data->img = NULL;
112    }
113}
114
115char const *TileSet::GetName()
116{
117    return data->name;
118}
119
120void TileSet::BlitTile(uint32_t id, int x, int y, int z, int o)
121{
122    float tx = data->tx * ((id & 0xffff) % data->nw);
123    float ty = data->ty * ((id & 0xffff) / data->nw);
124
125    float sqrt2 = sqrtf(2.0f);
126    int off = o ? 32 : 0;
127
128    if (!data->img)
129    {
130        glBindTexture(GL_TEXTURE_2D, data->texture);
131        glBegin(GL_QUADS);
132            glTexCoord2f(tx, ty + data->ty);
133            glVertex3f(x, sqrt2 * (y - 70 + off), sqrt2 * (z + off));
134            glTexCoord2f(tx + data->tx, ty + data->ty);
135            glVertex3f(x + 32, sqrt2 * (y - 70 + off), sqrt2 * (z + off));
136            glTexCoord2f(tx + data->tx, ty);
137            glVertex3f(x + 32, sqrt2 * (y - 38), sqrt2 * z);
138            glTexCoord2f(tx, ty);
139            glVertex3f(x, sqrt2 * (y - 38), sqrt2 * z);
140        glEnd();
141    }
142}
143
Note: See TracBrowser for help on using the repository browser.