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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "main.h"
#include "list.h"
#include "storage.h"
#include "interface.h"
#include "image.h"
static list_t *listStorage;
static bool_t isImageDataInit = FALSE;
bool_t isImageInicialized()
{
return isImageDataInit;
}
/*
* Inicializuje globalny zoznam obrazkov
*/
void initImageData()
{
assert(isInterfaceInicialized() == TRUE);
DEBUG_MSG(_("Initializing image database\n"));
listStorage = newStorage();
isImageDataInit = TRUE;
}
static SDL_Surface *loadImage(const char *filename, int alpha)
{
SDL_Surface *tmp;
SDL_Surface *ret;
char str[STR_PATH_SIZE];
if (isFillPath(filename)) {
strcpy(str, filename);
} else {
sprintf(str, PATH_IMAGE "%s", filename);
}
accessExistFile(str);
if ((tmp = IMG_Load(str)) == NULL) {
fprintf(stderr, "%s\n", SDL_GetError());
return NULL;
}
if ((ret = (alpha) ? SDL_DisplayFormatAlpha(tmp) : SDL_DisplayFormat(tmp)) == NULL) {
fprintf(stderr, "%s\n", SDL_GetError());
SDL_FreeSurface(tmp);
return NULL;
}
SDL_FreeSurface(tmp);
return ret;
}
#ifndef SUPPORT_OPENGL
image_t *newImage(SDL_Surface * surface)
{
image_t *new;
assert(surface != NULL);
new = malloc(sizeof(image_t));
new->image = surface;
new->w = surface->w;
new->h = surface->h;
return new;
}
#endif
#ifdef SUPPORT_OPENGL
/*
* returns closest bigger power of 2 to i
*/
unsigned int closestpoweroftwo(unsigned int i)
{
int p;
p=0;
while(i){
i=i>>1;
p++;
}
return 1<<(p-0);
}
/* convert from SDL_Surface to image_t
* WARNING: newImage is indestructive to surface, caller is responsible for freeing surface
* surface is not needed for image_t after execution of newImage
*/
image_t* newImage(SDL_Surface *surface)
{
image_t *new;
Uint32 rmask, gmask, bmask, amask;
SDL_Surface *sdl_rgba_surface;
unsigned int bpp;
//it is unoptimal to blit to buffer surface before actual loading to opengl texture but it is safer and simpler to implement
//it is unoptimal to always use RGBA texture
// we set properties of our buffer sdl_rgba_surface
rmask = 0x000000ff;
gmask = 0x0000ff00;
bmask = 0x00ff0000;
amask = 0xff000000;
bpp = 32; /*bits per pixel*/
assert(surface != NULL);
new = malloc(sizeof(image_t));
new->w = surface->w;
new->h = surface->h;
//because some older hw is really slow when using textures with width and height which is not a power of two
new->tw = closestpoweroftwo(new->w);
new->th = closestpoweroftwo(new->h);
sdl_rgba_surface = SDL_CreateRGBSurface( SDL_SWSURFACE, new->tw, new->th, bpp, rmask, gmask, bmask, amask);
SDL_SetAlpha(surface, 0, SDL_ALPHA_OPAQUE); // we unset SDL_SRCALPHA to preserve alpha channel of original image
SDL_BlitSurface(surface, 0, sdl_rgba_surface, 0);
SDL_LockSurface(sdl_rgba_surface); // we ensure that we can read pixels from sdl_rgba_surface
GLuint tid;
glGenTextures(1, &tid); //we ask for free texture id
glBindTexture(GL_TEXTURE_2D, tid);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // scale linearly when image bigger than texture
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // scale linearly when image smalled than texture
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, new->tw, new->th, 0, GL_RGBA, GL_UNSIGNED_BYTE, sdl_rgba_surface->pixels);
SDL_UnlockSurface(sdl_rgba_surface);
SDL_FreeSurface(sdl_rgba_surface);
new->tex_id = tid;
SDL_FreeSurface(surface);
return new;
}
#endif
void destroyImage(image_t * p)
{
assert(p != NULL);
#ifndef SUPPORT_OPENGL
SDL_FreeSurface((SDL_Surface *) p->image);
#endif
#ifdef SUPPORT_OPENGL
glDeleteTextures(1, &p->tex_id);
#endif
free(p);
}
/*
* Prida obrazok do globalneho zoznamu obrazkov
* *file - nazov suboru v ktorom je obrazok ulozeny
* *name - nazov obrazku ( pouzije sa ako vyhadavaci retazec v zazanem )
* alpha - 0 - nema alpha kanal | 1 - ma alpha kanal
*/
image_t *addImageData(char *file, int alpha, char *name, char *group)
{
SDL_Surface *surface;
image_t *new;
assert(file != NULL);
assert(name != NULL);
assert(group != NULL);
surface = loadImage(file, alpha);
new = newImage(surface);
addItemToStorage(listStorage, group, name, new);
DEBUG_MSG(_("Loading image %s\n"), file);
return new;
}
/*
* Vrati odkaz na image_data v globalnom zozname obrazkov
* a nazvom *s
*/
image_t *getImage(char *group, char *name)
{
assert(group != NULL);
assert(name != NULL);
return getItemFromStorage(listStorage, group, name);
}
void delImage(char *group, char *name)
{
assert(group != NULL);
assert(name != NULL);
delItemFromStorage(listStorage, group, name, destroyImage);
}
void delAllImageInGroup(char *group)
{
assert(group != NULL);
delAllItemFromStorage(listStorage, group, destroyImage);
}
#ifndef SUPPORT_OPENGL
void drawImage(image_t * p, int x, int y, int px, int py, int w, int h)
{
static SDL_Surface *screen = NULL;
SDL_Rect dst_rect, src_rect;
if (screen == NULL) {
screen = getSDL_Screen();
}
dst_rect.x = x;
dst_rect.y = y;
src_rect.x = px;
src_rect.y = py;
src_rect.w = w;
src_rect.h = h;
SDL_BlitSurface(p->image, &src_rect, screen, &dst_rect);
}
#endif
#ifdef SUPPORT_OPENGL
/*
* Draws image on screen at [x,y], with width w and height h, top-left corner on image is at [px,py]
*/
void drawImage(image_t *image, int x,int y, int px, int py, int w, int h)
{
/* x -coordinate of left border xx- coordinate of right border,
* y -coordinate of top border yy- coordinate of bottom border,
* t_ texture space d_ screen space,
*/
float t_x, t_y, t_xx, t_yy;
float d_x, d_y, d_xx, d_yy;
// screen space
d_x = x;
d_y = y;
d_xx = x+w;
d_yy = y+h;
// texture space (remember that texture space is from 0.0 to 1.0)
t_x = px/(float)image->tw;
t_y = py/(float)image->th;
t_xx = (px+w)/(float)image->tw;
t_yy = (py+h)/(float)image->th;
glBindTexture(GL_TEXTURE_2D, image->tex_id);
// 2--4
// |\ |
// | \|
// 1--3
glBegin(GL_TRIANGLE_STRIP);
//1
glTexCoord2f(t_x,t_yy);
glVertex2f(d_x, d_yy);
//2
glTexCoord2f(t_x, t_y);
glVertex2f(d_x, d_y);
//3
glTexCoord2f(t_xx, t_yy);
glVertex2f(d_xx, d_yy);
//4
glTexCoord2f(t_xx,t_y);
glVertex2f(d_xx, d_y);
glEnd();
}
#endif
/*
* Odstrani zoznam obrazkov z pamate
*/
void quitImageData()
{
DEBUG_MSG(_("Quitting image database\n"));
destroyStorage(listStorage, destroyImage);
isImageDataInit = FALSE;
}
|