@@ -43,7 +43,7 @@ add_executable( | |||
src/packages/game/input/IZ_keyboard.h | |||
src/packages/game/IZ_config.c | |||
src/packages/game/IZ_config.h | |||
src/packages/game/geometry/IZ_point2d.c src/packages/game/geometry/IZ_point2d.h src/packages/game/geometry/IZ_vector2d.c src/packages/game/geometry/IZ_vector2d.h src/packages/game/geometry/IZ_rect.c src/packages/game/geometry/IZ_rect.h src/packages/game/core/IZ_object.c src/packages/game/core/IZ_object.h src/packages/game/core/IZ_creature.c src/packages/game/core/IZ_creature.h src/packages/game/core/IZ_entity.c src/packages/game/core/IZ_entity.h src/packages/game/memory/IZ_pool.c src/packages/game/memory/IZ_pool.h) | |||
src/packages/game/geometry/IZ_point2d.c src/packages/game/geometry/IZ_point2d.h src/packages/game/geometry/IZ_vector2d.c src/packages/game/geometry/IZ_vector2d.h src/packages/game/geometry/IZ_rect.c src/packages/game/geometry/IZ_rect.h src/packages/game/core/IZ_object.c src/packages/game/core/IZ_object.h src/packages/game/core/IZ_creature.c src/packages/game/core/IZ_creature.h src/packages/game/core/IZ_entity.c src/packages/game/core/IZ_entity.h src/packages/game/memory/IZ_pool.c src/packages/game/memory/IZ_pool.h src/packages/game/input/IZ_input.c src/packages/game/input/IZ_input.h) | |||
target_link_libraries( | |||
game | |||
@@ -13,29 +13,18 @@ IZ_ProcedureResult IZ_InitializeApp(IZ_App* app) { | |||
char config_path[128]; | |||
IZ_GetConfigPath(config_path, 128); | |||
IZ_LoadVideoConfig(config_path, &app->video_config); | |||
if (IZ_SaveVideoConfig(config_path, &app->video_config)) { | |||
fprintf_s(stderr, "Error committing video config.\n"); | |||
} | |||
app->video_update_at = 0u; | |||
if (IZ_InitializeVideo(&app->video_config, &app->window, &app->screen_surface)) { | |||
if (IZ_InitializeVideo(config_path, &app->video_state)) { | |||
return 2; | |||
} | |||
for (uint8_t p = 0; p < PLAYERS; p += 1) { | |||
IZ_LoadKeyboardConfig(config_path, &app->keyboard_state->config, p); | |||
if (IZ_SaveKeyboardConfig(config_path, &app->keyboard_state->config, p)) { | |||
fprintf_s(stderr, "Error committing keyboard config for player %d.\n", p); | |||
} | |||
IZ_LoadJoystickConfig(config_path, &app->joystick_state->config, p); | |||
if (IZ_SaveJoystickConfig(config_path, &app->joystick_state->config, p)) { | |||
fprintf_s(stderr, "Error committing joystick config for player %d.\n", p); | |||
} | |||
app->actions[p] = 0; | |||
IZ_InitializeInput(config_path, &app->input_state[p], p); | |||
} | |||
return 0; | |||
} | |||
void IZ_TeardownApp(IZ_App* app) { | |||
IZ_TeardownVideo(&app->video_state); | |||
SDL_Quit(); | |||
} |
@@ -2,28 +2,20 @@ | |||
#define IZ_APP_H | |||
#include <SDL.h> | |||
#include "input/IZ_keyboard.h" | |||
#include "input/IZ_joystick.h" | |||
#include "input/IZ_input.h" | |||
#include "output/IZ_video.h" | |||
#include "memory/IZ_pool.h" | |||
#include "input/IZ_action.h" | |||
typedef struct { | |||
IZ_Action actions[PLAYERS]; | |||
// input | |||
IZ_KeyboardState keyboard_state[PLAYERS]; | |||
IZ_JoystickState joystick_state[PLAYERS]; | |||
// output, video state | |||
IZ_VideoConfig video_config; | |||
uint64_t video_update_at; | |||
SDL_Window* window; | |||
SDL_Surface* screen_surface; | |||
IZ_InputState input_state[PLAYERS]; | |||
IZ_VideoState video_state; | |||
IZ_Pool memory_pool; | |||
} IZ_App; | |||
IZ_ProcedureResult IZ_InitializeApp(IZ_App*); | |||
void IZ_TeardownApp(IZ_App*); | |||
#endif |
@@ -0,0 +1,18 @@ | |||
#include "IZ_input.h" | |||
void IZ_HandleInputEvents(SDL_Event e, IZ_InputState* state) { | |||
IZ_HandleJoystickEvents(e, &state->joystick_state, &state->action); | |||
IZ_HandleKeyboardEvents(e, &state->keyboard_state, &state->action); | |||
} | |||
void IZ_InitializeInput(const char* config_path, IZ_InputState* state, uint8_t p) { | |||
if (IZ_InitializeKeyboardConfig(config_path, &state->keyboard_state.config, p)) { | |||
fprintf_s(stderr, "Error committing keyboard config for player %d.\n", p); | |||
} | |||
if (IZ_InitializeJoystickConfig(config_path, &state->joystick_state.config, p)) { | |||
fprintf_s(stderr, "Error committing joystick config for player %d.\n", p); | |||
} | |||
state->action = 0; | |||
} |
@@ -0,0 +1,18 @@ | |||
#ifndef IZ_INPUT_H | |||
#define IZ_INPUT_H | |||
#include "IZ_action.h" | |||
#include "IZ_keyboard.h" | |||
#include "IZ_joystick.h" | |||
typedef struct { | |||
IZ_Action action; | |||
IZ_KeyboardState keyboard_state; | |||
IZ_JoystickState joystick_state; | |||
} IZ_InputState; | |||
void IZ_HandleInputEvents(SDL_Event, IZ_InputState*); | |||
void IZ_InitializeInput(const char*, IZ_InputState*, uint8_t); | |||
#endif |
@@ -130,3 +130,8 @@ IZ_ProcedureResult IZ_SaveJoystickConfig(const char* config_path, IZ_JoystickCon | |||
return 0; | |||
} | |||
IZ_ProcedureResult IZ_InitializeJoystickConfig(const char* config_path, IZ_JoystickConfig* config, uint8_t p) { | |||
IZ_LoadJoystickConfig(config_path, config, p); | |||
return IZ_SaveJoystickConfig(config_path, config, p); | |||
} |
@@ -22,7 +22,6 @@ typedef struct { | |||
typedef struct { | |||
SDL_Joystick* joystick_instance; | |||
IZ_JoystickConfig config; | |||
} IZ_JoystickState; | |||
@@ -47,10 +46,10 @@ static IZ_PadButton IZ_DEFAULT_JOYSTICK_CONTROLS[PLAYERS][CONTROLS] = { | |||
}, | |||
}; | |||
void IZ_LoadJoystickConfig(const char*, IZ_JoystickConfig*, uint8_t); | |||
IZ_ProcedureResult IZ_SaveJoystickConfig(const char*, IZ_JoystickConfig*, uint8_t); | |||
void IZ_HandleJoystickEvents(SDL_Event, IZ_JoystickState*, IZ_Action*); | |||
IZ_ProcedureResult IZ_InitializeJoystickConfig(const char*, IZ_JoystickConfig*, uint8_t); | |||
#endif |
@@ -50,3 +50,8 @@ void IZ_LoadKeyboardConfig(const char* config_path, IZ_KeyboardConfig* config, u | |||
config->control_mapping[i] = SDL_GetKeyFromName(buffer); | |||
} | |||
} | |||
IZ_ProcedureResult IZ_InitializeKeyboardConfig(const char* config_path, IZ_KeyboardConfig* config, uint8_t p) { | |||
IZ_LoadKeyboardConfig(config_path, config, p); | |||
return IZ_SaveKeyboardConfig(config_path, config, p); | |||
} |
@@ -35,10 +35,10 @@ static const SDL_KeyCode IZ_DEFAULT_KEYBOARD_CONTROLS[PLAYERS][CONTROLS] = { | |||
}, | |||
}; | |||
void IZ_LoadKeyboardConfig(const char*, IZ_KeyboardConfig*, uint8_t); | |||
IZ_ProcedureResult IZ_SaveKeyboardConfig(const char*, IZ_KeyboardConfig*, uint8_t); | |||
void IZ_HandleKeyboardEvents(SDL_Event, IZ_KeyboardState*, IZ_Action*); | |||
IZ_ProcedureResult IZ_InitializeKeyboardConfig(const char*, IZ_KeyboardConfig*, uint8_t); | |||
#endif |
@@ -1,7 +1,6 @@ | |||
#include <SDL.h> | |||
#include <stdbool.h> | |||
#include "input/IZ_action.h" | |||
#include "IZ_app.h" | |||
int main(int argc, char* args[]) { | |||
@@ -18,7 +17,6 @@ int main(int argc, char* args[]) { | |||
uint64_t ticks = SDL_GetTicks64(); | |||
// TODO do audio processing | |||
// TODO do networking? | |||
// process events | |||
@@ -29,8 +27,7 @@ int main(int argc, char* args[]) { | |||
} | |||
for (uint8_t p = 0; p < PLAYERS; p += 1) { | |||
IZ_HandleJoystickEvents(e, &(app.joystick_state[p]), &app.actions[p]); | |||
IZ_HandleKeyboardEvents(e, &(app.keyboard_state[p]), &app.actions[p]); | |||
IZ_HandleInputEvents(e, &(app.input_state[p])); | |||
} | |||
} | |||
@@ -38,46 +35,9 @@ int main(int argc, char* args[]) { | |||
break; | |||
} | |||
if (ticks - app.video_update_at > 1000 / app.video_config.max_fps) { | |||
// Update window | |||
SDL_FillRect(app.screen_surface, NULL, SDL_MapRGB(app.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(app.screen_surface, &(SDL_Rect) { | |||
column * size, | |||
app.video_config.height - ((row + 1) * size), | |||
size, | |||
size | |||
}, SDL_MapRGB(app.screen_surface->format, 0x00, 0xff, 0xff)); | |||
} | |||
} | |||
for (uint8_t p = 0; p < PLAYERS; p += 1) { | |||
for (uint8_t i = 0; i < CONTROLS; i += 1) { | |||
const uint8_t column = (i % 4) + (p * 4); | |||
const uint8_t row = i / 4; | |||
const IZ_Action bitflag = (0x1 << i); | |||
const uint8_t size = 4; | |||
if (app.actions[p] & bitflag) { | |||
SDL_FillRect(app.screen_surface, &(SDL_Rect) { | |||
column * size, | |||
row * size, | |||
size, | |||
size | |||
}, SDL_MapRGB(app.screen_surface->format, 0xff, 0xff, 0x00)); | |||
} | |||
} | |||
} | |||
SDL_UpdateWindowSurface(app.window); | |||
app.video_update_at = ticks; | |||
} | |||
IZ_UpdateVideo(&app.video_state, &app.input_state, ticks); | |||
} | |||
SDL_DestroyWindow(app.window); | |||
SDL_Quit(); | |||
IZ_TeardownApp(&app); | |||
return 0; | |||
} |
@@ -20,19 +20,72 @@ void IZ_LoadVideoConfig(const char* config_path, IZ_VideoConfig* config) { | |||
config->max_fps = ini_getl("Video", "MaxFps", 30, config_path); | |||
} | |||
IZ_ProcedureResult IZ_InitializeVideo(IZ_VideoConfig* config, SDL_Window** window, SDL_Surface** screen_surface) { | |||
*window = SDL_CreateWindow( | |||
IZ_ProcedureResult IZ_InitializeVideo(const char* config_path, IZ_VideoState* state) { | |||
IZ_LoadVideoConfig(config_path, &state->config); | |||
if (IZ_SaveVideoConfig(config_path, &state->config)) { | |||
fprintf_s(stderr, "Error committing video config.\n"); | |||
} | |||
state->last_update_at = 0u; | |||
SDL_Window* window = SDL_CreateWindow( | |||
APP_NAME, | |||
SDL_WINDOWPOS_CENTERED, | |||
SDL_WINDOWPOS_CENTERED, | |||
config->width, | |||
config->height, | |||
state->config.width, | |||
state->config.height, | |||
SDL_WINDOW_SHOWN | |||
); | |||
if (*window == NULL) { | |||
if (window == NULL) { | |||
fprintf_s(stderr, "Window could not be created! SDL_Error: %s\n", SDL_GetError()); | |||
return 1; | |||
} | |||
*screen_surface = SDL_GetWindowSurface(*window); | |||
state->window = window; | |||
state->surface = SDL_GetWindowSurface(window); | |||
return 0; | |||
} | |||
void IZ_UpdateVideo(IZ_VideoState* video_state, IZ_InputState(* input_states)[PLAYERS], uint64_t ticks) { | |||
if (ticks - video_state->last_update_at > 1000 / video_state->config.max_fps) { | |||
// Update window | |||
SDL_FillRect(video_state->surface, NULL, SDL_MapRGB(video_state->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(video_state->surface, &(SDL_Rect) { | |||
column * size, | |||
video_state->config.height - ((row + 1) * size), | |||
size, | |||
size | |||
}, SDL_MapRGB(video_state->surface->format, 0x00, 0xff, 0xff)); | |||
} | |||
} | |||
// TODO refer to app's state | |||
for (uint8_t p = 0; p < PLAYERS; p += 1) { | |||
for (uint8_t i = 0; i < CONTROLS; i += 1) { | |||
const uint8_t column = (i % 4) + (p * 4); | |||
const uint8_t row = i / 4; | |||
const IZ_Action bitflag = (0x1 << i); | |||
const uint8_t size = 4; | |||
if (input_states[p]->action & bitflag) { | |||
SDL_FillRect(video_state->surface, &(SDL_Rect) { | |||
column * size, | |||
row * size, | |||
size, | |||
size | |||
}, SDL_MapRGB(video_state->surface->format, 0xff, 0xff, 0x00)); | |||
} | |||
} | |||
} | |||
SDL_UpdateWindowSurface(video_state->window); | |||
video_state->last_update_at = ticks; | |||
} | |||
} | |||
void IZ_TeardownVideo(IZ_VideoState* state) { | |||
SDL_DestroyWindow(state->window); | |||
} |
@@ -4,6 +4,9 @@ | |||
#include <stdio.h> | |||
#include <minIni.h> | |||
#include <SDL_video.h> | |||
// TODO move this out from video, refer to app's state instead | |||
#include "../input/IZ_input.h" | |||
#include "../IZ_common.h" | |||
#include "../IZ_config.h" | |||
@@ -13,10 +16,19 @@ typedef struct { | |||
uint8_t max_fps; | |||
} IZ_VideoConfig; | |||
typedef struct { | |||
IZ_VideoConfig config; | |||
uint64_t last_update_at; | |||
SDL_Window* window; | |||
SDL_Surface* surface; | |||
} IZ_VideoState; | |||
IZ_ProcedureResult IZ_InitializeVideo(const char*, IZ_VideoState*); | |||
IZ_ProcedureResult IZ_SaveVideoConfig(const char*, IZ_VideoConfig*); | |||
void IZ_LoadVideoConfig(const char*, IZ_VideoConfig*); | |||
void IZ_UpdateVideo(IZ_VideoState*, IZ_InputState(*)[PLAYERS], uint64_t); | |||
IZ_ProcedureResult IZ_InitializeVideo(IZ_VideoConfig*, SDL_Window**, SDL_Surface**); | |||
void IZ_TeardownVideo(IZ_VideoState*); | |||
#endif |