]> git.lizzy.rs Git - nothing.git/blob - src/main.c
55db244c6eee1ddb9be34fb31d51a5413e391337
[nothing.git] / src / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <SDL2/SDL.h>
4
5 #include "./player.h"
6 #include "./platforms.h"
7 #include "./error.h"
8 #include "./game.h"
9 #include "./lt.h"
10
11 #define SCREEN_WIDTH 800
12 #define SCREEN_HEIGHT 600
13 #define GAME_FPS 60
14 #define LEVEL_FILE_NAME "./levels/platforms.txt"
15
16 /* LT module adapter for SDL_Quit */
17 static void SDL_Quit_lt(void* ignored)
18 {
19     (void) ignored;
20     SDL_Quit();
21 }
22
23 int main(int argc, char *argv[])
24 {
25     (void) argc;                /* unused */
26     (void) argv;                /* unused */
27
28     lt_t *const lt = create_lt();
29
30     if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
31         print_error_msg(ERROR_TYPE_SDL2, "Could not initialize SDL");
32         LT_RETURN(lt, -1);
33     }
34     LT_PUSH(lt, 42, SDL_Quit_lt);
35
36     SDL_Window *const window = LT_PUSH(
37         lt,
38         SDL_CreateWindow("Nothing", 100, 100, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN),
39         SDL_DestroyWindow);
40
41     if (window == NULL) {
42         print_error_msg(ERROR_TYPE_SDL2, "Could not create SDL window");
43         LT_RETURN(lt, -1);
44     }
45
46     SDL_Renderer *const renderer = LT_PUSH(
47         lt,
48         SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC),
49         SDL_DestroyRenderer);
50     if (renderer == NULL) {
51         print_error_msg(ERROR_TYPE_SDL2, "Could not create SDL renderer");
52         LT_RETURN(lt, -1);
53     }
54
55     SDL_Joystick *the_stick_of_joy = NULL;
56
57     if (SDL_NumJoysticks() > 0) {
58         the_stick_of_joy = LT_PUSH(lt, SDL_JoystickOpen(0), SDL_JoystickClose);
59
60         if (the_stick_of_joy == NULL) {
61             print_error_msg(ERROR_TYPE_SDL2, "Could not open 0th Stick of the Joy: %s\n");
62             LT_RETURN(lt, -1);
63         }
64
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));
70
71         SDL_JoystickEventState(SDL_ENABLE);
72     } else {
73         fprintf(stderr, "[WARNING] Could not find any Sticks of the Joy\n");
74     }
75
76     // ------------------------------
77
78     game_t *const game = LT_PUSH(lt, create_game(LEVEL_FILE_NAME), destroy_game);
79     if (game == NULL) {
80         print_current_error_msg("Could not create the game object");
81         LT_RETURN(lt, -1);
82     }
83
84     const Uint8 *const keyboard_state = SDL_GetKeyboardState(NULL);
85     const Uint32 delay_ms = (Uint32) roundf(1000.0f / GAME_FPS);
86     SDL_Event e;
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");
91                 LT_RETURN(lt, -1);
92             }
93         }
94
95         if (game_input(game, keyboard_state, the_stick_of_joy) < 0) {
96             print_current_error_msg("Failed handling input");
97             LT_RETURN(lt, -1);
98         }
99
100         if (game_update(game, delay_ms) < 0) {
101             print_current_error_msg("Failed handling updating");
102             LT_RETURN(lt, -1);
103         }
104
105         if (game_render(game, renderer) < 0) {
106             print_current_error_msg("Failed rendering the game");
107             LT_RETURN(lt, -1);
108         }
109     }
110
111     LT_RETURN(lt, 0);
112 }