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/log.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 log_fail("Cannot parse FPS: %s is not a number\n", argv[i + 1]);
47 log_fail("Value of FPS is not provided\n");
52 level_filename = argv[i];
57 if (level_filename == NULL) {
58 log_fail("Path to level file is not provided\n");
63 if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
64 log_fail("Could not initialize SDL: %s\n", SDL_GetError());
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 log_fail("Could not create SDL window: %s\n", SDL_GetError());
85 SDL_Renderer *const renderer = PUSH_LT(
87 SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC),
89 if (renderer == NULL) {
90 log_fail("Could not create SDL renderer: %s\n", SDL_GetError());
93 if (SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND) < 0) {
94 log_fail("Could not set up blending mode for the renderer: %s\n", SDL_GetError());
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 log_fail("Could not open 0th Stick of the Joy: %s\n", SDL_GetError());
108 log_info("Opened Joystick 0\n");
109 log_info("Name: %s\n", SDL_JoystickNameForIndex(0));
110 log_info("Number of Axes: %d\n", SDL_JoystickNumAxes(the_stick_of_joy));
111 log_info("Number of Buttons: %d\n", SDL_JoystickNumButtons(the_stick_of_joy));
112 log_info("Number of Balls: %d\n", SDL_JoystickNumBalls(the_stick_of_joy));
114 SDL_JoystickEventState(SDL_ENABLE);
116 log_warn("Could not find any Sticks of the Joy\n");
120 MIX_DEFAULT_FREQUENCY,
124 log_fail("Could not initialize the audio: %s\n", Mix_GetError());
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,
149 const Uint8 *const keyboard_state = SDL_GetKeyboardState(NULL);
153 const int64_t delta_time = (int64_t) roundf(1000.0f / 60.0f);
154 int64_t render_timer = (int64_t) roundf(1000.0f / (float) fps);
155 while (!game_over_check(game)) {
156 const int64_t begin_frame_time = (int64_t) SDL_GetTicks();
158 while (!game_over_check(game) && SDL_PollEvent(&e)) {
159 if (game_event(game, &e) < 0) {
164 if (game_input(game, keyboard_state, the_stick_of_joy) < 0) {
168 if (game_update(game, (float) delta_time * 0.001f) < 0) {
172 if (game_sound(game) < 0) {
176 render_timer -= delta_time;
177 if (render_timer <= 0) {
178 if (game_render(game) < 0) {
181 SDL_RenderPresent(renderer);
182 render_timer = (int64_t) roundf(1000.0f / (float) fps);
185 const int64_t end_frame_time = (int64_t) SDL_GetTicks();
186 #define max_int64(a, b) (a > b ? a : b)
187 SDL_Delay((unsigned int) max_int64(10, delta_time - (end_frame_time - begin_frame_time)));