PSP程序运行成功!



掌叔
2008-06-10 09:01:01

摘自:[url]http://blog.sina.com.cn/vcbear[/url]
作者:vcbear

上次把devkitPro环境搭好后,又自己研究了下,现在已经可以在多种IDE下编写及直接编译我的PSP程序了,比如EditPlus,UE,.net,VC6。直接在Cygwin的 vi里或是在cmd下写代码确实有些不爽,主要是不方便。有语法高亮总觉得亲切些,呵呵,推荐在VC6下编写代码,配合Visual AssistX插件,有函数提示,用起来还是很方便的。
[attach]71[/attach]
下面就是我第一个PSP程序的代码了,程序很简单。
运行过程:程序运行起来后循环检测用户按键,并给出屏幕提示,当用户按下[X]键时,记数器启动,并在PSP屏幕上循环显示变数,当用户再次按下[○]键时,程序停止,同时显示记数器运行次数。

以下是源代码 -- psptest.c

[code=c]
/// PSP Test - My First App for the PSP

#include
#include
#include


PSP_MODULE_INFO("PSP Test", 0, 1, 1); //程序的名字,属性,最大版本,最小版本//

#define printf pspDebugScreenPrintf //宏替换


//------------------以下为退出线程的功能------------------------//
// Exit callback
int exit_callback(int arg1, int arg2, void *common)
{
sceKernelExitGame(); //退出游戏到主界面
return 0;
}



// Callback thread
int CallbackThread(SceSize args, void *argp)
{
int cbid;
cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL); //创建退出回调线程
sceKernelRegisterExitCallback(cbid); //注册退出回调线程
sceKernelSleepThreadCB(); //使退出回调线程睡眠,在需要的时候调用
return 0;
}



// Sets up the callback thread and returns its thread id
int SetupCallbacks(void)
{
int thid = 0;
thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11, 0xFA0, 0, 0); //建立退出线程
if(thid >= 0)
{
sceKernelStartThread(thid, 0, 0); //启动退出线程
}
return thid;
}



//------------------以下为主函数------------------------//
int main(int argc,char *argv[] )
{
pspDebugScreenInit(); //初始化PSP屏幕
char path[256];
strcpy(path,argv[0]);
*(path+strlen(path)-9)=''; //获取可执行程序路径(去掉EBOOT.PBP)

SetupCallbacks(); //退出线程就绪

int counter = 0;

SceCtrlData pad;

while (1)
{
printf("Please Press
");
sceCtrlReadBufferPositive(&pad,1);
if (pad.Buttons == PSP_CTRL_TRIANGLE)
{
printf("you pressed [TRIANGLE] button !
");
}
if (pad.Buttons == PSP_CTRL_SQUARE)
{
printf("you pressed [SQUARE] button !
");
}
if (pad.Buttons == PSP_CTRL_CIRCLE)
{
printf("you pressed [CIRCLE] button !
");
}
if (pad.Buttons == PSP_CTRL_CROSS)
{
printf("you pressed [CROSS] button ! start timer!
");
while (1)
{
printf("path = %s
",path);
printf("Press [CIRCLE] To Stop the Timer
");
printf("Counter = %i ",counter);
counter++;

sceCtrlReadBufferPositive(&pad,1);
if (pad.Buttons == PSP_CTRL_CIRCLE)
{
pspDebugScreenClear();
printf("Timer Finish
");
printf("Final Counter = %i ",counter);
break;
}
}
break;
}
}

sceKernelSleepThread(); //程序暂停以便显示上面的字符串

return 0;
}
[/code]


下面是Makefile


TARGET = PSPTEST
OBJS = psptest.o

CFLAGS = -O2 -G0 -Wall
CXXFLAGS = $(CFLAGS) -fno-exceptions -fno-rtti
ASFLAGS = $(CFLAGS)

EXTRA_TARGETS = EBOOT.PBP
PSP_EBOOT_TITLE = PSP TEST

PSPSDK=$(shell psp-config --pspsdk-path)
include $(PSPSDK)/lib/build.mak

*************************************************************************

编译一下,OK! 没问题。
[attach]72[/attach]
好!然后把PSP和电脑连接好,进入USB模式,用工具软件KXploit Tool 0.3 将生成的可执行文件拷入小P,该工具软件会自动在小P的GAME文件夹下生成相关程序文件,这里要说明的一下是,这种自己写的程序只能运行在1.5系统下(包括模拟1.5系统),在其他系统环境下均会运行失败。
[attach]73[/attach]
拷贝成功后,到GAME/Memory Stick目录下,就会看见我们新写的PSPTEST
[attach]74[/attach]
好!运行!

主循环开始
[attach]75[/attach]
按下“三角”键
[attach]76[/attach]
按下“方块”键
[attach]77[/attach]
按下“圆圈”键
[attach]78[/attach]
按下“大叉”键,记数器开始运行并显示运行次数
[attach]79[/attach]
再次按下“圆圈”键,程序停止,同时显示记数器运行次数。
[attach]80[/attach]
这样,一个基本的PSP自制程序就运行成功了,再下来,就是继续的深入了,呵呵

GO GO!加油!!