用devkitpro编程教程,2-标准程序格式



掌叔
2008-06-13 09:00:11

摘自:pspchina
作者:cowtony

在任意目录下(不一定是安装devkitpro的目录)建立一个名为projects的文件夹(名字是什么其实无所谓)
在文件夹内建立多个子文件夹,每个文件夹是将来要编的一个程序
例如:projects
-helloworld
-main.c
-makefile
-counter
-main.c
-makefile
这里面的“helloworld”和“counter”就是以两个程序名称命名的文件夹,“main.c”是程序的源代码,“makefile”是为编译所作的信息文件

每一个程序编译前都要有main.c和makefile放在“程序名称”的文件夹内

先说makefile:
makefile没有后缀,里面写上:
TARGET = hello
OBJS = main.o
CFLAGS = -O2 -G0 -Wall
CXXFLAGS = $(CFLAGS) -fno-exceptions -fno-rtti
ASFLAGS = $(CFLAGS)
EXTRA_TARGETS = EBOOT.PBP
PSP_EBOOT_TITLE = Hello World
PSPSDK=$(shell psp-config --pspsdk-path)
include $(PSPSDK)/lib/build.mak
这里目前每个程序不一样的就是倒数第三行“PSP_EBOOT_TITLE = Hello World”里的“Hello World”,它是你的程序名称,显示在PSP上。
所以每做一个新程序,需要改变的就是makefile里的“PSP_EBOOT_TITLE”

再说main.c:
main.c的格式主要为:
[code=c]
#include //PSP程序必不可少的头文件
#include //我认为有些类似于stdio.h
#include //按键有关函数的头文件

PSP_MODULE_INFO("Hello World", 0, 1, 1); //不会被编译,写上你的程序名和版本号

#define printf pspDebugScreenPrintf //定义函数pspDebugScreenPrintf为printf,以后好用


int exit_callback(int arg1,int arg2,void *common)
{sceKernelExitGame();
return 0;}/* Exit callback */

int CallbackThread(SceSize args,void *argp)
{int cbid=sceKernelCreateCallback("Exit Callback",exit_callback,NULL);
sceKernelRegisterExitCallback(cbid);
sceKernelSleepThreadCB();
return 0;}/* Callback thread */

int SetupCallbacks(void)
{int thid=sceKernelCreateThread("update_thread",CallbackThread,0x11,0xFA0,0,0);
if(thid>=0)sceKernelStartThread(thid,0,0);
return thid;}/* Sets up the callback thread and returns its thread id */

//以上这三个函数先不用管,是为了能按home时退出用的

int main() //终于到main函数了
{
pspDebugScreenInit(); //设置PSP屏幕,这样就可以显示文本了
SetupCallbacks(); //设置按home可以退出

//这里才是写程序内容的地方!!!!

sceKernelSleepThread(); //让程序运行完后停到这里
return 0;
}
[/code]

这样写一个程序太麻烦了,每次都得复制一大堆东西,所以为了方便,在projects文件夹下建一个名为include的文件夹
里面放一个common.h来代替main函数前面那一大堆,这样main.c就变成了:
#include "..includecommon.h"
[code=c]
int main()
{
pspDebugScreenInit();
SetupCallbacks();

//写程序内容的地方

sceKernelSleepThread();
return 0;
}
[/code]
方便多了!!附件里有做好的common.h,里面可能还有一些目前没有讲到的语句,是以后几课中要用到的,到时候你就会知道啦~~

注意:
1.如果以后要用到common.h以外的头文件,要把common.h放到最后写或者直接加到common.h里
2.文件编译时只能将main.c和makefile放在projects的子文件夹内,不然会找不到common.h的位置
或者将main.c与common.h放在同一文件夹内,并将#include "..includecommon.h"改为#include "common.h"


shaick
2010-09-26 20:20:56

pspDebugScreenPrintf的参数是什么,是pspDebugScreenPrintf("");么?