-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
333 lines (289 loc) · 7.72 KB
/
main.cpp
File metadata and controls
333 lines (289 loc) · 7.72 KB
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
//The headers
#include "SDL/SDL.h"
#include "SDL/SDL_opengl.h"
#include "SDL/SDL_ttf.h"
#include "sstream"
#include "personality.h"
#include "culture.h"
#include "timer.h"
#include "text.h"
//Screen attributes
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;
const int SCREEN_BPP = 32;
//The frame rate
const int FRAMES_PER_SECOND = 60;
int frame;
//Event handler
SDL_Event event;
//font
TTF_Font *font = NULL;
bool init_GL()
{
//Set clear (background) color
glClearColor( 0.5f, 0.7f, 1.0f, 1.0f );
//Antialiasing
glEnable(GL_BLEND);
glEnable(GL_LINE_SMOOTH);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glHint(GL_LINE_SMOOTH_HINT, GL_FASTEST);
//text rendering
glEnable(GL_TEXTURE_2D);
//Set projection
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0, SCREEN_WIDTH * 1.5, SCREEN_HEIGHT * 1.5, 0, -1, 1 );
//Initialize modelview matrix
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
//If there was any errors
if( glGetError() != GL_NO_ERROR )
{
return false;
}
//If everything initialized
return true;
}
bool init()
{
//Initialize SDL
if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
{
return false;
}
//Create Window
if(SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_OPENGL ) == NULL )
{
return false;
}
//Initialize OpenGL
if( init_GL() == false )
{
return false;
}
//Set caption
SDL_WM_SetCaption( "Preservation", NULL );
return true;
}
void clean_up()
{
//Quit SDL
SDL_Quit();
}
void print_avg_details(Culture culture)
{
//Set projection
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1, 1 );
//Initialize modelview matrix
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
font = TTF_OpenFont( "November.ttf", 12 );
ostringstream ostr;
ostr << culture.getAverage()[0];
string strVal = ostr.str();
Text::render_text(font, 100, 255, 255, 0, 0, 0, "red: " + strVal);
ostr.str("");
strVal = "";
ostr << culture.getAverage()[1];
strVal = ostr.str();
Text::render_text(font, 100, 255, 255, 0, 15, 0, "green: " + strVal);
ostr.str("");
strVal = "";
ostr << culture.getAverage()[2];
strVal = ostr.str();
Text::render_text(font, 100, 255, 255, 0, 30, 0, "blue: " + strVal);
ostr.str("");
strVal = "";
ostr << culture.getAverage()[3];
strVal = ostr.str();
Text::render_text(font, 100, 255, 255, 0, 45, 0, "p1x: " + strVal);
ostr.str("");
strVal = "";
ostr << culture.getAverage()[4];
strVal = ostr.str();
Text::render_text(font, 100, 255, 255, 0, 60, 0, "p1y: " + strVal);
ostr.str("");
strVal = "";
ostr << culture.getAverage()[5];
strVal = ostr.str();
Text::render_text(font, 100, 255, 255, 0, 75, 0, "p2x: " + strVal);
ostr.str("");
strVal = "";
ostr << culture.getAverage()[6];
strVal = ostr.str();
Text::render_text(font, 100, 255, 255, 0, 90, 0, "p2y: " + strVal);
ostr.str("");
strVal = "";
ostr << culture.getAverage()[7];
strVal = ostr.str();
Text::render_text(font, 100, 255, 255, 0, 105, 0, "p3x: " + strVal);
ostr.str("");
strVal = "";
ostr << culture.getAverage()[8];
strVal = ostr.str();
Text::render_text(font, 100, 255, 255, 0, 120, 0, "p3y: " + strVal);
ostr.str("");
strVal = "";
ostr << culture.getAverage()[9];
strVal = ostr.str();
Text::render_text(font, 100, 255, 255, 0, 135, 0, "p4x: " + strVal);
ostr.str("");
strVal = "";
ostr << culture.getAverage()[10];
strVal = ostr.str();
Text::render_text(font, 100, 255, 255, 0, 150, 0, "p4y: " + strVal);
ostr.str("");
strVal = "";
ostr << culture.getAverage()[11];
strVal = ostr.str();
Text::render_text(font, 100, 255, 255, 0, 165, 0, "p5x: " + strVal);
ostr.str("");
strVal = "";
ostr << culture.getAverage()[12];
strVal = ostr.str();
Text::render_text(font, 100, 255, 255, 0, 180, 0, "p5y: " + strVal);
ostr.str("");
strVal = "";
ostr << culture.getAverage()[13];
strVal = ostr.str();
Text::render_text(font, 100, 255, 255, 0, 195, 0, "p6x: " + strVal);
ostr.str("");
strVal = "";
ostr << culture.getAverage()[14];
strVal = ostr.str();
Text::render_text(font, 100, 255, 255, 0, 210, 0, "p6y: " + strVal);
ostr.str("");
strVal = "";
ostr << culture.getAverage()[15];
strVal = ostr.str();
Text::render_text(font, 100, 255, 255, 0, 225, 0, "p7x: " + strVal);
ostr.str("");
strVal = "";
ostr << culture.getAverage()[16];
strVal = ostr.str();
Text::render_text(font, 100, 255, 255, 0, 240, 0, "p7y: " + strVal);
ostr.str("");
strVal = "";
ostr << culture.getAverage()[17];
strVal = ostr.str();
Text::render_text(font, 100, 255, 255, 0, 255, 0, "p8x: " + strVal);
ostr.str("");
strVal = "";
ostr << culture.getAverage()[18];
strVal = ostr.str();
Text::render_text(font, 100, 255, 255, 0, 270, 0, "p8y: " + strVal);
ostr.str("");
strVal = "";
ostr << culture.getAverage()[19];
strVal = ostr.str();
Text::render_text(font, 100, 255, 255, 0, 285, 0, "p9x: " + strVal);
ostr.str("");
strVal = "";
ostr << culture.getAverage()[20];
strVal = ostr.str();
Text::render_text(font, 100, 255, 255, 0, 300, 0, "p9y: " + strVal);
ostr.str("");
strVal = "";
ostr << culture.getAverage()[21];
strVal = ostr.str();
Text::render_text(font, 100, 255, 255, 0, 315, 0, "p10x:" + strVal);
ostr.str("");
strVal = "";
ostr << culture.getAverage()[22];
strVal = ostr.str();
Text::render_text(font, 100, 255, 255, 0, 330, 0, "p10y:" + strVal);
ostr.str("");
strVal = "";
ostr << culture.getAverage()[23];
strVal = ostr.str();
Text::render_text(font, 100, 255, 255, 0, 345, 0, "p11x:" + strVal);
ostr.str("");
strVal = "";
ostr << culture.getAverage()[24];
strVal = ostr.str();
Text::render_text(font, 100, 255, 255, 0, 360, 0, "p11y:" + strVal);
ostr.str("");
strVal = "";
ostr << culture.getAverage()[25];
strVal = ostr.str();
Text::render_text(font, 100, 255, 255, 0, 375, 0, "p12x:" + strVal);
ostr.str("");
strVal = "";
ostr << culture.getAverage()[26];
strVal = ostr.str();
Text::render_text(font, 100, 255, 255, 0, 390, 0, "p12y:" + strVal);
//Set projection
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0, SCREEN_WIDTH * 1.5, SCREEN_HEIGHT * 1.5, 0, -1, 1 );
//Initialize modelview matrix
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
}
int main( int argc, char *argv[] )
{
//Quit flag
bool quit = false;
//Initialize
if( init() == false )
{
return 1;
}
TTF_Init();
//initialize random
srand((int)time(NULL));
//The frame rate regulator
Timer fps;
Timer update;
//Start the frame timer
fps.start();
update.start();
//for displaying fps
string strVal;
//Wait for user exit
while( quit == false )
{
//While there are events to handle
while( SDL_PollEvent( &event ) )
{
if( event.type == SDL_QUIT )
{
quit = true;
}
}
//make personalities in culture slightly more like each other
culture.normalize();
//Move the cultures
culture.move();
//Clear the screen
glClear( GL_COLOR_BUFFER_BIT );
//Show the cultures
culture.draw();
culture.drawOutline();
//print details of average personality to screen
print_avg_details(culture);
//print fps to screen
frame++;
if(update.get_ticks() > 1000)
{
font = TTF_OpenFont( "November.ttf", 12 );
ostringstream ostr;
ostr << (int)(frame / (fps.get_ticks() / 1000.0f));
strVal = ostr.str();
update.start();
}
Text::render_text(font, 100, 255, 255, 750, 0, 0, "fps: " + strVal);
//Update screen
SDL_GL_SwapBuffers();
//Cap the frame rate
if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
{
SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
}
}
//Clean up
clean_up();
return 0;
}