5 #include <SDL2/SDL_mixer.h>
7 #include "./game/level/player.h"
8 #include "./game/level/platforms.h"
13 #include "./math/point.h"
14 #include "./game/sound_medium.h"
16 #define SCREEN_WIDTH 800
17 #define SCREEN_HEIGHT 600
20 /* LT module adapter for Mix_CloseAudio */
21 static void Mix_CloseAudio_lt(void* ignored)
27 /* LT module adapter for SDL_Quit */
28 static void SDL_Quit_lt(void* ignored)
34 static void print_usage(FILE *stream)
36 fprintf(stream, "Usage: nothing <level-file>\n");
39 int main(int argc, char *argv[])
41 srand((unsigned int) time(NULL));
43 lt_t *const lt = create_lt();
50 if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
51 print_error_msg(ERROR_TYPE_SDL2, "Could not initialize SDL");
54 PUSH_LT(lt, 42, SDL_Quit_lt);
56 SDL_Window *const window = PUSH_LT(
61 SCREEN_WIDTH, SCREEN_HEIGHT,
62 SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE),
66 print_error_msg(ERROR_TYPE_SDL2, "Could not create SDL window");
70 SDL_Renderer *const renderer = PUSH_LT(
72 SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC),
74 if (renderer == NULL) {
75 print_error_msg(ERROR_TYPE_SDL2, "Could not create SDL renderer");
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");
83 SDL_Joystick *the_stick_of_joy = NULL;
85 if (SDL_NumJoysticks() > 0) {
86 the_stick_of_joy = PUSH_LT(lt, SDL_JoystickOpen(0), SDL_JoystickClose);
88 if (the_stick_of_joy == NULL) {
89 print_error_msg(ERROR_TYPE_SDL2, "Could not open 0th Stick of the Joy: %s\n");
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));
99 SDL_JoystickEventState(SDL_ENABLE);
101 fprintf(stderr, "[WARNING] Could not find any Sticks of the Joy\n");
105 MIX_DEFAULT_FREQUENCY,
109 print_error_msg(ERROR_TYPE_SDL2_MIXER, "Could not initialize the audio\n");
112 PUSH_LT(lt, 42, Mix_CloseAudio_lt);
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)
118 const size_t sound_samples_count = sizeof(sound_samples) / sizeof(Mix_Chunk*);
120 sound_medium_t *sound_medium =
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");
130 // ------------------------------
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");
138 game_t *const game = PUSH_LT(lt, create_game(argv[1], sound_medium), destroy_game);
140 print_current_error_msg("Could not create the game object");
144 const Uint8 *const keyboard_state = SDL_GetKeyboardState(NULL);
145 const float delay_ms = 1.0f / GAME_FPS;
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");
155 if (game_input(game, keyboard_state, the_stick_of_joy) < 0) {
156 print_current_error_msg("Failed handling input");
160 if (game_render(game, renderer) < 0) {
161 print_current_error_msg("Failed rendering the game");
165 if (game_update(game, delay_ms) < 0) {
166 print_current_error_msg("Failed handling updating");
170 if (game_sound(game) < 0) {
171 print_current_error_msg("Failed handling the sound");