source: trunk/src/tileset.cpp @ 100

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

Slightly improve the documentation in a few files, add the missing
joystick class, and put a header in each source file.

  • Property svn:keywords set to Id
File size: 2.4 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
12#ifdef WIN32
13#   define WIN32_LEAN_AND_MEAN
14#   include <windows.h>
15#endif
16#if defined __APPLE__ && defined __MACH__
17#   include <OpenGL/gl.h>
18#else
19#   define GL_GLEXT_PROTOTYPES
20#   include <GL/gl.h>
21#endif
22
23#include <SDL.h>
24#include <SDL_image.h>
25
26#include "tileset.h"
27
28/*
29 * TileSet implementation class
30 */
31
32class TileSetData
33{
34    friend class TileSet;
35
36private:
37    char *name;
38    int *tiles;
39    int ntiles;
40
41    SDL_Surface *img;
42    GLuint texture;
43};
44
45/*
46 * Public TileSet class
47 */
48
49TileSet::TileSet(char const *path)
50{
51    data = new TileSetData();
52    data->name = strdup(path);
53    data->tiles = NULL;
54    data->ntiles = 0;
55    data->img = NULL;
56    data->texture = 0;
57
58    for (char const *name = path; *name; name++)
59        if ((data->img = IMG_Load(name)))
60            break;
61
62    if (!data->img)
63    {
64        SDL_Quit();
65        exit(1);
66    }
67}
68
69TileSet::~TileSet()
70{
71    free(data->tiles);
72    free(data->name);
73    delete data;
74}
75
76void TileSet::TickRender(float delta_time)
77{
78    Asset::TickRender(delta_time);
79
80    if (data->img)
81    {
82        glGenTextures(1, &data->texture);
83        glBindTexture(GL_TEXTURE_2D, data->texture);
84
85        glTexImage2D(GL_TEXTURE_2D, 0, 4, data->img->w, data->img->h, 0,
86                     GL_RGBA, GL_UNSIGNED_BYTE, data->img->pixels);
87
88        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
89        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
90
91        SDL_FreeSurface(data->img);
92        data->img = NULL;
93    }
94    else if (ref == 0)
95    {
96        glDeleteTextures(1, &data->texture);
97        destroy = 1;
98    }
99}
100
101char const *TileSet::GetName()
102{
103    return data->name;
104}
105
106void TileSet::BlitTile(uint32_t id, int x, int y)
107{
108    float tx = .0625f * (id & 0xf);
109    float ty = .0625f * ((id >> 4) & 0xf);
110
111    if (!data->img)
112    {
113        glBindTexture(GL_TEXTURE_2D, data->texture);
114        glBegin(GL_QUADS);
115            glTexCoord2f(tx, ty);
116            glVertex2f(x, y);
117            glTexCoord2f(tx + .0625f, ty);
118            glVertex2f(x + 32, y);
119            glTexCoord2f(tx + .0625f, ty + .0625f);
120            glVertex2f(x + 32, y + 32);
121            glTexCoord2f(tx, ty + .0625f);
122            glVertex2f(x, y + 32);
123        glEnd();
124    }
125}
126
Note: See TracBrowser for help on using the repository browser.