source: trunk/src/tileset.cpp @ 210

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

Allow for different priorities in game and draw tick levels.

  • 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    drawgroup = DRAWGROUP_BEFORE;
76}
77
78TileSet::~TileSet()
79{
80    free(data->tiles);
81    free(data->name);
82    delete data;
83}
84
85void TileSet::TickDraw(float deltams)
86{
87    Entity::TickDraw(deltams);
88
89    if (destroy)
90    {
91        if (data->img)
92            SDL_FreeSurface(data->img);
93        else
94            glDeleteTextures(1, &data->texture);
95    }
96    else if (data->img)
97    {
98        glGenTextures(1, &data->texture);
99        glBindTexture(GL_TEXTURE_2D, data->texture);
100
101        glTexImage2D(GL_TEXTURE_2D, 0, 4, data->img->w, data->img->h, 0,
102                     GL_RGBA, GL_UNSIGNED_BYTE, data->img->pixels);
103
104        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
105        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
106
107        SDL_FreeSurface(data->img);
108        data->img = NULL;
109    }
110}
111
112char const *TileSet::GetName()
113{
114    return data->name;
115}
116
117void TileSet::BlitTile(uint32_t id, int x, int y, int z, int o)
118{
119    float tx = data->tx * ((id & 0xffff) % data->nw);
120    float ty = data->ty * ((id & 0xffff) / data->nw);
121
122    float sqrt2 = sqrtf(2.0f);
123    int off = o ? 32 : 0;
124
125    if (!data->img)
126    {
127        glBindTexture(GL_TEXTURE_2D, data->texture);
128        glBegin(GL_QUADS);
129            glTexCoord2f(tx, ty);
130            glVertex3f(x, sqrt2 * (y - 38 - off), sqrt2 * (z + off));
131            glTexCoord2f(tx + data->tx, ty);
132            glVertex3f(x + 32, sqrt2 * (y - 38 - off), sqrt2 * (z + off));
133            glTexCoord2f(tx + data->tx, ty + data->ty);
134            glVertex3f(x + 32, sqrt2 * (y - 70), sqrt2 * z);
135            glTexCoord2f(tx, ty + data->ty);
136            glVertex3f(x, sqrt2 * (y - 70), sqrt2 * z);
137        glEnd();
138    }
139}
140
Note: See TracBrowser for help on using the repository browser.