momento/src/main.c

106 lines
2.4 KiB
C
Raw Normal View History

2021-03-28 19:35:07 +02:00
/* SPDX-License-Identifier: GPL-3.0-or-later */
/* Copyright (C) 2021 KikooDX */
2021-03-29 17:27:03 +02:00
#include "conf.h"
2021-03-28 19:35:07 +02:00
#include "level.h"
#include "particles.h"
#include "player.h"
2021-03-29 17:27:03 +02:00
#include <gint/clock.h>
2021-03-28 19:35:07 +02:00
#include <gint/display.h>
#include <gint/gint.h>
#include <gint/keyboard.h>
2021-03-29 17:27:03 +02:00
#include <gint/timer.h>
#define PANIC(msg) \
{ \
dclear(C_BLACK); \
dprint_opt(DWIDTH / 2, DHEIGHT / 2, C_RED, C_NONE, \
DTEXT_CENTER, DTEXT_MIDDLE, "ERROR: %s", \
msg); \
dupdate(); \
getkey(); \
return 0; \
}
2021-03-28 19:35:07 +02:00
extern struct Level level;
extern int level_id;
2021-03-29 17:27:03 +02:00
extern int fatal_error;
extern char *fatal_error_msg;
static int callback(volatile void *arg);
2021-03-28 19:35:07 +02:00
int main(void)
{
2021-03-29 17:27:03 +02:00
int i;
int timer;
2021-04-01 01:39:45 +02:00
int player_return_code;
2021-03-29 17:27:03 +02:00
volatile int has_ticked = 1;
2021-03-28 19:35:07 +02:00
/* load level */
level_id = 0;
gint_switch(level_load);
2021-03-29 17:27:03 +02:00
if (fatal_error == -1)
PANIC(fatal_error_msg);
/* create player */
struct Player player = player_init();
/* initialize particle engine */
particles_init();
2021-03-29 17:27:03 +02:00
/* timer setup */
timer = timer_setup(TIMER_ANY, 1000000 / TARGET_UPS, callback,
&has_ticked);
if (timer == -1)
PANIC("timer_setup failed");
timer_start(timer);
2021-03-28 19:35:07 +02:00
2021-03-29 17:27:03 +02:00
/* main game loop */
while (!keydown(KEY_EXIT)) {
/* do x updates per frame, x being ups/fps */
i = TARGET_UPS / TARGET_FPS;
while (i-- > 0) {
2021-03-29 17:27:03 +02:00
/* speed limiter */
while (!has_ticked)
sleep();
has_ticked = 0;
/* update */
clearevents();
particles_update();
2021-04-01 01:39:45 +02:00
player_return_code = player_update(&player);
switch (player_return_code) {
case 1:
level_id += 1;
gint_switch(level_load);
if (fatal_error == -1)
PANIC(fatal_error_msg);
particles_init();
2021-04-01 01:39:45 +02:00
player = player_init();
break;
case -1:
gint_switch(level_load);
if (fatal_error == -1)
PANIC(fatal_error_msg);
particles_init();
2021-04-01 01:39:45 +02:00
player = player_init();
break;
default:
break;
}
2021-03-29 17:27:03 +02:00
}
/* draw */
dclear(C_BLACK);
level_draw();
player_draw(player);
particles_draw();
2021-03-29 17:27:03 +02:00
dupdate();
}
2021-03-28 19:35:07 +02:00
2021-03-29 17:27:03 +02:00
timer_stop(timer);
2021-03-28 19:35:07 +02:00
return 1;
}
2021-03-29 17:27:03 +02:00
static int callback(volatile void *arg)
{
volatile int *has_ticked = arg;
*has_ticked = 1;
return 0;
}