]> git.lizzy.rs Git - nothing.git/blob - src/main.c
Fix PlayerLayer click-through problem
[nothing.git] / src / main.c
1 #include <SDL.h>
2
3 #include <stdint.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <time.h>
7
8 #include "game.h"
9 #include "game/level/platforms.h"
10 #include "game/level/player.h"
11 #include "game/sound_samples.h"
12 #include "game/sprite_font.h"
13 #include "math/extrema.h"
14 #include "math/point.h"
15 #include "sdl/renderer.h"
16 #include "system/log.h"
17 #include "system/lt.h"
18
19 #define SCREEN_WIDTH 800
20 #define SCREEN_HEIGHT 600
21
22 static void print_usage(FILE *stream)
23 {
24     fprintf(stream, "Usage: nothing [--fps <fps>]\n");
25 }
26
27 int main(int argc, char *argv[])
28 {
29     srand((unsigned int) time(NULL));
30
31     Lt *lt = create_lt();
32
33     int fps = 30;
34
35     for (int i = 1; i < argc;) {
36         if (strcmp(argv[i], "--fps") == 0) {
37             if (i + 1 < argc) {
38                 if (sscanf(argv[i + 1], "%d", &fps) == 0) {
39                     log_fail("Cannot parse FPS: %s is not a number\n", argv[i + 1]);
40                     print_usage(stderr);
41                     RETURN_LT(lt, -1);
42                 }
43                 i += 2;
44             } else {
45                 log_fail("Value of FPS is not provided\n");
46                 print_usage(stderr);
47                 RETURN_LT(lt, -1);
48             }
49         } else {
50             log_fail("Unknown flag %s\n", argv[i]);
51             print_usage(stderr);
52             RETURN_LT(lt, -1);
53         }
54     }
55
56     if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
57         log_fail("Could not initialize SDL: %s\n", SDL_GetError());
58         RETURN_LT(lt, -1);
59     }
60     PUSH_LT(lt, 42, SDL_Quit);
61
62     SDL_ShowCursor(SDL_DISABLE);
63
64     SDL_Window *const window = PUSH_LT(
65         lt,
66         SDL_CreateWindow(
67             "Nothing",
68             100, 100,
69             SCREEN_WIDTH, SCREEN_HEIGHT,
70             SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE),
71         SDL_DestroyWindow);
72
73     if (window == NULL) {
74         log_fail("Could not create SDL window: %s\n", SDL_GetError());
75         RETURN_LT(lt, -1);
76     }
77
78     SDL_Renderer *const renderer = PUSH_LT(
79         lt,
80         SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC),
81         SDL_DestroyRenderer);
82     if (renderer == NULL) {
83         log_fail("Could not create SDL renderer: %s\n", SDL_GetError());
84         RETURN_LT(lt, -1);
85     }
86     if (SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND) < 0) {
87         log_fail("Could not set up blending mode for the renderer: %s\n", SDL_GetError());
88         RETURN_LT(lt, -1);
89     }
90
91     SDL_Joystick *the_stick_of_joy = NULL;
92
93     if (SDL_NumJoysticks() > 0) {
94         the_stick_of_joy = PUSH_LT(lt, SDL_JoystickOpen(0), SDL_JoystickClose);
95
96         if (the_stick_of_joy == NULL) {
97             log_fail("Could not open 0th Stick of the Joy: %s\n", SDL_GetError());
98             RETURN_LT(lt, -1);
99         }
100
101         log_info("Opened Joystick 0\n");
102         log_info("Name: %s\n", SDL_JoystickNameForIndex(0));
103         log_info("Number of Axes: %d\n", SDL_JoystickNumAxes(the_stick_of_joy));
104         log_info("Number of Buttons: %d\n", SDL_JoystickNumButtons(the_stick_of_joy));
105         log_info("Number of Balls: %d\n", SDL_JoystickNumBalls(the_stick_of_joy));
106
107         SDL_JoystickEventState(SDL_ENABLE);
108     } else {
109         log_warn("Could not find any Sticks of the Joy\n");
110     }
111
112     // ------------------------------
113
114     const char * sound_sample_files[] = {
115         "./sounds/nothing.wav",
116         "./sounds/something.wav"
117     };
118     const size_t sound_sample_files_count = sizeof(sound_sample_files) / sizeof(char*);
119
120     Game *const game = PUSH_LT(
121         lt,
122         create_game(
123             "./levels/",
124             sound_sample_files,
125             sound_sample_files_count,
126             renderer),
127         destroy_game);
128     if (game == NULL) {
129         RETURN_LT(lt, -1);
130     }
131
132     const Uint8 *const keyboard_state = SDL_GetKeyboardState(NULL);
133
134     SDL_StopTextInput();
135     SDL_Event e;
136     const int64_t delta_time = (int64_t) roundf(1000.0f / 60.0f);
137     int64_t render_timer = (int64_t) roundf(1000.0f / (float) fps);
138     while (!game_over_check(game)) {
139         const int64_t begin_frame_time = (int64_t) SDL_GetTicks();
140
141         while (!game_over_check(game) && SDL_PollEvent(&e)) {
142             if (game_event(game, &e) < 0) {
143                 RETURN_LT(lt, -1);
144             }
145         }
146
147         if (game_input(game, keyboard_state, the_stick_of_joy) < 0) {
148             RETURN_LT(lt, -1);
149         }
150
151         if (game_update(game, (float) delta_time * 0.001f) < 0) {
152             RETURN_LT(lt, -1);
153         }
154
155         if (game_sound(game) < 0) {
156             RETURN_LT(lt, -1);
157         }
158
159         render_timer -= delta_time;
160         if (render_timer <= 0) {
161             if (game_render(game) < 0) {
162                 RETURN_LT(lt, -1);
163             }
164             SDL_RenderPresent(renderer);
165             render_timer = (int64_t) roundf(1000.0f / (float) fps);
166         }
167
168         const int64_t end_frame_time = (int64_t) SDL_GetTicks();
169         SDL_Delay((unsigned int) max_int64(10, delta_time - (end_frame_time - begin_frame_time)));
170     }
171
172     RETURN_LT(lt, 0);
173 }