momento/src/main.c

150 lines
3.8 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-04-09 15:11:00 +02:00
#include "transition.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) \
2021-04-09 15:11:00 +02:00
do { \
2021-03-29 17:27:03 +02:00
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-04-09 15:11:00 +02:00
} while (0);
#define LOAD_LEVEL() \
do { \
gint_switch(level_load); \
if (fatal_error == -1) \
PANIC(fatal_error_msg); \
particles_init(); \
player = player_init(); \
} while (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-04-09 15:11:00 +02:00
enum TransitionMode transition_previous_mode;
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-04-09 15:11:00 +02:00
/* initialize transitions */
struct Transition transition =
transition_init(0.0, C_BLACK, TransitionNone);
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();
2021-04-09 15:11:00 +02:00
if (transition.mode == TransitionNone) {
particles_update();
player_return_code =
player_update(&player);
switch (player_return_code) {
case 1:
level_id += 1;
transition = transition_init(
H_TRANS_SPD, C_BLUE,
TransitionHOut);
2021-04-09 15:11:00 +02:00
break;
case -1:
transition = transition_init(
V_TRANS_SPD, C_RED,
2021-04-09 15:11:00 +02:00
TransitionVOut);
break;
default:
break;
}
}
transition_previous_mode =
transition_update(&transition);
if (transition_previous_mode !=
transition.mode &&
transition_previous_mode !=
TransitionNone) {
switch (transition_previous_mode) {
case TransitionNone:
break;
case TransitionHIn:
break;
case TransitionHOut:
LOAD_LEVEL();
transition = transition_init(
transition.speed,
transition.color,
TransitionHIn);
2021-04-09 15:11:00 +02:00
break;
case TransitionVIn:
break;
case TransitionVOut:
LOAD_LEVEL();
2021-04-09 15:11:00 +02:00
transition = transition_init(
transition.speed,
2021-04-09 15:11:00 +02:00
transition.color,
TransitionVIn);
break;
default:
break;
}
2021-04-01 01:39:45 +02:00
}
2021-04-09 15:11:00 +02:00
/* draw */
dclear(C_BLACK);
level_draw();
particles_draw();
player_draw(player);
2021-04-09 15:11:00 +02:00
transition_draw(transition);
dupdate();
2021-03-29 17:27:03 +02:00
}
}
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;
}