5 #include <SDL2/SDL_mixer.h>
8 #include "./platforms.h"
14 #define SCREEN_WIDTH 800
15 #define SCREEN_HEIGHT 600
18 /* LT module adapter for SDL_Quit */
19 static void SDL_Quit_lt(void* ignored)
25 static void print_usage(FILE *stream)
27 fprintf(stream, "Usage: nothing <level-file>\n");
30 int main(int argc, char *argv[])
32 srand((unsigned int) time(NULL));
34 lt_t *const lt = create_lt();
41 if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
42 print_error_msg(ERROR_TYPE_SDL2, "Could not initialize SDL");
45 PUSH_LT(lt, 42, SDL_Quit_lt);
47 SDL_Window *const window = PUSH_LT(
52 SCREEN_WIDTH, SCREEN_HEIGHT,
53 SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE),
57 print_error_msg(ERROR_TYPE_SDL2, "Could not create SDL window");
61 SDL_Renderer *const renderer = PUSH_LT(
63 SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC),
65 if (renderer == NULL) {
66 print_error_msg(ERROR_TYPE_SDL2, "Could not create SDL renderer");
69 if (SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND) < 0) {
70 print_error_msg(ERROR_TYPE_SDL2, "Could not set up blending mode for the renderer");
74 SDL_Joystick *the_stick_of_joy = NULL;
76 if (SDL_NumJoysticks() > 0) {
77 the_stick_of_joy = PUSH_LT(lt, SDL_JoystickOpen(0), SDL_JoystickClose);
79 if (the_stick_of_joy == NULL) {
80 print_error_msg(ERROR_TYPE_SDL2, "Could not open 0th Stick of the Joy: %s\n");
84 printf("Opened Joystick 0\n");
85 printf("Name: %s\n", SDL_JoystickNameForIndex(0));
86 printf("Number of Axes: %d\n", SDL_JoystickNumAxes(the_stick_of_joy));
87 printf("Number of Buttons: %d\n", SDL_JoystickNumButtons(the_stick_of_joy));
88 printf("Number of Balls: %d\n", SDL_JoystickNumBalls(the_stick_of_joy));
90 SDL_JoystickEventState(SDL_ENABLE);
92 fprintf(stderr, "[WARNING] Could not find any Sticks of the Joy\n");
95 // ------------------------------
97 char *sounds_folder = PUSH_LT(lt, base_path_folder("sounds"), free);
98 if (sounds_folder == NULL) {
102 game_t *const game = PUSH_LT(lt, create_game(argv[1], sounds_folder), destroy_game);
104 print_current_error_msg("Could not create the game object");
108 const Uint8 *const keyboard_state = SDL_GetKeyboardState(NULL);
109 const Uint32 delay_ms = (Uint32) roundf(1000.0f / GAME_FPS);
111 while (!game_over_check(game)) {
112 while (!game_over_check(game) && SDL_PollEvent(&e)) {
113 if (game_event(game, &e) < 0) {
114 print_current_error_msg("Failed handling event");
119 if (game_input(game, keyboard_state, the_stick_of_joy) < 0) {
120 print_current_error_msg("Failed handling input");
124 if (game_update(game, delay_ms) < 0) {
125 print_current_error_msg("Failed handling updating");
129 if (game_render(game, renderer) < 0) {
130 print_current_error_msg("Failed rendering the game");