source: trunk/src/tileset.cpp @ 170

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

Properly implement program termination, including in the GTK program.

  • Property svn:keywords set to Id
File size: 2.7 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 ntiles;
41
42    SDL_Surface *img;
43    GLuint texture;
44};
45
46/*
47 * Public TileSet class
48 */
49
50TileSet::TileSet(char const *path)
51{
52    data = new TileSetData();
53    data->name = strdup(path);
54    data->tiles = NULL;
55    data->ntiles = 0;
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
70TileSet::~TileSet()
71{
72    free(data->tiles);
73    free(data->name);
74    delete data;
75}
76
77Entity::Group TileSet::GetGroup()
78{
79    return GROUP_BEFORE;
80}
81
82void TileSet::TickDraw(float deltams)
83{
84    Entity::TickDraw(deltams);
85
86    if (destroy)
87    {
88        if (data->img)
89            SDL_FreeSurface(data->img);
90        else
91            glDeleteTextures(1, &data->texture);
92    }
93    else if (data->img)
94    {
95        glGenTextures(1, &data->texture);
96        glBindTexture(GL_TEXTURE_2D, data->texture);
97
98        glTexImage2D(GL_TEXTURE_2D, 0, 4, data->img->w, data->img->h, 0,
99                     GL_RGBA, GL_UNSIGNED_BYTE, data->img->pixels);
100
101        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
102        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
103
104        SDL_FreeSurface(data->img);
105        data->img = NULL;
106    }
107}
108
109char const *TileSet::GetName()
110{
111    return data->name;
112}
113
114void TileSet::BlitTile(uint32_t id, int x, int y, int z, int o)
115{
116    float tx = .0625f * (id & 0xf);
117    float ty = .0625f * ((id >> 4) & 0xf);
118
119    float sqrt2 = sqrtf(2.0f);
120    int off = o ? 32 : 0;
121
122    if (!data->img)
123    {
124        glBindTexture(GL_TEXTURE_2D, data->texture);
125        glBegin(GL_QUADS);
126            glTexCoord2f(tx, ty);
127            glVertex3f(x, sqrt2 * (y + off), sqrt2 * (z + off));
128            glTexCoord2f(tx + .0625f, ty);
129            glVertex3f(x + 32, sqrt2 * (y + off), sqrt2 * (z + off));
130            glTexCoord2f(tx + .0625f, ty + .0625f);
131            glVertex3f(x + 32, sqrt2 * (y + 32), sqrt2 * z);
132            glTexCoord2f(tx, ty + .0625f);
133            glVertex3f(x, sqrt2 * (y + 32), sqrt2 * z);
134        glEnd();
135    }
136}
137
Note: See TracBrowser for help on using the repository browser.