5 #include <SDL2/SDL_mixer.h>
8 #include "./platforms.h"
13 #define SCREEN_WIDTH 800
14 #define SCREEN_HEIGHT 600
17 /* LT module adapter for SDL_Quit */
18 static void SDL_Quit_lt(void* ignored)
24 static void print_usage(FILE *stream)
26 fprintf(stream, "Usage: nothing <level-file>\n");
29 int main(int argc, char *argv[])
31 srand((unsigned int) time(NULL));
33 lt_t *const lt = create_lt();
40 if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
41 print_error_msg(ERROR_TYPE_SDL2, "Could not initialize SDL");
44 PUSH_LT(lt, 42, SDL_Quit_lt);
46 /* TODO(#128): introduce some kind of sound medium where all of the sound samples are propagated */
48 SDL_Window *const window = PUSH_LT(
50 SDL_CreateWindow("Nothing", 100, 100, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN),
54 print_error_msg(ERROR_TYPE_SDL2, "Could not create SDL window");
58 SDL_Renderer *const renderer = PUSH_LT(
60 SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC),
62 if (renderer == NULL) {
63 print_error_msg(ERROR_TYPE_SDL2, "Could not create SDL renderer");
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");
71 SDL_Joystick *the_stick_of_joy = NULL;
73 if (SDL_NumJoysticks() > 0) {
74 the_stick_of_joy = PUSH_LT(lt, SDL_JoystickOpen(0), SDL_JoystickClose);
76 if (the_stick_of_joy == NULL) {
77 print_error_msg(ERROR_TYPE_SDL2, "Could not open 0th Stick of the Joy: %s\n");
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));
87 SDL_JoystickEventState(SDL_ENABLE);
89 fprintf(stderr, "[WARNING] Could not find any Sticks of the Joy\n");
92 // ------------------------------
94 game_t *const game = PUSH_LT(lt, create_game(argv[1]), destroy_game);
96 print_current_error_msg("Could not create the game object");
100 const Uint8 *const keyboard_state = SDL_GetKeyboardState(NULL);
101 const Uint32 delay_ms = (Uint32) roundf(1000.0f / GAME_FPS);
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");
111 if (game_input(game, keyboard_state, the_stick_of_joy) < 0) {
112 print_current_error_msg("Failed handling input");
116 if (game_update(game, delay_ms) < 0) {
117 print_current_error_msg("Failed handling updating");
121 if (game_render(game, renderer) < 0) {
122 print_current_error_msg("Failed rendering the game");