Advertising (This ad goes away for registered users. You can Login or Register)

--> [PSP PROGRAMMING TUTORIALS IN C!!!] <--

Forum rules
Forum rule Nº 15 is strictly enforced in this subforum.
devshelper
Posts: 134
Joined: Sat Mar 19, 2011 12:09 pm

--> [PSP PROGRAMMING TUTORIALS IN C!!!] <--

Post by devshelper »

ALL OF THE TUTORIALS WILL BE ABOUT 15-20
BUT WHILE I MAKE THEM, HERE GO THE FIRST 7!! ;)
THEY WILL COVER SOME THINGS SUCH AS:
-DEBUG TEXT-
-GRAPHIC TEXT-
-IMAGES-
-MATHEMATIC FUNCTIONS-
-VLF-
-VIDEOS-
-MUSIC-
AS I DON'T HAVE MUCH TIME TO WRITE ALL OF THESE TUTORIALS IN TIME, I WOULD APPRECIATE IT MUCH IF YOU COULD PM ME YOUR TUTORIALS!
THAT WOULD HELP ME MUCH :D!!!

[TUTORIAL #1] SETUP PSPSDK
[spoiler]first of all, download and install these files:
http://dl.qj.net/dl.php?fid=110027&new=1
http://sourceforge.net/projects/minpspw ... ror=freefr
http://download.tuxfamily.org/notepadpl ... taller.exe
http://subversion.tigris.org/files/docu ... -setup.exe
http://www.mediafire.com/?doaimyfqytm <--install this both on c:/pspsdk & c:/pspdev
http://www.megaupload.com/?d=CQG0XQ9Q <--that goes on c:/pspsdk/psp/sdk/include

then go to c:/pspdev.bat, right click it and press edit. erase everything it writes and paste this and save it

Code: Select all

set path=%path%;C:\pspdev\bin
set PSPSDK=C:\pspdev\psp\sdk
cmd
after that open c:/pspdev.bat
and type these commands:

Code: Select all

mkdir projects
cd projects
mkdir programming_tutorials
the above code will create these directory:
c:/pspdev/projects/programming_tutorials
we will put all of these tutorials in there[/spoiler]

[TUTORIAL #2] HELLO WORLD
[spoiler]create this directory:
c:/pspdev/projects/programming_tutorials/TUT_1_HELLO_WORLD

create these three files in the above dir:
1)main.c
2)makefile
3)build.bat

1)right click main.c and choose edit with notepad++
paste this code and save it:

Code: Select all

 //Hello World Program.
#include <pspkernel.h>
#include <pspdebug.h>
#include <pspcallbacks.h>

PSP_MODULE_INFO("Hello World",0,1,1);

int main() {
     pspDebugScreenInit();
     pspDebugScreenClear();
     SetupCallbacks();
     pspDebugScreenprintf("Hello World");
     sceKernelSleepThread();
     return 0;
}	 
now, lets explain them:

Code: Select all

//Hello world Program.
when the 2 slashes are used at the beggining of a line,
then this line is just a comment line and it doesn't affect our programme

Code: Select all

#include <pspkernel.h>
#include <pspdebug.h>
#include <pspcallbacks.h>
the files inside the <> are header files.
they contain pre-written code that we
don't need to write it ourselves.
these command #include makes
the pre-processor copy their code
and paste it into our programme.
pspkernel code is about the main
psp functions. pspdebug has got
some simple debug text functions.
pspcallbacks has functions about
the "grey screen" that exits our programme
when the HOME button is pressed.

Code: Select all

PSP_MODULE_INFO("Hello World",0,1,1);
this line contains info about our project.
"hello world" is the name of our project
(NOT THE NAME OF THE EBOOT!!!)
"0,1" is the version
and the final "1" is the programme's mode(user mode)

Code: Select all

int main() {
     pspDebugScreenInit();
     pspDebugScreenClear();
     SetupCallbacks();
	 pspDebugScreenPrintf("Hello World");
     sceKernelSleepThread();
     return 0;
}
int main is the main function of
every C programme. int means integer
and it must return an integer number
at the end of it, in our case 0.
the main function starts with an
{ and ends with an }. inside these symbols
there is the code that our programme will execute.
pspDebugScreenInit initializes the debug
screen and it is pre-defined in pspdebug.h.
pspDebugScreenClear clears the debug screen.
setupcallbacks initializes the "gre" exit
screen. pspDebugScreenPrintf prints a message to the screen.
the message must be inside the ("").
sceKernelSleepThread "freezes" our programme so that
it won't exit. return 0 is there because of the -->int main<--
some functions have got arguements inside the parenthesis, some of them not,
because they just don't need them.
all of the functions end with a ";"
C language is case-sensitive, so watch out
for the "Caps Lock mistakes" :P

