-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphics.cpp
executable file
·372 lines (314 loc) · 7.54 KB
/
graphics.cpp
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#include "mastrix.hpp"
Graphics graphics;
void Image::load(const char *fileName)
{
if(!strcmp(fileName, "") || !strcmp(fileName, " ")) {
width = height = 0;
textureID = 0;
return;
}
SDL_Surface *image = 0;
image = IMG_Load(fileName);
if (image == 0)
{
//put some error message here?
console.printf("Could not load image '%s': %s", fileName, SDL_GetError());
// assert(false);
return;
}
//keep track of dimensions
width = image->w;
height = image->h;
if(image->format->BitsPerPixel != 32) {
textureID = 0;
console.printf("Could not load image %s: wrong color depth.", fileName);
return;
}
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
/* glTexImage2D(
GL_TEXTURE_2D,
0,
4,
image->w,
image->h,
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
image->pixels);*/
gluBuild2DMipmaps(GL_TEXTURE_2D, 4, image->w, image->h, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);
SDL_FreeSurface(image);
glBindTexture(GL_TEXTURE_2D, 0);
}
Graphics::Graphics()
{
screen = 0;
//some default values for the screen dimensions
screenWidth = 1024;
screenHeight = 768;
}
Graphics::~Graphics()
{
screen = 0;
}
void Graphics::initialize(int width, int height)
{
initSDL(width, height);
initOpenGL();
}
void Graphics::setFullScreen(bool isFullScreen)
{
int fullScreenFlags;
if(currentlyFullscreen == isFullScreen) return;
if (isFullScreen)
fullScreenFlags = SDL_OPENGL | SDL_FULLSCREEN;
else
fullScreenFlags = SDL_OPENGL;
currentlyFullscreen = isFullScreen;
screen = SDL_SetVideoMode(
screenWidth,
screenHeight,
0,
fullScreenFlags);
initOpenGL();
images.refreshTextures();
}
void Graphics::changeResolution(int width, int height)
{
//ignore the command if the new dimensions are the safe
if (width == screenWidth && height == screenHeight)
return;
int fullScreenFlags;
if (fullScreen)
fullScreenFlags = SDL_OPENGL | SDL_FULLSCREEN;
else
fullScreenFlags = SDL_OPENGL;
screen = SDL_SetVideoMode(
width,
height,
0,
fullScreenFlags);
//if the new resolution failed, fall back to the old one!
if (screen == 0)
{
screen = SDL_SetVideoMode(
screenWidth,
screenHeight,
0,
fullScreenFlags);
}
else //success! update the screen dimensions
{
screenWidth = width;
screenHeight = height;
}
initOpenGL();
images.refreshTextures();
}
static void cleanup_sdl(void) {
SDL_Quit();
}
void Graphics::initSDL(int width, int height)
{
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0)
{
printf("Couldn't initialize SDL!");
exit(1);
}
atexit(cleanup_sdl);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 0);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
int flags = SDL_OPENGL;
if(fullScreen) flags |= SDL_FULLSCREEN;
screen = SDL_SetVideoMode(
width,
height,
0,
flags);
if (screen == 0)
{
// assert(false);
exit(1);
}
}
void Graphics::initOpenGL()
{
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glShadeModel(GL_SMOOTH);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);
//the screen surface should be loaded by here!
glViewport(
0,
0,
screenWidth,
screenHeight
);
glMatrixMode(GL_PROJECTION);
glOrtho(0, screenWidth, screenHeight, 0, -1, 1 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
}
//(leave SDL_Quit() to the main program)
void Graphics::shutDown()
{
}
void Graphics::drawImage(
const Image *image,
float x,
float y,
float angleRadians,
float scale)
{
//TODO: make these static constants
static const Color colorWhite = Color(255, 255, 255);
setColor(colorWhite);
drawColoredImage(
image,
x,
y,
angleRadians,
scale);
}
//this does not make any call to glColor(), so you must use setColor()
//to set the appropriate shading color before calling this function
void Graphics::drawColoredImage(
const Image *image,
float x,
float y,
float angleRadians,
float scale)
{
glBindTexture(GL_TEXTURE_2D, image->getTexID());
float angleInDegrees = (angleRadians * 180) / M_PI;
//apply some transformations first
glPushMatrix();
glTranslatef(x, y, 0.0f);
glScalef(scale/2, scale/2, 1);
glRotatef(angleInDegrees, 0.0f, 0.0f, -1.0f);
float width = image->getWidth(),
height = image->getHeight();
//start in top right vertex and go counter-clockwise
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex2f(-width, -height);
glTexCoord2f(1.0f, 0.0f); glVertex2f( width, -height);
glTexCoord2f(1.0f, 1.0f); glVertex2f( width, height);
glTexCoord2f(0.0f, 1.0f); glVertex2f(-width, height);
glEnd();
glPopMatrix();
//unbind the texture
glBindTexture(GL_TEXTURE_2D, 0);
}
void Graphics::drawPoint(
const Color &color,
float x,
float y,
float size)
{
setColor(color);
glPointSize(size);
glBegin(GL_POINTS);
glVertex2f(x, y);
glEnd();
}
//you have to manually call glBegin(GL_POINTS) and glEnd() for this one!
//(useful for drawing the starfield)
void Graphics::drawVertex(
float x,
float y)
{
glVertex2f(x, y);
}
void Graphics::drawLine(
const Color &color,
float x1,
float y1,
float x2,
float y2,
float width)
{
setColor(color);
glLineWidth(width);
glBegin(GL_LINES);
glVertex2f(x1, y1);
glVertex2f(x2, y2);
glEnd();
}
void Graphics::drawRect(
const Color &color,
float xLeft,
float yTop,
float xRight,
float yBottom)
{
setColor(color);
//start with the top right corner and go counter-clockwise
glBegin(GL_QUADS);
glVertex2f(xRight, yTop);
glVertex2f(xRight, yBottom);
glVertex2f(xLeft, yBottom);
glVertex2f(xLeft, yTop);
glEnd();
}
void Graphics::drawText(const char *text, float x, float y, void *font)
{
glColor3ub(255,255,255);
glRasterPos2f(x, y);
int length = (int) strlen(text);
for (int i = 0; i < length; i++)
glutBitmapCharacter(font, text[i]);
}
void Graphics::drawTextN(const char *text, int maxlen, float x, float y, void *font)
{
glColor3ub(255,255,255);
glRasterPos2f(x, y);
int length = (int) strlen(text);
if(length>maxlen) length = maxlen;
for (int i = 0; i < length; i++)
glutBitmapCharacter(font, text[i]);
}
void Graphics::drawTextCentered(const char *text, float x, float y, void *font)
{
drawText(text, x - drawTextWidth(text,font)/2, y, font);
}
int Graphics::drawTextWidth(const char *text, void *font)
{
int ret=0;
for(int ii=0; text[ii]; ii++)
ret += glutBitmapWidth(font, text[ii]);
return ret;
}
int Graphics::drawCharWidth(char c, void *font)
{
return glutBitmapWidth(font, c);
}
//tells OpenGL to change the color (for drawing a primitive)
//saves some typing at least
void Graphics::setColor(const Color &color)
{
glColor4ub(color.red, color.green, color.blue, color.alpha);
}
void Graphics::clearScreen()
{
//since we only clear to black, and because I'd rather not do these
//divisions, let's just clear the screen to black by default...
//glClearColor(
// color.red / 255,
// color.green / 255,
// color.blue / 255,
// color.alpha / 255);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
}
void Graphics::swapBuffers()
{
SDL_GL_SwapBuffers();
}