2 #include <SDL2/SDL_mixer.h>
8 #include "game/level/platforms.h"
9 #include "game/level/player.h"
10 #include "game/sound_medium.h"
11 #include "math/point.h"
12 #include "system/error.h"
13 #include "system/lt.h"
15 #define SCREEN_WIDTH 800
16 #define SCREEN_HEIGHT 600
19 /* LT module adapter for Mix_CloseAudio */
20 static void Mix_CloseAudio_lt(void* ignored)
26 /* LT module adapter for SDL_Quit */
27 static void SDL_Quit_lt(void* ignored)
33 static void print_usage(FILE *stream)
35 fprintf(stream, "Usage: nothing <level-file>\n");
38 int main(int argc, char *argv[])
40 srand((unsigned int) time(NULL));
42 lt_t *const lt = create_lt();
49 if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
50 print_error_msg(ERROR_TYPE_SDL2, "Could not initialize SDL");
53 PUSH_LT(lt, 42, SDL_Quit_lt);
55 SDL_ShowCursor(SDL_DISABLE);
57 SDL_Window *const window = PUSH_LT(
62 SCREEN_WIDTH, SCREEN_HEIGHT,
63 SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE),
67 print_error_msg(ERROR_TYPE_SDL2, "Could not create SDL window");
71 SDL_Renderer *const renderer = PUSH_LT(
73 SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC),
75 if (renderer == NULL) {
76 print_error_msg(ERROR_TYPE_SDL2, "Could not create SDL renderer");
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");
84 SDL_Joystick *the_stick_of_joy = NULL;
86 if (SDL_NumJoysticks() > 0) {
87 the_stick_of_joy = PUSH_LT(lt, SDL_JoystickOpen(0), SDL_JoystickClose);
89 if (the_stick_of_joy == NULL) {
90 print_error_msg(ERROR_TYPE_SDL2, "Could not open 0th Stick of the Joy: %s\n");
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));
100 SDL_JoystickEventState(SDL_ENABLE);
102 fprintf(stderr, "[WARNING] Could not find any Sticks of the Joy\n");
106 MIX_DEFAULT_FREQUENCY,
110 print_error_msg(ERROR_TYPE_SDL2_MIXER, "Could not initialize the audio\n");
113 PUSH_LT(lt, 42, Mix_CloseAudio_lt);
115 Mix_Chunk * sound_samples[] = {
116 PUSH_LT(lt, Mix_LoadWAV("./sounds/nothing.wav"), Mix_FreeChunk),
117 PUSH_LT(lt, Mix_LoadWAV("./sounds/something.wav"), Mix_FreeChunk)
119 const size_t sound_samples_count = sizeof(sound_samples) / sizeof(Mix_Chunk*);
121 sound_medium_t *sound_medium =
124 create_sound_medium(sound_samples, sound_samples_count, 0, MIX_CHANNELS - 1),
125 destroy_sound_medium);
126 if (sound_medium == NULL) {
127 print_current_error_msg("Could not create sound medium");
131 // ------------------------------
133 game_t *const game = PUSH_LT(lt, create_game(argv[1], sound_medium, renderer), destroy_game);
135 print_current_error_msg("Could not create the game object");
139 const Uint8 *const keyboard_state = SDL_GetKeyboardState(NULL);
140 const float delay_ms = 1.0f / GAME_FPS;
142 while (!game_over_check(game)) {
143 while (!game_over_check(game) && SDL_PollEvent(&e)) {
144 if (game_event(game, &e) < 0) {
145 print_current_error_msg("Failed handling event");
150 if (game_input(game, keyboard_state, the_stick_of_joy) < 0) {
151 print_current_error_msg("Failed handling input");
155 if (game_render(game, renderer) < 0) {
156 print_current_error_msg("Failed rendering the game");
160 if (game_update(game, delay_ms) < 0) {
161 print_current_error_msg("Failed handling updating");
165 if (game_sound(game) < 0) {
166 print_current_error_msg("Failed handling the sound");