]> git.lizzy.rs Git - nothing.git/blob - src/main.c
(#157) Separate fps of physics and rendering
[nothing.git] / src / main.c
1 #include <SDL2/SDL.h>
2 #include <SDL2/SDL_mixer.h>
3 #include <SDL2/SDL_ttf.h>
4
5 #include <stdint.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <time.h>
9
10 #include "game.h"
11 #include "game/level/platforms.h"
12 #include "game/level/player.h"
13 #include "game/sound_samples.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 (TTF_Init() < 0) {
65         print_error_msg(ERROR_TYPE_SDL2_TTF, "Could not initialize SDL_ttf");
66         RETURN_LT(lt, -1);
67     }
68     PUSH_LT(lt, 42, TTF_Quit_lt);
69
70     if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
71         print_error_msg(ERROR_TYPE_SDL2, "Could not initialize SDL");
72         RETURN_LT(lt, -1);
73     }
74     PUSH_LT(lt, 42, SDL_Quit_lt);
75
76     SDL_ShowCursor(SDL_DISABLE);
77
78     SDL_Window *const window = PUSH_LT(
79         lt,
80         SDL_CreateWindow(
81             "Nothing",
82             100, 100,
83             SCREEN_WIDTH, SCREEN_HEIGHT,
84             SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE),
85         SDL_DestroyWindow);
86
87     if (window == NULL) {
88         print_error_msg(ERROR_TYPE_SDL2, "Could not create SDL window");
89         RETURN_LT(lt, -1);
90     }
91
92     SDL_Renderer *const renderer = PUSH_LT(
93         lt,
94         SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC),
95         SDL_DestroyRenderer);
96     if (renderer == NULL) {
97         print_error_msg(ERROR_TYPE_SDL2, "Could not create SDL renderer");
98         RETURN_LT(lt, -1);
99     }
100     if (SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND) < 0) {
101         print_error_msg(ERROR_TYPE_SDL2, "Could not set up blending mode for the renderer");
102         RETURN_LT(lt, -1);
103     }
104
105     SDL_Joystick *the_stick_of_joy = NULL;
106
107     if (SDL_NumJoysticks() > 0) {
108         the_stick_of_joy = PUSH_LT(lt, SDL_JoystickOpen(0), SDL_JoystickClose);
109
110         if (the_stick_of_joy == NULL) {
111             print_error_msg(ERROR_TYPE_SDL2, "Could not open 0th Stick of the Joy: %s\n");
112             RETURN_LT(lt, -1);
113         }
114
115         printf("Opened Joystick 0\n");
116         printf("Name: %s\n", SDL_JoystickNameForIndex(0));
117         printf("Number of Axes: %d\n", SDL_JoystickNumAxes(the_stick_of_joy));
118         printf("Number of Buttons: %d\n", SDL_JoystickNumButtons(the_stick_of_joy));
119         printf("Number of Balls: %d\n", SDL_JoystickNumBalls(the_stick_of_joy));
120
121         SDL_JoystickEventState(SDL_ENABLE);
122     } else {
123         fprintf(stderr, "[WARNING] Could not find any Sticks of the Joy\n");
124     }
125
126     if (Mix_OpenAudio(
127             MIX_DEFAULT_FREQUENCY,
128             MIX_DEFAULT_FORMAT,
129             2,
130             1024) < 0) {
131         print_error_msg(ERROR_TYPE_SDL2_MIXER, "Could not initialize the audio\n");
132         RETURN_LT(lt, -1);
133     }
134     PUSH_LT(lt, 42, Mix_CloseAudio_lt);
135
136     // ------------------------------
137
138     const char * sound_sample_files[] = {
139         "./sounds/nothing.wav",
140         "./sounds/something.wav"
141     };
142     const size_t sound_sample_files_count = sizeof(sound_sample_files) / sizeof(char*);
143
144     game_t *const game = PUSH_LT(
145         lt,
146         create_game(
147             level_filename,
148             sound_sample_files,
149             sound_sample_files_count,
150             renderer),
151         destroy_game);
152     if (game == NULL) {
153         print_current_error_msg("Could not create the game object");
154         RETURN_LT(lt, -1);
155     }
156
157     TTF_Font *const font = PUSH_LT(lt, TTF_OpenFont("fonts/UbuntuMono-R.ttf", 24), TTF_CloseFont);
158     if (font == NULL) {
159         print_error_msg(ERROR_TYPE_SDL2_TTF, "loading fonts");
160         RETURN_LT(lt, -1);
161     }
162
163     const Uint8 *const keyboard_state = SDL_GetKeyboardState(NULL);
164
165     SDL_Event e;
166     const int64_t delta_time = (int64_t) (1000.0f / (float) 60);
167     int64_t render_timer = (int64_t) (1000.0f / (float) fps);
168     while (!game_over_check(game)) {
169         const int64_t begin_frame_time = (int64_t) SDL_GetTicks();
170
171         while (!game_over_check(game) && SDL_PollEvent(&e)) {
172             if (game_event(game, &e) < 0) {
173                 print_current_error_msg("Failed handling event");
174                 RETURN_LT(lt, -1);
175             }
176         }
177
178         if (game_input(game, keyboard_state, the_stick_of_joy) < 0) {
179             print_current_error_msg("Failed handling input");
180             RETURN_LT(lt, -1);
181         }
182
183         if (game_update(game, (float) delta_time * 0.001f) < 0) {
184             print_current_error_msg("Failed handling updating");
185             RETURN_LT(lt, -1);
186         }
187
188         if (game_sound(game) < 0) {
189             print_current_error_msg("Failed handling the sound");
190             RETURN_LT(lt, -1);
191         }
192
193         if (game_render(game) < 0) {
194             print_current_error_msg("Failed rendering the game");
195             RETURN_LT(lt, -1);
196         }
197         const int64_t end_frame_time = (int64_t) SDL_GetTicks();
198
199         render_timer -= delta_time;
200         if (render_timer < 0) {
201             SDL_RenderPresent(renderer);
202             render_timer = (int64_t) (1000.0f / (float) fps);
203         }
204
205         SDL_Delay((unsigned int) max_int64(0, delta_time - (end_frame_time - begin_frame_time)));
206     }
207
208     RETURN_LT(lt, 0);
209 }