2)edit with notepad++ the makefile and paste this:

Code: Select all

#one-line comment
TARGET = hello
OBJS = main.o
 
CFLAGS = -O2 -G0 -Wall
CXXFLAGS = $(CFLAGS) -fno-exceptions -fno-rtti
ASFLAGS = $(CFLAGS)
 
#EXTRA_TARGETS is the output file
#PSP_EBOOT_TITLE is the EBOOT's name
EXTRA_TARGETS = EBOOT.PBP
PSP_EBOOT_TITLE = Hello World
 
PSPSDK=$(shell psp-config --pspsdk-path)
include $(PSPSDK)/lib/build.mak
3)edit with notepad++ the build.bat file

Code: Select all

make
then save it and run it!

an EBOOT.PBP will be created in this directory
copy it at X:/PSP/GAME/XXX[/spoiler]

[TUTORIAL #3] VARIABLES
[spoiler]if you have successfully compiled
and run the hello world programme, now it's
time to do something more fun :D
we will create a programme that will
handle variables. ok!!! no more talks!!!
let's start programming!!!

create the following directory:
c:/pspdev/projects/programming_tutorials/TUT_2_VARIABLES
then, create main.c, makefile and build.bat in there.
build.bat and the makefile are the same with the previous tutorial,
but change only this line at the makefile:

Code: Select all

PSP_EBOOT_TITLE = Variables
now open main.c with notepad++
paste this code:

Code: Select all

#include <pspkernel.h>
#include <pspdebug.h>
#include <pspcallbacks.h>
 
PSP_MODULE_INFO("Variables",0,1,1);

int var1 = 100;
float var2 = 5.2525;
char var3[7] = "Hello!";

int main() {
	pspDebugScreenInit();
	pspDebugScreenClear();
	SetupCallbacks();
	pspDebugScreenPrintf("%d\n", var1);
	pspDebugScreenPrintf("%f\n", var2);
	pspDebugScreenPrintf("%s\n", var3);
	pspDebugScreenPrintf("All of them together: %d %f %s", var1, var2, var3);
	sceKernelSleepThread();
	return 0;
}
now let's explain the new code:

Code: Select all

int var1 = 100;
float var2 = 5.2525;
char var3[7] = "Hello!";
we create 3 variables!
the first one is an integer,
its name is var1 and its value is 100.
the second one is a decimal,
its name is var2 and its value is 5,2525.
the third one is a string.
its value ("hello!") has got 6 characters,
so we must create them. but we must always use one
more, because it is used by a terminal.
so, we create a string with 7 characters.
(one char = one char
many chars = one string)
in C, there is no type of variable named string, so
we must use multiple characters in order to
manually create a string ;).

Code: Select all

pspDebugScreenPrintf("%d\n", var1);
pspDebugScreenPrintf("%f\n", var2);
pspDebugScreenPrintf("%s\n", var3);
pspDebugScreenPrintf("All of them together: %d %f %s!!!", var1, var2, var3);
%d prints the value of var1 and \n is a new line
%f is for float
%s is for string ;)[/spoiler]

