-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.cpp
149 lines (123 loc) · 4.72 KB
/
game.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
#include "game.h"
bool init() {
bool success = true;
if (SDL_Init(SDL_INIT_AUDIO | SDL_INIT_VIDEO) < 0) {
cout << "SDL could not be initialize! SDL Error: " << SDL_GetError() << endl;
success = false;
}
else {
gWindow = SDL_CreateWindow(GAME_TITLE, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (gWindow == NULL) {
cout << "Window could not be created! SDL Error: " << SDL_GetError() << endl;
success = false;
}
else {
gRenderer = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (gRenderer == NULL) {
cout << "Renderer could not be created! SDL Error: " << SDL_GetError() << endl;
success = false;
}
else {
SDL_SetRenderDrawColor(gRenderer, 255, 255, 255, 255);
int imgFlag = IMG_INIT_PNG;
if (!IMG_Init(imgFlag) & imgFlag) {
cout << "SDL_image could not be initialized! SDL_image Error: " << IMG_GetError() << endl;
success = false;
}
if (!TTF_Init() == -1) {
cout << "SDL_ttf could not be initialized! SDL_ttf Error: " << TTF_GetError() << endl;
success = false;
}
if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0) {
cout << "SDL_mixer could not be initialized! SDL_mixer Error: " << Mix_GetError() << endl;
success = false;
}
}
}
}
return success;
}
bool loadMedia() {
bool success = true;
if (!gBoard.loadTexture()) {
cout << "Fail to load board texture!" << endl;
success = false;
}
if (!gPlayButton.defineButton("media/play.png", "media/play1.png", (SCREEN_WIDTH - LONG_BUTTON_WIDTH)/2 + 1, (SCREEN_HEIGHT - LONG_BUTTON_HEIGHT)/2 + 1, LEVEL_SELECT)) {
cout << "Fail to define play button!" << endl;
success = false;
}
if (!gReplayButton.defineButton("media/replay.png", "media/replay1.png", (SCREEN_WIDTH - 3*LONG_BUTTON_WIDTH)/2 + 1, (SCREEN_HEIGHT - LONG_BUTTON_HEIGHT)/2 + 1, PLAYING)) {
cout << "Fail to define replay button!" << endl;
success = false;
}
if (!gMenuButton.defineButton("media/menu.png", "media/menu1.png", (SCREEN_WIDTH + LONG_BUTTON_WIDTH)/2 + 1, (SCREEN_HEIGHT - LONG_BUTTON_HEIGHT)/2 + 1, MAIN_MENU)) {
cout << "Fail to define menu button!" << endl;
success = false;
}
gFont = TTF_OpenFont("media/ITCKRIST.TTF", 45);
if (gFont == NULL) {
cout << "Fail to load font! SDL_ttf Error: " << TTF_GetError() << endl;
success = false;
}
if (!gLevelSelect.loadTexture()) {
cout << "Fail to load level select texture!" << endl;
success = false;
}
if (!gClickToContinue.loadFromFile("media/clickToContinue.png")) {
cout << "Fail to load clickToContinue texture!" << endl;
success = false;
}
if (!gTitleScreen.loadFromFile("media/title.png")) {
cout << "Fail to load title screen texture!" << endl;
success = false;
}
if (!gLoseScreen.loadFromFile("media/lose.png")) {
cout << "Fail to load lose screen texture!" << endl;
success = false;
}
if (!gWinScreen.loadFromFile("media/win.png")) {
cout << "Fail to load win screen texture!" << endl;
success = false;
}
if (!gPauseButton.defineButton("media/pause.png", "media/pause1.png", SCREEN_WIDTH - SQUARE_BUTTON_SIZE - 15, 15, PAUSING)) {
cout << "Fail to define pause button" << endl;
success = false;
}
if (!gContinueButton.defineButton("media/continue.png", "media/continue1.png", (SCREEN_WIDTH - 3*LONG_BUTTON_WIDTH)/2 + 1, (SCREEN_HEIGHT - LONG_BUTTON_HEIGHT)/2 + 1, PLAYING)) {
cout << "Fail to define continue button" << endl;
success = false;
}
if (!gMusic.loadMusic()) {
cout << "Fail to load music!" << endl;
success = false;
}
return success;
}
void close() {
gBoard.free();
gPlayButton.free();
gReplayButton.free();
gMenuButton.free();
gContinueButton.free();
gPauseButton.free();
gClickToContinue.free();
gTitleScreen.free();
gWinScreen.free();
gLoseScreen.free();
gLevelSelect.free();
gMusic.free();
SDL_DestroyWindow(gWindow);
SDL_DestroyRenderer(gRenderer);
gWindow = NULL;
gRenderer = NULL;
SDL_Quit();
TTF_Quit();
IMG_Quit();
Mix_Quit();
}
void waitHalfSecond() {
Uint32 startTick = SDL_GetTicks();
while (SDL_GetTicks() - startTick < 500) {
}
}