-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrune.c
235 lines (195 loc) · 5.28 KB
/
rune.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include "rune.h"
#include <SDL2/SDL_ttf.h>
#include <stdlib.h>
#include "matrix.h"
#include "uthash.h"
#include "util.h"
#define MAX_RUNE_PAGES 512
/* pixelFmt is the pixel format for textures destined for OpenGL */
static SDL_PixelFormat pixelFmt = {.palette = 0,
.BitsPerPixel = 32,
.BytesPerPixel = 4,
.Rshift = 0,
.Rloss = 0,
.Gloss = 0,
.Bloss = 0,
.Aloss = 0,
.Rmask = 0x000000ff,
.Gshift = 8,
.Gmask = 0x0000ff00,
.Bshift = 16,
.Bmask = 0x00ff0000,
.Ashift = 24,
.Amask = 0xff000000};
/* LoadedTex is the type stored in the texture -> name hash table */
typedef struct {
GLuint tex;
char name[32];
UT_hash_handle hh;
} LoadedTex;
static LoadedTex *loadedTextures = NULL;
/* renders the given character to a texture and returns its GL handle */
static GLuint latin1_to_texture(char c) {
static TTF_Font *font = NULL;
static SDL_Color color = {.r = 200, .g = 255, .b = 255, .a = 128};
char str[2];
SDL_Surface *surf;
LoadedTex *lup;
GLuint tex;
str[0] = c;
str[1] = '\0';
HASH_FIND_STR(loadedTextures, str, lup);
if (lup != NULL) {
return lup->tex;
}
if (font == NULL) {
font = TTF_OpenFont("C64.ttf", 32);
}
if (font == NULL) {
printf("failed to load font: %s\n", TTF_GetError());
return 0;
}
tex = -1;
if ((surf = TTF_RenderText_Solid(font, str, color))) {
int colors;
SDL_Surface *optSurf;
optSurf = SDL_ConvertSurface(surf, &pixelFmt, SDL_SWSURFACE);
colors = surf->format->BytesPerPixel;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, optSurf->w, optSurf->h,
0, GL_RGBA, GL_UNSIGNED_BYTE, optSurf->pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
GL_NEAREST);
SDL_FreeSurface(surf);
} else {
printf("error: failed to render char %c to texture: %s\n", c,
TTF_GetError());
tex = 0;
}
lup = malloc(sizeof(LoadedTex));
lup->name[0] = c;
lup->name[1] = '\0';
lup->tex = tex;
HASH_ADD_STR(loadedTextures, name, lup);
return tex;
}
/* bitmap to texture loads file and returns a handle an OpenGL texture of it */
static GLuint bitmap_to_texture(const char *file) {
GLuint tex;
SDL_Surface *surf;
surf = NULL;
tex = 0;
if ((surf = SDL_LoadBMP(file))) {
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, surf->w, surf->h, 0,
GL_RGB, GL_UNSIGNED_BYTE, surf->pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
GL_NEAREST);
SDL_FreeSurface(surf);
} else {
printf("error: failed to load texture %s\n", file);
}
return tex;
}
/* new_Runeset loads a texture file containings a page of runes */
uint32_t new_Runeset(const char *file) {
static uint32_t pages[MAX_RUNE_PAGES];
static uint32_t numPages = 0;
pages[numPages] = bitmap_to_texture(file);
return pages[numPages++];
}
/* ctor */
Rune *new_Rune() {
Rune *r = malloc(sizeof(Rune));
r->draw = NULL;
r->update = NULL;
return r;
}
/* dtor */
void del_Rune(Rune *r) { free(r); }
/* rune_Draw executes r's draw method */
void rune_Draw(Rune *r, uint32_t x, uint32_t y) { r->draw(r, x, y); }
/* rune_DrawChar renders the given rune at char position (x, y) */
RuneDrawResult rune_DrawChar(Rune *rune, uint32_t x, uint32_t y) {
RuneDrawResult res;
CharRune *r;
GLfloat u, v;
GLuint sampler;
r = (CharRune *)rune;
/* load texture */
if (r->texture == 0) {
r->texture = latin1_to_texture('a'); // r->id);
glGenSamplers(1, &sampler);
glSamplerParameteri(sampler, GL_TEXTURE_WRAP_S, GL_REPEAT);
glSamplerParameteri(sampler, GL_TEXTURE_WRAP_T, GL_REPEAT);
glSamplerParameteri(sampler, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glSamplerParameteri(sampler, GL_TEXTURE_MIN_FILTER,
GL_NEAREST_MIPMAP_NEAREST);
glSamplerParameterf(sampler, GL_TEXTURE_MAX_ANISOTROPY_EXT,
16.0f);
}
res.tex = r->texture;
res.pos.x = 0.0f;
res.pos.y = 0.0f;
res.pos.w = rune->w;
res.pos.h = rune->h;
res.clip.x = 0.0f;
res.clip.y = 0.0f;
res.clip.w = 1.0f;
res.clip.h = 1.0f;
return res;
}
/* rune_DrawImg renders the given image rune */
RuneDrawResult rune_DrawImg(Rune *rune, uint32_t x, uint32_t y) {
RuneDrawResult res;
ImgRune *r;
r = (ImgRune *)rune;
/* load image as texture */
if (r->texture == 0) {
r->texture = bitmap_to_texture(r->filename);
}
res.tex = r->texture;
res.pos.x = 0.0f;
res.pos.y = 0.0f;
res.pos.w = rune->w;
res.pos.h = rune->h;
res.clip.x = 0.0f;
res.clip.y = 0.0f;
res.clip.w = 1.0f;
res.clip.h = 1.0f;
return res;
}
/* rune_DrawMesh renders a mesh at char position (x, y) */
RuneDrawResult rune_DrawMesh(Rune *r, uint32_t x, uint32_t y) {
MeshRune *mr;
RuneDrawResult res = {};
mr = (MeshRune *)r;
if (mr->mesh.color == 0) {
printf("LOADING %s\n", mr->filename);
init_Mesh(&mr->mesh);
mesh_Load(&mr->mesh, mr->filename);
}
mesh_Draw(&mr->mesh);
res.tex = mr->mesh.color;
res.pos.x = 0.0f;
res.pos.y = 0.0f;
res.pos.w = r->w;
res.pos.h = r->h;
res.clip.x = 0.0f;
res.clip.y = 0.0f;
res.clip.w = 1.0f;
res.clip.h = 1.0f;
return res;
}
/* rune_Update executes r's update method */
void rune_Update(Rune *r) {
if (r->update != NULL) {
r->update(r);
}
}