]> git.lizzy.rs Git - nothing.git/blob - src/main.c
a5decc1e5b83ec21700fec55a2b58f3c2e546858
[nothing.git] / src / main.c
1 #include <SDL2/SDL.h>
2 #include <SDL2/SDL_mixer.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <time.h>
6
7 #include "game.h"
8 #include "game/level/platforms.h"
9 #include "game/level/player.h"
10 #include "game/sound_medium.h"
11 #include "math/point.h"
12 #include "system/error.h"
13 #include "system/lt.h"
14
15 #define SCREEN_WIDTH 800
16 #define SCREEN_HEIGHT 600
17 #define GAME_FPS 60
18
19 /* LT module adapter for Mix_CloseAudio */
20 static void Mix_CloseAudio_lt(void* ignored)
21 {
22     (void) ignored;
23     Mix_CloseAudio();
24 }
25
26 /* LT module adapter for SDL_Quit */
27 static void SDL_Quit_lt(void* ignored)
28 {
29     (void) ignored;
30     SDL_Quit();
31 }
32
33 static void print_usage(FILE *stream)
34 {
35     fprintf(stream, "Usage: nothing <level-file>\n");
36 }
37
38 int main(int argc, char *argv[])
39 {
40     srand((unsigned int) time(NULL));
41
42     lt_t *const lt = create_lt();
43
44     if (argc < 2) {
45         print_usage(stderr);
46         RETURN_LT(lt, -1);
47     }
48
49     if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
50         print_error_msg(ERROR_TYPE_SDL2, "Could not initialize SDL");
51         RETURN_LT(lt, -1);
52     }
53     PUSH_LT(lt, 42, SDL_Quit_lt);
54
55     SDL_Window *const window = PUSH_LT(
56         lt,
57         SDL_CreateWindow(
58             "Nothing",
59             100, 100,
60             SCREEN_WIDTH, SCREEN_HEIGHT,
61             SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE),
62         SDL_DestroyWindow);
63
64     if (window == NULL) {
65         print_error_msg(ERROR_TYPE_SDL2, "Could not create SDL window");
66         RETURN_LT(lt, -1);
67     }
68
69     SDL_Renderer *const renderer = PUSH_LT(
70         lt,
71         SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC),
72         SDL_DestroyRenderer);
73     if (renderer == NULL) {
74         print_error_msg(ERROR_TYPE_SDL2, "Could not create SDL renderer");
75         RETURN_LT(lt, -1);
76     }
77     if (SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND) < 0) {
78         print_error_msg(ERROR_TYPE_SDL2, "Could not set up blending mode for the renderer");
79         RETURN_LT(lt, -1);
80     }
81
82     SDL_Joystick *the_stick_of_joy = NULL;
83
84     if (SDL_NumJoysticks() > 0) {
85         the_stick_of_joy = PUSH_LT(lt, SDL_JoystickOpen(0), SDL_JoystickClose);
86
87         if (the_stick_of_joy == NULL) {
88             print_error_msg(ERROR_TYPE_SDL2, "Could not open 0th Stick of the Joy: %s\n");
89             RETURN_LT(lt, -1);
90         }
91
92         printf("Opened Joystick 0\n");
93         printf("Name: %s\n", SDL_JoystickNameForIndex(0));
94         printf("Number of Axes: %d\n", SDL_JoystickNumAxes(the_stick_of_joy));
95         printf("Number of Buttons: %d\n", SDL_JoystickNumButtons(the_stick_of_joy));
96         printf("Number of Balls: %d\n", SDL_JoystickNumBalls(the_stick_of_joy));
97
98         SDL_JoystickEventState(SDL_ENABLE);
99     } else {
100         fprintf(stderr, "[WARNING] Could not find any Sticks of the Joy\n");
101     }
102
103     if (Mix_OpenAudio(
104             MIX_DEFAULT_FREQUENCY,
105             MIX_DEFAULT_FORMAT,
106             2,
107             1024) < 0) {
108         print_error_msg(ERROR_TYPE_SDL2_MIXER, "Could not initialize the audio\n");
109         RETURN_LT(lt, -1);
110     }
111     PUSH_LT(lt, 42, Mix_CloseAudio_lt);
112
113     Mix_Chunk * sound_samples[] = {
114         PUSH_LT(lt, Mix_LoadWAV("./sounds/nothing.wav"), Mix_FreeChunk),
115         PUSH_LT(lt, Mix_LoadWAV("./sounds/something.wav"), Mix_FreeChunk)
116     };
117     const size_t sound_samples_count = sizeof(sound_samples) / sizeof(Mix_Chunk*);
118
119     sound_medium_t *sound_medium =
120         PUSH_LT(
121             lt,
122             create_sound_medium(sound_samples, sound_samples_count, 0, MIX_CHANNELS - 1),
123             destroy_sound_medium);
124     if (sound_medium == NULL) {
125         print_current_error_msg("Could not create sound medium");
126         RETURN_LT(lt, -1);
127     }
128
129     // ------------------------------
130
131     game_t *const game = PUSH_LT(lt, create_game(argv[1], sound_medium), destroy_game);
132     if (game == NULL) {
133         print_current_error_msg("Could not create the game object");
134         RETURN_LT(lt, -1);
135     }
136
137     const Uint8 *const keyboard_state = SDL_GetKeyboardState(NULL);
138     const float delay_ms = 1.0f / GAME_FPS;
139     SDL_Event e;
140     while (!game_over_check(game)) {
141         while (!game_over_check(game) && SDL_PollEvent(&e)) {
142             if (game_event(game, &e) < 0) {
143                 print_current_error_msg("Failed handling event");
144                 RETURN_LT(lt, -1);
145             }
146         }
147
148         if (game_input(game, keyboard_state, the_stick_of_joy) < 0) {
149             print_current_error_msg("Failed handling input");
150             RETURN_LT(lt, -1);
151         }
152
153         if (game_render(game, renderer) < 0) {
154             print_current_error_msg("Failed rendering the game");
155             RETURN_LT(lt, -1);
156         }
157
158         if (game_update(game, delay_ms) < 0) {
159             print_current_error_msg("Failed handling updating");
160             RETURN_LT(lt, -1);
161         }
162
163         if (game_sound(game) < 0) {
164             print_current_error_msg("Failed handling the sound");
165             RETURN_LT(lt, -1);
166         }
167     }
168
169     RETURN_LT(lt, 0);
170 }