-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
279 lines (245 loc) · 7.6 KB
/
main.cpp
File metadata and controls
279 lines (245 loc) · 7.6 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
#define SDL_MAIN_HANDLED true
#include <SDL.h>
#include <SDL_audio.h>
#include <chrono>
#include <iostream>
#include <thread>
#include <unistd.h>
#include "player.h"
#include "enemy.h"
#include "Ball.h"
using namespace std::chrono_literals;
const bool UNLIMITED_FPS = true;
const int TARGET_FPS = 60;
int initialPlayerPositionX = 64;
int initialPlayerPositionY = 64 + 128;
int initialEnemyPositionX = 640 - 64 - 32;
int initialEnemyPositionY = 64 + 128;
int ballLarge = 16;
int ballTall = 16;
int initialBallPositionX = 320 - (ballLarge/2);
int initialBallPositionY = 256 - (ballTall/2);
int ballSpeedX = 8;
int ballSpeedY = 8;
bool ballDirection = false;
int point = 0;
int enemyPoint = 0;
// Sistema de áudio otimizado
SDL_AudioSpec wav_spec;
Uint8* wav_buffer = nullptr;
Uint32 wav_length = 0;
bool audioLoaded = false;
Ball ball(initialBallPositionX, initialBallPositionY);
Player player(initialPlayerPositionX, initialPlayerPositionY);
Enemy enemy(initialEnemyPositionX, initialEnemyPositionY, ball);
enum class state { start, gameover, playing, reset };
state STATE = state::start;
bool reset = false;
bool touchTheBall();
bool enemyTouchTheBall();
void ballsLogicTick(float deltaTime);
int loadSound();
void playSound();
Uint32 fpsLastTime = 0;
int fpsFrames = 0;
void printFPS() {
fpsFrames++;
if (SDL_GetTicks() - fpsLastTime >= 1000) {
printf("FPS: %d\n", fpsFrames);
fpsFrames = 0;
fpsLastTime = SDL_GetTicks();
}
}
void renderTick(SDL_Renderer *renderer, SDL_Window *window) {
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
player.PlayerRender(renderer, window);
enemy.EnemyRender(renderer, window);
ball.BallRender(renderer, window);
}
void logicTick(float deltaTime) {
if(STATE == state::start){
player.PlayerTick(deltaTime);
enemy.enemyXposition = initialEnemyPositionX;
enemy.enemyYposition = initialEnemyPositionY;
ball.ballX = initialBallPositionX;
ball.ballY = initialBallPositionY;
}
if(STATE == state::playing){
player.PlayerTick(deltaTime);
enemy.EnemyTick(ball, deltaTime);
ballsLogicTick(deltaTime);
}
}
std::string getCurrentWorkingDirectory() {
char cwd[1024];
if (getcwd(cwd, sizeof(cwd)) != NULL) {
return std::string(cwd);
} else {
return std::string("");
}
}
int loadSound(){
std::string currentDir = getCurrentWorkingDirectory();
std::string audioFilePath = currentDir + "/hit.wav";
if (SDL_LoadWAV(audioFilePath.c_str(), &wav_spec, &wav_buffer, &wav_length) == NULL) {
fprintf(stderr, "Could not open hit.wav: %s\n", SDL_GetError());
return 1;
}
if (SDL_OpenAudio(&wav_spec, NULL) < 0) {
fprintf(stderr, "SDL_OpenAudio failed: %s\n", SDL_GetError());
SDL_FreeWAV(wav_buffer);
return 1;
}
audioLoaded = true;
return 0;
}
void playSound(){
if(audioLoaded && wav_buffer != nullptr){
SDL_ClearQueuedAudio(1);
SDL_QueueAudio(1, wav_buffer, wav_length);
SDL_PauseAudio(0);
}
}
bool touchTheBall() {
int x1 = player.playerXposition;
int y1 = player.playerYposition;
int w1 = player.playerWidth;
int h1 = player.playerHeight;
int x2 = ball.ballX;
int y2 = ball.ballY;
int w2 = ball.ballWidth;
int h2 = ball.ballHeight;
if(x2 > x1 && x2 < x1 + w1){
if (y2 > y1 && y2 < y1 + h1) {
return true;
}
}
return false;
}
bool enemyTouchTheBall() {
int x1 = enemy.enemyXposition;
int y1 = enemy.enemyYposition;
int w1 = enemy.enemyWidth;
int h1 = enemy.enemyHeight;
int x2 = ball.ballX;
int y2 = ball.ballY;
int w2 = ball.ballWidth;
int h2 = ball.ballHeight;
if(x2 > x1-w2 && x2 < x1 + w1){
if (y2 > y1 && y2 < y1 + h1) {
return true;
}
}
return false;
}
void ballsLogicTick(float deltaTime) {
ball.ballX = ball.ballX + (ball.ballSpeedX * TARGET_FPS * deltaTime);
ball.ballY = ball.ballY + (ball.ballSpeedY * TARGET_FPS * deltaTime);
if(touchTheBall()){
ball.ballSpeedX = -ball.ballSpeedX;
ball.ballX = player.playerXposition + player.playerWidth + 1;
playSound();
}
if(enemyTouchTheBall()){
ball.ballSpeedX = -ball.ballSpeedX;
ball.ballX = enemy.enemyXposition - ball.ballWidth - 1;
playSound();
}
if(ball.ballY > 512 - ball.ballHeight){
ball.ballSpeedY = -ball.ballSpeedY;
ball.ballY = 512 - ball.ballHeight;
}
if(ball.ballY < 0){
ball.ballSpeedY = -ball.ballSpeedY;
ball.ballY = 0;
}
if(ball.ballX < 32){
enemyPoint++;
STATE = state::start;
printf("points: %d | enemy points: %d\n", point, enemyPoint);
}
if(ball.ballX > 640 - 64){
point++;
STATE = state::start;
printf("points: %d | enemy points: %d\n", point, enemyPoint);
}
}
int main(int argc, char** argv) {
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO);
if(!UNLIMITED_FPS){
SDL_SetHint(SDL_HINT_RENDER_VSYNC, "1");
} else {
SDL_SetHint(SDL_HINT_RENDER_VSYNC, "0");
}
SDL_Window* window = SDL_CreateWindow(
"pong",
0,
50,
640,
512,
SDL_WINDOW_RESIZABLE
);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
loadSound();
SDL_Event e;
fpsLastTime = SDL_GetTicks();
Uint32 lastTime = SDL_GetTicks();
for (;;){
Uint32 currentTime = SDL_GetTicks();
float deltaTime = (currentTime - lastTime) / 1000.0f;
lastTime = currentTime;
if(deltaTime > 0.25f) deltaTime = 0.016f;
if(deltaTime < 0.0001f) deltaTime = 0.0001f;
while (SDL_PollEvent(&e)){
switch(e.type){
case SDL_KEYDOWN:
if(e.key.keysym.sym == SDLK_UP || e.key.keysym.sym == SDLK_w){
player.Move(1);
}
if(e.key.keysym.sym == SDLK_DOWN || e.key.keysym.sym == SDLK_s){
player.Move(2);
}
if(e.key.keysym.sym == SDLK_SPACE){
if(STATE == state::start){
STATE = state::playing;
}else if(STATE == state::playing){
STATE = state::start;
}
}
if(e.key.keysym.sym == SDLK_TAB){
ballDirection = !ballDirection;
}
break;
case SDL_KEYUP:
if(e.key.keysym.sym == SDLK_UP || e.key.keysym.sym == SDLK_w){
player.StopMove(1);
}
if(e.key.keysym.sym == SDLK_DOWN || e.key.keysym.sym == SDLK_s){
player.StopMove(2);
}
break;
case SDL_QUIT:
if(wav_buffer) SDL_FreeWAV(wav_buffer);
SDL_CloseAudio();
return 0;
default:
break;
}
}
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
logicTick(deltaTime);
renderTick(renderer, window);
printFPS();
SDL_RenderPresent(renderer);
if(!UNLIMITED_FPS){
Uint32 frameTime = SDL_GetTicks() - currentTime;
if(frameTime < 1000 / TARGET_FPS){
SDL_Delay((1000 / TARGET_FPS) - frameTime);
}
}
}
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}