2 #include <SDL2/SDL_mixer.h>
3 #include <SDL2/SDL_ttf.h>
11 #include "game/level/platforms.h"
12 #include "game/level/player.h"
13 #include "game/sound_samples.h"
14 #include "math/minmax.h"
15 #include "math/point.h"
16 #include "sdl/renderer.h"
17 #include "system/error.h"
18 #include "system/lt.h"
19 #include "system/lt/lt_adapters.h"
21 #define SCREEN_WIDTH 800
22 #define SCREEN_HEIGHT 600
24 static void print_usage(FILE *stream)
26 fprintf(stream, "Usage: nothing [--fps <fps>] <level-file>\n");
29 int main(int argc, char *argv[])
31 srand((unsigned int) time(NULL));
33 lt_t *const lt = create_lt();
35 char *level_filename = NULL;
38 for (int i = 1; i < argc;) {
39 if (strcmp(argv[i], "--fps") == 0) {
41 if (sscanf(argv[i + 1], "%d", &fps) == 0) {
42 fprintf(stderr, "Cannot parse FPS: %s is not a number\n", argv[i + 1]);
48 fprintf(stderr, "Value of FPS is not provided\n");
53 level_filename = argv[i];
58 if (level_filename == NULL) {
59 fprintf(stderr, "Path to level file is not provided\n");
65 print_error_msg(ERROR_TYPE_SDL2_TTF, "Could not initialize SDL_ttf");
68 PUSH_LT(lt, 42, TTF_Quit_lt);
70 if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
71 print_error_msg(ERROR_TYPE_SDL2, "Could not initialize SDL");
74 PUSH_LT(lt, 42, SDL_Quit_lt);
76 SDL_ShowCursor(SDL_DISABLE);
78 SDL_Window *const window = PUSH_LT(
83 SCREEN_WIDTH, SCREEN_HEIGHT,
84 SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE),
88 print_error_msg(ERROR_TYPE_SDL2, "Could not create SDL window");
92 SDL_Renderer *const renderer = PUSH_LT(
94 SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC),
96 if (renderer == NULL) {
97 print_error_msg(ERROR_TYPE_SDL2, "Could not create SDL renderer");
100 if (SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND) < 0) {
101 print_error_msg(ERROR_TYPE_SDL2, "Could not set up blending mode for the renderer");
105 SDL_Joystick *the_stick_of_joy = NULL;
107 if (SDL_NumJoysticks() > 0) {
108 the_stick_of_joy = PUSH_LT(lt, SDL_JoystickOpen(0), SDL_JoystickClose);
110 if (the_stick_of_joy == NULL) {
111 print_error_msg(ERROR_TYPE_SDL2, "Could not open 0th Stick of the Joy: %s\n");
115 printf("Opened Joystick 0\n");
116 printf("Name: %s\n", SDL_JoystickNameForIndex(0));
117 printf("Number of Axes: %d\n", SDL_JoystickNumAxes(the_stick_of_joy));
118 printf("Number of Buttons: %d\n", SDL_JoystickNumButtons(the_stick_of_joy));
119 printf("Number of Balls: %d\n", SDL_JoystickNumBalls(the_stick_of_joy));
121 SDL_JoystickEventState(SDL_ENABLE);
123 fprintf(stderr, "[WARNING] Could not find any Sticks of the Joy\n");
127 MIX_DEFAULT_FREQUENCY,
131 print_error_msg(ERROR_TYPE_SDL2_MIXER, "Could not initialize the audio\n");
134 PUSH_LT(lt, 42, Mix_CloseAudio_lt);
136 // ------------------------------
138 const char * sound_sample_files[] = {
139 "./sounds/nothing.wav",
140 "./sounds/something.wav"
142 const size_t sound_sample_files_count = sizeof(sound_sample_files) / sizeof(char*);
144 game_t *const game = PUSH_LT(
149 sound_sample_files_count,
153 print_current_error_msg("Could not create the game object");
157 TTF_Font *const font = PUSH_LT(lt, TTF_OpenFont("fonts/UbuntuMono-R.ttf", 24), TTF_CloseFont);
159 print_error_msg(ERROR_TYPE_SDL2_TTF, "loading fonts");
163 const Uint8 *const keyboard_state = SDL_GetKeyboardState(NULL);
166 int64_t last_time = (int64_t) SDL_GetTicks();
167 const int64_t expected_delay_ms = (int64_t) (1000.0f / (float) fps);
168 while (!game_over_check(game)) {
169 const int64_t current_time = (int64_t) SDL_GetTicks();
171 if (current_time == last_time) {
172 SDL_Delay((Uint32) expected_delay_ms);
173 last_time = current_time;
177 const int64_t actual_delta_ms = current_time - last_time;
180 while (!game_over_check(game) && SDL_PollEvent(&e)) {
181 if (game_event(game, &e) < 0) {
182 print_current_error_msg("Failed handling event");
187 if (game_input(game, keyboard_state, the_stick_of_joy) < 0) {
188 print_current_error_msg("Failed handling input");
192 if (game_render(game) < 0) {
193 print_current_error_msg("Failed rendering the game");
197 SDL_RenderPresent(renderer);
199 int64_t effective_delta_ms = max_int64(actual_delta_ms, expected_delay_ms);
200 while (effective_delta_ms > 0) {
201 if (game_update(game, (float) min_int64(expected_delay_ms, effective_delta_ms) * 0.001f) < 0) {
202 print_current_error_msg("Failed handling updating");
206 effective_delta_ms -= expected_delay_ms;
209 if (game_sound(game) < 0) {
210 print_current_error_msg("Failed handling the sound");
214 SDL_Delay((unsigned int) max_int64(0, expected_delay_ms - actual_delta_ms));
215 last_time = current_time;