aboutsummaryrefslogtreecommitdiff
path: root/source/game.c
diff options
context:
space:
mode:
Diffstat (limited to 'source/game.c')
-rw-r--r--source/game.c76
1 files changed, 41 insertions, 35 deletions
diff --git a/source/game.c b/source/game.c
index ea02cbb..b17bf73 100644
--- a/source/game.c
+++ b/source/game.c
@@ -1,13 +1,19 @@
#include "all.h"
-static void GameInitialize(game_t * game, char * window_name);
+static void GameInitialize(game_t * game);
static void GameDeinitialize(game_t * game);
static void GameLoop(game_t * game);
static void GameReport(game_t * game, f32 fps, f32 ups, u32 total_fps, u32 total_ups);
-void GameStart(char * program_name) {
+void GameReinitialize(game_t * game) {
+ GameDeinitialize(game);
+ GameInitialize(game);
+}
+
+void GameStart(config_t config) {
_Alignas(64) game_t game[1] = {0};
- GameInitialize(game, program_name);
+ game->config = config;
+ GameInitialize(game);
GameLoop(game);
GameDeinitialize(game);
}
@@ -33,19 +39,25 @@ void GameResize(game_t * game) {
}
}
-static void GameInitialize(game_t * game, char * window_name) {
-
- game->config = (config_t) {
- .resolution_x = 600,
- .resolution_y = 600,
- .fps = 60,
- .ups = 30,
- .font = "fonts/Atkinson/mono/AtkinsonHyperlegibleMono-Bold.otf",
- .spritesheet = "assets/simple.png",
- .spritesheet_scale = 128,
- .map_x = 13,
- .map_y = 13,
- };
+static void GameInitialize(game_t * game) {
+ /* Strict parameters */
+#define DEFAULT(a, b) ((b) ? (b) : (a))
+ game->config.resolution_x = MAX(200, DEFAULT(600, game->config.resolution_x));
+ game->config.resolution_y = MAX(200, DEFAULT(600, game->config.resolution_y));
+ game->config.fps = MAX(1, DEFAULT(60, game->config.fps));
+ game->config.ups = MAX(1, DEFAULT(30, game->config.ups));
+ if (!*game->config.font)
+ { strlcpy(game->config.font, "fonts/Atkinson/mono/AtkinsonHyperlegibleMono-Bold.otf", CONFIG_STRING_LIMIT); }
+ if (!*game->config.spritesheet)
+ { strlcpy(game->config.spritesheet, "assets/simple.png", CONFIG_STRING_LIMIT); }
+ game->config.spritesheet_scale = DEFAULT(128, game->config.spritesheet_scale);
+ game->config.map_x = MAX(5, DEFAULT(13, game->config.map_x));
+ game->config.map_y = MAX(5, DEFAULT(13, game->config.map_y));
+ game->config.player_count = CLAMP(DEFAULT(4, game->config.player_count), 1, 4);
+ if (!*game->config.window_name)
+ { strlcpy(game->config.window_name, "Unset Window Name, lol lmao", CONFIG_STRING_LIMIT); }
+#undef DEFAULT
+
{
int t = game->config.spritesheet_scale;
/* better, but not really good, it's FINE */
@@ -93,7 +105,7 @@ static void GameInitialize(game_t * game, char * window_name) {
memcpy(game->enemies.enemy, enemy, sizeof(enemy));
}
- MultiPlayer(game, game->config.map_x, game->config.map_y, 4);
+ MultiPlayer(game);
game->tiles.color = (rand() % 4) | ((rand() % 4) << 2) | ((rand() % 4) << 4) | GAME_OPAQUE;
if (game->tiles.color == GAME_OPAQUE) { game->tiles.color |= GAME_WHITE; }
@@ -104,9 +116,7 @@ static void GameInitialize(game_t * game, char * window_name) {
/* :config */
game->font = DefaultFont(game->config.font);
- /* this is retarded (intentionally) */
- RaylibInitialize(game->config.resolution_x-1, game->config.resolution_y-1, window_name, game->font);
- SetWindowSize(game->config.resolution_x, game->config.resolution_y);
+ RaylibInitialize(game->config.resolution_x, game->config.resolution_y, game->config.window_name, game->font);
GameRecalculateViewport(game);
game->spritesheet = LoadTexture(game->config.spritesheet);
@@ -123,19 +133,11 @@ static void GameDeinitialize(game_t * game) {
static void GameLoop(game_t * game) {
- #define StepSimpleStart(prefix,linear) \
- prefix##_delta = timespec_sub(now, prefix##_last); \
- if (timespec_cmp(prefix##_delta, linear) >= 0) { \
- (void)0
-
- #define StepSimpleStop(prefix) \
- prefix##_last = now; \
- }
-
timespec_t
now,
update_interval = {0, (f64) TIMESPEC_HZ / game->config.ups},
frame_interval = {0, (f64) TIMESPEC_HZ / game->config.fps},
+ print_interval = {0, (f64) TIMESPEC_HZ },
update_last, frame_last, print_last,
update_delta, frame_delta, print_delta,
wait;
@@ -153,10 +155,14 @@ static void GameLoop(game_t * game) {
while (1) {
StepStart(update);
+ updates_per_second++;
+ update_total++;
if (Update(game, now)) { return; }
StepStop(update);
StepStart(frame);
+ frames_per_second++;
+ frame_total++;
clock_gettime(CLOCK_MONOTONIC, &now);
interpolation =
CLAMP(
@@ -178,14 +184,14 @@ static void GameLoop(game_t * game) {
nanosleep(&wait, NULL);
}
- StepSimpleStart(print, one_second);
+ StepStart(print);
GameReport(game,
- round(frames_per_second / TIMESPEC_TO_F64(print_delta)),
- round(updates_per_second / TIMESPEC_TO_F64(print_delta)),
- frame_total,
- update_total);
+ round(frames_per_second / TIMESPEC_TO_F64(print_delta)),
+ round(updates_per_second / TIMESPEC_TO_F64(print_delta)),
+ frame_total,
+ update_total);
frames_per_second = updates_per_second = 0;
- StepSimpleStop(print);
+ StepStop(print);
clock_gettime(CLOCK_MONOTONIC, &now);
}