-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphics.cpp
338 lines (242 loc) · 7.86 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
#include <iostream>
#include "graphics.h"
#include <fstream>
#include <string>
#include <cmath>
texture allText[30];
int width, height;
int redbits, greenbits, bluebits, alphabits;
int depthbits, stencilbits;
int mode;
//--------------------------------------------
// initWindow - uses GLFW to initiate
// a window
//--------------------------------------------
/** Will try to initialize an OpenGL context.
* More details
*/
bool rendererAllmighty::initWindow(int _width, int _height, int bpp, int _mode, char* title)
{
width = _width;
height = _height;
if (bpp%3)
return 0;
redbits = greenbits = bluebits = bpp/3;
alphabits = 8;
mode = _mode;
// Initialize GLFW
glfwInit();
// V-sync
glfwSwapInterval(1);
int maxModes = 20;
GLFWvidmode list[maxModes];
int foundModes = glfwGetVideoModes( list, maxModes );
GLFWvidmode bestMode = list[foundModes-1];
std::cout << "Highest video mode :" << bestMode.Width << "x"<<bestMode.Height <<"\n";
std::cout << "Bpp: " << bestMode.RedBits+bestMode.GreenBits+bestMode.BlueBits<<"\n";
// Open window
int ok;
if (mode==GLFW_FULLSCREEN)
{
ok = glfwOpenWindow(
bestMode.Width, bestMode.Height, // Width and height of window
bestMode.RedBits, bestMode.GreenBits, bestMode.BlueBits, // Number of red, green, and blue bits for color buffer
alphabits, // Number of bits for alpha buffer
0, // Number of bits for depth buffer (Z-buffer)
0, // Number of bits for stencil buffer
mode // We want a desktop window (could be GLFW_FULLSCREEN)
);
}
else
{
ok = glfwOpenWindow(
width,height, // Width and height of window
8,8,8, // Number of red, green, and blue bits for color buffer
alphabits, // Number of bits for alpha buffer
24, // Number of bits for depth buffer (Z-buffer)
0, // Number of bits for stencil buffer
mode // We want a desktop window (could be GLFW_FULLSCREEN)
);
}
// If we could not open a window, exit now
if( !ok )
{
glfwTerminate();
std::cout << "glfwOpenWindow returned false\n";
return 0;
}
// Set window title
glfwSetWindowTitle( title );
// No v-sync
glfwSwapInterval( 1 );
// Enable sticky keys
glfwEnable( GLFW_STICKY_KEYS );
// Successful
return 1;
}
//--------------------------------------------
// setOrtho - sets up ortogonal viewing
// 2D, with blending etc
//--------------------------------------------
void rendererAllmighty::setOrtho()
{
glEnable( GL_TEXTURE_2D );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
int width,height;
glfwGetWindowSize( &width, &height );
glOrtho( 0, width, height, 0, -10, 10 );
glMatrixMode( GL_MODELVIEW );
glDepthMask( GL_FALSE );
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
glEnable( GL_BLEND );
}
void rendererAllmighty::drawRectangle( int x, int y, int w, int h, float r, float g, float b, float alpha )
{
glDisable( GL_TEXTURE_2D );
glLoadIdentity();
glColor4f( r, g, b, alpha );
glBegin( GL_QUADS );
glVertex3f( x, y, 0.1 );
glVertex3f( x+w, y, 0.1 );
glVertex3f( x+w, y+h, 0.1 );
glVertex3f( x, y+h, 0.1 );
glEnd();
glEnable( GL_TEXTURE_2D );
}
void rendererAllmighty::swapBuffers()
{
// Swap front and back buffers (we use a double buffered display)
// glFinish();
glfwSwapBuffers();
int width, height;
glfwGetWindowSize( &width, &height );
if ( height < 1 )
height = 1;
// Clear color and depht buffers
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
glClear( GL_COLOR_BUFFER_BIT );
glLoadIdentity();
glTranslatef( 0.375, 0.375, 0.0 );
glfwSleep(0.01);
}
//--------------------------------------------
// loadTexture - loads file as a texture of
// type type.
//--------------------------------------------
grError rendererAllmighty::loadTexture( char* file, gType type, texture* text )
{
std::stringstream out;
out << "Loading " << file << "\n";
// coLogg( out);
// Test if the file exists
std::ifstream fin( file, std::ios_base::in );
if ( !fin.is_open() )
{
std::stringstream out;
out << file << " does not exist\n";
std::cout << out;
// coLogg( out);
return NO_SUCH_FILE;
}
fin.close();
// Do we have an object?
if (text == 0)
text = new texture;
if (type == TGA)
{
// Start generating the texture
glGenTextures( 1, &(text->data) );
glBindTexture( GL_TEXTURE_2D, text->data );
// Is it a TGA-file? Open with glfw
GLFWimage img;
int result = glfwReadImage( file, &img, GLFW_NO_RESCALE_BIT );
if ( result == GL_FALSE )
{
std::cout << "glfwReadImage failed\n";
return GLFW_ERROR;
}
if ( &img == 0 )
{
std::cout << "glfwReadImage error\n";
return GLFW_ERROR;
}
text->width = img.Width;
text->height = img.Height;
glBindTexture( GL_TEXTURE_2D, text->data );
int err = gluBuild2DMipmaps(GL_TEXTURE_2D,
img.BytesPerPixel,
img.Width,
img.Height,
GL_RGB,
GL_UNSIGNED_BYTE,
img.Data);
if ( err ) {
std::stringstream out;
out << file << "Couldn't load " << file << " due to: " << gluErrorString( err ) << "\n";
std::cout << out;
// coLogg( out);
return GLUT_ERROR; //gluBuild failed, return false
}
glfwFreeImage( &img );
result = glfwLoadTexture2D( file, 0);
if (result == GL_FALSE)
return GLFW_ERROR;
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
return NO_ERROR;
}
if (type==PNG)
{
pngInfo info;
GLuint id = pngBind(file, PNG_NOMIPMAP, PNG_ALPHA, &info, GL_CLAMP, GL_NEAREST, GL_NEAREST);
if (id != 0)
{
text->data = id;
text->width = info.Width;
text->height = info.Height;
text->alpha = info.Alpha;
return NO_ERROR;
}
else return GLPNG_ERROR;
}
return NO_SUPPORTED_FORMAT;
}
void rendererAllmighty::drawSprite( texture* sprite, int x, int y, float rotation, float blended )
{
if (glfwGetKey(GLFW_KEY_ENTER))
std::cout << "2. drawSprite: rot: " << rotation << "\n";
drawSprite( sprite, x, y, sprite->width, sprite->height, rotation, blended );
}
void rendererAllmighty::drawSprite( texture* sprite, int x, int y)
{
drawSprite( sprite, x, y, sprite->width, sprite->height, 0.0, 1.0 );
}
void rendererAllmighty::drawSprite( texture* sprite, int x, int y, int w, int h, float rotation, float blended )
{
float colour=1;
glColor4f( colour, colour, colour, blended );
glBindTexture(GL_TEXTURE_2D, sprite->data);
float wsin = w/2*sin(rotation);
float wcos = w/2*cos(rotation);
float hsin = h/2*sin(rotation);
float hcos = h/2*cos(rotation);
glBegin( GL_QUADS );
glTexCoord2f(0.0, 0.0); glVertex2f( x-wcos+hsin, y-wsin-hcos ); // lower left
glTexCoord2f(1.0, 0.0); glVertex2f( x+wsin+hcos, y-wcos+hsin ); // lower right
glTexCoord2f(1.0, 1.0); glVertex2f( x+wcos-hsin, y+wsin+hcos ); // upper right
glTexCoord2f(0.0, 1.0); glVertex2f( x-wsin-hcos, y+wcos-hsin ); // upper left
glEnd();
}
void rendererAllmighty::drawSprite( texture* sprite, int x, int y, int w, int h)
{
float colour=1;
glColor4f( colour, colour, colour, 1.0 );
glBindTexture(GL_TEXTURE_2D, sprite->data);
glBegin( GL_QUADS );
glTexCoord2f(0.0, 0.0); glVertex2f( x, y ); // lower left
glTexCoord2f(1.0, 0.0); glVertex2f( x+w, y ); // lower right
glTexCoord2f(1.0, 1.0); glVertex2f( x+w, y+h ); // upper right
glTexCoord2f(0.0, 1.0); glVertex2f( x, y+h ); // upper left
glEnd();
}