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>] <level-folder>\n");
27 int main(int argc, char *argv[])
29 srand((unsigned int) time(NULL));
33 char *level_folder = 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 level_folder = argv[i];
56 if (level_folder == NULL) {
57 log_fail("Path to level file is not provided\n");
62 if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
63 log_fail("Could not initialize SDL: %s\n", SDL_GetError());
66 PUSH_LT(lt, 42, SDL_Quit);
68 SDL_ShowCursor(SDL_DISABLE);
70 SDL_Window *const window = PUSH_LT(
75 SCREEN_WIDTH, SCREEN_HEIGHT,
76 SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE),
80 log_fail("Could not create SDL window: %s\n", SDL_GetError());
84 SDL_Renderer *const renderer = PUSH_LT(
86 SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC),
88 if (renderer == NULL) {
89 log_fail("Could not create SDL renderer: %s\n", SDL_GetError());
92 if (SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND) < 0) {
93 log_fail("Could not set up blending mode for the renderer: %s\n", SDL_GetError());
97 SDL_Joystick *the_stick_of_joy = NULL;
99 if (SDL_NumJoysticks() > 0) {
100 the_stick_of_joy = PUSH_LT(lt, SDL_JoystickOpen(0), SDL_JoystickClose);
102 if (the_stick_of_joy == NULL) {
103 log_fail("Could not open 0th Stick of the Joy: %s\n", SDL_GetError());
107 log_info("Opened Joystick 0\n");
108 log_info("Name: %s\n", SDL_JoystickNameForIndex(0));
109 log_info("Number of Axes: %d\n", SDL_JoystickNumAxes(the_stick_of_joy));
110 log_info("Number of Buttons: %d\n", SDL_JoystickNumButtons(the_stick_of_joy));
111 log_info("Number of Balls: %d\n", SDL_JoystickNumBalls(the_stick_of_joy));
113 SDL_JoystickEventState(SDL_ENABLE);
115 log_warn("Could not find any Sticks of the Joy\n");
118 // ------------------------------
120 const char * sound_sample_files[] = {
121 "./sounds/nothing.wav",
122 "./sounds/something.wav"
124 const size_t sound_sample_files_count = sizeof(sound_sample_files) / sizeof(char*);
126 Game *const game = PUSH_LT(
131 sound_sample_files_count,
138 const Uint8 *const keyboard_state = SDL_GetKeyboardState(NULL);
142 const int64_t delta_time = (int64_t) roundf(1000.0f / 60.0f);
143 int64_t render_timer = (int64_t) roundf(1000.0f / (float) fps);
144 while (!game_over_check(game)) {
145 const int64_t begin_frame_time = (int64_t) SDL_GetTicks();
147 while (!game_over_check(game) && SDL_PollEvent(&e)) {
148 if (game_event(game, &e) < 0) {
153 if (game_input(game, keyboard_state, the_stick_of_joy) < 0) {
157 if (game_update(game, (float) delta_time * 0.001f) < 0) {
161 if (game_sound(game) < 0) {
165 render_timer -= delta_time;
166 if (render_timer <= 0) {
167 if (game_render(game) < 0) {
170 SDL_RenderPresent(renderer);
171 render_timer = (int64_t) roundf(1000.0f / (float) fps);
174 const int64_t end_frame_time = (int64_t) SDL_GetTicks();
175 SDL_Delay((unsigned int) max_int64(10, delta_time - (end_frame_time - begin_frame_time)));