-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
185 lines (163 loc) · 4.2 KB
/
main.c
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
/**
@file main.c
@author Alejandro Ambroa ([email protected])
@date 1 Oct 2017
@brief Pong game in three dimensions. Inspired in pong game by Liquid Media (http://www.liquid.se/pong/).
*/
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#ifdef _WINDOWS
#include <windows.h>
#endif
#include "geometry.h"
#include "msys.h"
#include "pong3d.h"
#include "renderer.h"
#include "screens.h"
#include "sound.h"
#include "tasks.h"
#include "text.h"
void run_game();
void init_game();
int process_state(int, int, SysEvent* event);
void cleanup();
int process_events_task(SysEvent* event);
#ifdef _WINDOWS
INT CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, INT nCmdShow)
{
#else
int main(int argc, char** argv)
{
#endif
if (sys_init_video(WINDOW_WIDTH, WINDOW_HEIGHT) < 0) {
cleanup();
exit(1000);
}
if (init_sound(SAMPLE_RATE) < 0) {
log_error("Couldn't initialize sound device. The game will run without sound :(");
}
if (init_renderer(WINDOW_WIDTH, WINDOW_HEIGHT) < 0) {
cleanup();
exit(1002);
}
if (init_text_renderer() < 0) {
cleanup();
exit(1003);
}
create_elements(WINDOW_WIDTH, WINDOW_HEIGHT, STAGE_BLOCKS);
init_screens();
run_game();
cleanup();
return 0;
}
void cleanup()
{
dispose_elements();
dispose_renderer();
dispose_text_renderer();
sys_dispose_video();
dispose_sound();
sys_quit();
}
void init_game()
{
balls = BALLS;
reset_player_stick_position();
reset_opponent_stick_position();
reset_ball_position();
player_score = 0;
opponent_score = 0;
}
void run_game()
{
int framesElapsed = 0;
unsigned int startTime, elapsedTime;
unsigned int period = (unsigned int)(1000.0 / FPS);
int pendingEvent = 0;
SysEvent event;
int reset_frames_counter = 0;
int wait_time = 0;
int time_last_frame = startTime = sys_get_ticks();
GAME_STATE currentState = STARTING;
change_state(STARTING);
// Game loop.
while (gameState != EXIT) {
startTime = sys_get_ticks();
if (pendingEvent) {
process_events_task(&event);
}
if (currentState != gameState || reset_frames_counter) {
framesElapsed = 0;
currentState = gameState;
}
reset_frames_counter = process_state(framesElapsed, pendingEvent, &event);
render();
framesElapsed++;
time_last_frame = sys_get_ticks();
elapsedTime = time_last_frame - startTime;
wait_time = period - elapsedTime;
if (wait_time < 0) {
wait_time = 0;
}
pendingEvent = sys_wait(&event, wait_time);
}
}
int process_state(int elapsedFrames, int pendingEvent, SysEvent* event)
{
int reset_frames = 1;
switch (gameState) {
case STARTING:
reset_frames = start_screen_task(elapsedFrames);
break;
case LOADING_PLAYERS:
reset_frames = loading_players_task(elapsedFrames);
break;
case PLAYER_SERVICE:
reset_frames = player_service_task(elapsedFrames, pendingEvent, event);
break;
case PLAYER_RETURN:
case OPP_RETURN:
reset_frames = playing_task(elapsedFrames);
break;
case PLAYER_WINS:
reset_frames = player_wins_task(elapsedFrames);
break;
case OPP_WINS:
reset_frames = opponent_wins_task(elapsedFrames);
break;
case OPP_SERVICE:
reset_frames = opponent_service_task(elapsedFrames);
break;
case FINISHED:
reset_frames = finished_task(elapsedFrames);
break;
case EXIT:
break;
case STARTED:
break;
}
return reset_frames;
}
int process_events_task(SysEvent* event)
{
switch (event->type) {
case CLOSE:
change_state(EXIT);
break;
case MOUSELBUTTONUP:
if (gameState == STARTING || gameState == FINISHED) {
change_state(LOADING_PLAYERS);
} else if (gameState == PLAYER_SERVICE && ball_in_player_stick()) {
play_player_pong_sound();
change_state(PLAYER_RETURN);
}
break;
case MOUSEMOTION:
break;
}
return 0;
}