|
- #include <SDL.h>
- #include <stdbool.h>
- #include <stdio.h>
- #include <string.h>
- #include <minIni.h>
-
- const char* APP_NAME = "SDL2";
- static const unsigned char CONTROLS = 16;
- static const unsigned char PLAYERS = 1;
-
- static const SDL_KeyCode KEYBOARD_CONTROLS[CONTROLS] = {
- SDLK_RIGHT,
- SDLK_DOWN,
- SDLK_LEFT,
- SDLK_UP,
- SDLK_RETURN, // yes
- SDLK_BACKSPACE, // no
- SDLK_a, // action0
- SDLK_s, // action1
- SDLK_d, // action2
- SDLK_f, // action3
- SDLK_z, // action4
- SDLK_x, // action5
- SDLK_c, // action6
- SDLK_v, // action7
- SDLK_w, // action8
- SDLK_e, // action9
- };
-
- static const char* ACTION_NAMES[CONTROLS] = {
- "Right",
- "Down",
- "Left",
- "Up",
- "Affirm",
- "Negate",
- "Action0",
- "Action1",
- "Action2",
- "Action3",
- "Action4",
- "Action5",
- "Action6",
- "Action7",
- "Action8",
- "Action9",
- };
-
- typedef struct {
- unsigned int width;
- unsigned int height;
- } IZ_VideoConfig;
-
- typedef SDL_KeyCode IZ_KeyCode;
-
- typedef int IZ_PadButton;
-
- typedef struct {
- IZ_KeyCode keyboard[CONTROLS];
- IZ_PadButton gamepad[CONTROLS];
- } IZ_ControlsConfig;
-
- typedef struct {
- IZ_VideoConfig video;
- IZ_ControlsConfig controls[PLAYERS];
- } IZ_Config;
-
- static void IZ_GetConfigPath(char* config_path) {
- //const char* config_path_dir = SDL_GetPrefPath("Modal Studios", APP_NAME);
- const char* config_path_dir = SDL_GetBasePath();
- memcpy_s(config_path, 128, config_path_dir, 128);
- strcat_s(config_path, 128, "config.ini");
- }
-
- static void IZ_SaveConfig(IZ_Config* config) {
- static char config_path[128];
- IZ_GetConfigPath(config_path);
- FILE* fp;
- fopen_s(&fp, config_path, "w");
- fprintf_s(fp, "[Video]\n");
- fprintf_s(fp, "Width=%u\n", config->video.width);
- fprintf_s(fp, "Height=%u\n", config->video.height);
- fprintf_s(fp, "\n");
-
- for (unsigned int p = 0; p < PLAYERS; p += 1) {
- fprintf_s(fp, "[Controls.%u.Keyboard]\n", p);
- for (unsigned int i = 0; i < CONTROLS; i += 1) {
- fprintf_s(fp, "%s=%s\n", ACTION_NAMES[i], SDL_GetKeyName(config->controls[p].keyboard[i]));
- }
- }
- }
-
- static void IZ_LoadConfig(IZ_Config* config) {
- static char config_path[128];
- IZ_GetConfigPath(config_path);
- // TODO check if file exists first
- config->video.width = ini_getl("Video", "Width", 640l, config_path);
- config->video.height = ini_getl("Video", "Height", 480l, config_path);
- char buffer[128];
- for (int i = 0; i < CONTROLS; i += 1) {
- ini_gets("Controls.0.Keyboard", ACTION_NAMES[i], SDL_GetKeyName(KEYBOARD_CONTROLS[i]), buffer, 128, config_path);
- config->controls[0].keyboard[i] = SDL_GetKeyFromName(buffer);
- }
- }
-
- int main(int argc, char* args[]) {
- SDL_Window* window = NULL;
- SDL_Surface* screen_surface = NULL;
- IZ_Config config;
- IZ_LoadConfig(&config);
- IZ_SaveConfig(&config);
-
- 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,
- config.video.width,
- config.video.height,
- SDL_WINDOW_SHOWN
- );
-
- if (window == NULL) {
- printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
- return -2;
- }
-
- bool quit = false;
- SDL_Event e;
- screen_surface = SDL_GetWindowSurface(window);
-
- unsigned short action = 0;
- while (!quit) {
- SDL_FillRect(screen_surface, NULL, SDL_MapRGB(screen_surface->format, 0x00, 0x00, 0x00));
- uint64_t ticks = SDL_GetTicks64();
- for (unsigned char i = 0; i < 64; i += 1) {
- const unsigned char column = (64 - i) % 32;
- const unsigned char row = i / 32;
- const uint64_t bitflag = (0x1lu << i);
- const unsigned char size = 4;
- if (ticks & bitflag) {
- SDL_FillRect(screen_surface, &(SDL_Rect) {
- column * size,
- config.video.height - ((row + 1) * size),
- size,
- size
- }, SDL_MapRGB(screen_surface->format, 0x00, 0xff, 0xff));
- }
- }
-
- while (SDL_PollEvent(&e) != 0) {
- if (e.type == SDL_QUIT) {
- quit = true;
- }
-
- for (unsigned char i = 0; i < CONTROLS; i += 1) {
- // TODO do same for gamepad
- if (e.key.keysym.sym == config.controls[0].keyboard[i]) {
- const unsigned short bitflag = (0x1 << i);
- if (e.type == SDL_KEYDOWN) {
- action |= bitflag;
- } else if (e.type == SDL_KEYUP) {
- action &= ~bitflag;
- }
- }
- }
-
- for (unsigned char i = 0; i < CONTROLS; i += 1) {
- const unsigned char column = i % 4;
- const unsigned char row = i / 4;
- const unsigned short bitflag = (0x1 << i);
- const unsigned char size = 4;
- if (action & bitflag) {
- SDL_FillRect(screen_surface, &(SDL_Rect) {
- column * size,
- row * size,
- size,
- size
- }, SDL_MapRGB(screen_surface->format, 0xff, 0xff, 0x00));
- }
- }
-
- SDL_UpdateWindowSurface(window);
- }
- }
- SDL_DestroyWindow(window);
- SDL_Quit();
-
- return 0;
- }
|