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