Ensure each initialization method is in its own source.feature/data-structs
@@ -1,19 +1,38 @@ | |||||
#include "IZ_app.h" | #include "IZ_app.h" | ||||
int IZ_InitializeApp(IZ_App* app) { | |||||
IZ_ProcedureResult IZ_InitializeApp(IZ_App* app) { | |||||
if (SDL_Init( | |||||
SDL_INIT_VIDEO | |||||
| SDL_INIT_GAMECONTROLLER | |||||
| SDL_INIT_EVENTS | |||||
) < 0) { | |||||
fprintf_s(stderr, "SDL could not initialize! SDL_Error: %s\n", SDL_GetError()); | |||||
return 1; | |||||
} | |||||
char config_path[128]; | char config_path[128]; | ||||
IZ_GetConfigPath(config_path, 128); | IZ_GetConfigPath(config_path, 128); | ||||
IZ_LoadVideoConfig(config_path, &app->video_config); | IZ_LoadVideoConfig(config_path, &app->video_config); | ||||
IZ_SaveVideoConfig(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; | app->video_update_at = 0u; | ||||
if (IZ_InitializeVideo(&app->video_config, &app->window, &app->screen_surface)) { | |||||
return 2; | |||||
} | |||||
for (uint8_t p = 0; p < PLAYERS; p += 1) { | for (uint8_t p = 0; p < PLAYERS; p += 1) { | ||||
IZ_LoadKeyboardConfig(config_path, &app->keyboard_state->config, p); | IZ_LoadKeyboardConfig(config_path, &app->keyboard_state->config, p); | ||||
IZ_SaveKeyboardConfig(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); | IZ_LoadJoystickConfig(config_path, &app->joystick_state->config, p); | ||||
IZ_SaveJoystickConfig(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; | app->actions[p] = 0; | ||||
} | } | ||||
@@ -1,6 +1,7 @@ | |||||
#ifndef IZ_APP_H | #ifndef IZ_APP_H | ||||
#define IZ_APP_H | #define IZ_APP_H | ||||
#include <SDL.h> | |||||
#include "input/IZ_keyboard.h" | #include "input/IZ_keyboard.h" | ||||
#include "input/IZ_joystick.h" | #include "input/IZ_joystick.h" | ||||
#include "output/IZ_video.h" | #include "output/IZ_video.h" | ||||
@@ -17,10 +18,12 @@ typedef struct { | |||||
// output, video state | // output, video state | ||||
IZ_VideoConfig video_config; | IZ_VideoConfig video_config; | ||||
uint64_t video_update_at; | uint64_t video_update_at; | ||||
SDL_Window* window; | |||||
SDL_Surface* screen_surface; | |||||
IZ_Pool memory_pool; | IZ_Pool memory_pool; | ||||
} IZ_App; | } IZ_App; | ||||
int IZ_InitializeApp(IZ_App*); | |||||
IZ_ProcedureResult IZ_InitializeApp(IZ_App*); | |||||
#endif | #endif |
@@ -1,6 +1,6 @@ | |||||
#include "IZ_point2d.h" | #include "IZ_point2d.h" | ||||
IZ_Point2D IZ_PointTranslate(IZ_Point2D point, IZ_Coordinate translate_x, IZ_Coordinate translate_y) { | |||||
IZ_Point2D IZ_PointTranslate(IZ_Point2D point, IZ_GeoCoord translate_x, IZ_GeoCoord translate_y) { | |||||
return (IZ_Point2D) { | return (IZ_Point2D) { | ||||
.x = point.x + translate_x, | .x = point.x + translate_x, | ||||
.y = point.y + translate_y, | .y = point.y + translate_y, | ||||
@@ -1,13 +1,13 @@ | |||||
#ifndef IZ_POINT2D_H | #ifndef IZ_POINT2D_H | ||||
#define IZ_POINT2D_H | #define IZ_POINT2D_H | ||||
typedef float IZ_Coordinate; | |||||
typedef float IZ_GeoCoord; | |||||
typedef struct { | typedef struct { | ||||
IZ_Coordinate x; | |||||
IZ_Coordinate y; | |||||
IZ_GeoCoord x; | |||||
IZ_GeoCoord y; | |||||
} IZ_Point2D; | } IZ_Point2D; | ||||
IZ_Point2D IZ_PointTranslate(IZ_Point2D, IZ_Coordinate, IZ_Coordinate); | |||||
IZ_Point2D IZ_PointTranslate(IZ_Point2D, IZ_GeoCoord, IZ_GeoCoord); | |||||
#endif | #endif |
@@ -5,17 +5,17 @@ | |||||
#include "IZ_point2d.h" | #include "IZ_point2d.h" | ||||
typedef struct { | typedef struct { | ||||
IZ_Coordinate left; | |||||
IZ_Coordinate top; | |||||
IZ_Coordinate right; | |||||
IZ_Coordinate bottom; | |||||
IZ_GeoCoord left; | |||||
IZ_GeoCoord top; | |||||
IZ_GeoCoord right; | |||||
IZ_GeoCoord bottom; | |||||
} IZ_Bounds; | } IZ_Bounds; | ||||
typedef struct { | typedef struct { | ||||
// top left | // top left | ||||
IZ_Point2D pos; | IZ_Point2D pos; | ||||
IZ_Coordinate width; | |||||
IZ_Coordinate height; | |||||
IZ_GeoCoord width; | |||||
IZ_GeoCoord height; | |||||
} IZ_Rect; | } IZ_Rect; | ||||
IZ_Bounds IZ_RectGetBounds(IZ_Rect); | IZ_Bounds IZ_RectGetBounds(IZ_Rect); | ||||
@@ -14,7 +14,7 @@ IZ_Vector2D IZ_VectorMultiply(IZ_Vector2D multiplicand, IZ_Vector2D multiplier) | |||||
}; | }; | ||||
} | } | ||||
IZ_Vector2D IZ_VectorScale(IZ_Vector2D vector, IZ_VectorMagnitude scalar) { | |||||
IZ_Vector2D IZ_VectorScale(IZ_Vector2D vector, IZ_GeoCoord scalar) { | |||||
return (IZ_Vector2D) { | return (IZ_Vector2D) { | ||||
.right = vector.right * scalar, | .right = vector.right * scalar, | ||||
.up = vector.up * scalar, | .up = vector.up * scalar, | ||||
@@ -1,17 +1,17 @@ | |||||
#ifndef IZ_VECTOR2D_H | #ifndef IZ_VECTOR2D_H | ||||
#define IZ_VECTOR2D_H | #define IZ_VECTOR2D_H | ||||
typedef float IZ_VectorMagnitude; | |||||
#include "IZ_point2d.h" | |||||
typedef struct { | typedef struct { | ||||
IZ_VectorMagnitude right; | |||||
IZ_VectorMagnitude up; | |||||
IZ_GeoCoord right; | |||||
IZ_GeoCoord up; | |||||
} IZ_Vector2D; | } IZ_Vector2D; | ||||
IZ_Vector2D IZ_VectorAdd(IZ_Vector2D, IZ_Vector2D); | IZ_Vector2D IZ_VectorAdd(IZ_Vector2D, IZ_Vector2D); | ||||
IZ_Vector2D IZ_VectorMultiply(IZ_Vector2D, IZ_Vector2D); | IZ_Vector2D IZ_VectorMultiply(IZ_Vector2D, IZ_Vector2D); | ||||
IZ_Vector2D IZ_VectorScale(IZ_Vector2D, IZ_VectorMagnitude); | |||||
IZ_Vector2D IZ_VectorScale(IZ_Vector2D, IZ_GeoCoord); | |||||
#endif | #endif |
@@ -84,7 +84,7 @@ spec("geometry") { | |||||
.up = 69.f, | .up = 69.f, | ||||
}; | }; | ||||
static IZ_VectorMagnitude s = 2.f; | |||||
static IZ_GeoCoord s = 2.f; | |||||
static IZ_Vector2D expected = { | static IZ_Vector2D expected = { | ||||
.right = 840.f, | .right = 840.f, | ||||
@@ -1,42 +1,19 @@ | |||||
#include <SDL.h> | #include <SDL.h> | ||||
#include <stdbool.h> | #include <stdbool.h> | ||||
#include <stdio.h> | |||||
#include "IZ_action.h" | #include "IZ_action.h" | ||||
#include "IZ_app.h" | #include "IZ_app.h" | ||||
int main(int argc, char* args[]) { | int main(int argc, char* args[]) { | ||||
SDL_Window* window = NULL; | |||||
SDL_Surface* screen_surface = NULL; | |||||
IZ_App app; | IZ_App app; | ||||
IZ_ProcedureResult result = IZ_InitializeApp(&app); | |||||
IZ_InitializeApp(&app); | |||||
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; | |||||
if (result) { | |||||
return result; | |||||
} | } | ||||
window = SDL_CreateWindow( | |||||
APP_NAME, | |||||
SDL_WINDOWPOS_CENTERED, | |||||
SDL_WINDOWPOS_CENTERED, | |||||
app.video_config.width, | |||||
app.video_config.height, | |||||
SDL_WINDOW_SHOWN | |||||
); | |||||
if (window == NULL) { | |||||
printf("Window could not be created! SDL_Error: %s\n", SDL_GetError()); | |||||
return -2; | |||||
} | |||||
screen_surface = SDL_GetWindowSurface(window); | |||||
bool quit = false; | bool quit = false; | ||||
SDL_Event e; | SDL_Event e; | ||||
while (true) { | while (true) { | ||||
uint64_t ticks = SDL_GetTicks64(); | uint64_t ticks = SDL_GetTicks64(); | ||||
@@ -63,19 +40,19 @@ int main(int argc, char* args[]) { | |||||
if (ticks - app.video_update_at > 1000 / app.video_config.max_fps) { | if (ticks - app.video_update_at > 1000 / app.video_config.max_fps) { | ||||
// Update window | // Update window | ||||
SDL_FillRect(screen_surface, NULL, SDL_MapRGB(screen_surface->format, 0x00, 0x00, 0x00)); | |||||
SDL_FillRect(app.screen_surface, NULL, SDL_MapRGB(app.screen_surface->format, 0x00, 0x00, 0x00)); | |||||
for (uint8_t i = 0; i < 64; i += 1) { | for (uint8_t i = 0; i < 64; i += 1) { | ||||
const uint8_t column = (64 - i) % 32; | const uint8_t column = (64 - i) % 32; | ||||
const uint8_t row = i / 32; | const uint8_t row = i / 32; | ||||
const uint64_t bitflag = (0x1lu << i); | const uint64_t bitflag = (0x1lu << i); | ||||
const uint8_t size = 4; | const uint8_t size = 4; | ||||
if (ticks & bitflag) { | if (ticks & bitflag) { | ||||
SDL_FillRect(screen_surface, &(SDL_Rect) { | |||||
SDL_FillRect(app.screen_surface, &(SDL_Rect) { | |||||
column * size, | column * size, | ||||
app.video_config.height - ((row + 1) * size), | app.video_config.height - ((row + 1) * size), | ||||
size, | size, | ||||
size | size | ||||
}, SDL_MapRGB(screen_surface->format, 0x00, 0xff, 0xff)); | |||||
}, SDL_MapRGB(app.screen_surface->format, 0x00, 0xff, 0xff)); | |||||
} | } | ||||
} | } | ||||
@@ -86,20 +63,20 @@ int main(int argc, char* args[]) { | |||||
const IZ_Action bitflag = (0x1 << i); | const IZ_Action bitflag = (0x1 << i); | ||||
const uint8_t size = 4; | const uint8_t size = 4; | ||||
if (app.actions[p] & bitflag) { | if (app.actions[p] & bitflag) { | ||||
SDL_FillRect(screen_surface, &(SDL_Rect) { | |||||
SDL_FillRect(app.screen_surface, &(SDL_Rect) { | |||||
column * size, | column * size, | ||||
row * size, | row * size, | ||||
size, | size, | ||||
size | size | ||||
}, SDL_MapRGB(screen_surface->format, 0xff, 0xff, 0x00)); | |||||
}, SDL_MapRGB(app.screen_surface->format, 0xff, 0xff, 0x00)); | |||||
} | } | ||||
} | } | ||||
} | } | ||||
SDL_UpdateWindowSurface(window); | |||||
SDL_UpdateWindowSurface(app.window); | |||||
app.video_update_at = ticks; | app.video_update_at = ticks; | ||||
} | } | ||||
} | } | ||||
SDL_DestroyWindow(window); | |||||
SDL_DestroyWindow(app.window); | |||||
SDL_Quit(); | SDL_Quit(); | ||||
return 0; | return 0; | ||||
@@ -19,3 +19,20 @@ void IZ_LoadVideoConfig(const char* config_path, IZ_VideoConfig* config) { | |||||
config->height = ini_getl("Video", "Height", 480l, config_path); | config->height = ini_getl("Video", "Height", 480l, config_path); | ||||
config->max_fps = ini_getl("Video", "MaxFps", 30, config_path); | 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( | |||||
APP_NAME, | |||||
SDL_WINDOWPOS_CENTERED, | |||||
SDL_WINDOWPOS_CENTERED, | |||||
config->width, | |||||
config->height, | |||||
SDL_WINDOW_SHOWN | |||||
); | |||||
if (*window == NULL) { | |||||
fprintf_s(stderr, "Window could not be created! SDL_Error: %s\n", SDL_GetError()); | |||||
return 1; | |||||
} | |||||
*screen_surface = SDL_GetWindowSurface(*window); | |||||
return 0; | |||||
} |
@@ -3,6 +3,7 @@ | |||||
#include <stdio.h> | #include <stdio.h> | ||||
#include <minIni.h> | #include <minIni.h> | ||||
#include <SDL_video.h> | |||||
#include "../IZ_common.h" | #include "../IZ_common.h" | ||||
#include "../IZ_config.h" | #include "../IZ_config.h" | ||||
@@ -12,8 +13,10 @@ typedef struct { | |||||
uint8_t max_fps; | uint8_t max_fps; | ||||
} IZ_VideoConfig; | } IZ_VideoConfig; | ||||
IZ_ProcedureResult IZ_SaveVideoConfig(const char*, IZ_VideoConfig* config); | |||||
IZ_ProcedureResult IZ_SaveVideoConfig(const char*, IZ_VideoConfig*); | |||||
void IZ_LoadVideoConfig(const char*, IZ_VideoConfig* config); | |||||
void IZ_LoadVideoConfig(const char*, IZ_VideoConfig*); | |||||
IZ_ProcedureResult IZ_InitializeVideo(IZ_VideoConfig*, SDL_Window**, SDL_Surface**); | |||||
#endif | #endif |