]> git.lizzy.rs Git - nothing.git/blob - src/main.c
(#200) Make rigid rects float
[nothing.git] / src / main.c
1 #include <SDL2/SDL.h>
2 #include <SDL2/SDL_mixer.h>
3
4 #include <stdint.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <time.h>
8
9 #include "game.h"
10 #include "game/level/platforms.h"
11 #include "game/level/player.h"
12 #include "game/sound_samples.h"
13 #include "math/minmax.h"
14 #include "math/point.h"
15 #include "system/error.h"
16 #include "system/lt.h"
17
18 #define SCREEN_WIDTH 800
19 #define SCREEN_HEIGHT 600
20
21 /* LT module adapter for Mix_CloseAudio */
22 static void Mix_CloseAudio_lt(void* ignored)
23 {
24     (void) ignored;
25     Mix_CloseAudio();
26 }
27
28 /* LT module adapter for SDL_Quit */
29 static void SDL_Quit_lt(void* ignored)
30 {
31     (void) ignored;
32     SDL_Quit();
33 }
34
35 static void print_usage(FILE *stream)
36 {
37     fprintf(stream, "Usage: nothing [--fps <fps>] <level-file>\n");
38 }
39
40 int main(int argc, char *argv[])
41 {
42     srand((unsigned int) time(NULL));
43
44     lt_t *const lt = create_lt();
45
46     char *level_filename = NULL;
47     int fps = 60;
48
49     for (int i = 1; i < argc;) {
50         if (strcmp(argv[i], "--fps") == 0) {
51             if (i + 1 < argc) {
52                 if (sscanf(argv[i + 1], "%d", &fps) == 0) {
53                     fprintf(stderr, "Cannot parse FPS: %s is not a number\n", argv[i + 1]);
54                     print_usage(stderr);
55                     RETURN_LT(lt, -1);
56                 }
57                 i += 2;
58             } else {
59                 fprintf(stderr, "Value of FPS is not provided\n");
60                 print_usage(stderr);
61                 RETURN_LT(lt, -1);
62             }
63         } else {
64             level_filename = argv[i];
65             i++;
66         }
67     }
68
69     if (level_filename == NULL) {
70         fprintf(stderr, "Path to level file is not provided\n");
71         print_usage(stderr);
72         RETURN_LT(lt, -1);
73     }
74
75     if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
76         print_error_msg(ERROR_TYPE_SDL2, "Could not initialize SDL");
77         RETURN_LT(lt, -1);
78     }
79     PUSH_LT(lt, 42, SDL_Quit_lt);
80
81     SDL_ShowCursor(SDL_DISABLE);
82
83     SDL_Window *const window = PUSH_LT(
84         lt,
85         SDL_CreateWindow(
86             "Nothing",
87             100, 100,
88             SCREEN_WIDTH, SCREEN_HEIGHT,
89             SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE),
90         SDL_DestroyWindow);
91
92     if (window == NULL) {
93         print_error_msg(ERROR_TYPE_SDL2, "Could not create SDL window");
94         RETURN_LT(lt, -1);
95     }
96
97     SDL_Renderer *const renderer = PUSH_LT(
98         lt,
99         SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC),
100         SDL_DestroyRenderer);
101     if (renderer == NULL) {
102         print_error_msg(ERROR_TYPE_SDL2, "Could not create SDL renderer");
103         RETURN_LT(lt, -1);
104     }
105     if (SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND) < 0) {
106         print_error_msg(ERROR_TYPE_SDL2, "Could not set up blending mode for the renderer");
107         RETURN_LT(lt, -1);
108     }
109
110     SDL_Joystick *the_stick_of_joy = NULL;
111
112     if (SDL_NumJoysticks() > 0) {
113         the_stick_of_joy = PUSH_LT(lt, SDL_JoystickOpen(0), SDL_JoystickClose);
114
115         if (the_stick_of_joy == NULL) {
116             print_error_msg(ERROR_TYPE_SDL2, "Could not open 0th Stick of the Joy: %s\n");
117             RETURN_LT(lt, -1);
118         }
119
120         printf("Opened Joystick 0\n");
121         printf("Name: %s\n", SDL_JoystickNameForIndex(0));
122         printf("Number of Axes: %d\n", SDL_JoystickNumAxes(the_stick_of_joy));
123         printf("Number of Buttons: %d\n", SDL_JoystickNumButtons(the_stick_of_joy));
124         printf("Number of Balls: %d\n", SDL_JoystickNumBalls(the_stick_of_joy));
125
126         SDL_JoystickEventState(SDL_ENABLE);
127     } else {
128         fprintf(stderr, "[WARNING] Could not find any Sticks of the Joy\n");
129     }
130
131     if (Mix_OpenAudio(
132             MIX_DEFAULT_FREQUENCY,
133             MIX_DEFAULT_FORMAT,
134             2,
135             1024) < 0) {
136         print_error_msg(ERROR_TYPE_SDL2_MIXER, "Could not initialize the audio\n");
137         RETURN_LT(lt, -1);
138     }
139     PUSH_LT(lt, 42, Mix_CloseAudio_lt);
140
141     // ------------------------------
142
143     const char * sound_sample_files[] = {
144         "./sounds/nothing.wav",
145         "./sounds/something.wav"
146     };
147     const size_t sound_sample_files_count = sizeof(sound_sample_files) / sizeof(char*);
148
149     game_t *const game = PUSH_LT(
150         lt,
151         create_game(
152             level_filename,
153             sound_sample_files,
154             sound_sample_files_count,
155             renderer),
156         destroy_game);
157     if (game == NULL) {
158         print_current_error_msg("Could not create the game object");
159         RETURN_LT(lt, -1);
160     }
161
162     const Uint8 *const keyboard_state = SDL_GetKeyboardState(NULL);
163
164     SDL_Event e;
165     int64_t last_time = (int64_t) SDL_GetTicks();
166     const int64_t expected_delay_ms = (int64_t) (1000.0f / (float) fps);
167     while (!game_over_check(game)) {
168         const int64_t current_time = (int64_t) SDL_GetTicks();
169
170         if (current_time == last_time) {
171             SDL_Delay((Uint32) expected_delay_ms);
172             last_time = current_time;
173             continue;
174         }
175
176         const int64_t actual_delta_ms = current_time - last_time;
177
178
179         while (!game_over_check(game) && SDL_PollEvent(&e)) {
180             if (game_event(game, &e) < 0) {
181                 print_current_error_msg("Failed handling event");
182                 RETURN_LT(lt, -1);
183             }
184         }
185
186         if (game_input(game, keyboard_state, the_stick_of_joy) < 0) {
187             print_current_error_msg("Failed handling input");
188             RETURN_LT(lt, -1);
189         }
190
191         if (game_render(game) < 0) {
192             print_current_error_msg("Failed rendering the game");
193             RETURN_LT(lt, -1);
194         }
195
196         int64_t effective_delta_ms = max_int64(actual_delta_ms, expected_delay_ms);
197         while (effective_delta_ms > 0) {
198             if (game_update(game, (float) min_int64(expected_delay_ms, effective_delta_ms) * 0.001f) < 0) {
199                 print_current_error_msg("Failed handling updating");
200                 RETURN_LT(lt, -1);
201             }
202
203             effective_delta_ms -= expected_delay_ms;
204         }
205
206         if (game_sound(game) < 0) {
207             print_current_error_msg("Failed handling the sound");
208             RETURN_LT(lt, -1);
209         }
210
211         SDL_Delay((unsigned int) max_int64(0, expected_delay_ms - actual_delta_ms));
212         last_time = current_time;
213     }
214
215     RETURN_LT(lt, 0);
216 }