-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
141 lines (127 loc) · 4.99 KB
/
main.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
#include "game.h"
#include "board.h"
#include "LButton.h"
#include "LTimer.h"
#include "LevelSelect.h"
#include "LMusic.h"
SDL_Window* gWindow = NULL;
SDL_Renderer* gRenderer = NULL;
TTF_Font* gFont = NULL;
Board gBoard;
LButton gPlayButton;
LButton gReplayButton;
LButton gMenuButton;
LButton gPauseButton;
LButton gContinueButton;
LTimer gTimer;
LevelSelect gLevelSelect;
LTexture gClickToContinue;
LTexture gTitleScreen;
LTexture gLoseScreen;
LTexture gWinScreen;
LMusic gMusic;
int gameState = MAIN_MENU;
int main(int argc, char** args) {
srand(time(0));
if (!init()) {
cout << "Fail to initialize!" << endl;
}
else {
if (!loadMedia()) {
cout << "Fail to load media!" << endl;
}
else {
bool boardIsGenerated = false;
bool quit = false;
SDL_Event e;
while (!quit) {
if (SDL_PollEvent(&e) != 0) {
if (e.type == SDL_QUIT) {
quit = true;
}
switch (gameState) {
case MAIN_MENU:
gPlayButton.handleEvent(e);
break;
case LEVEL_SELECT:
gLevelSelect.handleEvent(e);
break;
case PLAYING:
gBoard.handleEvent(e);
gPauseButton.handleEvent(e);
break;
case PAUSING:
gContinueButton.handleEvent(e);
gMenuButton.handleEvent(e);
break;
case REVEAL_BOMB:
if (e.type == SDL_MOUSEBUTTONDOWN) gameState = END_SCREEN;
case END_SCREEN:
gReplayButton.handleEvent(e);
gMenuButton.handleEvent(e);
break;
}
}
SDL_SetRenderDrawColor(gRenderer, 75, 75, 75, SDL_ALPHA_OPAQUE);
SDL_RenderClear(gRenderer);
switch (gameState) {
case MAIN_MENU:
boardIsGenerated = false;
if (gTimer.isStarted()) gTimer.stop();
gTitleScreen.render(0, 0);
gPlayButton.render();
gMusic.playOpeningTheme();
break;
case LEVEL_SELECT:
gLevelSelect.renderLevelSelectScreen();
break;
case PLAYING:
if (!boardIsGenerated) {
gBoard.generateBoard();
boardIsGenerated = true;
gTimer.stop();
}
else {
if (!gTimer.isStarted()) gTimer.start();
else if (gTimer.isPaused()) gTimer.unpause();
gBoard.renderBoard();
gTimer.render((TOP_LEFT_BOARD_X - 1 - gTimer.getTextureWidth())/2, 250);
gPauseButton.render();
gMusic.stopMusic();
}
if (gBoard.getBombRemaining() == 0 && gBoard.checkFlagPlaced()) {
waitHalfSecond();
gameState = END_SCREEN;
gTimer.pause();
boardIsGenerated = false;
}
else if (gBoard.bombActivated()) {
gameState = REVEAL_BOMB;
gTimer.pause();
boardIsGenerated = false;
}
break;
case PAUSING:
if (!gTimer.isPaused()) gTimer.pause();
gContinueButton.render();
gMenuButton.render();
break;
case REVEAL_BOMB:
gBoard.revealBomb();
gClickToContinue.render(SCREEN_WIDTH - gClickToContinue.getWidth(), (SCREEN_HEIGHT - gClickToContinue.getHeight())/2);
gTimer.render((TOP_LEFT_BOARD_X - 1 - gTimer.getTextureWidth())/2, 250);
break;
case END_SCREEN:
if (gBoard.bombActivated()) {gLoseScreen.render(0, 0); gMusic.playLoseTheme();}
else {gWinScreen.render(0, 0); gMusic.playWinTheme(); gTimer.render((SCREEN_WIDTH - gTimer.getTextureWidth())/2, 220);}
gReplayButton.render();
gMenuButton.render();
break;
}
SDL_RenderPresent(gRenderer);
}
}
}
close();
return 0;
}