]> git.lizzy.rs Git - nothing.git/blob - src/main.c
Move point to math "subpackage"
[nothing.git] / src / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <time.h>
4 #include <SDL2/SDL.h>
5 #include <SDL2/SDL_mixer.h>
6
7 #include "./game/level/player.h"
8 #include "./game/level/platforms.h"
9 #include "./error.h"
10 #include "./game.h"
11 #include "./lt.h"
12 #include "./path.h"
13 #include "./math/point.h"
14 #include "./game/sound_medium.h"
15
16 #define SCREEN_WIDTH 800
17 #define SCREEN_HEIGHT 600
18 #define GAME_FPS 60
19
20 /* LT module adapter for Mix_CloseAudio */
21 static void Mix_CloseAudio_lt(void* ignored)
22 {
23     (void) ignored;
24     Mix_CloseAudio();
25 }
26
27 /* LT module adapter for SDL_Quit */
28 static void SDL_Quit_lt(void* ignored)
29 {
30     (void) ignored;
31     SDL_Quit();
32 }
33
34 static void print_usage(FILE *stream)
35 {
36     fprintf(stream, "Usage: nothing <level-file>\n");
37 }
38
39 int main(int argc, char *argv[])
40 {
41     srand((unsigned int) time(NULL));
42
43     lt_t *const lt = create_lt();
44
45     if (argc < 2) {
46         print_usage(stderr);
47         RETURN_LT(lt, -1);
48     }
49
50     if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
51         print_error_msg(ERROR_TYPE_SDL2, "Could not initialize SDL");
52         RETURN_LT(lt, -1);
53     }
54     PUSH_LT(lt, 42, SDL_Quit_lt);
55
56     SDL_Window *const window = PUSH_LT(
57         lt,
58         SDL_CreateWindow(
59             "Nothing",
60             100, 100,
61             SCREEN_WIDTH, SCREEN_HEIGHT,
62             SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE),
63         SDL_DestroyWindow);
64
65     if (window == NULL) {
66         print_error_msg(ERROR_TYPE_SDL2, "Could not create SDL window");
67         RETURN_LT(lt, -1);
68     }
69
70     SDL_Renderer *const renderer = PUSH_LT(
71         lt,
72         SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC),
73         SDL_DestroyRenderer);
74     if (renderer == NULL) {
75         print_error_msg(ERROR_TYPE_SDL2, "Could not create SDL renderer");
76         RETURN_LT(lt, -1);
77     }
78     if (SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND) < 0) {
79         print_error_msg(ERROR_TYPE_SDL2, "Could not set up blending mode for the renderer");
80         RETURN_LT(lt, -1);
81     }
82
83     SDL_Joystick *the_stick_of_joy = NULL;
84
85     if (SDL_NumJoysticks() > 0) {
86         the_stick_of_joy = PUSH_LT(lt, SDL_JoystickOpen(0), SDL_JoystickClose);
87
88         if (the_stick_of_joy == NULL) {
89             print_error_msg(ERROR_TYPE_SDL2, "Could not open 0th Stick of the Joy: %s\n");
90             RETURN_LT(lt, -1);
91         }
92
93         printf("Opened Joystick 0\n");
94         printf("Name: %s\n", SDL_JoystickNameForIndex(0));
95         printf("Number of Axes: %d\n", SDL_JoystickNumAxes(the_stick_of_joy));
96         printf("Number of Buttons: %d\n", SDL_JoystickNumButtons(the_stick_of_joy));
97         printf("Number of Balls: %d\n", SDL_JoystickNumBalls(the_stick_of_joy));
98
99         SDL_JoystickEventState(SDL_ENABLE);
100     } else {
101         fprintf(stderr, "[WARNING] Could not find any Sticks of the Joy\n");
102     }
103
104     if (Mix_OpenAudio(
105             MIX_DEFAULT_FREQUENCY,
106             MIX_DEFAULT_FORMAT,
107             2,
108             1024) < 0) {
109         print_error_msg(ERROR_TYPE_SDL2_MIXER, "Could not initialize the audio\n");
110         RETURN_LT(lt, -1);
111     }
112     PUSH_LT(lt, 42, Mix_CloseAudio_lt);
113
114     Mix_Chunk * sound_samples[] = {
115         PUSH_LT(lt, Mix_LoadWAV("./sounds/nothing.wav"), Mix_FreeChunk),
116         PUSH_LT(lt, Mix_LoadWAV("./sounds/something.wav"), Mix_FreeChunk)
117     };
118     const size_t sound_samples_count = sizeof(sound_samples) / sizeof(Mix_Chunk*);
119
120     sound_medium_t *sound_medium =
121         PUSH_LT(
122             lt,
123             create_sound_medium(sound_samples, sound_samples_count, 0, MIX_CHANNELS - 1),
124             destroy_sound_medium);
125     if (sound_medium == NULL) {
126         print_current_error_msg("Could not create sound medium");
127         RETURN_LT(lt, -1);
128     }
129
130     // ------------------------------
131
132     char *sounds_folder = PUSH_LT(lt, base_path_folder("sounds"), free);
133     if (sounds_folder == NULL) {
134         print_current_error_msg("Could not get the sounds folder");
135         RETURN_LT(lt, -1);
136     }
137
138     game_t *const game = PUSH_LT(lt, create_game(argv[1], sound_medium), destroy_game);
139     if (game == NULL) {
140         print_current_error_msg("Could not create the game object");
141         RETURN_LT(lt, -1);
142     }
143
144     const Uint8 *const keyboard_state = SDL_GetKeyboardState(NULL);
145     const float delay_ms = 1.0f / GAME_FPS;
146     SDL_Event e;
147     while (!game_over_check(game)) {
148         while (!game_over_check(game) && SDL_PollEvent(&e)) {
149             if (game_event(game, &e) < 0) {
150                 print_current_error_msg("Failed handling event");
151                 RETURN_LT(lt, -1);
152             }
153         }
154
155         if (game_input(game, keyboard_state, the_stick_of_joy) < 0) {
156             print_current_error_msg("Failed handling input");
157             RETURN_LT(lt, -1);
158         }
159
160         if (game_render(game, renderer) < 0) {
161             print_current_error_msg("Failed rendering the game");
162             RETURN_LT(lt, -1);
163         }
164
165         if (game_update(game, delay_ms) < 0) {
166             print_current_error_msg("Failed handling updating");
167             RETURN_LT(lt, -1);
168         }
169
170         if (game_sound(game) < 0) {
171             print_current_error_msg("Failed handling the sound");
172             RETURN_LT(lt, -1);
173         }
174     }
175
176     RETURN_LT(lt, 0);
177 }