@@ -1,14 +1,14 @@
#include <SDL.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <minIni.h>
const char* APP_NAME = "SDL2";
const unsigned int SCREEN_WIDTH = 640 ;
const unsigned int SCREEN_HEIGHT = 480 ;
static const unsigned char CONTROLS = 16 ;
static const unsigned char PLAYERS = 1 ;
const unsigned char CONTROLS = 16;
const SDL_KeyCode KEYBOARD_CONTROLS[CONTROLS] = {
static const SDL_KeyCode KEYBOARD_CONTROLS[CONTROLS] = {
SDLK_RIGHT,
SDLK_DOWN,
SDLK_LEFT,
@@ -27,9 +27,69 @@ const SDL_KeyCode KEYBOARD_CONTROLS[CONTROLS] = {
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_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* screenSurface = NULL;
SDL_Surface* screen_surface = NULL;
IZ_Config config;
IZ_LoadConfig(&config);
if (SDL_Init(
SDL_INIT_VIDEO
@@ -42,10 +102,10 @@ int main(int argc, char* args[]) {
window = SDL_CreateWindow(
APP_NAME,
SDL_WINDOWPOS_UNDEFIN ED,
SDL_WINDOWPOS_UNDEFIN ED,
SCREEN_WIDTH ,
SCREEN_HEIGHT ,
SDL_WINDOWPOS_CENTER ED,
SDL_WINDOWPOS_CENTER ED,
config.video.width ,
config.video.height ,
SDL_WINDOW_SHOWN
);
@@ -56,11 +116,11 @@ int main(int argc, char* args[]) {
bool quit = false;
SDL_Event e;
screenS urface = SDL_GetWindowSurface(window);
screen_s urface = SDL_GetWindowSurface(window);
unsigned short action = 0;
while (!quit) {
SDL_FillRect(screenSurface, NULL, SDL_MapRGB(screenS urface->format, 0x00, 0x00, 0x00));
SDL_FillRect(screen_surface, NULL, SDL_MapRGB(screen_s urface->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;
@@ -68,12 +128,12 @@ int main(int argc, char* args[]) {
const uint64_t bitflag = (0x1lu << i);
const unsigned char size = 4;
if (ticks & bitflag) {
SDL_FillRect(screenS urface, &(SDL_Rect) {
SDL_FillRect(screen_s urface, &(SDL_Rect) {
column * size,
SCREEN_HEIGHT - ((row + 1) * size),
config.video.height - ((row + 1) * size),
size,
size
}, SDL_MapRGB(screenS urface->format, 0x00, 0xff, 0xff));
}, SDL_MapRGB(screen_s urface->format, 0x00, 0xff, 0xff));
}
}
@@ -84,7 +144,7 @@ int main(int argc, char* args[]) {
for (unsigned char i = 0; i < CONTROLS; i += 1) {
// TODO do same for gamepad
if (e.key.keysym.sym == KEYBOARD_CONTROLS [i]) {
if (e.key.keysym.sym == config.controls[0].keyboard [i]) {
const unsigned short bitflag = (0x1 << i);
if (e.type == SDL_KEYDOWN) {
action |= bitflag;
@@ -100,12 +160,12 @@ int main(int argc, char* args[]) {
const unsigned short bitflag = (0x1 << i);
const unsigned char size = 4;
if (action & bitflag) {
SDL_FillRect(screenS urface, &(SDL_Rect) {
SDL_FillRect(screen_s urface, &(SDL_Rect) {
column * size,
row * size,
size,
size
}, SDL_MapRGB(screenS urface->format, 0xff, 0xff, 0x00));
}, SDL_MapRGB(screen_s urface->format, 0xff, 0xff, 0x00));
}
}