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
|
#include "main.h"
#include <SDL2/SDL_rect.h>
#include <SDL2/SDL_timer.h>
#include <stdio.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include "player.h"
#include "input.h"
#include "characters.h"
#define DEFAULT_SCREEN_WIDTH 800
#define DEFAULT_SCREEN_HEIGHT 600
#define SCREEN_FPS 60
#define DELTA_TIME_SEC (1.0f / SCREEN_FPS)
#define DELTA_TIME_MS ((Uint32)floorf(DELTA_TIME_SEC * 1000.0f))
#define SIMULATION_STEP_MS 10
#define RENDER_ENABLED 1
#define SHOW_FPS 1
int main()
{
SDL_Window *window;
SDL_Renderer *renderer;
/* SDL_Texture *fps_texture; */
TTF_Font *Sans;
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
fprintf(stderr, "SDL ERROR: %s\n", SDL_GetError());
exit(1);
}
window =
SDL_CreateWindow("Space Battle", 100, 100, DEFAULT_SCREEN_WIDTH,
DEFAULT_SCREEN_HEIGHT, SDL_WINDOW_RESIZABLE);
if (window == NULL) {
fprintf(stderr, "COULD NOT CREATE SDL WINDOW: %s\n",
SDL_GetError());
exit(1);
}
if (RENDER_ENABLED) {
renderer = SDL_CreateRenderer(window, -1,
SDL_RENDERER_ACCELERATED);
if (renderer == NULL) {
fprintf(stderr, "COULD NOT CREATE SDL RENDERER: %s\n",
SDL_GetError());
exit(1);
}
TTF_Init();
Sans = TTF_OpenFont("resources/OpenSans-Regular.ttf", 12);
if (!Sans) {
fprintf(stderr, "COULD NOT OPEN FONT: %s\n",
SDL_GetError());
exit(1);
}
}
// TODO: outsource shape definitions
SDL_Vertex vert[3];
// center
vert[0].position.x = 400;
vert[0].position.y = 0;
vert[0].color.r = 255;
vert[0].color.g = 0;
vert[0].color.b = 0;
vert[0].color.a = 255;
// left
vert[1].position.x = 200;
vert[1].position.y = 450;
vert[1].color.r = 0;
vert[1].color.g = 0;
vert[1].color.b = 255;
vert[1].color.a = 255;
// right
vert[2].position.x = 600;
vert[2].position.y = 450;
vert[2].color.r = 0;
vert[2].color.g = 255;
vert[2].color.b = 0;
vert[2].color.a = 255;
struct Player player_1 = TEMPLATE_1;
player_1.geometry = vert;
player_1.geometry_len = 3;
int action_states[ACTION_STATE_COUNT];
for (int i = 0; i < ACTION_STATE_COUNT; i++) {
action_states[i] = 0;
}
int win_x = DEFAULT_SCREEN_WIDTH;
int win_y = DEFAULT_SCREEN_HEIGHT;
int quit = 0;
// based on:
// https://gafferongames.com/post/fix_your_timestep/
// doesn't include state-interpolation (yet?)
double physics_total_time = 0.0;
const double physics_timestep = 1 / 60.0;
const double render_timestep = 1 / 60.0;
double current_time = current_sdl_time_in_seconds();
double physics_time_accumulator = 0.0;
double render_time_accumulator = 0.0;
float fps = 0.0f;
char fps_text[123] = "start";
while (!quit) {
double new_time = current_sdl_time_in_seconds();
double frame_time = new_time - current_time;
current_time = new_time;
physics_time_accumulator += frame_time;
render_time_accumulator += frame_time;
while (physics_time_accumulator >= physics_timestep) {
SDL_Event event;
while (!quit && SDL_PollEvent(&event)) {
/* // e.g. https://stackoverflow.com/a/1252996 */
handleKeyboardEvent(action_states, event);
if (action_states[ACTION_QUIT])
quit = 1;
}
execute_actions(&player_1, action_states,
physics_timestep);
/* player_move(&player_1, physics_timestep); */
physics_time_accumulator -= physics_timestep;
physics_total_time += physics_timestep;
}
if (RENDER_ENABLED &&
render_time_accumulator >= render_timestep) {
/* SDL_SetRenderDrawColor(renderer, HEX_COLOR(BACKGROUND_COLOR)); */
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
SDL_RenderClear(renderer);
/* render_my_stuff(renderer); */
/* hexagon_draw(&hex, renderer); */
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
/* SDL_Rect box = { player_1.pos_x, player_1.pos_y, */
/* player_1.pos_x + 200, */
/* player_1.pos_y + 200 }; */
/* SDL_RenderFillRect(renderer, &box); */
/* SDL_RenderDrawLine(renderer, 0, 0, 1000, 1000); */
SDL_RenderGeometry(renderer, NULL, player_1.geometry,
player_1.geometry_len, NULL, 0);
SDL_GetWindowSize(window, &win_x, &win_y);
if (SHOW_FPS) {
fps = round(1.0f / render_time_accumulator);
sprintf(fps_text, "FPS:%.0f", fps);
SDL_Rect fps_pos = { 0, 0, 0, 0 };
render_text(fps_text, fps_pos, Sans, renderer);
// show action states
if (action_states[ACTION_MOVE_UP]) {
SDL_Rect up_text = { 100, 600, 120,
620 };
render_text("UP", up_text, Sans,
renderer);
}
if (action_states[ACTION_MOVE_DOWN]) {
SDL_Rect up_text = { 100, 620, 120,
640 };
render_text("DOWN", up_text, Sans,
renderer);
}
if (action_states[ACTION_MOVE_LEFT]) {
SDL_Rect up_text = { 60, 620, 80, 640 };
render_text("LEFT", up_text, Sans,
renderer);
}
if (action_states[ACTION_MOVE_RIGHT]) {
SDL_Rect up_text = { 140, 620, 160,
640 };
render_text("RIGHT", up_text, Sans,
renderer);
}
// show player attributes
char x_speed_text[123];
sprintf(x_speed_text, "x-speed: %f",
player_1.vel_x);
SDL_Rect x_speed_text_pos = { 900, 900, 910,
910 };
render_text(x_speed_text, x_speed_text_pos,
Sans, renderer);
char y_speed_text[123];
sprintf(y_speed_text, "y-speed: %f",
player_1.vel_y);
SDL_Rect y_speed_text_pos = { 900, 920, 910,
930 };
render_text(y_speed_text, y_speed_text_pos,
Sans, renderer);
}
render_time_accumulator = 0;
SDL_RenderPresent(renderer);
}
}
if (RENDER_ENABLED) {
TTF_CloseFont(Sans);
TTF_Quit();
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
}
SDL_Quit();
return 0;
}
void execute_actions(struct Player *player, int *action_states, float delta)
{
if (action_states[ACTION_MOVE_UP]) {
player_move(player, UP, delta);
}
if (action_states[ACTION_MOVE_DOWN]) {
player_move(player, DOWN, delta);
}
if (action_states[ACTION_MOVE_LEFT]) {
player_move(player, LEFT, delta);
}
if (action_states[ACTION_MOVE_RIGHT]) {
player_move(player, RIGHT, delta);
}
}
void render_text(char *text, SDL_Rect dest, TTF_Font *Sans,
SDL_Renderer *renderer)
{
SDL_Color fps_color = { 0, 255, 0, 255 };
SDL_Surface *surf = TTF_RenderText_Solid(Sans, text, fps_color);
dest.w = surf->w;
dest.h = surf->h;
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surf);
SDL_RenderCopy(renderer, texture, NULL, &dest);
SDL_DestroyTexture(texture);
SDL_FreeSurface(surf);
}
double current_sdl_time_in_seconds()
{
return ((double)SDL_GetPerformanceCounter() /
(double)SDL_GetPerformanceFrequency());
}
|