supercasiobros/src/keyboard.c

176 lines
2.6 KiB
C
Raw Normal View History

2020-01-29 14:34:47 +01:00
#include <keyboard.h>
#include <mario.h>
#include <score.h>
2019-11-21 19:30:54 +01:00
#include <gint/keyboard.h>
#include <gint/display.h>
2020-01-29 14:34:47 +01:00
//#include <gint/timer.h>
#include <gint/clock.h>
2020-01-29 14:34:47 +01:00
#include <config.h>
2019-11-21 19:30:54 +01:00
2020-01-29 14:34:47 +01:00
static mkb_t keys[6]={0};
2019-11-21 19:30:54 +01:00
2020-01-29 14:34:47 +01:00
int mkb_getstate(mkb_t const k)
{
if (k!=MK_NONE)
return keys[k];
return 0;
}
2020-01-29 14:34:47 +01:00
void mkb_clear()
{
2020-01-06 20:56:10 +01:00
for (int i=0; i<6; i++)
keys[i]=0;
clearevents();
}
2020-01-29 14:34:47 +01:00
int mkb_getkey()
{
mkb_clear();
while (1)
{
key_event_t const e=pollevent();
if (e.type==KEYEV_DOWN) // Returns only whan a key is down
return e.key;
else
sleep(); // Processor friendly :)
}
}
static int menu_pause() // 1 exit, 0 continue
2020-01-06 20:56:10 +01:00
{
extern image_t img_menu_pause;
extern image_t img_select_arrow;
clearevents();
int choice=0;
int x=64-img_menu_pause.width/2;
int y=32-img_menu_pause.height/2;
for (int i=0; i<6; i++)
keys[i]=0;
while (1)
{
dimage(x,y,&img_menu_pause);
dimage(x+2, y+2+7*choice, &img_select_arrow);
dupdate();
2020-01-29 14:34:47 +01:00
switch (mkb_getkey())
{
case KEY_EXIT:
return 0;
case KEY_MENU:
return 1;
case KEY_OPTN:
choice=2;
2020-01-29 14:34:47 +01:00
// fall through
case KEY_EXE:
if (choice==2)
configmenu();
return choice & 1;
case KEY_UP:
if (choice>0)
2020-01-06 20:56:10 +01:00
choice--;
break;
case KEY_DOWN:
if (choice<2)
2020-01-06 20:56:10 +01:00
choice++;
break;
2020-01-29 14:34:47 +01:00
#ifdef KONAMI
case KEY_F1:
for (int a=0; a<10; a++)
2019-12-07 14:32:38 +01:00
{
key_event_t e=pollevent();
if (e.type==KEYEV_DOWN)
2019-12-07 14:32:38 +01:00
{
2020-01-29 14:34:47 +01:00
static int const konami[]={KEY_UP,KEY_UP,KEY_DOWN,KEY_DOWN,KEY_LEFT,KEY_RIGHT,KEY_LEFT,KEY_RIGHT,KEY_ALPHA,KEY_SHIFT};
if (e.key!=konami[a])
2019-12-07 14:32:38 +01:00
break;
}
if (keydown(KEY_EXIT))
break;
if (a==10) // Cheat code
{
mario_bigger();
mario_has_bullets=1;
2020-01-29 14:34:47 +01:00
for (int i=0; i<20; i++)
lifes_add();
extern image_t img_dev;
dimage(0,0,&img_dev);
dupdate();
sleep_ms(3,1000);
return 0;
}
else
e=pollevent();
2019-12-07 14:32:38 +01:00
}
2020-01-29 14:34:47 +01:00
#endif
}
}
}
2020-01-29 14:34:47 +01:00
void mkb_update()
2019-11-21 19:30:54 +01:00
{
key_event_t e;
e=pollevent();
int menu=0;
for (int i=0; i<6; i++)
if (keys[i]==2) keys[i]=1;
2019-11-21 19:30:54 +01:00
while(e.type!=KEYEV_NONE)
{
2020-01-29 14:34:47 +01:00
mkb_t k = MK_NONE;
if(e.key==KEY_LEFT)
k=MK_LEFT;
if(e.key==KEY_RIGHT)
k=MK_RIGHT;
if(e.key==KEY_SHIFT)
2020-01-29 14:34:47 +01:00
k=MK_JUMP;
if(e.key==KEY_UP)
k=MK_UP;
if(e.key==KEY_DOWN)
2020-01-29 14:34:47 +01:00
k=MK_DOWN;
if(e.key==KEY_ALPHA)
k=MK_RUN;
if(keydown(KEY_EXIT))
2020-01-29 14:34:47 +01:00
menu=1; // Displays menu after updating the keyboard
if (k!=MK_NONE)
{
if (e.type==KEYEV_DOWN)
{
keys[k]=2;
}
if (e.type==KEYEV_UP)
{
keys[k]=0;
}
}
e=pollevent();
}
if (menu)
{
int t=menu_pause();
if (t==1)
2020-01-29 14:34:47 +01:00
finish_level=-1; // Exits the level
return;
}
}
2020-01-29 14:34:47 +01:00