放假啊,这叫玩啊,天天不务正业,还玩些可能和未来工作无关的技术!内心充满了罪恶感啊
写HelloWorld太无聊了,转了一圈PSPSDK发现有自带sample,方便快速上手的好东西,位置在开发包/psp/sdk/samples下 面,其中包括声音,控制器,红外,usb,wifi,电源等等。还真不少,先挑简单的看看。恩~~我看controller挺简单!就看这个!
打开controller/base目录,只有一个main.c 和 Makefile。肯定不会很难,先make一个试试,还好很顺利,编译得到四个文件: main.o controller_basic.elf EBOOT.PBP PARAM.SFO。
main.o 不用说肯定是main.c的生成的目标文件。
controller_basic.elf 我知道ELF是linux下的二进制文件格式,可能PSP也是这个格式。
EBOOT.PBP 这个就是最终的执行文件了,这个应该是包装过的,其中包含一些图标文件什么的。
PARAM.SFO 这个我就不知道,有高手可以告诉我,(我怀疑!我猜测!是源码中定义的那些宏有关,是一些信息,最后打包到EBOOT.PBP。不要太相信,只是个思路)。
然后就是插上usb,靠到psp/game下,建个文件夹,叫什么名都可以!然后把EBOOT.PBP(不要改名字!)放进去,运行就可以了。
也看了运行后什么模样了!下面就是分析源码了,分析都写成注释了!
以下是源码:

/**//*
*PSPSoftwareDevelopmentKit-http://www.pspdev.org
*-----------------------------------------------------------------------
*LicensedundertheBSDlicense,seeLICENSEinPSPSDKrootfordetails.
*
*main.c-BasicInputdemo--readsfromcontrolpadandindicatesbutton
*presses.
*
*Copyright(c)2005MarcusR.Brown<mrbrown@ocgnet.org>
*Copyright(c)2005JamesForshaw<tyranid@gmail.com>
*Copyright(c)2005JohnKelley<ps2dev@kelley.ca>
*Copyright(c)2005DonourSizemore<donour@uchicago.edu>
*
*$Id:main.c10952005-09-2721:02:16Zjim$
*/


/**//*psp的一些头文件,从文件名应该能看出来时干嘛使得*/
#include<pspkernel.h>
#include<pspdebug.h>
#include<pspctrl.h>


/**//*pspsdk中自带标准c库和标准c++库*/
#include<stdlib.h>
#include<string.h>



/**//*定义模块信息包括版本*/

/**//*Definethemoduleinfosection*/
PSP_MODULE_INFO("CONTROLTEST",0,1,1);


/**//*定义主线程属性还没具体研究,有时间再改上*/

/**//*Definethemainthread'sattributevalue(optional)*/
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER|THREAD_ATTR_VFPU);



/**//*这个纯属为了方便pspDebugScreenPrintf的用法和c库中的printf用法完全一样,不过pspDebugScreenPrintf只能用在初始化了DebugScreen以后*/

/**//*Defineprintf,justtomaketypingeasier*/
#defineprintfpspDebugScreenPrintf

voiddump_threadstatus(void);


/**//*下面的代码会看见,这是控制主循环推出的条件变量*/
intdone=0;


/**//*当用户按下home键退出时的回调函数*/

/**//*Exitcallback*/
intexit_callback(intarg1,intarg2,void*common)

...{

/**//*让主循环推出*/
done=1;
return0;
}


/**//*这么看来需要一个专门的线程来监听用户的操作,建立这么一个线程监听用户来调用相应的回调函数*/

/**//*Callbackthread*/
intCallbackThread(SceSizeargs,void*argp)

...{
intcbid;

/**//*创建一个ExitCallback回调函数返回一个类似handle的东西*/
cbid=sceKernelCreateCallback("ExitCallback",exit_callback,NULL);

/**//*在把这个回调函数注册商*/
sceKernelRegisterExitCallback(cbid);

/**//*让他睡眠当用户要退出的时候会唤醒*/
sceKernelSleepThreadCB();

return0;
}


/**//*这个纯粹是为了模块化了,把安装回调函数的代码都写着里了*/

/**//*Setsupthecallbackthreadandreturnsitsthreadid*/
intSetupCallbacks(void)

...{
intthid=0;


/**//*创建那个回调函数线程*/
thid=sceKernelCreateThread("update_thread",CallbackThread,
0x11,0xFA0,0,0);
if(thid>=0)

...{

/**//*开始执行*/
sceKernelStartThread(thid,0,0);
}

returnthid;
}

intmain(void)

...{

SceCtrlDatapad;/**//*控制器的结构体*/



pspDebugScreenInit();/**//*初始化DebugScreen这样可以用pspDebugScreenPrintf在上面输出了*/


/**//*那个回调函数线程*/
SetupCallbacks();


/**//*好像是什么取样周期,莫非可以设置游戏时连发的速度,默认是0*/
sceCtrlSetSamplingCycle(0);


/**//*控制器的模式有数字(PSP_CTRL_MODE_DIGITAL)和类比(PSP_CTRL_MODE_ANALOG)两个这里用的是类比的*/
sceCtrlSetSamplingMode(PSP_CTRL_MODE_ANALOG);


/**//*done!那个主循环变量*/

while(!done)...{

/**//*设置光标的位置*/
pspDebugScreenSetXY(0,2);


/**//*读取用户的输入,psp没键盘,就是那些按键了*/
sceCtrlReadBufferPositive(&pad,1);


/**//*类比摇杆的坐标*/
printf("AnalogX=%d",pad.Lx);
printf("AnalogY=%d ",pad.Ly);

/**//*其他按键的判断,按了哪个就输出相应的文字,用宏都表示出来了,看名字可以看出来*/

if(pad.Buttons!=0)...{

if(pad.Buttons&PSP_CTRL_SQUARE)...{
printf("Squarepressed ");
}

if(pad.Buttons&PSP_CTRL_TRIANGLE)...{
printf("Trianglepressed ");
}

if(pad.Buttons&PSP_CTRL_CIRCLE)...{
printf("Ciclepressed ");
}

if(pad.Buttons&PSP_CTRL_CROSS)...{
printf("Crosspressed ");
}


if(pad.Buttons&PSP_CTRL_UP)...{
printf("Uppressed ");
}

if(pad.Buttons&PSP_CTRL_DOWN)...{
printf("Downpressed ");
}

if(pad.Buttons&PSP_CTRL_LEFT)...{
printf("Leftpressed ");
}

if(pad.Buttons&PSP_CTRL_RIGHT)...{
printf("Rightpressed ");
}


if(pad.Buttons&PSP_CTRL_START)...{
printf("Startpressed ");
}

if(pad.Buttons&PSP_CTRL_SELECT)...{
printf("Selectpressed ");
}

if(pad.Buttons&PSP_CTRL_LTRIGGER)...{
printf("L-triggerpressed ");
}

if(pad.Buttons&PSP_CTRL_RTRIGGER)...{
printf("R-triggerpressed ");
}
}
}


/**//*彻底退出了*/
sceKernelExitGame();
return0;
}
以上就是这个sample了,一点也不难!还算是再玩得程度,不过开发包公布出来的函数还真是少的可怜啊!要想写出更好的东西,那就要看对开源代码库的移植了!