6 #include "./platforms.h"
11 #define SCREEN_WIDTH 800
12 #define SCREEN_HEIGHT 600
14 #define LEVEL_FILE_NAME "./levels/platforms.txt"
16 /* LT module adapter for SDL_Quit */
17 static void SDL_Quit_lt(void* ignored)
23 int main(int argc, char *argv[])
25 (void) argc; /* unused */
26 (void) argv; /* unused */
28 lt_t *const lt = create_lt();
30 if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
31 print_error_msg(ERROR_TYPE_SDL2, "Could not initialize SDL");
34 LT_PUSH(lt, 42, SDL_Quit_lt);
36 SDL_Window *const window = LT_PUSH(
38 SDL_CreateWindow("Nothing", 100, 100, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN),
42 print_error_msg(ERROR_TYPE_SDL2, "Could not create SDL window");
46 SDL_Renderer *const renderer = LT_PUSH(
48 SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC),
50 if (renderer == NULL) {
51 print_error_msg(ERROR_TYPE_SDL2, "Could not create SDL renderer");
55 SDL_Joystick *the_stick_of_joy = NULL;
57 if (SDL_NumJoysticks() > 0) {
58 the_stick_of_joy = LT_PUSH(lt, SDL_JoystickOpen(0), SDL_JoystickClose);
60 if (the_stick_of_joy == NULL) {
61 print_error_msg(ERROR_TYPE_SDL2, "Could not open 0th Stick of the Joy: %s\n");
65 printf("Opened Joystick 0\n");
66 printf("Name: %s\n", SDL_JoystickNameForIndex(0));
67 printf("Number of Axes: %d\n", SDL_JoystickNumAxes(the_stick_of_joy));
68 printf("Number of Buttons: %d\n", SDL_JoystickNumButtons(the_stick_of_joy));
69 printf("Number of Balls: %d\n", SDL_JoystickNumBalls(the_stick_of_joy));
71 SDL_JoystickEventState(SDL_ENABLE);
73 fprintf(stderr, "[WARNING] Could not find any Sticks of the Joy\n");
76 // ------------------------------
78 game_t *const game = LT_PUSH(lt, create_game(LEVEL_FILE_NAME), destroy_game);
80 print_current_error_msg("Could not create the game object");
84 const Uint8 *const keyboard_state = SDL_GetKeyboardState(NULL);
85 const Uint32 delay_ms = (Uint32) roundf(1000.0f / GAME_FPS);
87 while (!is_game_over(game)) {
88 while (!is_game_over(game) && SDL_PollEvent(&e)) {
89 if (game_event(game, &e) < 0) {
90 print_current_error_msg("Failed handling event");
95 if (game_input(game, keyboard_state, the_stick_of_joy) < 0) {
96 print_current_error_msg("Failed handling input");
100 if (game_update(game, delay_ms) < 0) {
101 print_current_error_msg("Failed handling updating");
105 if (game_render(game, renderer) < 0) {
106 print_current_error_msg("Failed rendering the game");