-
Notifications
You must be signed in to change notification settings - Fork 1
/
opengl.c
executable file
·295 lines (217 loc) · 6.79 KB
/
opengl.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
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
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include "Header/global.h"
#include "Header/log.h"
#include "Header/opengl.h"
int next_p2(int a) {
int rval=2;
while(rval<a) rval<<=1;
return rval;
}
Uint32 *texturaPotencia2(Uint32 *pixels, int w, int h, int *width, int *height) {
int j, i;
*width = next_p2(w);
*height = next_p2(h);
Uint32 *expanded_data = (Uint32 *) malloc(*width * *height * sizeof(Uint32));
for(j=0; j < *height;j++) {
for(i=0; i < *width; i++){
expanded_data[i + j * *width] = (i>=w || j>=h) ? 0 : pixels[i + j*w];
}
}
return expanded_data;
}
void trocarTextura(GLuint textId) {
static GLuint texturaAtual=-1;
if (texturaAtual != textId) {
if (textId!=-1)
glBindTexture(GL_TEXTURE_2D, textId );
texturaAtual = textId;
}
}
void deleteTextura(Textura **textura) {
if (textura == NULL) return;
glDeleteTextures(1, &(*textura)->id);
free(*textura);
*textura=NULL;
}
SDL_Surface* convertToStandardFormat(SDL_Surface* surface) {
Uint32 rmask, gmask, bmask, amask;
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
rmask = 0xff000000;
gmask = 0x00ff0000;
bmask = 0x0000ff00;
amask = 0x000000ff;
#else
rmask = 0x000000ff;
gmask = 0x0000ff00;
bmask = 0x00ff0000;
amask = 0xff000000;
#endif
SDL_Surface *colorSurface = SDL_CreateRGBSurface(SDL_SWSURFACE, 0, 0, 32, rmask, gmask, bmask, amask);
SDL_Surface *tmp = NULL;
if (colorSurface != NULL) {
tmp = SDL_ConvertSurface(surface, colorSurface->format, SDL_SWSURFACE);
SDL_FreeSurface(colorSurface);
}
return tmp;
}
Textura *carregar_memoria(Uint32 *pixels, int w, int h, char alpha) {
Textura *textura=(Textura *) calloc(1,sizeof(Textura));
GLuint texture;
glPixelStorei(GL_UNPACK_ALIGNMENT,4);
glGenTextures(1,&texture);
glBindTexture(GL_TEXTURE_2D,texture);
int width;
int height;
Uint32 *expanded_data = texturaPotencia2(pixels, w, h, &width, &height);
glTexImage2D(GL_TEXTURE_2D, 0, 4, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, expanded_data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_NEAREST);
textura->id=texture;
textura->w=width;
textura->h=height;
textura->alpha=alpha;
free(expanded_data);
return textura;
}
Textura *carregar_img(enum TipoImagem tipo, char *text, ... ) {
char imagem[256];
va_list ap;
if (text == NULL) return 0;
va_start(ap, text);
vsprintf(imagem, text, ap);
va_end(ap);
if (imagem[0] == '\0') return 0;
SDL_Surface *loadedSurface = IMG_Load(imagem);
if (loadedSurface == NULL) {
logtemp=1;
logging(3,"Incapaz de carregar a imagem: %s", imagem);
return NULL;
}
SDL_Surface *Surface = convertToStandardFormat(loadedSurface);
SDL_FreeSurface(loadedSurface);
if (tipo == IMG_FUNDOROSA) {
int percorreX;
int percorreY;
Uint32 *pixels=(Uint32 *) Surface->pixels;
#ifdef __BIG_ENDIAN__
const unsigned int magicPink = 0xff00ffff;
#else
const unsigned int magicPink = 0xffff00ff;
#endif
for (percorreY=0; percorreY < Surface->h ; percorreY++) {
for (percorreX = 0; percorreX < Surface->w; percorreX++) {
if (pixels[percorreX + percorreY * Surface->w] == magicPink)
pixels[percorreX + percorreY * Surface->w] = 0x00000000;
}
}
}
Textura *retorno=carregar_memoria(Surface->pixels,Surface->w, Surface->h,(tipo != IMG_NORMAL));
SDL_FreeSurface(Surface);
return retorno;
}
void DrawIMG(Textura *textura, int dstX, int dstY, int width, int height, int srcX, int srcY) {
if ((textura->w < width) || (textura->h < height))
Error("tamanho errado",ERRO_LOG);
trocarTextura(textura->id);
if (!textura->alpha)
glDisable(GL_BLEND);
//glLoadIdentity();
float texX1 = srcX / (float)textura->w;
float texY1 = srcY / (float)textura->h;
float texX2 = (srcX+width) / (float)textura->w;
float texY2 = (srcY+height) / (float)textura->h;
glBegin(GL_QUADS);
glTexCoord2f(texX1, texY1);
glVertex3i(dstX, dstY, 0);
glTexCoord2f(texX1, texY2);
glVertex3i(dstX, dstY + height, 0);
glTexCoord2f(texX2, texY2);
glVertex3i(dstX + width, dstY + height, 0);
glTexCoord2f(texX2, texY1);
glVertex3i(dstX + width, dstY, 0);
glEnd();
if (!textura->alpha)
glEnable(GL_BLEND);
}
void DrawIMG_RGBA(Textura *textura, int dstX, int dstY, int width, int height, int srcX, int srcY, unsigned char r, unsigned char g, unsigned char b, unsigned char a) {
glColor4ub(r, g, b, a);
DrawIMG(textura, dstX, dstY, width, height, srcX, srcY);
glColor4ub(255, 255, 255, 255);
}
void DrawPixel(int x, int y, unsigned char r, unsigned char g, unsigned char b, unsigned char a) {
glColor4ub(r, g, b, a);
glDisable(GL_TEXTURE_2D);
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
glEnable(GL_TEXTURE_2D);
glColor4ub(255, 255, 255, 255);
}
void drawLine(int x1, int y1, int x2, int y2, unsigned char r, unsigned char g, unsigned char b, unsigned char a) {
glColor4ub(r, g, b, a);
glDisable(GL_TEXTURE_2D);
glBegin(GL_LINES);
glVertex2f(x1 + 0.375f, y1 + 0.375f);
glVertex2f(x2 + 1.0f - 0.375f, y2 + 1.0f - 0.375f);
glEnd();
glEnable(GL_TEXTURE_2D);
glColor4ub(255, 255, 255, 255);
}
void drawRectangle(int x, int y, int w, int h, unsigned char r, unsigned char g, unsigned char b, unsigned char a) {
glColor4ub(r, g, b, a);
glDisable(GL_TEXTURE_2D);
glBegin(GL_LINE_LOOP);
glVertex2f(x, y);
glVertex2f(x + w - 1.0f + 0.375f, y + 0.375f);
glVertex2f(x + w - 1.0f + 0.375f, y + h);
glVertex2f(x, y + h);
glEnd();
glEnable(GL_TEXTURE_2D);
glColor4ub(255, 255, 255, 255);
}
void fillRectangle(int x, int y, int w, int h, unsigned char r, unsigned char g, unsigned char b, unsigned char a) {
glColor4ub(r, g, b, a);
glDisable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
glVertex2i(x, y);
glVertex2i(x + w, y);
glVertex2i(x + w, y + h);
glVertex2i(x, y + h);
glEnd();
glEnable(GL_TEXTURE_2D);
glColor4ub(255, 255, 255, 255);
}
int ScreenShot() {
char nome[256];
SDL_Surface *temp=NULL;
unsigned char *pixels;
int i;
static int nFotos=1;
sprintf(nome,"Fotos/foto%04d.bmp",nFotos++);
if (!(screen->flags & SDL_OPENGL)) {
SDL_SaveBMP(screen, nome);
return nFotos-1;
}
temp = SDL_CreateRGBSurface(SDL_SWSURFACE, screen->w, screen->h, 24,
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
0x000000FF, 0x0000FF00, 0x00FF0000, 0
#else
0x00FF0000, 0x0000FF00, 0x000000FF, 0
#endif
);
if (temp == NULL)
return -1;
pixels = malloc(3 * screen->w * screen->h);
if (pixels == NULL) {
SDL_FreeSurface(temp);
return 0;
}
glReadPixels(0, 0, screen->w, screen->h, GL_RGB, GL_UNSIGNED_BYTE, pixels);
for (i=0; i<screen->h; i++)
memcpy(((char *) temp->pixels) + temp->pitch * i, pixels + 3*screen->w * (screen->h-i-1), screen->w*3);
free(pixels);
SDL_SaveBMP(temp, nome);
SDL_FreeSurface(temp);
return nFotos-1;
}