]> git.lizzy.rs Git - nothing.git/blob - src/main.c
b5b5f01d3ef60bd7589a0af43ab804fdb4222af3
[nothing.git] / src / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <time.h>
4 #include <SDL2/SDL.h>
5 #include <SDL2/SDL_mixer.h>
6
7 #include "./player.h"
8 #include "./platforms.h"
9 #include "./error.h"
10 #include "./game.h"
11 #include "./lt.h"
12 #include "./path.h"
13
14 #define SCREEN_WIDTH 800
15 #define SCREEN_HEIGHT 600
16 #define GAME_FPS 60
17
18 /* LT module adapter for SDL_Quit */
19 static void SDL_Quit_lt(void* ignored)
20 {
21     (void) ignored;
22     SDL_Quit();
23 }
24
25 static void print_usage(FILE *stream)
26 {
27     fprintf(stream, "Usage: nothing <level-file>\n");
28 }
29
30 int main(int argc, char *argv[])
31 {
32     srand((unsigned int) time(NULL));
33
34     lt_t *const lt = create_lt();
35
36     if (argc < 2) {
37         print_usage(stderr);
38         RETURN_LT(lt, -1);
39     }
40
41     if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
42         print_error_msg(ERROR_TYPE_SDL2, "Could not initialize SDL");
43         RETURN_LT(lt, -1);
44     }
45     PUSH_LT(lt, 42, SDL_Quit_lt);
46
47     SDL_Window *const window = PUSH_LT(
48         lt,
49         SDL_CreateWindow(
50             "Nothing",
51             100, 100,
52             SCREEN_WIDTH, SCREEN_HEIGHT,
53             SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE),
54         SDL_DestroyWindow);
55
56     if (window == NULL) {
57         print_error_msg(ERROR_TYPE_SDL2, "Could not create SDL window");
58         RETURN_LT(lt, -1);
59     }
60
61     SDL_Renderer *const renderer = PUSH_LT(
62         lt,
63         SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC),
64         SDL_DestroyRenderer);
65     if (renderer == NULL) {
66         print_error_msg(ERROR_TYPE_SDL2, "Could not create SDL renderer");
67         RETURN_LT(lt, -1);
68     }
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");
71         RETURN_LT(lt, -1);
72     }
73
74     SDL_Joystick *the_stick_of_joy = NULL;
75
76     if (SDL_NumJoysticks() > 0) {
77         the_stick_of_joy = PUSH_LT(lt, SDL_JoystickOpen(0), SDL_JoystickClose);
78
79         if (the_stick_of_joy == NULL) {
80             print_error_msg(ERROR_TYPE_SDL2, "Could not open 0th Stick of the Joy: %s\n");
81             RETURN_LT(lt, -1);
82         }
83
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));
89
90         SDL_JoystickEventState(SDL_ENABLE);
91     } else {
92         fprintf(stderr, "[WARNING] Could not find any Sticks of the Joy\n");
93     }
94
95     // ------------------------------
96
97     char *sounds_folder = PUSH_LT(lt, base_path_folder("sounds"), free);
98     if (sounds_folder == NULL) {
99         RETURN_LT(lt, -1);
100     }
101
102     game_t *const game = PUSH_LT(lt, create_game(argv[1], sounds_folder), destroy_game);
103     if (game == NULL) {
104         print_current_error_msg("Could not create the game object");
105         RETURN_LT(lt, -1);
106     }
107
108     const Uint8 *const keyboard_state = SDL_GetKeyboardState(NULL);
109     const Uint32 delay_ms = (Uint32) roundf(1000.0f / GAME_FPS);
110     SDL_Event e;
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");
115                 RETURN_LT(lt, -1);
116             }
117         }
118
119         if (game_input(game, keyboard_state, the_stick_of_joy) < 0) {
120             print_current_error_msg("Failed handling input");
121             RETURN_LT(lt, -1);
122         }
123
124         if (game_update(game, delay_ms) < 0) {
125             print_current_error_msg("Failed handling updating");
126             RETURN_LT(lt, -1);
127         }
128
129         if (game_render(game, renderer) < 0) {
130             print_current_error_msg("Failed rendering the game");
131             RETURN_LT(lt, -1);
132         }
133     }
134
135     RETURN_LT(lt, 0);
136 }