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 | |
---|
32 | class TileSetData |
---|
33 | { |
---|
34 | friend class TileSet; |
---|
35 | |
---|
36 | private: |
---|
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 | |
---|
49 | TileSet::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 | |
---|
69 | TileSet::~TileSet() |
---|
70 | { |
---|
71 | free(data->tiles); |
---|
72 | free(data->name); |
---|
73 | delete data; |
---|
74 | } |
---|
75 | |
---|
76 | Asset::Group TileSet::GetGroup() |
---|
77 | { |
---|
78 | return GROUP_BEFORE; |
---|
79 | } |
---|
80 | |
---|
81 | void TileSet::TickRender(float delta_time) |
---|
82 | { |
---|
83 | Asset::TickRender(delta_time); |
---|
84 | |
---|
85 | if (data->img) |
---|
86 | { |
---|
87 | glGenTextures(1, &data->texture); |
---|
88 | glBindTexture(GL_TEXTURE_2D, data->texture); |
---|
89 | |
---|
90 | glTexImage2D(GL_TEXTURE_2D, 0, 4, data->img->w, data->img->h, 0, |
---|
91 | GL_RGBA, GL_UNSIGNED_BYTE, data->img->pixels); |
---|
92 | |
---|
93 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
---|
94 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
---|
95 | |
---|
96 | SDL_FreeSurface(data->img); |
---|
97 | data->img = NULL; |
---|
98 | } |
---|
99 | else if (ref == 0) |
---|
100 | { |
---|
101 | glDeleteTextures(1, &data->texture); |
---|
102 | destroy = 1; |
---|
103 | } |
---|
104 | } |
---|
105 | |
---|
106 | char const *TileSet::GetName() |
---|
107 | { |
---|
108 | return data->name; |
---|
109 | } |
---|
110 | |
---|
111 | void TileSet::BlitTile(uint32_t id, int x, int y) |
---|
112 | { |
---|
113 | float tx = .0625f * (id & 0xf); |
---|
114 | float ty = .0625f * ((id >> 4) & 0xf); |
---|
115 | |
---|
116 | if (!data->img) |
---|
117 | { |
---|
118 | glBindTexture(GL_TEXTURE_2D, data->texture); |
---|
119 | glBegin(GL_QUADS); |
---|
120 | glTexCoord2f(tx, ty); |
---|
121 | glVertex2f(x, y); |
---|
122 | glTexCoord2f(tx + .0625f, ty); |
---|
123 | glVertex2f(x + 32, y); |
---|
124 | glTexCoord2f(tx + .0625f, ty + .0625f); |
---|
125 | glVertex2f(x + 32, y + 32); |
---|
126 | glTexCoord2f(tx, ty + .0625f); |
---|
127 | glVertex2f(x, y + 32); |
---|
128 | glEnd(); |
---|
129 | } |
---|
130 | } |
---|
131 | |
---|