supercasiobros/src/keyboard.c

178 lines
2.7 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>
2020-02-02 16:05:56 +01:00
#include <liblog.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];
2020-01-29 14:34:47 +01:00
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;
2020-01-06 20:56:10 +01:00
clearevents();
}
2020-01-29 14:34:47 +01:00
int mkb_getkey()
{
mkb_clear();
while (1)
{
key_event_t const e=pollevent();
2020-02-02 16:05:56 +01:00
if (e.key==KEY_ARROW)
ll_pause();
2020-01-29 14:34:47 +01:00
if (e.type==KEYEV_DOWN) // Returns only whan a key is down
return e.key;
2020-01-29 14:34:47 +01:00
else
sleep(); // Processor friendly :)
2020-01-29 14:34:47 +01:00
}
}
static int menu_pause() // 1 exit, 0 continue
{
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;
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)
choice--;
break;
case KEY_DOWN:
if (choice<2)
choice++;
break;
#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])
break;
2019-12-07 14:32:38 +01:00
}
if (keydown(KEY_EXIT))
break;
if (a==10) // Cheat code
{
mario_bigger();
mario_has_bullets=1;
LifesAdd(20);
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
}
#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();
2020-02-02 16:05:56 +01:00
int menu=0, log=0;
for (int i=0; i<6; i++) if (keys[i]==2) keys[i]=1; else if (keys[i]<0 || keys[i]>2) keys[i]=0;
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)
k=MK_JUMP;
if(e.key==KEY_COS)
k=MK_JUMP;
if(e.key==KEY_UP)
k=MK_UP;
if(e.key==KEY_DOWN)
k=MK_DOWN;
if(e.key==KEY_XOT)
k=MK_RUN;
if(e.key==KEY_OPTN)
k=MK_RUN;
if(keydown(KEY_EXIT))
menu=1; // Displays menu after updating the keyboard
2020-02-02 16:05:56 +01:00
if (e.key==KEY_ARROW)
log=1;
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)
finish_level=-1; // Exits the level
return;
}
2020-02-02 16:05:56 +01:00
if (log)
ll_pause();
}