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