📄 menu.c
字号:
#include <stdio.h>#include <vga.h>#include <vgagl.h>#include <vgakeyboard.h>#include <stdlib.h>#include <fcntl.h>#include <unistd.h>#include <string.h>void restore_bkcolor(int,int);void draw_bkcolor(int,int);void restore(int,int);void draw_text(int x,int y,unsigned char *ch);void draw_menu();int leftx=172;int lefty=47;int main(void){ vga_init(); vga_setmode(G800x600x256); draw_bkcolor(leftx,lefty); draw_menu(); if(keyboard_init()) { printf("keyboard initilize failure\n"); exit(1); } keyboard_translatekeys(TRANSLATE_CURSORKEYS | TRANSLATE_KEYPADENTER | TRANSLATE_DIAGONAL); for (;;) { gl_putbox(0, 0, 128, 1, keyboard_getstate()); keyboard_update(); /* Move. */ if (keyboard_keypressed(SCANCODE_CURSORLEFT)) { restore_bkcolor(leftx,lefty); leftx-=400; } if (keyboard_keypressed(SCANCODE_CURSORRIGHT)) { restore_bkcolor(leftx,lefty); leftx+=400; } if (keyboard_keypressed(SCANCODE_CURSORUP)) { restore_bkcolor(leftx,lefty); lefty-=20; } if (keyboard_keypressed(SCANCODE_CURSORDOWN)) { restore_bkcolor(leftx,lefty); lefty+=20; } /* Boundary checks. */ if (leftx<172) leftx=172; if (leftx>572) leftx=572; if (lefty<47) lefty=47; if (lefty>107) lefty=107; draw_bkcolor(leftx,lefty); /*terminate the loop and exit with button"Q" or "ESC"*/ if (keyboard_keypressed(SCANCODE_Q) || keyboard_keypressed(SCANCODE_ESCAPE)) break; } keyboard_close(); gl_clearscreen(14); system("./1"); vga_setmode(TEXT); vga_getch(); return 0;}/* mark the menu you select with a special colour */void draw_bkcolor(int leftx,int lefty){ vga_setcolor(2); vga_drawline(leftx,lefty,leftx+100,lefty); vga_drawline(leftx,lefty+20,leftx+100,lefty+20); vga_drawline(leftx,lefty,leftx,lefty+20); vga_drawline(leftx+100,lefty,leftx+100,lefty+20); usleep(50000);}/* restore the colour of the menu that you've released */void restore_bkcolor(int leftx,int lefty){ vga_setcolor(0); vga_drawline(leftx,lefty,leftx+100,lefty); vga_drawline(leftx,lefty+20,leftx+100,lefty+20); vga_drawline(leftx,lefty,leftx,lefty+20); vga_drawline(leftx+100,lefty,leftx+100,lefty+20); usleep(50000);}/* draw the menu on the screen */void draw_menu(){ vga_setrgbcolor(251,251,0); draw_text(200,50,(unsigned char *)"菜单一"); draw_text(200,70,(unsigned char *)"菜单二"); draw_text(200,90,(unsigned char *)"菜单三"); draw_text(200,110,(unsigned char *)"菜单四"); draw_text(600,50,(unsigned char *)"菜单五"); draw_text(600,70,(unsigned char *)"菜单六"); draw_text(600,90,(unsigned char *)"菜单七"); draw_text(600,110,(unsigned char *)"菜单八");}void draw_text(int x,int y,unsigned char *ch){ int i,j,k,c,l; unsigned char mat[16][2]; unsigned char mat1[16]; char *p; FILE *hzk,*ywk; vga_setcolor(1); hzk=fopen("hzk16","rb"); ywk=fopen("asc16","rb"); if (hzk==NULL) { perror("Open hzk16 file error"); exit(1); } if(ywk==NULL) { perror("Open asc16 file error"); exit(1); } l=strlen(ch); vga_setrgbcolor(251,251,0); for(c=0;c<l;c++) { if(ch[c]>127) { i=ch[c]-0xa0;j=ch[c+1]-0xa0; fseek(hzk,(94*(i-1)+(j-1))*32,SEEK_SET); fread(mat,32,1,hzk); for(i=0;i<16;i++) for(j=0;j<2;j++) for(k=0;k<8;k++) if(mat[i][j]&(0x80>>k)) vga_drawpixel(x+c*8+8*j+k,y+i); c++; } else { fseek(ywk,ch[c]*16,SEEK_SET); fread(mat1,16,1,ywk); for(i=0;i<16;i++) for(k=0;k<8;k++) if(mat1[i]&(0x80>>k)) vga_drawpixel(x+c*8+k,y+i); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -