網(wǎng)站是用什么做的嗎百度一下就知道手機(jī)版
文章目錄
- 前言
- 交叉編譯工具鏈?zhǔn)鼓?C++
- nes 游戲模擬器移植
- 游戲手柄調(diào)試
前言
很多小伙伴為了不讓自己的 V3s 吃灰,進(jìn)而將其打造成游戲機(jī)。
我們 DIY 的板子具備屏幕、揚聲器、USB Host(可以接游戲手柄),當(dāng)然也要湊一湊熱鬧。
交叉編譯工具鏈?zhǔn)鼓?C++
怎么突然講到工具鏈了?
因為待會 nes 游戲模擬器編譯需要用到 C++,所以就先提前講一下
這里遇到了一個坑,編譯 g++ 時,依賴一些庫,這些庫是我之前沒有使能 g++ 時編的,現(xiàn)在使能 g++ 后需要重新編,不然 g++ 編譯有問題,一開始我沒發(fā)現(xiàn)這個問題,怎么編都有問題。
host-gcc-final-12.3.0/
就是其中一個依賴,將這個目錄手動刪除,然后重新編譯整個工程,g++ 就編譯 OK 了。
nes 游戲模擬器移植
源碼:https://github.com/nejidev/arm-NES-linux
step1:
修改交叉編譯鏈
CC=/home/liyongjun/project/board/buildroot/DIY_V3S/host/bin/arm-buildroot-linux-gnueabihf-gcc
CCFLAGS += -I /home/liyongjun/project/board/buildroot/DIY_V3S/staging/usr/include
LDFILGS += -L /home/liyongjun/project/board/buildroot/DIY_V3S/staging/usr/lib
step2:
修改 InfoNES_System_Linux.cpp 文件中的 lcd_fb_display_px 函數(shù) (調(diào)整 spi 屏幕的顏色)
static int lcd_fb_display_px(WORD color, int x, int y)
{
#if 0unsigned char *pen8;unsigned short *pen16;pen8 = (unsigned char *)(fb_mem + y*line_width + x*px_width);pen16 = (unsigned short *)pen8;*pen16 = color;return 0;
#endifWORD *pen16;unsigned char r, g, b;r = ((color >> 10) & 0x1f);g = ((color >> 5) & 0x3f);b = (color & 0x1f);color = r<<11|g<<6|b;pen16 = (WORD *)(fb_mem + y*line_width + x*px_width);*pen16 = color;return 0;
}
step3:
下載游戲合集并解壓,然后改名為游戲名為英文
鏈接:https://pan.baidu.com/s/16hIWwYQQEX9aOBDG1dVa0A
提取碼:asdf
step4:
運行
root@v3s-diy:~/nes# ./InfoNES rom/218_hdl.nes
/dev/joypad dev not found
/dev/input/js0 dev not found
fb width:240 height:240
游戲手柄調(diào)試
插入游戲手柄
root@v3s-diy:~# ls /dev/input/
event0
root@v3s-diy:~#
[ 126.159751] usb 1-1.2: new low-speed USB device number 4 using ehci-platform
[ 126.421937] input: USB gamepad as /devices/platform/soc/1c1a000.usb/usb1/1-1/1-1.2/1-1.2:1.0/0003:081F:E401.0002/input/input2
[ 126.435728] hid-generic 0003:081F:E401.0002: input: USB HID v1.10 Joystick [USB gamepad] on usb-1c1a000.usb-1.2/input0
root@v3s-diy:~# ls /dev/input/
event0 event1 js0
運行游戲
root@v3s-diy:~/nes# ./InfoNES rom/218_hdl.nes
/dev/joypad dev not found
fb width:240 height:240
發(fā)現(xiàn)游戲手柄按鍵沒有反應(yīng)
不過,監(jiān)聽 /dev/input/event1
發(fā)現(xiàn)按鍵是有事件產(chǎn)生的
root@v3s-diy:~/nes# hexdump /dev/input/event1
0000000 c086 0003 fa8b 000e 0004 0004 0003 0009
0000010 c086 0003 fa8b 000e 0001 0122 0001 0000
0000020 c086 0003 fa8b 000e 0000 0000 0000 0000
0000030 c087 0003 0a0b 0002 0004 0004 0003 0009
0000040 c087 0003 0a0b 0002 0001 0122 0000 0000
0000050 c087 0003 0a0b 0002 0000 0000 0000 0000
0000060 c087 0003 c80b 000c 0004 0004 0001 0009
0000070 c087 0003 c80b 000c 0001 0120 0001 0000
0000080 c087 0003 c80b 000c 0000 0000 0000 0000
0000090 c087 0003 19cb 000f 0004 0004 0001 0009
00000a0 c087 0003 19cb 000f 0001 0120 0000 0000
00000b0 c087 0003 19cb 000f 0000 0000 0000 0000
00000c0 c089 0003 114f 0006 0003 0000 00ff 0000
00000d0 c089 0003 114f 0006 0000 0000 0000 0000
00000e0 c089 0003 6309 0008 0003 0000 007f 0000
00000f0 c089 0003 6309 0008 0000 0000 0000 0000
那大概率是游戲手柄按鍵和模擬器設(shè)定不匹配
在獲取按鍵值的地方加點打印,記錄按鍵值
// arm-NES-linux/linux/joypad_input.cpp
static int USBjoypadGet(void)
{/*** FC手柄 bit 鍵位對應(yīng)關(guān)系 真實手柄中有一個定時器,處理 連A 連B * 0 1 2 3 4 5 6 7* A B Select Start Up Down Left Right*///因為 USB 手柄每次只能讀到一位鍵值 所以要有靜態(tài)變量保存上一次的值static unsigned char joypad = 0;struct js_event e;if(0 < read (USBjoypad_fd, &e, sizeof(e))){
printf("e.type = 0x%x\t", e.type);
printf("e.value = 0x%x\t", e.value);
printf("e.number = 0x%x\n", e.number);if(0x2 == e.type){/*上:value:0x8001 type:0x2 number:0x5value:0x0 type:0x2 number:0x5*/if(0x8001 == e.value && 0x5 == e.number)
然后將各個按鍵值改正確
static int USBjoypadGet(void)
{/*** FC手柄 bit 鍵位對應(yīng)關(guān)系 真實手柄中有一個定時器,處理 連A 連B * 0 1 2 3 4 5 6 7* A B Select Start Up Down Left Right*///因為 USB 手柄每次只能讀到一位鍵值 所以要有靜態(tài)變量保存上一次的值static unsigned char joypad = 0;struct js_event e;if(0 < read (USBjoypad_fd, &e, sizeof(e))){
printf("e.type = 0x%x\t", e.type);
printf("e.value = 0x%x\t", e.value);
printf("e.number = 0x%x\n", e.number);if(0x2 == e.type){/* 上 */if(0x8001 == e.value && 0x1 == e.number){joypad |= 1<<4;}/* 下 */if(0x7fff == e.value && 0x1 == e.number){joypad |= 1<<5;}//松開if(0x0 == e.value && 0x1 == e.number){joypad &= ~(1<<4 | 1<<5);}/* 左 */if(0x8001 == e.value && 0x0 == e.number){joypad |= 1<<6;}/* 右 */if(0x7fff == e.value && 0x0 == e.number){joypad |= 1<<7;}//松開if(0x0 == e.value && 0x0 == e.number){joypad &= ~(1<<6 | 1<<7);}}if(0x1 == e.type){/* 選擇 */if(0x1 == e.value && 0x8 == e.number){joypad |= 1<<2;}if(0x0 == e.value && 0x8 == e.number){joypad &= ~(1<<2);}/* 開始 */if(0x1 == e.value && 0x9 == e.number){joypad |= 1<<3;}if(0x0 == e.value && 0x9 == e.number){joypad &= ~(1<<3);}/* A */if(0x1 == e.value && 0x1 == e.number){joypad |= 1<<0;}if(0x0 == e.value && 0x1 == e.number){joypad &= ~(1<<0);}/* B */if(0x1 == e.value && 0x2 == e.number){joypad |= 1<<1;}if(0x0 == e.value && 0x2 == e.number){joypad &= ~(1<<1);}/* X */if(0x1 == e.value && 0x0 == e.number){joypad |= 1<<0;}if(0x0 == e.value && 0x0 == e.number){joypad &= ~(1<<0);}/* Y */if(0x1 == e.value && 0x3 == e.number){joypad |= 1<<1;}if(0x0 == e.value && 0x3 == e.number){joypad &= ~(1<<1);}}return joypad;}
printf("read failed!!!\n");return -1;
}
就可以愉快地玩游戲了