]> git.lizzy.rs Git - nothing.git/blob - src/main.c
Remove the wave state (#90)
[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
15 /* LT module adapter for SDL_Quit */
16 static void SDL_Quit_lt(void* ignored)
17 {
18     (void) ignored;
19     SDL_Quit();
20 }
21
22 static void print_usage(FILE *stream)
23 {
24     fprintf(stream, "Usage: nothing <level-file>\n");
25 }
26
27 int main(int argc, char *argv[])
28 {
29     lt_t *const lt = create_lt();
30
31     if (argc < 2) {
32         print_usage(stderr);
33         RETURN_LT(lt, -1);
34     }
35
36     if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
37         print_error_msg(ERROR_TYPE_SDL2, "Could not initialize SDL");
38         RETURN_LT(lt, -1);
39     }
40     PUSH_LT(lt, 42, SDL_Quit_lt);
41
42     SDL_Window *const window = PUSH_LT(
43         lt,
44         SDL_CreateWindow("Nothing", 100, 100, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN),
45         SDL_DestroyWindow);
46
47     if (window == NULL) {
48         print_error_msg(ERROR_TYPE_SDL2, "Could not create SDL window");
49         RETURN_LT(lt, -1);
50     }
51
52     SDL_Renderer *const renderer = PUSH_LT(
53         lt,
54         SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC),
55         SDL_DestroyRenderer);
56     if (renderer == NULL) {
57         print_error_msg(ERROR_TYPE_SDL2, "Could not create SDL renderer");
58         RETURN_LT(lt, -1);
59     }
60     if (SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND) < 0) {
61         print_error_msg(ERROR_TYPE_SDL2, "Could not set up blending mode for the renderer");
62         RETURN_LT(lt, -1);
63     }
64
65     SDL_Joystick *the_stick_of_joy = NULL;
66
67     if (SDL_NumJoysticks() > 0) {
68         the_stick_of_joy = PUSH_LT(lt, SDL_JoystickOpen(0), SDL_JoystickClose);
69
70         if (the_stick_of_joy == NULL) {
71             print_error_msg(ERROR_TYPE_SDL2, "Could not open 0th Stick of the Joy: %s\n");
72             RETURN_LT(lt, -1);
73         }
74
75         printf("Opened Joystick 0\n");
76         printf("Name: %s\n", SDL_JoystickNameForIndex(0));
77         printf("Number of Axes: %d\n", SDL_JoystickNumAxes(the_stick_of_joy));
78         printf("Number of Buttons: %d\n", SDL_JoystickNumButtons(the_stick_of_joy));
79         printf("Number of Balls: %d\n", SDL_JoystickNumBalls(the_stick_of_joy));
80
81         SDL_JoystickEventState(SDL_ENABLE);
82     } else {
83         fprintf(stderr, "[WARNING] Could not find any Sticks of the Joy\n");
84     }
85
86     // ------------------------------
87
88     game_t *const game = PUSH_LT(lt, create_game(argv[1]), destroy_game);
89     if (game == NULL) {
90         print_current_error_msg("Could not create the game object");
91         RETURN_LT(lt, -1);
92     }
93
94     const Uint8 *const keyboard_state = SDL_GetKeyboardState(NULL);
95     const Uint32 delay_ms = (Uint32) roundf(1000.0f / GAME_FPS);
96     SDL_Event e;
97     while (!game_over_check(game)) {
98         while (!game_over_check(game) && SDL_PollEvent(&e)) {
99             if (game_event(game, &e) < 0) {
100                 print_current_error_msg("Failed handling event");
101                 RETURN_LT(lt, -1);
102             }
103         }
104
105         if (game_input(game, keyboard_state, the_stick_of_joy) < 0) {
106             print_current_error_msg("Failed handling input");
107             RETURN_LT(lt, -1);
108         }
109
110         if (game_update(game, delay_ms) < 0) {
111             print_current_error_msg("Failed handling updating");
112             RETURN_LT(lt, -1);
113         }
114
115         if (game_render(game, renderer) < 0) {
116             print_current_error_msg("Failed rendering the game");
117             RETURN_LT(lt, -1);
118         }
119     }
120
121     RETURN_LT(lt, 0);
122 }