From 0cee207138abd81a81ff466071ac002b841f3056 Mon Sep 17 00:00:00 2001 From: TheoryOfNekomata Date: Sat, 28 May 2022 08:19:15 +0800 Subject: [PATCH] Implement abstractions Ensure each initialization method is in its own source. --- src/packages/game/IZ_app.c | 27 ++++++++++++-- src/packages/game/IZ_app.h | 5 ++- src/packages/game/geometry/IZ_point2d.c | 2 +- src/packages/game/geometry/IZ_point2d.h | 8 ++-- src/packages/game/geometry/IZ_rect.h | 12 +++--- src/packages/game/geometry/IZ_vector2d.c | 2 +- src/packages/game/geometry/IZ_vector2d.h | 8 ++-- src/packages/game/geometry/geometry.test.c | 2 +- src/packages/game/main.c | 43 +++++----------------- src/packages/game/output/IZ_video.c | 17 +++++++++ src/packages/game/output/IZ_video.h | 7 +++- 11 files changed, 76 insertions(+), 57 deletions(-) diff --git a/src/packages/game/IZ_app.c b/src/packages/game/IZ_app.c index e51721e..9ac8ade 100644 --- a/src/packages/game/IZ_app.c +++ b/src/packages/game/IZ_app.c @@ -1,19 +1,38 @@ #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]; IZ_GetConfigPath(config_path, 128); 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; + if (IZ_InitializeVideo(&app->video_config, &app->window, &app->screen_surface)) { + return 2; + } + for (uint8_t p = 0; p < PLAYERS; p += 1) { 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_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; } diff --git a/src/packages/game/IZ_app.h b/src/packages/game/IZ_app.h index 3c6bdf9..c759681 100644 --- a/src/packages/game/IZ_app.h +++ b/src/packages/game/IZ_app.h @@ -1,6 +1,7 @@ #ifndef IZ_APP_H #define IZ_APP_H +#include #include "input/IZ_keyboard.h" #include "input/IZ_joystick.h" #include "output/IZ_video.h" @@ -17,10 +18,12 @@ typedef struct { // output, video state IZ_VideoConfig video_config; uint64_t video_update_at; + SDL_Window* window; + SDL_Surface* screen_surface; IZ_Pool memory_pool; } IZ_App; -int IZ_InitializeApp(IZ_App*); +IZ_ProcedureResult IZ_InitializeApp(IZ_App*); #endif diff --git a/src/packages/game/geometry/IZ_point2d.c b/src/packages/game/geometry/IZ_point2d.c index 27b25bd..84caab7 100644 --- a/src/packages/game/geometry/IZ_point2d.c +++ b/src/packages/game/geometry/IZ_point2d.c @@ -1,6 +1,6 @@ #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) { .x = point.x + translate_x, .y = point.y + translate_y, diff --git a/src/packages/game/geometry/IZ_point2d.h b/src/packages/game/geometry/IZ_point2d.h index 5637653..9c80321 100644 --- a/src/packages/game/geometry/IZ_point2d.h +++ b/src/packages/game/geometry/IZ_point2d.h @@ -1,13 +1,13 @@ #ifndef IZ_POINT2D_H #define IZ_POINT2D_H -typedef float IZ_Coordinate; +typedef float IZ_GeoCoord; typedef struct { - IZ_Coordinate x; - IZ_Coordinate y; + IZ_GeoCoord x; + IZ_GeoCoord y; } IZ_Point2D; -IZ_Point2D IZ_PointTranslate(IZ_Point2D, IZ_Coordinate, IZ_Coordinate); +IZ_Point2D IZ_PointTranslate(IZ_Point2D, IZ_GeoCoord, IZ_GeoCoord); #endif diff --git a/src/packages/game/geometry/IZ_rect.h b/src/packages/game/geometry/IZ_rect.h index 06e469a..6c0e062 100644 --- a/src/packages/game/geometry/IZ_rect.h +++ b/src/packages/game/geometry/IZ_rect.h @@ -5,17 +5,17 @@ #include "IZ_point2d.h" 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; typedef struct { // top left IZ_Point2D pos; - IZ_Coordinate width; - IZ_Coordinate height; + IZ_GeoCoord width; + IZ_GeoCoord height; } IZ_Rect; IZ_Bounds IZ_RectGetBounds(IZ_Rect); diff --git a/src/packages/game/geometry/IZ_vector2d.c b/src/packages/game/geometry/IZ_vector2d.c index d320aa9..5d86aa4 100644 --- a/src/packages/game/geometry/IZ_vector2d.c +++ b/src/packages/game/geometry/IZ_vector2d.c @@ -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) { .right = vector.right * scalar, .up = vector.up * scalar, diff --git a/src/packages/game/geometry/IZ_vector2d.h b/src/packages/game/geometry/IZ_vector2d.h index ddf9b32..35580cf 100644 --- a/src/packages/game/geometry/IZ_vector2d.h +++ b/src/packages/game/geometry/IZ_vector2d.h @@ -1,17 +1,17 @@ #ifndef IZ_VECTOR2D_H #define IZ_VECTOR2D_H -typedef float IZ_VectorMagnitude; +#include "IZ_point2d.h" typedef struct { - IZ_VectorMagnitude right; - IZ_VectorMagnitude up; + IZ_GeoCoord right; + IZ_GeoCoord up; } IZ_Vector2D; IZ_Vector2D IZ_VectorAdd(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 diff --git a/src/packages/game/geometry/geometry.test.c b/src/packages/game/geometry/geometry.test.c index 0eee07e..40c218b 100644 --- a/src/packages/game/geometry/geometry.test.c +++ b/src/packages/game/geometry/geometry.test.c @@ -84,7 +84,7 @@ spec("geometry") { .up = 69.f, }; - static IZ_VectorMagnitude s = 2.f; + static IZ_GeoCoord s = 2.f; static IZ_Vector2D expected = { .right = 840.f, diff --git a/src/packages/game/main.c b/src/packages/game/main.c index b56a220..9561c7a 100644 --- a/src/packages/game/main.c +++ b/src/packages/game/main.c @@ -1,42 +1,19 @@ #include #include -#include #include "IZ_action.h" #include "IZ_app.h" int main(int argc, char* args[]) { - SDL_Window* window = NULL; - SDL_Surface* screen_surface = NULL; 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; SDL_Event e; - while (true) { 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) { // 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) { 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(screen_surface, &(SDL_Rect) { + SDL_FillRect(app.screen_surface, &(SDL_Rect) { column * size, app.video_config.height - ((row + 1) * 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 uint8_t size = 4; if (app.actions[p] & bitflag) { - SDL_FillRect(screen_surface, &(SDL_Rect) { + SDL_FillRect(app.screen_surface, &(SDL_Rect) { column * size, row * 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; } } - SDL_DestroyWindow(window); + SDL_DestroyWindow(app.window); SDL_Quit(); return 0; diff --git a/src/packages/game/output/IZ_video.c b/src/packages/game/output/IZ_video.c index b938201..84125a7 100644 --- a/src/packages/game/output/IZ_video.c +++ b/src/packages/game/output/IZ_video.c @@ -19,3 +19,20 @@ void IZ_LoadVideoConfig(const char* config_path, IZ_VideoConfig* config) { config->height = ini_getl("Video", "Height", 480l, 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; +} diff --git a/src/packages/game/output/IZ_video.h b/src/packages/game/output/IZ_video.h index e9f56e7..075734f 100644 --- a/src/packages/game/output/IZ_video.h +++ b/src/packages/game/output/IZ_video.h @@ -3,6 +3,7 @@ #include #include +#include #include "../IZ_common.h" #include "../IZ_config.h" @@ -12,8 +13,10 @@ typedef struct { uint8_t max_fps; } 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