mystnb/src/main.c

141 lines
2.3 KiB
C
Raw Normal View History

2020-06-17 21:12:35 +02:00
#include <gint/display.h>
#include <gint/keyboard.h>
2020-08-21 11:15:33 +02:00
#include <gint/timer.h>
#include <gint/clock.h>
2020-06-17 21:12:35 +02:00
2020-08-20 17:04:31 +02:00
#include "engine.h"
2020-07-25 12:16:15 +02:00
static void draw_menu(int selected)
2020-06-17 21:12:35 +02:00
{
2020-07-25 10:37:07 +02:00
extern bopti_image_t img_title;
extern bopti_image_t img_levels;
2020-06-17 21:12:35 +02:00
dclear(C_WHITE);
2020-07-25 10:37:07 +02:00
dimage(0, 2, &img_title);
for(int i = 1; i <= 8; i++)
{
int x = 20 + 11*(i-1);
int y = 36;
if(i != 8)
{
dsubimage(x, y, &img_levels, 0,0,10,10, DIMAGE_NONE);
dprint(x+3, y+2, C_BLACK, "%d", i);
}
else
{
dsubimage(x, y, &img_levels, 11,0,10,10, DIMAGE_NONE);
}
2020-07-25 12:16:15 +02:00
if(i == selected)
{
drect(x+1, y+1, x+8, y+8, C_INVERT);
}
2020-07-25 10:37:07 +02:00
}
2020-07-25 12:16:15 +02:00
}
2020-08-20 17:04:31 +02:00
static int main_menu(void)
2020-07-25 12:16:15 +02:00
{
extern font_t font_mystere;
dfont(&font_mystere);
int selected = 1;
int key = 0;
2020-07-25 10:37:07 +02:00
2020-07-25 12:16:15 +02:00
while(key != KEY_EXE)
{
draw_menu(selected);
dupdate();
key = getkey().key;
if(key == KEY_LEFT && selected > 1)
selected--;
if(key == KEY_RIGHT && selected < 8)
selected++;
}
2020-06-17 21:12:35 +02:00
2020-08-20 17:04:31 +02:00
return selected;
}
/* Returns a direction to move in */
static int get_inputs(void)
{
int opt = GETKEY_DEFAULT & ~GETKEY_REP_ARROWS;
2020-08-21 11:15:33 +02:00
int timeout = 1;
2020-08-20 17:04:31 +02:00
while(1)
{
2020-08-21 11:15:33 +02:00
key_event_t ev = getkey_opt(opt, &timeout);
if(ev.type == KEYEV_NONE) return -1;
2020-08-20 17:04:31 +02:00
2020-08-21 11:15:33 +02:00
int key = ev.key;
2020-08-20 17:04:31 +02:00
if(key == KEY_DOWN) return DIR_DOWN;
if(key == KEY_RIGHT) return DIR_RIGHT;
if(key == KEY_UP) return DIR_UP;
if(key == KEY_LEFT) return DIR_LEFT;
}
}
2020-08-21 11:15:33 +02:00
static int callback_tick(volatile int *tick)
{
*tick = 1;
return TIMER_CONTINUE;
}
2020-08-20 17:04:31 +02:00
int main(void)
{
GUNUSED int level = main_menu();
struct player singleplayer = {
.x = 2,
2020-08-21 11:15:33 +02:00
.y = 3,
.dir = DIR_DOWN,
.anim.function = anim_player_idle,
.anim.dir = DIR_DOWN,
2020-08-20 17:04:31 +02:00
};
2020-08-21 11:15:33 +02:00
singleplayer.idle = !anim_player_idle(&singleplayer.anim, 1);
2020-12-22 17:01:59 +01:00
extern struct map map_lv1;
2020-08-20 17:04:31 +02:00
struct game game = {
2020-12-22 17:01:59 +01:00
.map = &map_lv1,
2020-08-20 17:04:31 +02:00
.players = { &singleplayer, NULL },
.time = 0,
};
2020-08-21 11:15:33 +02:00
/* Global tick clock */
static volatile int tick = 1;
int t = timer_setup(TIMER_ANY, ENGINE_TICK*1000, callback_tick, &tick);
if(t >= 0) timer_start(t);
2020-08-20 17:04:31 +02:00
2020-08-21 11:15:33 +02:00
int level_finished = 0;
2020-08-20 17:04:31 +02:00
while(!level_finished)
{
2020-08-21 11:15:33 +02:00
while(!tick) sleep();
tick = 0;
engine_draw(&game);
dupdate();
int dir = get_inputs();
2020-08-20 17:04:31 +02:00
int turn_finished = 0;
2020-08-21 11:15:33 +02:00
if(dir >= 0)
{
2020-08-20 17:04:31 +02:00
turn_finished = engine_move(&game, &singleplayer, dir);
}
2020-08-21 11:15:33 +02:00
if(turn_finished)
{
/* Update doors, etc */
}
2020-08-20 17:04:31 +02:00
2020-08-21 11:15:33 +02:00
engine_tick(&game, ENGINE_TICK);
2020-08-20 17:04:31 +02:00
}
2020-08-21 11:15:33 +02:00
if(t >= 0) timer_stop(t);
2020-06-17 21:12:35 +02:00
return 1;
}