jtmm2/src/main.c

117 lines
1.9 KiB
C
Raw Permalink Normal View History

2021-12-17 11:14:56 +01:00
#include "conf.h"
2021-12-19 00:30:09 +01:00
#include "editor.h"
2021-12-15 23:55:44 +01:00
#include "input.h"
2021-12-16 13:56:02 +01:00
#include "level.h"
2021-12-21 00:14:49 +01:00
#include "missile.h"
2021-12-15 23:43:04 +01:00
#include "player.h"
2021-12-20 12:06:32 +01:00
#include "time.h"
2021-12-20 18:00:17 +01:00
#include "util.h"
2021-12-17 11:14:56 +01:00
#include <gint/cpu.h>
2021-12-15 23:43:04 +01:00
#include <gint/display.h>
2021-12-23 11:52:37 +01:00
#include <gint/gint.h>
2021-12-17 11:14:56 +01:00
#include <gint/timer.h>
2021-12-15 23:43:04 +01:00
2021-12-16 12:16:16 +01:00
static struct Player player;
2021-12-17 11:14:56 +01:00
static int timer;
2021-12-19 00:30:09 +01:00
volatile int has_ticked;
2021-12-20 12:06:32 +01:00
int time = 0;
2021-12-15 23:43:04 +01:00
static void init(void);
static void deinit(void);
static void draw(void);
static void update(void);
2021-12-17 11:14:56 +01:00
static int timer_callback(volatile int *);
2021-12-15 23:43:04 +01:00
2021-12-15 23:07:26 +01:00
int
main(void)
{
2021-12-17 11:14:56 +01:00
int frameskip = 1;
2021-12-16 13:56:02 +01:00
2021-12-15 23:43:04 +01:00
init();
2021-12-17 23:07:06 +01:00
level_load(0);
2021-12-15 23:43:04 +01:00
2021-12-20 18:00:17 +01:00
for (;;) {
2021-12-17 11:14:56 +01:00
int i;
2021-12-15 23:43:04 +01:00
draw();
2021-12-17 11:14:56 +01:00
for (i = 0; i < frameskip; i++) {
if (has_ticked > frameskip) frameskip = has_ticked;
while (!has_ticked)
sleep();
2021-12-20 12:06:32 +01:00
while (has_ticked) {
time += has_ticked;
2021-12-17 11:14:56 +01:00
has_ticked = 0;
2021-12-20 12:06:32 +01:00
}
2021-12-17 11:14:56 +01:00
update();
2021-12-20 18:00:17 +01:00
if (input_pressed(K_EXIT) && ask_confirm("exit game"))
goto game_loop_end;
if (input_pressed(K_SKIP) && ask_confirm("skip level"))
level_next();
2021-12-17 11:14:56 +01:00
}
2021-12-20 18:00:17 +01:00
};
game_loop_end:
2021-12-15 23:43:04 +01:00
deinit();
2021-12-15 23:07:26 +01:00
return 0;
}
2021-12-15 23:43:04 +01:00
static void
init(void)
{
2021-12-18 14:40:24 +01:00
extern font_t font_dina;
dfont(&font_dina);
2021-12-17 11:14:56 +01:00
timer = timer_configure(TIMER_ANY, 1000000 / TARGET_FPS,
GINT_CALL(timer_callback, &has_ticked));
timer_start(timer);
2021-12-15 23:55:44 +01:00
input_init();
2021-12-17 23:07:06 +01:00
level_init(&player);
2021-12-15 23:43:04 +01:00
}
static void
deinit(void)
{
2021-12-16 13:56:02 +01:00
level_deinit();
2021-12-21 00:14:49 +01:00
missile_manager_free();
2021-12-17 11:14:56 +01:00
timer_stop(timer);
2021-12-15 23:43:04 +01:00
}
static void
update(void)
{
2021-12-15 23:55:44 +01:00
input_update();
2021-12-21 00:14:49 +01:00
missile_manager_update(vecf(player_middle(&player)));
2021-12-15 23:43:04 +01:00
player_update(&player);
2021-12-19 00:30:09 +01:00
/* enter editor */
if (input_pressed(K_EDITOR)) editor();
2021-12-23 15:54:44 +01:00
/* save replay */
if (input_pressed(K_SAVE_REPLAY)) {
gint_world_switch(GINT_CALL(input_dump_replay));
has_ticked = 0;
}
/* playback */
if (input_pressed(K_PLAYBACK))
gint_world_switch(GINT_CALL(input_load_replay));
2021-12-15 23:43:04 +01:00
}
static void
draw(void)
{
dclear(C_BLACK);
2021-12-16 15:57:50 +01:00
level_draw();
2021-12-21 00:14:49 +01:00
missile_manager_draw();
2021-12-15 23:43:04 +01:00
player_draw(&player);
2021-12-19 00:30:09 +01:00
level_draw_name();
2021-12-20 12:06:32 +01:00
time_draw();
2021-12-15 23:43:04 +01:00
dupdate();
}
2021-12-17 11:14:56 +01:00
static int
timer_callback(volatile int *arg)
{
*arg += 1;
return 0;
}