]> git.lizzy.rs Git - nothing.git/blob - src/main.c
7ed089702e39bf5879ddc21264f6c3af6c3c89c9
[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/minmax.h"
15 #include "math/point.h"
16 #include "sdl/renderer.h"
17 #include "system/error.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-file>\n");
27 }
28
29 int main(int argc, char *argv[])
30 {
31     srand((unsigned int) time(NULL));
32
33     lt_t *const lt = create_lt();
34
35     char *level_filename = NULL;
36     int fps = 60;
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                     fprintf(stderr, "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                 fprintf(stderr, "Value of FPS is not provided\n");
49                 print_usage(stderr);
50                 RETURN_LT(lt, -1);
51             }
52         } else {
53             level_filename = argv[i];
54             i++;
55         }
56     }
57
58     if (level_filename == NULL) {
59         fprintf(stderr, "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         print_error_msg(ERROR_TYPE_SDL2, "Could not initialize SDL");
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         print_error_msg(ERROR_TYPE_SDL2, "Could not create SDL window");
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         print_error_msg(ERROR_TYPE_SDL2, "Could not create SDL renderer");
92         RETURN_LT(lt, -1);
93     }
94     if (SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND) < 0) {
95         print_error_msg(ERROR_TYPE_SDL2, "Could not set up blending mode for the renderer");
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             print_error_msg(ERROR_TYPE_SDL2, "Could not open 0th Stick of the Joy: %s\n");
106             RETURN_LT(lt, -1);
107         }
108
109         printf("Opened Joystick 0\n");
110         printf("Name: %s\n", SDL_JoystickNameForIndex(0));
111         printf("Number of Axes: %d\n", SDL_JoystickNumAxes(the_stick_of_joy));
112         printf("Number of Buttons: %d\n", SDL_JoystickNumButtons(the_stick_of_joy));
113         printf("Number of Balls: %d\n", SDL_JoystickNumBalls(the_stick_of_joy));
114
115         SDL_JoystickEventState(SDL_ENABLE);
116     } else {
117         fprintf(stderr, "[WARNING] 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         print_error_msg(ERROR_TYPE_SDL2_MIXER, "Could not initialize the audio\n");
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_t *const game = PUSH_LT(
139         lt,
140         create_game(
141             level_filename,
142             sound_sample_files,
143             sound_sample_files_count,
144             renderer),
145         destroy_game);
146     if (game == NULL) {
147         print_current_error_msg("Could not create the game object");
148         RETURN_LT(lt, -1);
149     }
150
151     sprite_font_t * const sprite_font =
152         PUSH_LT(lt,
153                 create_sprite_font_from_file("fonts/charmap-oldschool_white.bmp", renderer),
154                 destroy_sprite_font);
155     if (sprite_font == NULL) {
156         print_current_error_msg("Loading up sprite font");
157         RETURN_LT(lt, -1);
158     }
159
160     const Uint8 *const keyboard_state = SDL_GetKeyboardState(NULL);
161
162     SDL_Event e;
163     const int64_t delta_time = (int64_t) roundf(1000.0f / 60.0f);
164     int64_t render_timer = (int64_t) roundf(1000.0f / (float) fps);
165     while (!game_over_check(game)) {
166         const int64_t begin_frame_time = (int64_t) SDL_GetTicks();
167
168         while (!game_over_check(game) && SDL_PollEvent(&e)) {
169             if (game_event(game, &e) < 0) {
170                 print_current_error_msg("Failed handling event");
171                 RETURN_LT(lt, -1);
172             }
173         }
174
175         if (game_input(game, keyboard_state, the_stick_of_joy) < 0) {
176             print_current_error_msg("Failed handling input");
177             RETURN_LT(lt, -1);
178         }
179
180         if (game_update(game, (float) delta_time * 0.001f) < 0) {
181             print_current_error_msg("Failed handling updating");
182             RETURN_LT(lt, -1);
183         }
184
185         if (game_sound(game) < 0) {
186             print_current_error_msg("Failed handling the sound");
187             RETURN_LT(lt, -1);
188         }
189
190         if (game_render(game) < 0) {
191             print_current_error_msg("Failed rendering the game");
192             RETURN_LT(lt, -1);
193         }
194
195         if (sprite_font_render_text(sprite_font,
196                                     renderer,
197                                     vec(0.0f, 0.0f),
198                                     4,
199                                     "HELLO, WORLD!!!") < 0) {
200             print_current_error_msg("Failed rendering debug info");
201             RETURN_LT(lt, -1);
202         }
203
204         const int64_t end_frame_time = (int64_t) SDL_GetTicks();
205
206         render_timer -= delta_time;
207         if (render_timer <= 0) {
208             SDL_RenderPresent(renderer);
209             render_timer = (int64_t) roundf(1000.0f / (float) fps);
210         }
211
212         SDL_Delay((unsigned int) max_int64(10, delta_time - (end_frame_time - begin_frame_time)));
213     }
214
215     RETURN_LT(lt, 0);
216 }