[TUTORIAL #4] CALCULATIONS
[spoiler]create this directory:
c:/pspdev/projects/programming_tutorials/TUT_3_CALCULATIONS
let's create a simple calculator :D
makefile is the same with the previous tuts, except the eboot name and build.bat is also the same.
now open main.c and paste the following code:

Code: Select all

#include <pspkernel.h>
#include <pspdebug.h>
#include <pspcallbacks.h>

PSP_MODULE_INFO("Calculations",0,1,1);

int x = 100;
int y = 100;
int result;

int main() {
	pspDebugScreenInit();
	pspDebugScreenClear();
	SetupCallbacks();
	pspDebugScreenPrintf("x = %d\n", x);
	pspDebugScreenPrintf("y = %d\n", y);
	pspDebugScreenPrintf("Please wait...\n");
	sceKernelDelayThread(5*1000*1000);
	result = x + y;
	pspDebugScreenPrintf("result = %d", result);
	sceKernelSleepThread();
	return 0;
}
code explanations:

Code: Select all

pspDebugScreenPrintf("x = %d\n", x);
pspDebugScreenPrintf("y = %d\n", y);
pspDebugScreenPrintf("Please wait...\n");
sceKernelDelayThread(5*1000*1000);
result = x + y;
pspDebugScreenPrintf("result = %d", result);
The above lines do exactly what is described below:
The value of x is printed.
The value of y is printed below of the value x.
A "please wait" message is printed in order to make
our programme look like more professional xD
The programme freezes for 5 seconds(5.000.000 ms)
the result value gets equivalent to the value of x + y
the result value is printed below "please wait..."
You can also do these calculations:
result = x - y;
result = x * y;
result = x / y;
try them all and compile different eboots, each one ofr each calculation ;)!!![/spoiler]

[TUTORIAL #5] BUTTON INPUT
[spoiler]first, create this directory:
c:/pspdev/projects/programming_tutorials/TUT_4_BUTTON_INPUT
makefile and build.bat are the same with previous tuts, except the eboot name ;)

Code: Select all

#include <pspdebug.h>
#include <pspkernel.h>
#include <pspcallbacks.h>
#include <pspctrl.h>

PSP_MODULE_INFO("Button Input",0,1,1);

int main() {
	pspDebugScreenInit();
	pspDebugScreenClear();
	SetupCallbacks();
	SceCtrlData pad;
	pspDebugScreenPrintf("Press X or O or /\ or []!!! :D");
	while(1) {
		pspDebugScreenSetXY(0, 0);
		sceCtrlReadBufferPositive(&pad, 1);
		if (pad.Buttons & PSP_CTRL_CIRCLE)
			{pspDebugScreenPrintf("Circle pressed\n"); }
		if (pad.Buttons & PSP_CTRL_CROSS)
			{pspDebugScreenPrintf("Cross pressed\n"); }
		if (pad.Buttons & PSP_CTRL_SQUARE)
			{pspDebugScreenPrintf("Square pressed\n"); }
		if (pad.Buttons & PSP_CTRL_TRIANGLE)
			{pspDebugScreenPrintf("Triangle pressed\n"); }
	}
	return 0;
}
code explanations:

Code: Select all

#include <pspctrl.h>
header file with pre-defined controller functions

Code: Select all

SceCtrlData pad;
Initializes the psp controller ;)

Code: Select all

while(1) {...}
this is a loop. it starts and ends with {}
it repeates itself over and over again ;)

Code: Select all

pspDebugScreenSetXY(0, 0);
sceCtrlReadBufferPositive(&pad, 1);
the first line makes the text start from the top-left of the scene
the second one is always used when we call controller functions inside a loop.

Code: Select all

if (pad.Buttons & PSP_CTRL_CIRCLE)
	{pspDebugScreenPrintf("Circle pressed\n"); }
if (the circle button is pressed) {the screen will print "Circle Pressed"}

If you want to use other PSP buttons, here are they:

Code: Select all

RTRIGGER
LTRIGGER
HOME
SELECT
NOTE
START
[/spoiler]

