]> git.lizzy.rs Git - nothing.git/blob - src/main.c
(#101) Load sounds
[nothing.git] / src / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <SDL2/SDL.h>
4 #include <SDL2/SDL_mixer.h>
5
6 #include "./player.h"
7 #include "./platforms.h"
8 #include "./error.h"
9 #include "./game.h"
10 #include "./lt.h"
11
12 #define SCREEN_WIDTH 800
13 #define SCREEN_HEIGHT 600
14 #define GAME_FPS 60
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 /* LT module adapter for Mix_Quit */
24 static void Mix_Quit_lt(void* ignored)
25 {
26     (void) ignored;
27     Mix_Quit();
28 }
29
30 static void print_usage(FILE *stream)
31 {
32     fprintf(stream, "Usage: nothing <level-file>\n");
33 }
34
35 int main(int argc, char *argv[])
36 {
37     lt_t *const lt = create_lt();
38
39     if (argc < 2) {
40         print_usage(stderr);
41         RETURN_LT(lt, -1);
42     }
43
44     if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
45         print_error_msg(ERROR_TYPE_SDL2, "Could not initialize SDL");
46         RETURN_LT(lt, -1);
47     }
48     PUSH_LT(lt, 42, SDL_Quit_lt);
49
50     if ((Mix_Init(MIX_INIT_MP3) & MIX_INIT_MP3) != MIX_INIT_MP3) {
51         print_error_msg(ERROR_TYPE_SDL2_MIXER, "Could not initialize SDL_mixer");
52         RETURN_LT(lt, -1);
53     }
54     PUSH_LT(lt, 42, Mix_Quit_lt);
55
56     if (Mix_OpenAudio(22050, AUDIO_S16SYS, 2, 640) < 0) {
57         print_error_msg(ERROR_TYPE_SDL2_MIXER, "Could not initialize SDL_mixer twice");
58         RETURN_LT(lt, -1);
59     }
60
61     SDL_Window *const window = PUSH_LT(
62         lt,
63         SDL_CreateWindow("Nothing", 100, 100, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN),
64         SDL_DestroyWindow);
65
66     if (window == NULL) {
67         print_error_msg(ERROR_TYPE_SDL2, "Could not create SDL window");
68         RETURN_LT(lt, -1);
69     }
70
71     SDL_Renderer *const renderer = PUSH_LT(
72         lt,
73         SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC),
74         SDL_DestroyRenderer);
75     if (renderer == NULL) {
76         print_error_msg(ERROR_TYPE_SDL2, "Could not create SDL renderer");
77         RETURN_LT(lt, -1);
78     }
79     if (SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND) < 0) {
80         print_error_msg(ERROR_TYPE_SDL2, "Could not set up blending mode for the renderer");
81         RETURN_LT(lt, -1);
82     }
83
84     SDL_Joystick *the_stick_of_joy = NULL;
85
86     if (SDL_NumJoysticks() > 0) {
87         the_stick_of_joy = PUSH_LT(lt, SDL_JoystickOpen(0), SDL_JoystickClose);
88
89         if (the_stick_of_joy == NULL) {
90             print_error_msg(ERROR_TYPE_SDL2, "Could not open 0th Stick of the Joy: %s\n");
91             RETURN_LT(lt, -1);
92         }
93
94         printf("Opened Joystick 0\n");
95         printf("Name: %s\n", SDL_JoystickNameForIndex(0));
96         printf("Number of Axes: %d\n", SDL_JoystickNumAxes(the_stick_of_joy));
97         printf("Number of Buttons: %d\n", SDL_JoystickNumButtons(the_stick_of_joy));
98         printf("Number of Balls: %d\n", SDL_JoystickNumBalls(the_stick_of_joy));
99
100         SDL_JoystickEventState(SDL_ENABLE);
101     } else {
102         fprintf(stderr, "[WARNING] Could not find any Sticks of the Joy\n");
103     }
104
105     // ------------------------------
106
107     game_t *const game = PUSH_LT(lt, create_game(argv[1]), destroy_game);
108     if (game == NULL) {
109         print_current_error_msg("Could not create the game object");
110         RETURN_LT(lt, -1);
111     }
112
113     const Uint8 *const keyboard_state = SDL_GetKeyboardState(NULL);
114     const Uint32 delay_ms = (Uint32) roundf(1000.0f / GAME_FPS);
115     SDL_Event e;
116     while (!game_over_check(game)) {
117         while (!game_over_check(game) && SDL_PollEvent(&e)) {
118             if (game_event(game, &e) < 0) {
119                 print_current_error_msg("Failed handling event");
120                 RETURN_LT(lt, -1);
121             }
122         }
123
124         if (game_input(game, keyboard_state, the_stick_of_joy) < 0) {
125             print_current_error_msg("Failed handling input");
126             RETURN_LT(lt, -1);
127         }
128
129         if (game_update(game, delay_ms) < 0) {
130             print_current_error_msg("Failed handling updating");
131             RETURN_LT(lt, -1);
132         }
133
134         if (game_render(game, renderer) < 0) {
135             print_current_error_msg("Failed rendering the game");
136             RETURN_LT(lt, -1);
137         }
138     }
139
140     RETURN_LT(lt, 0);
141 }