安卓月光宝盒项目源码讲解
作者:互联网
这里使用的是MAME 模拟器,当前手头实现了全部模拟器,FBA MD FC PSP PS1 ,支持拳皇97 98等8000多个游戏。 通过修改,去掉游戏机里面触摸按键,介入上面说的游戏按键。
输入设备当前支持 ,串口,USB ,按键,遥控器,有线无线鼠标,都已经支持。可以支持3P 4P对打。
同时画面经过渲染,完成了高清画质。
这里分享一段串口代码:合作联系QQ 332324956
#include “Com.h”
#include “Random.h”
#include “SerialPort.h”
#include “Hardware.h”
#include <stdio.h>
#include <string.h>
#include “TEA.h”
const char* SerialPortName = “/dev/ttyS4”; //2
const int Budrate = 115200;
int serialPortFD = -1;
enum Pack_Def
{
Pack_Def_Len = 0,
Pack_Def_CC1,
Pack_Def_CC2,
Pack_Def_Random,
Pack_Def_Data,
};
const int DATA_PACK_MAX_LEN = 128;
const int InBufferLen = 4096;
unsigned char inBuffer[InBufferLen]={0};
int inBufferTop = 0;
int inBufferEnd = 0;
float nonePacketTime = 0;
//******************************* ͨ�Ŵ�� ***************************************************************
void SerialPushCharToInBuffer(unsigned char data)
{
inBuffer[inBufferEnd] = data;
if(++inBufferEnd >= InBufferLen) inBufferEnd = 0;
}
int GetInBufferLen(void)
{
int len = inBufferEnd - inBufferTop;
if (len < 0) len += InBufferLen;
return len;
}
void CopyDataFromInBuffer(unsigned char *data, int len)
{
int index = inBufferTop;
for (int i = 0; i < len; i++)
{
data[i] = inBuffer[index];
if(++index >= InBufferLen)
{
index = 0;
}
}
}
void InBufferStepByStep(int step)
{
inBufferTop += step;
inBufferTop %= InBufferLen;
}
unsigned char GetCheckCode(unsigned char *data, unsigned char len, unsigned char random)
{
unsigned char add = random;
unsigned char i = 0;
for(i = 0; i < len; i++)
{
add += data[i];
}
return add;
}
int epcnt = 0;
bool GetMsgPack(unsigned char* data, int* len)
{
static unsigned char dataBuffer[DATA_PACK_MAX_LEN];
if (GetInBufferLen() > 0)
{
if(GetInBufferLen() >= inBuffer[inBufferTop])
{
if(inBuffer[inBufferTop] >= Pack_Def_Data + 8
&& inBuffer[inBufferTop] <= DATA_PACK_MAX_LEN
&& (inBuffer[inBufferTop] - Pack_Def_Data) % 8 == 0)
{
CopyDataFromInBuffer(dataBuffer, inBuffer[inBufferTop]);
if(GetCheckCode(&dataBuffer[Pack_Def_Data], dataBuffer[Pack_Def_Len] - Pack_Def_Data, dataBuffer[Pack_Def_Random]) == dataBuffer[Pack_Def_CC1]
&& GetCheckCode(&dataBuffer[Pack_Def_Data], dataBuffer[Pack_Def_Len] - Pack_Def_Data, dataBuffer[Pack_Def_Random]^0xdb) == dataBuffer[Pack_Def_CC2])
{
//У��ɹ�
//����
int *p = (int*)(&dataBuffer[Pack_Def_Data]);
int key[4];
GetTeaKey(dataBuffer[Pack_Def_Random]^0x43, key);
XXTea(p, key, -1 * (dataBuffer[Pack_Def_Len] - Pack_Def_Data) / 4);
//�����ݱ��浽����
for (int i = 0; i < dataBuffer[Pack_Def_Len] - Pack_Def_Data; i++)
{
data[i] = dataBuffer[Pack_Def_Data + i];
}
*len = dataBuffer[Pack_Def_Len] - Pack_Def_Data;
InBufferStepByStep(dataBuffer[Pack_Def_Len]);
}
else
{
InBufferStepByStep(1);
*len = 0;
}
}
else
{
InBufferStepByStep(1);
*len = 0;
}
return true;
}
}
*len = 0;
return false;
}
void CommSendData(unsigned char* data, int len)
{
static unsigned char dataBuffer[DATA_PACK_MAX_LEN];
if(len + Pack_Def_Data > DATA_PACK_MAX_LEN)
return;
int aLen = (8 - len%8)%8 + len;
dataBuffer[Pack_Def_Len] = aLen + Pack_Def_Data; //���ݳ���
dataBuffer[Pack_Def_CC1] = 0; //��
dataBuffer[Pack_Def_CC2] = 0; //��
dataBuffer[Pack_Def_Random] = Rand.Rand(0, 0xff); //���ֵ
//�������
int i = 0;
for(i = 0; i < len; i++)
{
dataBuffer[Pack_Def_Data+i] = data[i];
}
//Ϊ�˲���8�ı�����������ֵ
for(; i < aLen; i++)
{
dataBuffer[Pack_Def_Data+i] = Rand.Rand(0, 0xff);//���ֵ;
}
//����
int *p = (int*)(&dataBuffer[Pack_Def_Data]);
int key[4];
GetTeaKey(dataBuffer[Pack_Def_Random]^0x43, key);
XXTea(p, key, aLen / 4);
//������
dataBuffer[Pack_Def_CC1] = GetCheckCode(&dataBuffer[Pack_Def_Data], aLen, dataBuffer[Pack_Def_Random]);
dataBuffer[Pack_Def_CC2] = GetCheckCode(&dataBuffer[Pack_Def_Data], aLen, dataBuffer[Pack_Def_Random]^0xdb);
PortSend(serialPortFD, (char*)dataBuffer, aLen + Pack_Def_Data);
}
//************************************************************************************************************************
S_KeyState PlayerKey[PK_Count];
void ClearKey(void)
{
for (int j = 0; j < PK_Count; j++)
{
PlayerKey[j].click = false;
PlayerKey[j].u_dt = 0;
PlayerKey[j].d_dt = 0;
PlayerKey[j].a_dt = 0;
PlayerKey[j].state = false;
}
}
unsigned int get_key =0;
void GetKeyData(unsigned char* data)
{
memcpy((unsigned char*)&get_key, data, 4);
for(int i = 0; i < PK_Count; i++)
{
if((get_key & (0x00000001<<i)) > 0)
{
//����
if(!(PlayerKey[i].state))
{
PlayerKey[i].click = true;
PlayerKey[i].u_dt = 0;
PlayerKey[i].a_dt = 0;
}
PlayerKey[i].state = true;
}
else
{
//�ɿ�
if(PlayerKey[i].state)
{
PlayerKey[i].d_dt = 0;
PlayerKey[i].a_dt = 0;
}
PlayerKey[i].state = false;
PlayerKey[i].click = false;
}
}
}
void DoInData(void)
{
static unsigned char data[DATA_PACK_MAX_LEN];
static int len = 0;
while(GetMsgPack(data, &len))
{
nonePacketTime = 0;
switch(data[0])
{
case 1: // ɨ�谴��
GetKeyData(&data[1]);
break;
default:
break;
}
}
}
void ComUpdate(float dt)
{
//��������
nonePacketTime += dt;
char buffer[1024];
int len = 0;
do
{
len = PortRecv(serialPortFD, buffer, 1024);
if(len <= 0) break;
else
{
nonePacketTime = 0;
for (int i = 0; i < len; i++)
{
SerialPushCharToInBuffer((unsigned char)buffer[i]);
}
DoInData();
}
} while (true);
}
void ComInit(void)
{
inBufferTop = 0;
inBufferEnd = 0;
for(int i = 0; i < InBufferLen; i++)
{
inBuffer[i] = 0;
}
Tea_KeyInit();
}
void OpenCom(void)
{
if(serialPortFD == -1)
{
serialPortFD = PortOpen(SerialPortName);
if(serialPortFD >= 0)
{
PortSet(serialPortFD, Budrate);
}
ClearAllPortBuffer(serialPortFD);
}
}
void CloseCom(void)
{
PortClose(serialPortFD);
serialPortFD = -1;
inBufferTop = 0;
inBufferEnd = 0;
}
void KeyUpdate(float dt)
{
for (int j = 0; j < PK_Count; j++)
{
if(PlayerKey[j].state)
{
PlayerKey[j].u_dt += dt;
}
else
{
PlayerKey[j].d_dt += dt;
if(PlayerKey[j].click && PlayerKey[j].d_dt > 0.1f)
{
PlayerKey[j].click = false;
}
}
}
}
标签:int,安卓,char,源码,void,月光宝盒,PlayerKey,Def,Pack 来源: https://blog.csdn.net/a332324956/article/details/114238824