10 #include "game/level/platforms.h"
11 #include "game/level/player.h"
12 #include "game/sound_samples.h"
13 #include "game/sprite_font.h"
14 #include "math/extrema.h"
16 #include "sdl/renderer.h"
17 #include "system/log.h"
18 #include "system/lt.h"
20 #define SCREEN_WIDTH 800
21 #define SCREEN_HEIGHT 600
23 static void print_usage(FILE *stream)
25 fprintf(stream, "Usage: nothing [--fps <fps>]\n");
28 int main(int argc, char *argv[])
30 srand((unsigned int) time(NULL));
36 for (int i = 1; i < argc;) {
37 if (strcmp(argv[i], "--fps") == 0) {
39 if (sscanf(argv[i + 1], "%d", &fps) == 0) {
40 log_fail("Cannot parse FPS: %s is not a number\n", argv[i + 1]);
46 log_fail("Value of FPS is not provided\n");
51 log_fail("Unknown flag %s\n", argv[i]);
57 if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
58 log_fail("Could not initialize SDL: %s\n", SDL_GetError());
61 PUSH_LT(lt, 42, SDL_Quit);
63 setlocale(LC_NUMERIC, "C");
65 SDL_ShowCursor(SDL_DISABLE);
67 SDL_Window *const window = PUSH_LT(
72 SCREEN_WIDTH, SCREEN_HEIGHT,
73 SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE),
77 log_fail("Could not create SDL window: %s\n", SDL_GetError());
81 SDL_Renderer *const renderer = PUSH_LT(
83 SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC),
85 if (renderer == NULL) {
86 log_fail("Could not create SDL renderer: %s\n", SDL_GetError());
89 if (SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND) < 0) {
90 log_fail("Could not set up blending mode for the renderer: %s\n", SDL_GetError());
94 SDL_Joystick *the_stick_of_joy = NULL;
96 if (SDL_NumJoysticks() > 0) {
97 the_stick_of_joy = PUSH_LT(lt, SDL_JoystickOpen(0), SDL_JoystickClose);
99 if (the_stick_of_joy == NULL) {
100 log_fail("Could not open 0th Stick of the Joy: %s\n", SDL_GetError());
104 log_info("Opened Joystick 0\n");
105 log_info("Name: %s\n", SDL_JoystickNameForIndex(0));
106 log_info("Number of Axes: %d\n", SDL_JoystickNumAxes(the_stick_of_joy));
107 log_info("Number of Buttons: %d\n", SDL_JoystickNumButtons(the_stick_of_joy));
108 log_info("Number of Balls: %d\n", SDL_JoystickNumBalls(the_stick_of_joy));
110 SDL_JoystickEventState(SDL_ENABLE);
112 log_warn("Could not find any Sticks of the Joy\n");
115 // ------------------------------
117 const char * sound_sample_files[] = {
118 "./sounds/nothing.wav",
119 "./sounds/something.wav"
121 const size_t sound_sample_files_count = sizeof(sound_sample_files) / sizeof(char*);
123 Game *const game = PUSH_LT(
128 sound_sample_files_count,
135 const Uint8 *const keyboard_state = SDL_GetKeyboardState(NULL);
139 const int64_t delta_time = (int64_t) roundf(1000.0f / 60.0f);
140 int64_t render_timer = (int64_t) roundf(1000.0f / (float) fps);
141 while (!game_over_check(game)) {
142 const int64_t begin_frame_time = (int64_t) SDL_GetTicks();
144 while (!game_over_check(game) && SDL_PollEvent(&e)) {
145 if (game_event(game, &e) < 0) {
150 if (game_input(game, keyboard_state, the_stick_of_joy) < 0) {
154 if (game_update(game, (float) delta_time * 0.001f) < 0) {
158 if (game_sound(game) < 0) {
162 render_timer -= delta_time;
163 if (render_timer <= 0) {
164 if (game_render(game) < 0) {
167 SDL_RenderPresent(renderer);
168 render_timer = (int64_t) roundf(1000.0f / (float) fps);
171 const int64_t end_frame_time = (int64_t) SDL_GetTicks();
172 SDL_Delay((unsigned int) max_int64(10, delta_time - (end_frame_time - begin_frame_time)));