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