Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
328 lines (260 loc) · 6.19 KB
/
Copy pathmain.cpp
File metadata and controls
328 lines (260 loc) · 6.19 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
//The software was written by me, with references ideas from :
//Reference source 1 : LazyFoo's Tutorial (initSDL, Game-Loop, Texture load, Player with SpriteSheet load, Musics, Text)
//Reference source 2 : Phattrienphanmem123az's Tutorial (Map algorithm, Collision Detection)
//Reference source 3 : Tran Thi Minh Chau's Tutorial (Way to organize, sort .h and .cpp files)
//05 - 2020
// AquaDig Remake 1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <SDL.h>
#include <SDL_image.h>
#include <iostream>
#include <string>
using namespace std;
#include "SDL_CommonFunc.h"
#include "SDL_BaseObj.h"
#include "SDL_OceanMap.h"
#include "SDL_Player.h"
#include "SDL_DisplayText.h"
//Load Icon
SDL_Surface* icon = IMG_Load("icon/AquaDigRe.png");
//Create Background
BaseObject gBackground;
//Fonts
TTF_Font* countdownTime = NULL;
//Free Musics
void FreeMusic()
{
if (gSoundCoin != NULL)
{
Mix_FreeChunk(gSoundCoin);
gSoundCoin = NULL;
}
}
//Free Theme Musics
void FreeTheme()
{
if (gSoundTheme != NULL)
{
Mix_FreeMusic(gSoundTheme);
gSoundTheme = NULL;
}
}
//Init SDL
bool initSDL()
{
bool success = true;
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
cout << "SDL Error " << SDL_GetError() << endl;
success = false;
}
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1");
gWindow = SDL_CreateWindow( "AquaDig Remake v1.0", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (gWindow == NULL)
{
cout << "SDL Error " << SDL_GetError() << endl;
success = false;
}
else
{
gScreen = SDL_CreateRenderer(gWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); //Prevent overloading CPU & GPU. DS:04.29.20
if (gScreen == NULL) //important!!! Lack of "=" symbol can make this code error. DS:04.15.20
{
cout << "SDL Error " << SDL_GetError() << endl;
success = false;
}
else
{
SDL_SetRenderDrawColor(gScreen, 255, 255, 255, 255);
int imgFlags = IMG_INIT_PNG;
if (!(IMG_Init(imgFlags) && imgFlags))
{
success = false;
}
//sound
if (Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, 2, 4096) < 0)
{
success = false;
}
FreeTheme();
gSoundTheme = Mix_LoadMUS("Music/ThemeSound.wav");
FreeMusic();
gSoundCoin = Mix_LoadWAV("Music/CollectMoney.wav");
if (TTF_Init() == -1)
{
success = false;
}
countdownTime = TTF_OpenFont("font/PixelFont.ttf", 13);
if (countdownTime == NULL)
{
success = false;
}
}
}
return success;
}
bool loadBackground()
{
bool test = gBackground.LoadIMG("Image/Background.png", gScreen);
if (test == false)
{
cout << "SDL Error " << SDL_GetError() << endl;
return false;
}
return true;
}
void loadAndRenderMenu(){
BaseObject Menu;
bool test = Menu.LoadIMG("Image/Menu.png", gScreen);
if (test == false)
{
cout << "SDL Error " << SDL_GetError() << endl;
}
Menu.Render(gScreen, NULL);
SDL_RenderPresent(gScreen);
SDL_Delay(5000);
}
void loadGameOver(){
BaseObject GameOver;
bool test = GameOver.LoadIMG("Image/GameOver.png", gScreen);
if (test == false)
{
cout << "SDL Error " << SDL_GetError() << endl;
}
GameOver.Render(gScreen, NULL);
SDL_RenderPresent(gScreen);
SDL_Delay(2000);
}
void loadFinal(bool &quitSDL){
BaseObject Final;
bool test = Final.LoadIMG("Image/Final.png", gScreen);
if (test == false)
{
cout << "SDL Error " << SDL_GetError() << endl;
}
Final.Render(gScreen, NULL);
SDL_RenderPresent(gScreen);
SDL_Delay(5000);
quitSDL = true;
}
void loadIcon()
{
SDL_SetWindowIcon(gWindow, icon);
}
void close()
{
gBackground.Free();
SDL_DestroyRenderer(gScreen);
gScreen = NULL;
SDL_DestroyWindow(gWindow);
gWindow = NULL;
IMG_Quit();
SDL_Quit();
}
Uint32 playGame(bool &playAgain, Uint32 totalTime)
{
bool quitSDL = false;
loadIcon();
loadAndRenderMenu();
loadBackground();
//play theme
Mix_PlayMusic(gSoundTheme, -1);
//load map
OceanMap game_map;
game_map.LoadMap("map/map01.dat");
game_map.loadTiles(gScreen);
//load player
MainPlayer player_;
player_.LoadIMG("Image/Player/player_right.png", gScreen);
player_.SetClips();
//show text
TextShow game_point;
game_point.SetColor(TextShow::YELLOW_TEXT);
//time countdown
TextShow game_time;
game_time.SetColor(TextShow::WHITE_TEXT);
Uint32 prevMoney = 0;
Uint32 startTime = 185;
Uint32 timeVal;
SDL_Event gEvent;
while (!quitSDL)
{
while (SDL_PollEvent(&gEvent) != 0)
{
if (gEvent.type == SDL_QUIT)
{
quitSDL = true;
}
player_.KeyboardAction(gEvent, gScreen);
}
SDL_SetRenderDrawColor(gScreen, 255, 255, 255, 255);
SDL_RenderClear(gScreen);
gBackground.Render(gScreen, NULL);
// game_map.DrawMap(gScreen);
Map mapData = game_map.getMap();
player_.SetMapView(mapData.startX, mapData.startY);
player_.MovePlayer(mapData, gSoundCoin, quitSDL);
player_.Show(gScreen);
game_map.SetMap(mapData);
game_map.DrawMap(gScreen, gSoundTheme);
//point
string pointStr = "MONEY: ";
int moneyCount = player_.GetCoinValue();
std::string moneyStr = std::to_string(moneyCount);
pointStr += moneyStr;
game_point.SetText(pointStr);
game_point.LoadText(countdownTime, gScreen);
game_point.RenderText(gScreen, SCREEN_WIDTH - 170, 15);
//time
string timeStr = "TIME LEFT: ";
timeVal = SDL_GetTicks() / 1000 - totalTime;
Uint32 valTime = startTime - timeVal;
if (moneyCount - prevMoney >= 50)
{
prevMoney += moneyCount - prevMoney;
startTime += 10;
}
else
{
if (valTime <= 15)
{
game_time.SetColor(TextShow::RED_TEXT);
}
if (valTime <= 0)
{
loadGameOver();
playAgain = true;
quitSDL = true;
// break;
}
std::string strVal = std::to_string(valTime);
timeStr += strVal;
game_time.SetText(timeStr);
game_time.LoadText(countdownTime, gScreen);
game_time.RenderText(gScreen, SCREEN_WIDTH - 170, 50);
}
SDL_RenderPresent(gScreen);
}
cout << timeVal << endl;
return timeVal;
}
int main(int argc, char* argv[])
{
if (initSDL() == false)
{
cout << "SDL Error " << SDL_GetError() << endl;
return -1;
}
//Game Loop
Uint32 totalTime = 0;
bool playAgain = true;
while (playAgain) {
playAgain = false;
totalTime += playGame(playAgain, totalTime);
cout << totalTime << endl;
}
close();
return 0;
}