9 #include "game/level/platforms.h"
10 #include "game/level/player.h"
11 #include "game/sound_samples.h"
12 #include "game/sprite_font.h"
13 #include "math/extrema.h"
14 #include "math/point.h"
15 #include "sdl/renderer.h"
16 #include "system/log.h"
17 #include "system/lt.h"
19 #define SCREEN_WIDTH 800
20 #define SCREEN_HEIGHT 600
22 static void print_usage(FILE *stream)
24 fprintf(stream, "Usage: nothing [--fps <fps>]\n");
27 int main(int argc, char *argv[])
29 srand((unsigned int) time(NULL));
35 for (int i = 1; i < argc;) {
36 if (strcmp(argv[i], "--fps") == 0) {
38 if (sscanf(argv[i + 1], "%d", &fps) == 0) {
39 log_fail("Cannot parse FPS: %s is not a number\n", argv[i + 1]);
45 log_fail("Value of FPS is not provided\n");
50 log_fail("Unknown flag %s\n", argv[i]);
56 if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
57 log_fail("Could not initialize SDL: %s\n", SDL_GetError());
60 PUSH_LT(lt, 42, SDL_Quit);
62 SDL_ShowCursor(SDL_DISABLE);
64 SDL_Window *const window = PUSH_LT(
69 SCREEN_WIDTH, SCREEN_HEIGHT,
70 SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE),
74 log_fail("Could not create SDL window: %s\n", SDL_GetError());
78 SDL_Renderer *const renderer = PUSH_LT(
80 SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC),
82 if (renderer == NULL) {
83 log_fail("Could not create SDL renderer: %s\n", SDL_GetError());
86 if (SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND) < 0) {
87 log_fail("Could not set up blending mode for the renderer: %s\n", SDL_GetError());
91 SDL_Joystick *the_stick_of_joy = NULL;
93 if (SDL_NumJoysticks() > 0) {
94 the_stick_of_joy = PUSH_LT(lt, SDL_JoystickOpen(0), SDL_JoystickClose);
96 if (the_stick_of_joy == NULL) {
97 log_fail("Could not open 0th Stick of the Joy: %s\n", SDL_GetError());
101 log_info("Opened Joystick 0\n");
102 log_info("Name: %s\n", SDL_JoystickNameForIndex(0));
103 log_info("Number of Axes: %d\n", SDL_JoystickNumAxes(the_stick_of_joy));
104 log_info("Number of Buttons: %d\n", SDL_JoystickNumButtons(the_stick_of_joy));
105 log_info("Number of Balls: %d\n", SDL_JoystickNumBalls(the_stick_of_joy));
107 SDL_JoystickEventState(SDL_ENABLE);
109 log_warn("Could not find any Sticks of the Joy\n");
112 // ------------------------------
114 const char * sound_sample_files[] = {
115 "./sounds/nothing.wav",
116 "./sounds/something.wav"
118 const size_t sound_sample_files_count = sizeof(sound_sample_files) / sizeof(char*);
120 Game *const game = PUSH_LT(
125 sound_sample_files_count,
132 const Uint8 *const keyboard_state = SDL_GetKeyboardState(NULL);
136 const int64_t delta_time = (int64_t) roundf(1000.0f / 60.0f);
137 int64_t render_timer = (int64_t) roundf(1000.0f / (float) fps);
138 while (!game_over_check(game)) {
139 const int64_t begin_frame_time = (int64_t) SDL_GetTicks();
141 while (!game_over_check(game) && SDL_PollEvent(&e)) {
142 if (game_event(game, &e) < 0) {
147 if (game_input(game, keyboard_state, the_stick_of_joy) < 0) {
151 if (game_update(game, (float) delta_time * 0.001f) < 0) {
155 if (game_sound(game) < 0) {
159 render_timer -= delta_time;
160 if (render_timer <= 0) {
161 if (game_render(game) < 0) {
164 SDL_RenderPresent(renderer);
165 render_timer = (int64_t) roundf(1000.0f / (float) fps);
168 const int64_t end_frame_time = (int64_t) SDL_GetTicks();
169 SDL_Delay((unsigned int) max_int64(10, delta_time - (end_frame_time - begin_frame_time)));