[TUTORIAL #6] IMAGE LOADING
[spoiler]IN THIS TUTORIAL, WE WILL LOAD AN IMAGE! :D

WE WILL USE THE CODE FROM http://www.psp-programming.com

first open c:/pspdev/pspdev.bat and type in these commands:

Code: Select all

svn checkout http://psp.jim.sh/svn/psp/trunk/zlib
cd zlib
make
make install
cd
rm -Rf zlib
cd ..
svn checkout http://psp.jim.sh/svn/psp/trunk/libpng
cd libpng
make
make install
cd
rm -Rf libpng
you might get some errors, but it's ok ;)
congrats!!! you have successfully installed zlib and libpng!!! :D

now exit the pspdev.bat and create this directory:
c:/pspdev/projects/programming_tutorials/TUT_5_IMAGE_LOADING

inside this dir, create an image called "image.png"
and draw at it whatever you want. then, save it!
it's size must be 480x272.(just like the psp screen ;))
then download this archieve:
http://www.psp-programming.com/tutorials/c/lesson04.zip
and extract everything
inside this dir, except for ourimage.png.
then create main.c and paste there this code:

Code: Select all

#include <pspkernel.h>
#include <pspdebug.h>
#include <pspcallbacks.h>
#include "graphics.h"
 
PSP_MODULE_INFO("Image Example",0,1,1);

int main() {
	SetupCallbacks();
	initGraphics();
	Image* background = loadImage("Background.png");
	blitAlphaImageToScreen(0,0,480,272,background,0,0);	
	flipScreen();
	sceKernelSleepThread();
	return 0;
}
Let's explain the above code:

Code: Select all

#include "graphics.h"
we use the "" because this header file is in
the same directory with the EBOOT. this header
files has got functions about image
processing.

Code: Select all

initGraphics();
initializes the graphics library

Code: Select all

Image* ourimage = loadImage("image.png");
the * ise used because we declare a pointer. pionters
point to a specific memory address. In this line we load an image
called ourimage and it is named "image.png".

Code: Select all

blitAlphaImageToScreen(0,0,480,272,ourimage,0,0);
we print the image to screen.
the first two zeroes are the starting x and y of
the image. the next two numbers are the length and the width.
then is is the image variable and the final zeroes indicate where
do we want to put the image start position.

Code: Select all

flipScreen();
this draws the above things to screen. :P

here goes the makefile:

Code: Select all

TARGET = image
OBJS = main.o graphics.o framebuffer.o
 
CFLAGS = -O2 -G0 -Wall
CXXFLAGS = $(CFLAGS) -fno-exceptions -fno-rtti
ASFLAGS = $(CFLAGS)
 
LIBS = -lpspgu -lpng -lz -lm
 
EXTRA_TARGETS = EBOOT.PBP
PSP_EBOOT_TITLE = Image
 
PSPSDK=$(shell psp-config --pspsdk-path)
include $(PSPSDK)/lib/build.mak
build.bat is the same with previous tutorials.
now, build it and put the image and the eboot at:
X:/PSP/GAME/XXX[/spoiler]

[TUTORIAL #7] PLAYER MOVEMENT
[spoiler]Now, let's create a moving player...

Open up pspdev.bet and type in:
c:/pspdev/projects/programming_tutorials/TUT_6_PLAYER_MOVEMENT

exctract the archieve from the previous tut in there and delete ourimage.png
create main.c , makefile and build.bat

paste this at build.bat:

Code: Select all

make
use the makefile from the image loading tut, change the eboots name ;)

open main.c and paste this code:

Code: Select all

#include <pspkernel.h>
#include <pspcallbaks.h>
#include <pspdebug.h>
#include <pspctrl.h>
#include "graphics.h"
 
#define prints printTextScreen
#define RGB(r, g, b) ((r)|((g)<<8)|((b)<<16))
 
PSP_MODULE_INFO("Movement Example",0,1,1);

int main() {
	initGraphics();
	SetupCallbacks();
	SceCtrlData pad;	
	int x = 100;
	int y = 100;
	Image* player = loadImage("Player.png");
	Image* background = loadImage("Background.png");
	Color white = RGB(255,255,255);
	while(1) {
		sceCtrlPeekBufferPositive(&pad, 1);
		clearScreen(white);
		blitAlphaImageToScreen(0,0,480,272,background,0,0);
		blitAlphaImageToScreen(0,0,32,32,player,x,y);
		if(pad.Buttons & PSP_CTRL_UP) {
			y--;
		}
		if(pad.Buttons & PSP_CTRL_DOWN) {
			y++;
		}
		if(pad.Buttons & PSP_CTRL_RIGHT) {
			x++;
		}
		if(pad.Buttons & PSP_CTRL_LEFT) {
			x--;
		}
		prints(0,0,"This shows you how to print text",white);
		flipScreen();
	}
	return 0;
}		
now, lets explain the code:

Code: Select all

#define prints printTextScreen
instead of typing printTextScreen, we can just type prints
printTextScreen is a function in graphics.h ;)

Code: Select all

#define RGB(r, g, b) ((r)|((g)<<8)|((b)<<16))
That introduces a new way of creating colors, called RGB...we will use that later in order to make a color ;)

Code: Select all

Color white = RGB(255,255,255);
We create the white color using 255 red, 255 green, 255 blue;)

Code: Select all

clearScreen(white);
it fills the screen with the selected color(white ;))

Code: Select all

