]> git.lizzy.rs Git - nothing.git/blob - src/main.c
(#135) Play sounds on a free channel
[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_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(lt, create_sound_medium(sound_samples, sound_samples_count), destroy_sound_medium);
122
123     // ------------------------------
124
125     char *sounds_folder = PUSH_LT(lt, base_path_folder("sounds"), free);
126     if (sounds_folder == NULL) {
127         print_current_error_msg("Could not get the sounds folder");
128         RETURN_LT(lt, -1);
129     }
130
131     game_t *const game = PUSH_LT(lt, create_game(argv[1], sound_medium), destroy_game);
132     if (game == NULL) {
133         print_current_error_msg("Could not create the game object");
134         RETURN_LT(lt, -1);
135     }
136
137     const Uint8 *const keyboard_state = SDL_GetKeyboardState(NULL);
138     const Uint32 delay_ms = (Uint32) roundf(1000.0f / GAME_FPS);
139     SDL_Event e;
140     while (!game_over_check(game)) {
141         while (!game_over_check(game) && SDL_PollEvent(&e)) {
142             if (game_event(game, &e) < 0) {
143                 print_current_error_msg("Failed handling event");
144                 RETURN_LT(lt, -1);
145             }
146         }
147
148         if (game_input(game, keyboard_state, the_stick_of_joy) < 0) {
149             print_current_error_msg("Failed handling input");
150             RETURN_LT(lt, -1);
151         }
152
153         if (game_update(game, delay_ms) < 0) {
154             print_current_error_msg("Failed handling updating");
155             RETURN_LT(lt, -1);
156         }
157
158         if (game_render(game, renderer) < 0) {
159             print_current_error_msg("Failed rendering the game");
160             RETURN_LT(lt, -1);
161         }
162     }
163
164     RETURN_LT(lt, 0);
165 }