|
- #include <SDL.h>
- #include <stdbool.h>
- #include <stdio.h>
-
- #include "IZ_action.h"
- #include "IZ_app.h"
-
- int main(int argc, char* args[]) {
- SDL_Window* window = NULL;
- SDL_Surface* screen_surface = NULL;
- IZ_App app;
-
- IZ_InitializeApp(&app);
-
- if (SDL_Init(
- SDL_INIT_VIDEO
- | SDL_INIT_GAMECONTROLLER
- | SDL_INIT_EVENTS
- ) < 0) {
- printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
- return -1;
- }
-
- window = SDL_CreateWindow(
- APP_NAME,
- SDL_WINDOWPOS_CENTERED,
- SDL_WINDOWPOS_CENTERED,
- app.config.video.width,
- app.config.video.height,
- SDL_WINDOW_SHOWN
- );
-
- if (window == NULL) {
- printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
- return -2;
- }
-
- screen_surface = SDL_GetWindowSurface(window);
-
- bool quit = false;
- SDL_Event e;
-
- while (true) {
- uint64_t ticks = SDL_GetTicks64();
-
- while (SDL_PollEvent(&e) != 0) {
- if (e.type == SDL_QUIT) {
- quit = true;
- break;
- }
-
- // Handle joystick events
- for (uint8_t current_player = 0; current_player < PLAYERS; current_player += 1) {
- if (e.type == SDL_JOYDEVICEADDED) {
- if (SDL_NumJoysticks() <= PLAYERS && !app.assigned_joysticks[current_player]) {
- app.assigned_joysticks[current_player] = SDL_JoystickOpen(e.jdevice.which);
- }
- }
-
- if (e.type == SDL_JOYDEVICEREMOVED) {
- if (
- app.assigned_joysticks[current_player]
- && SDL_JoystickInstanceID(app.assigned_joysticks[current_player]) == e.jdevice.which
- ) {
- app.assigned_joysticks[current_player] = NULL;
- }
- }
-
- if (e.type == SDL_JOYAXISMOTION) {
- if (e.jaxis.axis == 0 || e.jaxis.axis == 3) {
- app.actions[current_player] &= ~(0x1 << IZ_ACTION_INDEX_RIGHT);
- app.actions[current_player] &= ~(0x1 << IZ_ACTION_INDEX_LEFT);
- if (e.jaxis.value > app.config.input.gamepad_axis_threshold) {
- app.actions[current_player] |= (0x1 << IZ_ACTION_INDEX_RIGHT);
- } else if (e.jaxis.value <= -app.config.input.gamepad_axis_threshold) {
- app.actions[current_player] |= (0x1 << IZ_ACTION_INDEX_LEFT);
- }
- }
- if (e.jaxis.axis == 1 || e.jaxis.axis == 4) {
- app.actions[current_player] &= ~(0x1 << IZ_ACTION_INDEX_UP);
- app.actions[current_player] &= ~(0x1 << IZ_ACTION_INDEX_DOWN);
- if (e.jaxis.value > app.config.input.gamepad_axis_threshold) {
- app.actions[current_player] |= (0x1 << IZ_ACTION_INDEX_DOWN);
- } else if (e.jaxis.value <= -app.config.input.gamepad_axis_threshold) {
- app.actions[current_player] |= (0x1 << IZ_ACTION_INDEX_UP);
- }
- }
- }
-
- if (e.type == SDL_JOYHATMOTION) {
- app.actions[current_player] &= ~(0x1 << IZ_ACTION_INDEX_UP);
- app.actions[current_player] &= ~(0x1 << IZ_ACTION_INDEX_RIGHT);
- app.actions[current_player] &= ~(0x1 << IZ_ACTION_INDEX_DOWN);
- app.actions[current_player] &= ~(0x1 << IZ_ACTION_INDEX_LEFT);
- if (e.jhat.value != 0) {
- app.actions[current_player] |= e.jhat.value;
- }
- }
- }
-
- // Handle keyboard events
- for (uint8_t current_player = 0; current_player < PLAYERS; current_player += 1) {
- for (uint8_t i = 0; i < CONTROLS; i += 1) {
- if (e.key.keysym.sym == app.config.controls[current_player].keyboard[i]) {
- const uint16_t bitflag = (0x1 << i);
- if (e.type == SDL_KEYDOWN) {
- app.actions[current_player] |= bitflag;
- } else if (e.type == SDL_KEYUP) {
- app.actions[current_player] &= ~bitflag;
- }
- }
-
- if (i >= 4) {
- if (e.jbutton.button == app.config.controls[current_player].gamepad[i]) {
- const uint16_t bitflag = (0x1 << i);
- if (e.type == SDL_JOYBUTTONDOWN) {
- app.actions[current_player] |= bitflag;
- } else if (e.type == SDL_JOYBUTTONUP) {
- app.actions[current_player] &= ~bitflag;
- }
- }
- }
- }
- }
- }
-
- if (quit) {
- break;
- }
-
- if (ticks - app.video_update_at > 1000 / app.config.video.max_fps) {
- // Update window
- SDL_FillRect(screen_surface, NULL, SDL_MapRGB(screen_surface->format, 0x00, 0x00, 0x00));
-
- for (uint8_t i = 0; i < 64; i += 1) {
- const uint8_t column = (64 - i) % 32;
- const uint8_t row = i / 32;
- const uint64_t bitflag = (0x1lu << i);
- const uint8_t size = 4;
- if (ticks & bitflag) {
- SDL_FillRect(screen_surface, &(SDL_Rect) {
- column * size,
- app.config.video.height - ((row + 1) * size),
- size,
- size
- }, SDL_MapRGB(screen_surface->format, 0x00, 0xff, 0xff));
- }
- }
-
- for (uint8_t current_player = 0; current_player < PLAYERS; current_player += 1) {
- for (uint8_t i = 0; i < CONTROLS; i += 1) {
- const uint8_t column = i % 4;
- const uint8_t row = i / 4;
- const IZ_Action bitflag = (0x1 << i);
- const uint8_t size = 4;
- if (app.actions[current_player] & bitflag) {
- SDL_FillRect(screen_surface, &(SDL_Rect) {
- column * size,
- row * size,
- size,
- size
- }, SDL_MapRGB(screen_surface->format, 0xff, 0xff, 0x00));
- }
- }
- }
- SDL_UpdateWindowSurface(window);
-
- app.video_update_at = ticks;
- }
- }
- SDL_DestroyWindow(window);
- SDL_Quit();
-
- return 0;
- }
|