|
@@ -1,114 +1,16 @@ |
|
|
#include <SDL.h> |
|
|
#include <SDL.h> |
|
|
#include <stdbool.h> |
|
|
#include <stdbool.h> |
|
|
#include <stdio.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); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
#include "IZ_action.h" |
|
|
|
|
|
#include "IZ_app.h" |
|
|
|
|
|
|
|
|
int main(int argc, char* args[]) { |
|
|
int main(int argc, char* args[]) { |
|
|
SDL_Window* window = NULL; |
|
|
SDL_Window* window = NULL; |
|
|
SDL_Surface* screen_surface = NULL; |
|
|
SDL_Surface* screen_surface = NULL; |
|
|
IZ_Config config; |
|
|
|
|
|
IZ_LoadConfig(&config); |
|
|
|
|
|
IZ_SaveConfig(&config); |
|
|
|
|
|
|
|
|
IZ_App app; |
|
|
|
|
|
|
|
|
|
|
|
IZ_InitializeApp(&app); |
|
|
|
|
|
|
|
|
if (SDL_Init( |
|
|
if (SDL_Init( |
|
|
SDL_INIT_VIDEO |
|
|
SDL_INIT_VIDEO |
|
@@ -123,8 +25,8 @@ int main(int argc, char* args[]) { |
|
|
APP_NAME, |
|
|
APP_NAME, |
|
|
SDL_WINDOWPOS_CENTERED, |
|
|
SDL_WINDOWPOS_CENTERED, |
|
|
SDL_WINDOWPOS_CENTERED, |
|
|
SDL_WINDOWPOS_CENTERED, |
|
|
config.video.width, |
|
|
|
|
|
config.video.height, |
|
|
|
|
|
|
|
|
app.config.video.width, |
|
|
|
|
|
app.config.video.height, |
|
|
SDL_WINDOW_SHOWN |
|
|
SDL_WINDOW_SHOWN |
|
|
); |
|
|
); |
|
|
|
|
|
|
|
@@ -137,19 +39,19 @@ int main(int argc, char* args[]) { |
|
|
SDL_Event e; |
|
|
SDL_Event e; |
|
|
screen_surface = SDL_GetWindowSurface(window); |
|
|
screen_surface = SDL_GetWindowSurface(window); |
|
|
|
|
|
|
|
|
unsigned short action = 0; |
|
|
|
|
|
|
|
|
IZ_Action action = 0; |
|
|
while (!quit) { |
|
|
while (!quit) { |
|
|
SDL_FillRect(screen_surface, NULL, SDL_MapRGB(screen_surface->format, 0x00, 0x00, 0x00)); |
|
|
SDL_FillRect(screen_surface, NULL, SDL_MapRGB(screen_surface->format, 0x00, 0x00, 0x00)); |
|
|
uint64_t ticks = SDL_GetTicks64(); |
|
|
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; |
|
|
|
|
|
|
|
|
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 uint64_t bitflag = (0x1lu << i); |
|
|
const unsigned char size = 4; |
|
|
|
|
|
|
|
|
const uint8_t size = 4; |
|
|
if (ticks & bitflag) { |
|
|
if (ticks & bitflag) { |
|
|
SDL_FillRect(screen_surface, &(SDL_Rect) { |
|
|
SDL_FillRect(screen_surface, &(SDL_Rect) { |
|
|
column * size, |
|
|
column * size, |
|
|
config.video.height - ((row + 1) * size), |
|
|
|
|
|
|
|
|
app.config.video.height - ((row + 1) * size), |
|
|
size, |
|
|
size, |
|
|
size |
|
|
size |
|
|
}, SDL_MapRGB(screen_surface->format, 0x00, 0xff, 0xff)); |
|
|
}, SDL_MapRGB(screen_surface->format, 0x00, 0xff, 0xff)); |
|
@@ -161,10 +63,10 @@ int main(int argc, char* args[]) { |
|
|
quit = true; |
|
|
quit = true; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
for (unsigned char i = 0; i < CONTROLS; i += 1) { |
|
|
|
|
|
|
|
|
for (uint8_t i = 0; i < CONTROLS; i += 1) { |
|
|
// TODO do same for gamepad |
|
|
// TODO do same for gamepad |
|
|
if (e.key.keysym.sym == config.controls[0].keyboard[i]) { |
|
|
|
|
|
const unsigned short bitflag = (0x1 << i); |
|
|
|
|
|
|
|
|
if (e.key.keysym.sym == app.config.controls[0].keyboard[i]) { |
|
|
|
|
|
const uint16_t bitflag = (0x1 << i); |
|
|
if (e.type == SDL_KEYDOWN) { |
|
|
if (e.type == SDL_KEYDOWN) { |
|
|
action |= bitflag; |
|
|
action |= bitflag; |
|
|
} else if (e.type == SDL_KEYUP) { |
|
|
} else if (e.type == SDL_KEYUP) { |
|
@@ -174,10 +76,10 @@ int main(int argc, char* args[]) { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
for (unsigned char i = 0; i < CONTROLS; i += 1) { |
|
|
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; |
|
|
|
|
|
|
|
|
const uint8_t column = i % 4; |
|
|
|
|
|
const uint8_t row = i / 4; |
|
|
|
|
|
const IZ_Action bitflag = (0x1 << i); |
|
|
|
|
|
const uint8_t size = 4; |
|
|
if (action & bitflag) { |
|
|
if (action & bitflag) { |
|
|
SDL_FillRect(screen_surface, &(SDL_Rect) { |
|
|
SDL_FillRect(screen_surface, &(SDL_Rect) { |
|
|
column * size, |
|
|
column * size, |
|
|