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