]> git.lizzy.rs Git - nothing.git/blob - src/main.c
878bb2bdf9185a5821d9ca326ac070891f16fae2
[nothing.git] / src / main.c
1 #include <SDL.h>
2
3 #include <locale.h>
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 "game/sprite_font.h"
14 #include "math/extrema.h"
15 #include "math/vec.h"
16 #include "sdl/renderer.h"
17 #include "system/log.h"
18 #include "system/lt.h"
19
20 #define SCREEN_WIDTH 800
21 #define SCREEN_HEIGHT 600
22
23 static void print_usage(FILE *stream)
24 {
25     fprintf(stream, "Usage: nothing [--fps <fps>]\n");
26 }
27
28 static float current_display_scale = 1.0f;
29
30
31 // export this for other parts of the code to use.
32 float get_display_scale(void)
33 {
34     return current_display_scale;
35 }
36
37 static
38 void recalculate_display_scale(SDL_Window* win, SDL_Renderer* rend)
39 {
40     int w0 = 0;
41     SDL_GetWindowSize(win, &w0, NULL);
42
43     int w1 = 0;
44     SDL_GetRendererOutputSize(rend, &w1, NULL);
45
46     current_display_scale = (float) w1 / (float) w0;
47 }
48
49 static
50 void maybe_fixup_input_for_display_scale(SDL_Window* win, SDL_Renderer* rend, SDL_Event* e)
51 {
52     // note: we check for window move as well, because you may move the window to
53     // another monitor with a different display scale.
54     switch (e->type) {
55     case SDL_WINDOWEVENT: {
56         switch (e->window.event) {
57         case SDL_WINDOWEVENT_MOVED:
58         case SDL_WINDOWEVENT_SIZE_CHANGED:
59             recalculate_display_scale(win, rend);
60             break;
61         }
62     } break;
63
64     // this is the fixup.
65     case SDL_MOUSEMOTION: {
66         // note: do it this way *just in case* there are non-integer display scales out there.
67         e->motion.x = (int) ((float) e->motion.x * current_display_scale);
68         e->motion.y = (int) ((float) e->motion.y * current_display_scale);
69     } break;
70
71     case SDL_MOUSEBUTTONUP:
72     case SDL_MOUSEBUTTONDOWN: {
73         e->button.x = (int) ((float) e->button.x * current_display_scale);
74         e->button.y = (int) ((float) e->button.y * current_display_scale);
75     } break;
76     }
77 }
78
79
80 int main(int argc, char *argv[])
81 {
82     srand((unsigned int) time(NULL));
83
84     Lt *lt = create_lt();
85
86     int fps = 60;
87
88     for (int i = 1; i < argc;) {
89         if (strcmp(argv[i], "--fps") == 0) {
90             if (i + 1 < argc) {
91                 if (sscanf(argv[i + 1], "%d", &fps) == 0) {
92                     log_fail("Cannot parse FPS: %s is not a number\n", argv[i + 1]);
93                     print_usage(stderr);
94                     RETURN_LT(lt, -1);
95                 }
96                 i += 2;
97             } else {
98                 log_fail("Value of FPS is not provided\n");
99                 print_usage(stderr);
100                 RETURN_LT(lt, -1);
101             }
102         } else {
103             log_fail("Unknown flag %s\n", argv[i]);
104             print_usage(stderr);
105             RETURN_LT(lt, -1);
106         }
107     }
108
109     if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
110         log_fail("Could not initialize SDL: %s\n", SDL_GetError());
111         RETURN_LT(lt, -1);
112     }
113     PUSH_LT(lt, 42, SDL_Quit);
114
115     setlocale(LC_NUMERIC, "C");
116
117     SDL_ShowCursor(SDL_DISABLE);
118
119     SDL_Window *const window = PUSH_LT(
120         lt,
121         SDL_CreateWindow(
122             "Nothing",
123             100, 100,
124             SCREEN_WIDTH, SCREEN_HEIGHT,
125             SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI),
126         SDL_DestroyWindow);
127
128     if (window == NULL) {
129         log_fail("Could not create SDL window: %s\n", SDL_GetError());
130         RETURN_LT(lt, -1);
131     }
132
133     SDL_Renderer *const renderer = PUSH_LT(
134         lt,
135         SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC),
136         SDL_DestroyRenderer);
137     if (renderer == NULL) {
138         log_fail("Could not create SDL renderer: %s\n", SDL_GetError());
139         RETURN_LT(lt, -1);
140     }
141     if (SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND) < 0) {
142         log_fail("Could not set up blending mode for the renderer: %s\n", SDL_GetError());
143         RETURN_LT(lt, -1);
144     }
145
146     SDL_Joystick *the_stick_of_joy = NULL;
147
148     if (SDL_NumJoysticks() > 0) {
149         the_stick_of_joy = PUSH_LT(lt, SDL_JoystickOpen(0), SDL_JoystickClose);
150
151         if (the_stick_of_joy == NULL) {
152             log_fail("Could not open 0th Stick of the Joy: %s\n", SDL_GetError());
153             RETURN_LT(lt, -1);
154         }
155
156         log_info("Opened Joystick 0\n");
157         log_info("Name: %s\n", SDL_JoystickNameForIndex(0));
158         log_info("Number of Axes: %d\n", SDL_JoystickNumAxes(the_stick_of_joy));
159         log_info("Number of Buttons: %d\n", SDL_JoystickNumButtons(the_stick_of_joy));
160         log_info("Number of Balls: %d\n", SDL_JoystickNumBalls(the_stick_of_joy));
161
162         SDL_JoystickEventState(SDL_ENABLE);
163     } else {
164         log_warn("Could not find any Sticks of the Joy\n");
165     }
166
167     // ------------------------------
168
169     const char * sound_sample_files[] = {
170         "./assets/sounds/nothing.wav",
171         "./assets/sounds/something.wav",
172         "./assets/sounds/dev/ding.wav",
173         "./assets/sounds/dev/click.wav",
174         "./assets/sounds/dev/save.wav"
175     };
176     const size_t sound_sample_files_count = sizeof(sound_sample_files) / sizeof(char*);
177
178     Game *const game = PUSH_LT(
179         lt,
180         create_game(
181             "./assets/levels/",
182             sound_sample_files,
183             sound_sample_files_count,
184             renderer),
185         destroy_game);
186     if (game == NULL) {
187         RETURN_LT(lt, -1);
188     }
189
190     // calculate the display scale for the first time.
191     recalculate_display_scale(window, renderer);
192
193     const Uint8 *const keyboard_state = SDL_GetKeyboardState(NULL);
194
195     SDL_StopTextInput();
196     SDL_Event e;
197     const int64_t delta_time = (int64_t) roundf(1000.0f / 60.0f);
198     int64_t render_timer = (int64_t) roundf(1000.0f / (float) fps);
199     while (!game_over_check(game)) {
200         const int64_t begin_frame_time = (int64_t) SDL_GetTicks();
201
202         while (!game_over_check(game) && SDL_PollEvent(&e)) {
203
204             // this function potentially fixes mouse events by scaling them according
205             // to the window DPI scale. (eg. *2 on retina displays). it also updates
206             // the cached DPI scale on window scale/move events.
207             maybe_fixup_input_for_display_scale(window, renderer, &e);
208
209             if (game_event(game, &e) < 0) {
210                 RETURN_LT(lt, -1);
211             }
212         }
213
214         if (game_input(game, keyboard_state, the_stick_of_joy) < 0) {
215             RETURN_LT(lt, -1);
216         }
217
218         if (game_update(game, (float) delta_time * 0.001f) < 0) {
219             RETURN_LT(lt, -1);
220         }
221
222         if (game_sound(game) < 0) {
223             RETURN_LT(lt, -1);
224         }
225
226         render_timer -= delta_time;
227         if (render_timer <= 0) {
228             if (game_render(game) < 0) {
229                 RETURN_LT(lt, -1);
230             }
231             SDL_RenderPresent(renderer);
232             render_timer = (int64_t) roundf(1000.0f / (float) fps);
233         }
234
235         const int64_t end_frame_time = (int64_t) SDL_GetTicks();
236         SDL_Delay((unsigned int) MAX(int64_t, 10, delta_time - (end_frame_time - begin_frame_time)));
237     }
238
239     RETURN_LT(lt, 0);
240 }