]> git.lizzy.rs Git - nothing.git/blob - src/main.c
Merge pull request #571 from lastedge/patch-1
[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/log.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                     log_fail("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                 log_fail("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         log_fail("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         log_fail("Could not initialize SDL: %s\n", SDL_GetError());
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         log_fail("Could not create SDL window: %s\n", SDL_GetError());
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         log_fail("Could not create SDL renderer: %s\n", SDL_GetError());
91         RETURN_LT(lt, -1);
92     }
93     if (SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND) < 0) {
94         log_fail("Could not set up blending mode for the renderer: %s\n", SDL_GetError());
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             log_fail("Could not open 0th Stick of the Joy: %s\n", SDL_GetError());
105             RETURN_LT(lt, -1);
106         }
107
108         log_info("Opened Joystick 0\n");
109         log_info("Name: %s\n", SDL_JoystickNameForIndex(0));
110         log_info("Number of Axes: %d\n", SDL_JoystickNumAxes(the_stick_of_joy));
111         log_info("Number of Buttons: %d\n", SDL_JoystickNumButtons(the_stick_of_joy));
112         log_info("Number of Balls: %d\n", SDL_JoystickNumBalls(the_stick_of_joy));
113
114         SDL_JoystickEventState(SDL_ENABLE);
115     } else {
116         log_warn("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         log_fail("Could not initialize the audio: %s\n", Mix_GetError());
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         RETURN_LT(lt, -1);
147     }
148
149     const Uint8 *const keyboard_state = SDL_GetKeyboardState(NULL);
150
151     SDL_StopTextInput();
152     SDL_Event e;
153     const int64_t delta_time = (int64_t) roundf(1000.0f / 60.0f);
154     int64_t render_timer = (int64_t) roundf(1000.0f / (float) fps);
155     while (!game_over_check(game)) {
156         const int64_t begin_frame_time = (int64_t) SDL_GetTicks();
157
158         while (!game_over_check(game) && SDL_PollEvent(&e)) {
159             if (game_event(game, &e) < 0) {
160                 RETURN_LT(lt, -1);
161             }
162         }
163
164         if (game_input(game, keyboard_state, the_stick_of_joy) < 0) {
165             RETURN_LT(lt, -1);
166         }
167
168         if (game_update(game, (float) delta_time * 0.001f) < 0) {
169             RETURN_LT(lt, -1);
170         }
171
172         if (game_sound(game) < 0) {
173             RETURN_LT(lt, -1);
174         }
175
176         render_timer -= delta_time;
177         if (render_timer <= 0) {
178             if (game_render(game) < 0) {
179                 RETURN_LT(lt, -1);
180             }
181             SDL_RenderPresent(renderer);
182             render_timer = (int64_t) roundf(1000.0f / (float) fps);
183         }
184
185         const int64_t end_frame_time = (int64_t) SDL_GetTicks();
186 #define max_int64(a, b) (a > b ? a : b)
187         SDL_Delay((unsigned int) max_int64(10, delta_time - (end_frame_time - begin_frame_time)));
188 #undef max_int64
189     }
190
191     RETURN_LT(lt, 0);
192 }