Keyintr.cpp
资源名称:泡泡龙源代码.zip [点击查看]
上传用户:junwei_58
上传日期:2007-05-15
资源大小:79k
文件大小:3k
源码类别:
其他智力游戏
开发平台:
Visual C++
- #ifndef KEYINTR_CPP
- #define KEYINTR_CPP
- //#pragma inline
- #include <stdio.h>
- #include <dos.h>
- #include <conio.h>
- #include "keyintr.h"
- void installKeyFunc(){
- inGame=0;
- oldkey=getvect(INTR_KEY);
- setvect(INTR_KEY,NewKey);
- keyboard.status=READY;
- keytable[0]=0L;
- keytable[1]=0L;
- keytable[2]=0L;
- keytable[3]=0L;
- }
- void uninstallKeyFunc(){setvect(INTR_KEY,oldkey);}
- void interrupt NewKey(__CPPFLAG){
- // char theKey;
- if(inGame&KEYNEW){
- //inGame^=KEYNEW;
- //if(keyboard.status!=READY)goto ENDi;
- asm{
- sti //;if i do not open interrupt it runs better.
- // read the key in ah and al
- in al,0x60
- mov ah,al
- push ax
- in al,0x61
- mov ah,al
- or al,0x80
- out 0x61,al
- xchg ah,al
- out 0x61,al
- pop ax
- cmp byte ptr keyboard.status,READY
- jnz END
- and al,0x7f //mov theKey,al
- // if(!keytable[theKey>>5]&(1<<(theKey&0x0f))){
- mov bl,al
- shr bl,3
- xor bh,bh //calculate index of keytable
- mov cl,al
- and cl,0x07
- test ah,0x80;jnz short KEY_RELEASED
- mov ah,1
- shl ah,cl
- // push down
- test keytable[bx],ah;jnz short END
- or keytable[bx],ah //keytable[theKey>>4]|=1<<(theKey&0x0f);
- mov byte ptr keyboard.status,PRESS //keyboard.status=PRESS;
- mov byte ptr keyboard.recentPressKey,al //keyboard.recentKey=theKey;
- jmp short END
- }KEY_RELEASED:
- asm{
- mov ah,1
- shl ah,cl
- //released
- test keytable[bx],ah;jz short END
- not ah
- and keytable[bx],ah //keytable[theKey>>4]|=1<<(theKey&0x0f);
- mov byte ptr keyboard.status,RELEASE //keyboard.status=RELEASE;
- mov byte ptr keyboard.recentReleaseKey,al //keyboard.recentKey=theKey;
- }
- END:asm{
- cli //;i have not start the interrupt (sti),so it isn't needed
- mov al,0x20
- out 0x20,al
- }
- //inGame^=KEYNEW;
- }else oldkey();
- }
- int getch_Key(){
- uchar key=1;
- while(key){
- while(keyboard.status==READY);
- key=keyboard.recentPressKey;
- keyboard.status=READY;
- while(keyboard.status==READY);
- key-=keyboard.recentReleaseKey;
- keyboard.status=READY;
- }
- return key;
- }
- int wait_Key(){
- if(keyboard.status==READY)return 1;
- else {
- keyboard.status=READY;
- while(keyboard.status==READY);
- keyboard.status=READY;
- return 0;
- }
- }
- void keyLock(){
- keyboard.savestatus=keyboard.status;
- keyboard.status=WAIT;
- }
- void keyUnlock(){
- keyboard.status=keyboard.savestatus;
- }
- void inputString(char *str,int x,int y,int length,STRMode mode){
- int i=0;
- char a[2];
- a[1]=' ';
- inGame=NONE;
- while((a[0]=getch())!=0x0d){
- if(i&&a[0]=='b'){
- textxy(x+i*9,y," ");
- i--;
- }
- if(i<length){
- if(a[0]>='a'&&a[0]<='z'&&(mode&SMLCHARA))goto ADD;
- if(a[0]>='A'&&a[0]<='Z'&&(mode&CAPCHARA))goto ADD;
- if(a[0]>='0'&&a[0]<='9'&&(mode&NUMBER))goto ADD;
- if(a[0]>=' '&&a[0]<='/'&&(mode&NORMSYMBLES))goto ADD;
- if(a[0]>=':'&&a[0]<='@'&&(mode&SPECSYMBLES))goto ADD;
- if((a[0]>='['&&a[0]<=''')||(a[0]>='{'&&a[0]<='~')&&(mode&SPECSYMBLES))goto ADD;
- continue;
- ADD:
- str[i++]=a[0];
- textxy(x+i*9,y,a);
- }
- }
- str[i]=a[1];
- inGame=KEYNEW;
- }
- #endif