Explorar el Código

Refactor structure

Extract config-related code to their own source files.
master
TheoryOfNekomata hace 2 años
padre
commit
e5b0e5129d
Se han modificado 8 ficheros con 178 adiciones y 119 borrados
  1. +1
    -1
      CMakeLists.txt
  2. +27
    -0
      src/packages/game/IZ_action.h
  3. +8
    -0
      src/packages/game/IZ_app.c
  4. +12
    -0
      src/packages/game/IZ_app.h
  5. +12
    -0
      src/packages/game/IZ_common.h
  6. +47
    -0
      src/packages/game/config/IZ_config.c
  7. +51
    -0
      src/packages/game/config/IZ_config.h
  8. +20
    -118
      src/packages/game/main.c

+ 1
- 1
CMakeLists.txt Ver fichero

@@ -24,7 +24,7 @@ add_executable(
dependencies/minIni/dev/minIni.h
dependencies/minIni/dev/minIni.c
src/packages/game/main.c
)
src/packages/game/config/IZ_config.h src/packages/game/config/IZ_config.c src/packages/game/IZ_common.h src/packages/game/IZ_action.h src/packages/game/IZ_app.c src/packages/game/IZ_app.h)
target_link_libraries(izanagi SDL2main SDL2)

if (WIN32)


+ 27
- 0
src/packages/game/IZ_action.h Ver fichero

@@ -0,0 +1,27 @@
#ifndef IZ_ACTION_H
#define IZ_ACTION_H

#include "IZ_common.h"

typedef uint16_t IZ_Action;

static const char* ACTION_NAMES[CONTROLS] = {
"Right",
"Down",
"Left",
"Up",
"Affirm",
"Negate",
"Action0",
"Action1",
"Action2",
"Action3",
"Action4",
"Action5",
"Action6",
"Action7",
"Action8",
"Action9",
};

#endif

+ 8
- 0
src/packages/game/IZ_app.c Ver fichero

@@ -0,0 +1,8 @@
#include "IZ_app.h"

int IZ_InitializeApp(IZ_App* app) {
IZ_LoadConfig(&app->config);
IZ_SaveConfig(&app->config);

return 0;
}

+ 12
- 0
src/packages/game/IZ_app.h Ver fichero

@@ -0,0 +1,12 @@
#ifndef IZ_APP_H
#define IZ_APP_H

#include "config/IZ_config.h"

typedef struct {
IZ_Config config;
} IZ_App;

int IZ_InitializeApp(IZ_App*);

#endif

+ 12
- 0
src/packages/game/IZ_common.h Ver fichero

@@ -0,0 +1,12 @@
#ifndef IZ_COMMON_H
#define IZ_COMMON_H

#include <stdint.h>

static const char* APP_NAME = "SDL2";

static const uint8_t CONTROLS = 16;

static const uint8_t PLAYERS = 1;

#endif

+ 47
- 0
src/packages/game/config/IZ_config.c Ver fichero

@@ -0,0 +1,47 @@
#include <stdio.h>
#include <string.h>
#include <minIni.h>
#include <SDL_filesystem.h>
#include <SDL_keyboard.h>

#include "../IZ_action.h"
#include "IZ_config.h"

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");
}

void IZ_SaveConfig(IZ_Config* config) {
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 (uint8_t p = 0; p < PLAYERS; p += 1) {
fprintf_s(fp, "[Controls.%u.Keyboard]\n", p);
for (uint8_t i = 0; i < CONTROLS; i += 1) {
fprintf_s(fp, "%s=%s\n", ACTION_NAMES[i], SDL_GetKeyName(config->controls[p].keyboard[i]));
}
}
}

void IZ_LoadConfig(IZ_Config* config) {
char config_path[128];
IZ_GetConfigPath(config_path);
config->video.width = ini_getl("Video", "Width", 640l, config_path);
config->video.height = ini_getl("Video", "Height", 480l, config_path);
char buffer[128];
char section_name[20] = "Controls.0.Keyboard";
for (uint8_t i = 0; i < CONTROLS; i += 1) {
section_name[9] = (char) (48 + i);
ini_gets(section_name, ACTION_NAMES[i], SDL_GetKeyName(IZ_DEFAULT_KEYBOARD_CONTROLS[i]), buffer, 128, config_path);
config->controls[0].keyboard[i] = SDL_GetKeyFromName(buffer);
}
}

+ 51
- 0
src/packages/game/config/IZ_config.h Ver fichero

@@ -0,0 +1,51 @@
#ifndef IZ_CONFIG_H
#define IZ_CONFIG_H

#include "SDL_keycode.h"
#include "../IZ_common.h"

typedef struct {
uint16_t width;
uint16_t 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 const IZ_KeyCode IZ_DEFAULT_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
};

void IZ_GetConfigPath(char* config_path);

void IZ_SaveConfig(IZ_Config* config);

void IZ_LoadConfig(IZ_Config* config);

#endif

+ 20
- 118
src/packages/game/main.c Ver fichero

@@ -1,114 +1,16 @@
#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);
}
}
#include "IZ_action.h"
#include "IZ_app.h"

int main(int argc, char* args[]) {
SDL_Window* window = NULL;
SDL_Surface* screen_surface = NULL;
IZ_Config config;
IZ_LoadConfig(&config);
IZ_SaveConfig(&config);
IZ_App app;

IZ_InitializeApp(&app);

if (SDL_Init(
SDL_INIT_VIDEO
@@ -123,8 +25,8 @@ int main(int argc, char* args[]) {
APP_NAME,
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
config.video.width,
config.video.height,
app.config.video.width,
app.config.video.height,
SDL_WINDOW_SHOWN
);

@@ -137,19 +39,19 @@ int main(int argc, char* args[]) {
SDL_Event e;
screen_surface = SDL_GetWindowSurface(window);

unsigned short action = 0;
IZ_Action 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;
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 unsigned char size = 4;
const uint8_t size = 4;
if (ticks & bitflag) {
SDL_FillRect(screen_surface, &(SDL_Rect) {
column * size,
config.video.height - ((row + 1) * size),
app.config.video.height - ((row + 1) * size),
size,
size
}, SDL_MapRGB(screen_surface->format, 0x00, 0xff, 0xff));
@@ -161,10 +63,10 @@ int main(int argc, char* args[]) {
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
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) {
action |= bitflag;
} 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) {
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) {
SDL_FillRect(screen_surface, &(SDL_Rect) {
column * size,


Cargando…
Cancelar
Guardar