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"
16 #include "sdl/renderer.h"
17 #include "system/log.h"
18 #include "system/lt.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>]\n");
28 static float current_display_scale = 1.0f;
31 // export this for other parts of the code to use.
32 float get_display_scale(void)
34 return current_display_scale;
38 void recalculate_display_scale(SDL_Window* win, SDL_Renderer* rend)
41 SDL_GetWindowSize(win, &w0, NULL);
44 SDL_GetRendererOutputSize(rend, &w1, NULL);
46 current_display_scale = (float) w1 / (float) w0;
50 void maybe_fixup_input_for_display_scale(SDL_Window* win, SDL_Renderer* rend, SDL_Event* e)
52 // note: we check for window move as well, because you may move the window to
53 // another monitor with a different display scale.
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);
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);
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);
80 int main(int argc, char *argv[])
82 srand((unsigned int) time(NULL));
88 for (int i = 1; i < argc;) {
89 if (strcmp(argv[i], "--fps") == 0) {
91 if (sscanf(argv[i + 1], "%d", &fps) == 0) {
92 log_fail("Cannot parse FPS: %s is not a number\n", argv[i + 1]);
98 log_fail("Value of FPS is not provided\n");
103 log_fail("Unknown flag %s\n", argv[i]);
109 if (SDL_Init(SDL_INIT_EVERYTHING & ~SDL_INIT_HAPTIC) < 0) {
110 log_fail("Could not initialize SDL: %s\n", SDL_GetError());
113 PUSH_LT(lt, 42, SDL_Quit);
115 setlocale(LC_NUMERIC, "C");
117 SDL_ShowCursor(SDL_DISABLE);
119 SDL_Window *const window = PUSH_LT(
124 SCREEN_WIDTH, SCREEN_HEIGHT,
125 SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI),
128 if (window == NULL) {
129 log_fail("Could not create SDL window: %s\n", SDL_GetError());
133 SDL_Renderer *const renderer = PUSH_LT(
135 SDL_CreateRenderer(window, -1, RENDERER_CONFIG),
136 SDL_DestroyRenderer);
137 if (renderer == NULL) {
138 log_fail("Could not create SDL renderer: %s\n", SDL_GetError());
142 SDL_RendererInfo info;
143 SDL_GetRendererInfo(renderer, &info);
144 log_info("Using SDL Renderer: %s\n", info.name);
146 if (SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND) < 0) {
147 log_fail("Could not set up blending mode for the renderer: %s\n", SDL_GetError());
151 SDL_Joystick *the_stick_of_joy = NULL;
153 if (SDL_NumJoysticks() > 0) {
154 the_stick_of_joy = PUSH_LT(lt, SDL_JoystickOpen(0), SDL_JoystickClose);
156 if (the_stick_of_joy == NULL) {
157 log_fail("Could not open 0th Stick of the Joy: %s\n", SDL_GetError());
161 log_info("Opened Joystick 0\n");
162 log_info("Name: %s\n", SDL_JoystickNameForIndex(0));
163 log_info("Number of Axes: %d\n", SDL_JoystickNumAxes(the_stick_of_joy));
164 log_info("Number of Buttons: %d\n", SDL_JoystickNumButtons(the_stick_of_joy));
165 log_info("Number of Balls: %d\n", SDL_JoystickNumBalls(the_stick_of_joy));
167 SDL_JoystickEventState(SDL_ENABLE);
169 log_warn("Could not find any Sticks of the Joy\n");
172 // ------------------------------
174 const char * sound_sample_files[] = {
175 "./assets/sounds/nothing.wav",
176 "./assets/sounds/something.wav",
177 "./assets/sounds/dev/ding.wav",
178 "./assets/sounds/dev/click.wav",
179 "./assets/sounds/dev/save.wav"
181 const size_t sound_sample_files_count = sizeof(sound_sample_files) / sizeof(char*);
183 Game *const game = PUSH_LT(
188 sound_sample_files_count,
195 // calculate the display scale for the first time.
196 recalculate_display_scale(window, renderer);
198 const Uint8 *const keyboard_state = SDL_GetKeyboardState(NULL);
202 const int64_t delta_time = (int64_t) roundf(1000.0f / 60.0f);
203 int64_t render_timer = (int64_t) roundf(1000.0f / (float) fps);
204 while (!game_over_check(game)) {
205 const int64_t begin_frame_time = (int64_t) SDL_GetTicks();
207 while (!game_over_check(game) && SDL_PollEvent(&e)) {
209 // this function potentially fixes mouse events by scaling them according
210 // to the window DPI scale. (eg. *2 on retina displays). it also updates
211 // the cached DPI scale on window scale/move events.
212 maybe_fixup_input_for_display_scale(window, renderer, &e);
214 if (game_event(game, &e) < 0) {
219 if (game_input(game, keyboard_state, the_stick_of_joy) < 0) {
223 if (game_update(game, (float) delta_time * 0.001f) < 0) {
227 if (game_sound(game) < 0) {
231 render_timer -= delta_time;
232 if (render_timer <= 0) {
233 if (game_render(game) < 0) {
236 SDL_RenderPresent(renderer);
237 render_timer = (int64_t) roundf(1000.0f / (float) fps);
240 const int64_t end_frame_time = (int64_t) SDL_GetTicks();
241 SDL_Delay((unsigned int) MAX(int64_t, 10, delta_time - (end_frame_time - begin_frame_time)));