2 #include <SDL2/SDL_mixer.h>
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/point.h"
15 #include "sdl/renderer.h"
16 #include "system/error.h"
17 #include "system/log.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 *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 log_fail("Cannot parse FPS: %s is not a number\n", argv[i + 1]);
48 log_fail("Value of FPS is not provided\n");
53 level_filename = argv[i];
58 if (level_filename == NULL) {
59 log_fail("Path to level file is not provided\n");
64 if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
65 print_error_msg(ERROR_TYPE_SDL2, "Could not initialize SDL");
68 PUSH_LT(lt, 42, SDL_Quit_lt);
70 SDL_ShowCursor(SDL_DISABLE);
72 SDL_Window *const window = PUSH_LT(
77 SCREEN_WIDTH, SCREEN_HEIGHT,
78 SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE),
82 print_error_msg(ERROR_TYPE_SDL2, "Could not create SDL window");
86 SDL_Renderer *const renderer = PUSH_LT(
88 SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC),
90 if (renderer == NULL) {
91 print_error_msg(ERROR_TYPE_SDL2, "Could not create SDL renderer");
94 if (SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND) < 0) {
95 print_error_msg(ERROR_TYPE_SDL2, "Could not set up blending mode for the renderer");
99 SDL_Joystick *the_stick_of_joy = NULL;
101 if (SDL_NumJoysticks() > 0) {
102 the_stick_of_joy = PUSH_LT(lt, SDL_JoystickOpen(0), SDL_JoystickClose);
104 if (the_stick_of_joy == NULL) {
105 print_error_msg(ERROR_TYPE_SDL2, "Could not open 0th Stick of the Joy: %s\n");
109 log_info("Opened Joystick 0\n");
110 log_info("Name: %s\n", SDL_JoystickNameForIndex(0));
111 log_info("Number of Axes: %d\n", SDL_JoystickNumAxes(the_stick_of_joy));
112 log_info("Number of Buttons: %d\n", SDL_JoystickNumButtons(the_stick_of_joy));
113 log_info("Number of Balls: %d\n", SDL_JoystickNumBalls(the_stick_of_joy));
115 SDL_JoystickEventState(SDL_ENABLE);
117 log_warn("Could not find any Sticks of the Joy\n");
121 MIX_DEFAULT_FREQUENCY,
125 print_error_msg(ERROR_TYPE_SDL2_MIXER, "Could not initialize the audio\n");
128 PUSH_LT(lt, 42, Mix_CloseAudio_lt);
130 // ------------------------------
132 const char * sound_sample_files[] = {
133 "./sounds/nothing.wav",
134 "./sounds/something.wav"
136 const size_t sound_sample_files_count = sizeof(sound_sample_files) / sizeof(char*);
138 Game *const game = PUSH_LT(
143 sound_sample_files_count,
147 print_current_error_msg("Could not create the game object");
151 const Uint8 *const keyboard_state = SDL_GetKeyboardState(NULL);
155 const int64_t delta_time = (int64_t) roundf(1000.0f / 60.0f);
156 int64_t render_timer = (int64_t) roundf(1000.0f / (float) fps);
157 while (!game_over_check(game)) {
158 const int64_t begin_frame_time = (int64_t) SDL_GetTicks();
160 while (!game_over_check(game) && SDL_PollEvent(&e)) {
161 if (game_event(game, &e) < 0) {
162 print_current_error_msg("Failed handling event");
167 if (game_input(game, keyboard_state, the_stick_of_joy) < 0) {
168 print_current_error_msg("Failed handling input");
172 if (game_update(game, (float) delta_time * 0.001f) < 0) {
173 print_current_error_msg("Failed handling updating");
177 if (game_sound(game) < 0) {
178 print_current_error_msg("Failed handling the sound");
182 render_timer -= delta_time;
183 if (render_timer <= 0) {
184 if (game_render(game) < 0) {
185 print_current_error_msg("Failed rendering the game");
188 SDL_RenderPresent(renderer);
189 render_timer = (int64_t) roundf(1000.0f / (float) fps);
192 const int64_t end_frame_time = (int64_t) SDL_GetTicks();
193 #define max_int64(a, b) (a > b ? a : b)
194 SDL_Delay((unsigned int) max_int64(10, delta_time - (end_frame_time - begin_frame_time)));