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