mystnb/src/main.c

143 lines
2.3 KiB
C

#include <gint/display.h>
#include <gint/keyboard.h>
#include <gint/timer.h>
#include <gint/clock.h>
#include "engine.h"
static void draw_menu(int selected)
{
extern bopti_image_t img_title;
extern bopti_image_t img_levels;
dclear(C_WHITE);
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);
}
if(i == selected)
{
drect(x+1, y+1, x+8, y+8, C_INVERT);
}
}
}
static int main_menu(void)
{
extern font_t font_mystere;
dfont(&font_mystere);
int selected = 1;
int key = 0;
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++;
}
return selected;
}
/* Returns a direction to move in */
static int get_inputs(void)
{
int opt = GETKEY_DEFAULT & ~GETKEY_REP_ARROWS;
int timeout = 1;
while(1)
{
key_event_t ev = getkey_opt(opt, &timeout);
if(ev.type == KEYEV_NONE) return -1;
int key = ev.key;
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;
}
}
static int callback_tick(volatile int *tick)
{
*tick = 1;
return TIMER_CONTINUE;
}
int main(void)
{
GUNUSED int level = main_menu();
struct player singleplayer = {
.x = 2,
.y = 3,
.dir = DIR_DOWN,
.anim.function = anim_player_idle,
.anim.dir = DIR_DOWN,
};
singleplayer.idle = !anim_player_idle(&singleplayer.anim, 1);
struct map map = {
.w = 13,
.h = 7
};
struct game game = {
.map = &map,
.players = { &singleplayer, NULL },
.time = 0,
};
/* 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);
int level_finished = 0;
while(!level_finished)
{
while(!tick) sleep();
tick = 0;
engine_draw(&game);
dupdate();
int dir = get_inputs();
int turn_finished = 0;
if(dir >= 0)
{
turn_finished = engine_move(&game, &singleplayer, dir);
}
if(turn_finished)
{
/* Update doors, etc */
}
engine_tick(&game, ENGINE_TICK);
}
if(t >= 0) timer_stop(t);
return 1;
}