]> git.lizzy.rs Git - nothing.git/blob - src/main.c
Merge pull request #864 from tsoding/532
[nothing.git] / src / main.c
1 #include <SDL.h>
2
3 #include <stdint.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <time.h>
7
8 #include "game.h"
9 #include "game/level/platforms.h"
10 #include "game/level/player.h"
11 #include "game/sound_samples.h"
12 #include "game/sprite_font.h"
13 #include "math/extrema.h"
14 #include "math/point.h"
15 #include "sdl/renderer.h"
16 #include "system/log.h"
17 #include "system/lt.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-folder>\n");
25 }
26
27 int main(int argc, char *argv[])
28 {
29     srand((unsigned int) time(NULL));
30
31     Lt *lt = create_lt();
32
33     char *level_folder = NULL;
34     int fps = 30;
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                     log_fail("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                 log_fail("Value of FPS is not provided\n");
47                 print_usage(stderr);
48                 RETURN_LT(lt, -1);
49             }
50         } else {
51             level_folder = argv[i];
52             i++;
53         }
54     }
55
56     if (level_folder == NULL) {
57         log_fail("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         log_fail("Could not initialize SDL: %s\n", SDL_GetError());
64         RETURN_LT(lt, -1);
65     }
66     PUSH_LT(lt, 42, SDL_Quit);
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         log_fail("Could not create SDL window: %s\n", SDL_GetError());
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         log_fail("Could not create SDL renderer: %s\n", SDL_GetError());
90         RETURN_LT(lt, -1);
91     }
92     if (SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND) < 0) {
93         log_fail("Could not set up blending mode for the renderer: %s\n", SDL_GetError());
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             log_fail("Could not open 0th Stick of the Joy: %s\n", SDL_GetError());
104             RETURN_LT(lt, -1);
105         }
106
107         log_info("Opened Joystick 0\n");
108         log_info("Name: %s\n", SDL_JoystickNameForIndex(0));
109         log_info("Number of Axes: %d\n", SDL_JoystickNumAxes(the_stick_of_joy));
110         log_info("Number of Buttons: %d\n", SDL_JoystickNumButtons(the_stick_of_joy));
111         log_info("Number of Balls: %d\n", SDL_JoystickNumBalls(the_stick_of_joy));
112
113         SDL_JoystickEventState(SDL_ENABLE);
114     } else {
115         log_warn("Could not find any Sticks of the Joy\n");
116     }
117
118     // ------------------------------
119
120     const char * sound_sample_files[] = {
121         "./sounds/nothing.wav",
122         "./sounds/something.wav"
123     };
124     const size_t sound_sample_files_count = sizeof(sound_sample_files) / sizeof(char*);
125
126     Game *const game = PUSH_LT(
127         lt,
128         create_game(
129             level_folder,
130             sound_sample_files,
131             sound_sample_files_count,
132             renderer),
133         destroy_game);
134     if (game == NULL) {
135         RETURN_LT(lt, -1);
136     }
137
138     const Uint8 *const keyboard_state = SDL_GetKeyboardState(NULL);
139
140     SDL_StopTextInput();
141     SDL_Event e;
142     const int64_t delta_time = (int64_t) roundf(1000.0f / 60.0f);
143     int64_t render_timer = (int64_t) roundf(1000.0f / (float) fps);
144     while (!game_over_check(game)) {
145         const int64_t begin_frame_time = (int64_t) SDL_GetTicks();
146
147         while (!game_over_check(game) && SDL_PollEvent(&e)) {
148             if (game_event(game, &e) < 0) {
149                 RETURN_LT(lt, -1);
150             }
151         }
152
153         if (game_input(game, keyboard_state, the_stick_of_joy) < 0) {
154             RETURN_LT(lt, -1);
155         }
156
157         if (game_update(game, (float) delta_time * 0.001f) < 0) {
158             RETURN_LT(lt, -1);
159         }
160
161         if (game_sound(game) < 0) {
162             RETURN_LT(lt, -1);
163         }
164
165         render_timer -= delta_time;
166         if (render_timer <= 0) {
167             if (game_render(game) < 0) {
168                 RETURN_LT(lt, -1);
169             }
170             SDL_RenderPresent(renderer);
171             render_timer = (int64_t) roundf(1000.0f / (float) fps);
172         }
173
174         const int64_t end_frame_time = (int64_t) SDL_GetTicks();
175         SDL_Delay((unsigned int) max_int64(10, delta_time - (end_frame_time - begin_frame_time)));
176     }
177
178     RETURN_LT(lt, 0);
179 }