Browse Source

Implement abstractions

Ensure each initialization method is in its own source.
feature/data-structs
TheoryOfNekomata 2 years ago
parent
commit
0cee207138
11 changed files with 76 additions and 57 deletions
  1. +23
    -4
      src/packages/game/IZ_app.c
  2. +4
    -1
      src/packages/game/IZ_app.h
  3. +1
    -1
      src/packages/game/geometry/IZ_point2d.c
  4. +4
    -4
      src/packages/game/geometry/IZ_point2d.h
  5. +6
    -6
      src/packages/game/geometry/IZ_rect.h
  6. +1
    -1
      src/packages/game/geometry/IZ_vector2d.c
  7. +4
    -4
      src/packages/game/geometry/IZ_vector2d.h
  8. +1
    -1
      src/packages/game/geometry/geometry.test.c
  9. +10
    -33
      src/packages/game/main.c
  10. +17
    -0
      src/packages/game/output/IZ_video.c
  11. +5
    -2
      src/packages/game/output/IZ_video.h

+ 23
- 4
src/packages/game/IZ_app.c View File

@@ -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;
}


+ 4
- 1
src/packages/game/IZ_app.h View File

@@ -1,6 +1,7 @@
#ifndef IZ_APP_H
#define IZ_APP_H

#include <SDL.h>
#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

+ 1
- 1
src/packages/game/geometry/IZ_point2d.c View File

@@ -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,


+ 4
- 4
src/packages/game/geometry/IZ_point2d.h View File

@@ -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

+ 6
- 6
src/packages/game/geometry/IZ_rect.h View File

@@ -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);


+ 1
- 1
src/packages/game/geometry/IZ_vector2d.c View File

@@ -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,


+ 4
- 4
src/packages/game/geometry/IZ_vector2d.h View File

@@ -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

+ 1
- 1
src/packages/game/geometry/geometry.test.c View File

@@ -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,


+ 10
- 33
src/packages/game/main.c View File

@@ -1,42 +1,19 @@
#include <SDL.h>
#include <stdbool.h>
#include <stdio.h>

#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;


+ 17
- 0
src/packages/game/output/IZ_video.c View File

@@ -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;
}

+ 5
- 2
src/packages/game/output/IZ_video.h View File

@@ -3,6 +3,7 @@

#include <stdio.h>
#include <minIni.h>
#include <SDL_video.h>
#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

Loading…
Cancel
Save