-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameLoop.hpp
454 lines (414 loc) · 13.9 KB
/
GameLoop.hpp
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
//GameLoop.h
/*Game looping section of Sudoku class*/
#pragma once
#include "Sudoku.hpp"
void Sudoku::Sudoku::display()
{
// Initialise SDL
if (!initialiseSDL())
{
close();
}
bool stop = false;
Buttons clicked;
//Initially at Home Screen
clicked = homeScreen();
while(!stop)
{
switch(clicked)
{
case Buttons::HOME:
clicked = homeScreen();
break;
case Buttons::USERNAME:
clicked = userName();
break;
case Buttons::ABOUT:
clicked = about_best(Buttons::ABOUT);
break;
case Buttons::BEST:
clicked = about_best(Buttons::BEST);
break;
case Buttons::HELP:
clicked = about_best(Buttons::HELP);
break;
case Buttons::LEVEL:
clicked = levels();
break;
case Buttons::HARD:
clicked = play(Buttons::HARD);
break;
case Buttons::MEDIUM:
clicked = play(Buttons::MEDIUM);
break;
case Buttons::EASY:
clicked = play(Buttons::EASY);
break;
case Buttons::CLOSE:
stop = true;
break;
default:
stop = true;
break;
}
}
//Distroy and Quit
close();
}
//Homescreen Interface
Buttons Sudoku::Sudoku::homeScreen()
{
//Bool to choose the place to go
bool start = false, about = false, best = false, help = false;
mImage();
int x, y;
const int numOfButton = 4;
const int buttonHeight = mGridHeight / 10;
const int buttonWidth = mGridWidth / 2 + 10;
const int gap = buttonHeight / 2;
x = buttonHeight;
y = (mGridHeight - numOfButton * buttonHeight - (numOfButton-1) * gap) / 2;
bool draw = 0;
//load START, BEST, ABOUT and HELP texture
SDL_Texture* texture[numOfButton];
loadTexture(texture[0], "START", GrayColor);
loadTexture(texture[1], "BEST", GrayColor);
loadTexture(texture[2], "ABOUT", GrayColor);
loadTexture(texture[3], "HELP", GrayColor);
//we already used the mtexts upto 5
int n = 6;
// Display the Buttons
for(int i = 0; i<numOfButton; i++)
{
SDL_Rect buttonRect = { x, y, buttonWidth, buttonHeight };
mTexts[n].renderTexts(mRenderer, buttonRect, texture[i]);
mRectangle.renderRectangle(mRenderer, buttonRect, draw);
y += buttonHeight + gap;
n++;
}
SDL_Event event;
bool stop = false;
while (!stop) {
// Handle events on queue
while (SDL_PollEvent(&event) != 0) {
// Handle quitting and completion
if (event.type == SDL_QUIT)
{
// Set stop flag
stop = true;
}
if(mTexts[6].getMouseEvent(&event) == ButtonState::BUTTON_MOUSE_DOWN)
{
start = stop = true;
}
else if(mTexts[7].getMouseEvent(&event) == ButtonState::BUTTON_MOUSE_DOWN)
{
best = stop = true;
}
else if(mTexts[8].getMouseEvent(&event) == ButtonState::BUTTON_MOUSE_DOWN)
{
about = stop = true;
}
else if(mTexts[9].getMouseEvent(&event) == ButtonState::BUTTON_MOUSE_DOWN)
{
help = stop = true;
}
for(int i = 0; i<numOfButton; i++)
{
mTexts[6+i].renderEvent(mRenderer);
}
}
// Update the screen
SDL_RenderPresent(mRenderer);
}
//Clear with Black clolr
SDL_SetRenderDrawColor(mRenderer, BlackColor.r, BlackColor.g, BlackColor.b, BlackColor.a);
SDL_RenderClear(mRenderer);
if(start)
return Buttons::USERNAME;
else if(about)
return Buttons::ABOUT;
else if(best)
return Buttons::BEST;
else if(help)
return Buttons::HELP;
else
return Buttons::CLOSE;
}
Buttons Sudoku::Sudoku::about_best(Buttons mButton)
{
mImage();
//Define X and Y offset
const int offset = mWindowWidth/26;
int x = offset;
int y = offset;
int buttonLength = mWindowWidth/1.8;
int buttonHeight = mWindowHeight - 2*x;
//Display the rectangle
SDL_Rect Rect = { x, y, buttonLength, buttonHeight };
mTexts[10].renderRectangle(mRenderer, Rect, 0);
//Redefine to be back
Rect = { x + buttonLength, y - offset , mWindowWidth - buttonLength, mWindowHeight - offset};
SDL_Texture* texture;
std::string texts[15];
int totalLines = 0;
if(mButton == Buttons::BEST)
{
totalLines = myfile.getData(texts, Buttons::BEST);
}
else if(mButton == Buttons::ABOUT)
{
totalLines = myfile.getData(texts, Buttons::ABOUT);
}
else if(mButton == Buttons::HELP)
{
totalLines = myfile.getData(texts, Buttons::HELP);
}
//Redefine for texts
x += 10;
y += 10;
buttonLength -= 2*x;
buttonHeight /= 1.25*totalLines;
for(int i = 0; i<totalLines; i++)
{
loadTexture(texture, texts[i].c_str(), BlackColor);
SDL_Rect textRect = { x, y, buttonHeight, buttonLength };
mTexts[8].renderTexts(mRenderer, textRect, texture, 1.5, 'n');
y += buttonHeight;
}
mTexts[8].setButtonRect(Rect);
SDL_Event event;
bool stop = false;
bool userClicked = false; // A flag to indicate if the user clicked outside
while(!stop)
{
while(SDL_PollEvent(&event) != 0)
{
if (event.type == SDL_QUIT)
{
stop = true;
}
if(mTexts[8].getMouseEvent(&event) == ButtonState::BUTTON_MOUSE_DOWN)
{
stop = userClicked = true;
}
}
// Update the screen
SDL_RenderPresent(mRenderer);
}
//Reset to Black and Clear the rendarer
SDL_SetRenderDrawColor(mRenderer, BlackColor.r, BlackColor.g, BlackColor.b, BlackColor.a);
SDL_RenderClear(mRenderer);
if(userClicked)
return Buttons::HOME;
else
{
return Buttons::CLOSE;
}
}
Buttons Sudoku::Sudoku::userName()
{
//Set Bg image
mImage();
//Display Back button
backButton();
int buttonWidth = mWindowWidth/2.5;
int buttonHeight = mWindowHeight/15;
int initialX = (mWindowWidth - buttonWidth)/2;
int initialY = (mWindowHeight - 2*buttonHeight)/2;
//Load and Display Textures
loadTexture(mTextureCache[16], "User Name:", BlackColor);
SDL_Rect userRect = { initialX, initialY, buttonWidth, buttonHeight };
mTexts[9].renderTexts(mRenderer, userRect, mTextureCache[16], 1.5, 'n');
initialY += buttonHeight;
loadTexture(mTextureCache[17], "User name appers Here", GrayColor);
userRect = { initialX, initialY, buttonWidth, buttonHeight };
//Initially display in gray color
SDL_SetRenderDrawColor(mRenderer, 255, 255, 255, 255);
mTexts[10].renderRectangle(mRenderer, userRect, 1);
mTexts[10].renderTexts(mRenderer, userRect, mTextureCache[17], 1.5);
//Store something temporarily and proceed
SDL_Surface* temp = NULL;
SDL_Texture* textImage = SDL_CreateTextureFromSurface(mRenderer, temp);
SDL_FreeSurface(temp);
SDL_StartTextInput();
std::string name = " ";
bool stop = false;
bool exit = false;
bool userEntered = false; //A flaf to check if User enters the enter key or not
while(!stop)
{
//Handle events
SDL_Event event;
while(SDL_PollEvent(&event) != 0)
{
exit = stop = userEntered = false;
//Handle Possible Events
switch(event.type)
{
case SDL_QUIT:
stop = true;
break;
//Get input texts
case SDL_TEXTINPUT:
name += event.text.text;
if(textImage)
{
SDL_DestroyTexture(textImage);
textImage = NULL;
}
temp = TTF_RenderText_Solid(mFont, name.c_str(), BlackColor);
if(temp)
{
textImage = SDL_CreateTextureFromSurface(mRenderer, temp);
SDL_FreeSurface(temp);
temp = NULL;
}
//Render the texts
mTexts[10].renderRectangle(mRenderer, userRect, 1);
mTexts[10].renderTexts(mRenderer, userRect, textImage, 1.5);
break;
//Check keyboard events Enter key and Backspace
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_RETURN || event.key.keysym.sym == SDLK_KP_ENTER)
{
stop = userEntered = true;
}
//check Backspace event
else if(event.key.keysym.sym == SDLK_BACKSPACE)
{
// Check if the string is not empty
if (!name.empty())
{
// Remove the last character
name.pop_back();
}
if(textImage)
{
SDL_DestroyTexture(textImage);
textImage = NULL;
}
temp = TTF_RenderText_Solid(mFont, name.c_str(), BlackColor);
if(temp)
{
textImage = SDL_CreateTextureFromSurface(mRenderer, temp);
SDL_FreeSurface(temp);
temp = NULL;
}
//Render the texts
mTexts[10].renderRectangle(mRenderer, userRect, 1);
mTexts[10].renderTexts(mRenderer, userRect, textImage, 1.5);
}
break;
case SDL_MOUSEMOTION:
//Render Exit and restore back to white color
mBack.renderEvent(mRenderer);
SDL_SetRenderDrawColor(mRenderer, 255, 255, 255, SDL_ALPHA_OPAQUE);
case SDL_MOUSEBUTTONDOWN://Bug is around here
if(mBack.getMouseEvent(&event) == ButtonState::BUTTON_MOUSE_DOWN)
{
exit = stop = true;
}
break;
}
}
// Present the screen
SDL_RenderPresent(mRenderer);
}
if(name.length() <= 1)
name = "unknown";
mUserName = name;
SDL_StopTextInput();
//Reset to Black and Clear the rendarer
SDL_SetRenderDrawColor(mRenderer, BlackColor.r, BlackColor.g, BlackColor.b, BlackColor.a);
SDL_RenderClear(mRenderer);
if(userEntered)
return Buttons::LEVEL;
else if(exit)
return Buttons::HOME;
else
return Buttons::CLOSE;
}
Buttons Sudoku::Sudoku::levels()
{
//load bg image
mImage();
//Display Back Button
backButton();
//Where to go
bool easy = false, normal = false, hard = false, exit = false;
int x, y;
const int numOfButton = 3;
const int buttonHeight = mGridHeight / 10;
const int buttonWidth = mGridWidth / 2 + 10;
const int gap = buttonHeight / 2;
x = buttonHeight;
y = (mGridHeight - numOfButton * buttonHeight - (numOfButton-1) * gap) / 2;
bool draw = 0;
//Load EASY, MEDIUM and HARD textures
SDL_Texture* texture[numOfButton];
loadTexture(texture[0], "EASY", GrayColor);
loadTexture(texture[1], "MEDIUM", GrayColor);
loadTexture(texture[2], "HARD", GrayColor);
//we already used the mtexts upto 10
int n = 11;
// Display the Buttons
for(int i = 0; i<numOfButton; i++)
{
SDL_Rect buttonRect = { x, y, buttonWidth, buttonHeight };
mTexts[n].renderTexts(mRenderer, buttonRect, texture[i]);
mRectangle.renderRectangle(mRenderer, buttonRect, draw);
y += buttonHeight + gap;
n++;
}
SDL_Event event;
bool stop = false;
while (!stop) {
// Handle events on queue
while (SDL_PollEvent(&event) != 0) {
exit = false;
// Handle quitting and completion
if (event.type == SDL_QUIT)
{
// Set stop flag
stop = true;
}
if(mTexts[11].getMouseEvent(&event) == ButtonState::BUTTON_MOUSE_DOWN)
{
easy = stop = true;
}
else if(mTexts[12].getMouseEvent(&event) == ButtonState::BUTTON_MOUSE_DOWN)
{
normal = stop = true;
}
else if(mTexts[13].getMouseEvent(&event) == ButtonState::BUTTON_MOUSE_DOWN)
{
hard = stop = true;
}
else if(mBack.getMouseEvent(&event) == ButtonState::BUTTON_MOUSE_DOWN)
{
exit = stop = true;
}
mBack.renderEvent(mRenderer);
for(int i = 0; i<numOfButton; i++)
{
mTexts[11+i].renderEvent(mRenderer);
}
}
// Update the screen
SDL_SetRenderDrawColor(mRenderer, BlackColor.r, BlackColor.g, BlackColor.b, BlackColor.a);
SDL_RenderPresent(mRenderer);
}
//Clear the rendarer
SDL_RenderClear(mRenderer);
if(easy)
return Buttons::EASY;
else if(normal)
return Buttons::MEDIUM;
else if(hard)
return Buttons::HARD;
else if(exit)
return Buttons::HOME;
else
return Buttons::CLOSE;
}