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