blitAlphaImageToScreen(0,0,32,32,player,x,y);
we used x,y becuz' their postitions in screen will change...
REMEMBER! movement ;)

Code: Select all

		if(pad.Buttons & PSP_CTRL_UP) {
			y--;
		}
		if(pad.Buttons & PSP_CTRL_DOWN) {
			y++;
		}
		if(pad.Buttons & PSP_CTRL_RIGHT) {
			x++;
		}
		if(pad.Buttons & PSP_CTRL_LEFT) {
			x--;
		}
This is how the position of the image gonna change...;)

Code: Select all

prints(0,0,"This shows you how to print text",white);
it's something like this:
print to screen(at position x=0, y=0, "the text i want to print", desired color);

easy, ain't it???

COMPILE IT AND RUN IT ON UR PSP ;)[/spoiler]
Advertising
Last edited by devshelper on Sun Dec 25, 2011 6:12 pm, edited 13 times in total.
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

Re: --> [PSP PROGRAMMING TUTORIALS IN C!!!] <--

Post by m0skit0 »

Good job and thanks for sharing! :D
Advertising
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"
Stobby
Posts: 21
Joined: Mon Aug 29, 2011 7:28 pm

Re: --> [PSP PROGRAMMING TUTORIALS IN C!!!] <--

Post by Stobby »

Thank you,
information is never enough :)
xxtripledeathnotexx
Posts: 18
Joined: Sun Sep 18, 2011 7:47 pm
Location: ur MOM house

Re: --> [PSP PROGRAMMING TUTORIALS IN C!!!] <--

Post by xxtripledeathnotexx »

im still having trouble with helloworld. and its the build.bat part. can read what it says bu its say something like "make"***** stop.

i manage to fix it but now it says no target
P.S. I slept with ur MOM

Image
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

Re: --> [PSP PROGRAMMING TUTORIALS IN C!!!] <--

Post by m0skit0 »

Please post the exact whole error, not "like whatever", otherwise nobody will be able to help you.

And I insist: learn to program on PC first. Learning to program on PSP is like learning to dance ballet wearing an armor...
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"
Dman49
Posts: 55
Joined: Wed May 11, 2011 10:49 pm

Re: --> [PSP PROGRAMMING TUTORIALS IN C!!!] <--

Post by Dman49 »

**so far so good :D
PSP 3000 PRO-B6 - PSP GO PRO-B6
Creations: BeastieBox (1.3) ; Flat Paintball (1.0) ; HeroClix PSP (unreleased)
Powerblaster
Posts: 46
Joined: Thu Aug 11, 2011 6:18 pm

Re: --> [PSP PROGRAMMING TUTORIALS IN C!!!] <--

Post by Powerblaster »

[TUTORIAL #3] VARIABLES got (") at line 22:. so thats why it got missing character.. :)
Image
[spoiler]Image -- NAH! This is how you crack your PSVITA Savedata!-- [[~@ by Jackson Zee~]][/spoiler]
xxtripledeathnotexx
Posts: 18
Joined: Sun Sep 18, 2011 7:47 pm
Location: ur MOM house

Re: --> [PSP PROGRAMMING TUTORIALS IN C!!!] <--

Post by xxtripledeathnotexx »

m0skit0 wrote:Please post the exact whole error, not "like whatever", otherwise nobody will be able to help you.

And I insist: learn to program on PC first. Learning to program on PSP is like learning to dance ballet wearing an armor...
of course lol, its like matrix coding. but anyways heres the full error

Code: Select all

"make:"****no targets.stop.
P.S. I slept with ur MOM

Image
Dman49
Posts: 55
Joined: Wed May 11, 2011 10:49 pm

Re: --> [PSP PROGRAMMING TUTORIALS IN C!!!] <--

Post by Dman49 »

Can you make a tutorial on how to load model files (.mdl)?
PSP 3000 PRO-B6 - PSP GO PRO-B6
Creations: BeastieBox (1.3) ; Flat Paintball (1.0) ; HeroClix PSP (unreleased)
pspgouser
Posts: 244
Joined: Mon Nov 15, 2010 8:42 pm
Location: Flash0:/

Re: --> [PSP PROGRAMMING TUTORIALS IN C!!!] <--

Post by pspgouser »

Very nice job :mrgreen: very easy and really covers every basic thing
Locked

Return to “Programming and Security”