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/lt.h"
18 #include "system/lt/lt_adapters.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>] <level-file>\n");
28 int main(int argc, char *argv[])
30 srand((unsigned int) time(NULL));
32 Lt *const lt = create_lt();
34 char *level_filename = NULL;
37 for (int i = 1; i < argc;) {
38 if (strcmp(argv[i], "--fps") == 0) {
40 if (sscanf(argv[i + 1], "%d", &fps) == 0) {
41 fprintf(stderr, "Cannot parse FPS: %s is not a number\n", argv[i + 1]);
47 fprintf(stderr, "Value of FPS is not provided\n");
52 level_filename = argv[i];
57 if (level_filename == NULL) {
58 fprintf(stderr, "Path to level file is not provided\n");
63 if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
64 print_error_msg(ERROR_TYPE_SDL2, "Could not initialize SDL");
67 PUSH_LT(lt, 42, SDL_Quit_lt);
69 SDL_ShowCursor(SDL_DISABLE);
71 SDL_Window *const window = PUSH_LT(
76 SCREEN_WIDTH, SCREEN_HEIGHT,
77 SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE),
81 print_error_msg(ERROR_TYPE_SDL2, "Could not create SDL window");
85 SDL_Renderer *const renderer = PUSH_LT(
87 SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC),
89 if (renderer == NULL) {
90 print_error_msg(ERROR_TYPE_SDL2, "Could not create SDL renderer");
93 if (SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND) < 0) {
94 print_error_msg(ERROR_TYPE_SDL2, "Could not set up blending mode for the renderer");
98 SDL_Joystick *the_stick_of_joy = NULL;
100 if (SDL_NumJoysticks() > 0) {
101 the_stick_of_joy = PUSH_LT(lt, SDL_JoystickOpen(0), SDL_JoystickClose);
103 if (the_stick_of_joy == NULL) {
104 print_error_msg(ERROR_TYPE_SDL2, "Could not open 0th Stick of the Joy: %s\n");
108 printf("Opened Joystick 0\n");
109 printf("Name: %s\n", SDL_JoystickNameForIndex(0));
110 printf("Number of Axes: %d\n", SDL_JoystickNumAxes(the_stick_of_joy));
111 printf("Number of Buttons: %d\n", SDL_JoystickNumButtons(the_stick_of_joy));
112 printf("Number of Balls: %d\n", SDL_JoystickNumBalls(the_stick_of_joy));
114 SDL_JoystickEventState(SDL_ENABLE);
116 fprintf(stderr, "[WARNING] Could not find any Sticks of the Joy\n");
120 MIX_DEFAULT_FREQUENCY,
124 print_error_msg(ERROR_TYPE_SDL2_MIXER, "Could not initialize the audio\n");
127 PUSH_LT(lt, 42, Mix_CloseAudio_lt);
129 // ------------------------------
131 const char * sound_sample_files[] = {
132 "./sounds/nothing.wav",
133 "./sounds/something.wav"
135 const size_t sound_sample_files_count = sizeof(sound_sample_files) / sizeof(char*);
137 Game *const game = PUSH_LT(
142 sound_sample_files_count,
146 print_current_error_msg("Could not create the game object");
150 const Uint8 *const keyboard_state = SDL_GetKeyboardState(NULL);
152 SDL_StartTextInput();
154 const int64_t delta_time = (int64_t) roundf(1000.0f / 60.0f);
155 int64_t render_timer = (int64_t) roundf(1000.0f / (float) fps);
156 while (!game_over_check(game)) {
157 const int64_t begin_frame_time = (int64_t) SDL_GetTicks();
159 while (!game_over_check(game) && SDL_PollEvent(&e)) {
160 if (game_event(game, &e) < 0) {
161 print_current_error_msg("Failed handling event");
166 if (game_input(game, keyboard_state, the_stick_of_joy) < 0) {
167 print_current_error_msg("Failed handling input");
171 if (game_update(game, (float) delta_time * 0.001f) < 0) {
172 print_current_error_msg("Failed handling updating");
176 if (game_sound(game) < 0) {
177 print_current_error_msg("Failed handling the sound");
181 render_timer -= delta_time;
182 if (render_timer <= 0) {
183 if (game_render(game) < 0) {
184 print_current_error_msg("Failed rendering the game");
187 SDL_RenderPresent(renderer);
188 render_timer = (int64_t) roundf(1000.0f / (float) fps);
191 const int64_t end_frame_time = (int64_t) SDL_GetTicks();
192 #define max_int64(a, b) (a > b ? a : b)
193 SDL_Delay((unsigned int) max_int64(10, delta_time - (end_frame_time - begin_frame_time)));