supercasiobros/src/keyboard.c

182 lines
2.7 KiB
C
Executable File

#include <keyboard.h>
#include <mario.h>
#include <score.h>
#include <gint/keyboard.h>
#include <gint/display.h>
//#include <gint/timer.h>
#include <gint/clock.h>
#include <config.h>
#include <liblog.h>
static mkb_t keys[6]={0};
int mkb_getstate(mkb_t const k)
{
if (k!=MK_NONE)
return keys[k];
return 0;
}
void mkb_clear()
{
for (int i=0; i<6; i++)
keys[i]=0;
clearevents();
}
int mkb_getkey()
{
mkb_clear();
while (1)
{
key_event_t const e=pollevent();
if (e.key==KEY_ARROW)
ll_pause();
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
{
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();
switch (mkb_getkey())
{
case KEY_EXIT:
return 0;
case KEY_MENU:
return 1;
case KEY_OPTN:
choice=2;
// 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++)
{
key_event_t e=pollevent();
if (e.type==KEYEV_DOWN)
{
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;
}
if (keydown(KEY_EXIT))
break;
if (a==10) // Cheat code
{
mario_bigger();
mario_has_bullets=1;
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();
}
#endif
}
}
}
void mkb_update()
{
key_event_t e;
e=pollevent();
int menu=0, log=0;
for (int i=0; i<6; i++)
if (keys[i]==2) keys[i]=1;
while(e.type!=KEYEV_NONE)
{
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_UP)
k=MK_UP;
if(e.key==KEY_DOWN)
k=MK_DOWN;
if(e.key==KEY_ALPHA)
k=MK_RUN;
if(keydown(KEY_EXIT))
menu=1; // Displays menu after updating the keyboard
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;
}
if (log)
ll_pause();
}