Shape objects in C++ (PSP)

This post was written by Brandon on December 14, 2008
Posted Under: C++,psp

We already discussed shape object creation and movements in VB.NET, but now lets take a look at it in C++.

The principals are the same, create an Array or ArrayList for the rectangle object, and set its x and y position as well as its width, height and color. This example will show shape object creation in C++. This code was originally written to run on the Playstation Portable (PSP) but if you know your way around C++, you should be able to get it running on windows without a problem.


The compiled binary (EBOOT) for PSP for the full game will be posted in a few weeks. The original game was written in VB.NET (Posted soon!), then ported to Java (Posted in a few weeks) and now being ported to C++ for PSP. In the future it may be ported to Objective C so it can run on the iPhone/iPod touch.

Here is a useful function for generating a random number on the PSP in C++ (Given to me by thecobra over at the forums)

static int GetRandomNum(int lo, int hi)
 SceKernelUtilsMt19937Context ctx;
 int rs = 10 + rand() % (500 - 10) + 1;
 sceKernelUtilsMt19937Init(&ctx, rs); //SEED TO TIME
 u32 rand_val = sceKernelUtilsMt19937UInt(&ctx);
 rand_val = lo + rand_val % hi;
 return (int)rand_val;

Although it looks more complex than the one in VB.NET, which is:

Function Random(ByVal Lowerbound As Long, ByVal Upperbound As Long)
Random = Int(Rnd() * Upperbound) + Lowerbound
End Function

The basic formula is the same

Here are some things we should declare

 int squaresin; //the total amount of vertical squares in play
 int arrowDirs[1000]; //The directions for movement of the vertical squares
 int Hsquaresin; //the total amount of horizonal squares in play
 int HarrowDirs[1000]; //The directions for movement of the horizontal squares
Rectangle squares[1000]; //declare the array of vertical squares
Rectangle Hsquares[1000]; //declare the array of horizontal squares
Color red = RGB(255, 0, 0); //declare various colours
Color green = RGB(0, 255, 0);
Color blue = RGB(0, 0, 255);
Color yellow = RGB(255, 255, 0);

Note: There is no such thing as a boolean in C++, integers must be used. 1 represents true, 0 represents false.

Next, lets use some methods to create the squares easily:

	void addVertRectangle(int Vertrectnum){
     squares[Vertrectnum].x = GetRandomNum(1, 480); //random number between 1 and the dimensions of the psp screen
     squares[Vertrectnum].y = GetRandomNum(1, 272);
     squares[Vertrectnum].width = 5;
     squares[Vertrectnum].height = 5;
     squares[Vertrectnum].mycolor = green; //vertical squares are green
     void addHorizRectangle(int Horizrectnum){
     Hsquares[Horizrectnum].x = GetRandomNum(1, 480);
     Hsquares[Horizrectnum].y = GetRandomNum(1, 272);
     Hsquares[Horizrectnum].width = 5;
     Hsquares[Horizrectnum].height = 5;
     Hsquares[Horizrectnum].mycolor = blue; //horizontal squares are blue

Put these within the main method.

Within our game loop we will need a few things. Here is the start of the game loop and the things we need within it. This is not the full code:

while(1) {
SceCtrlData pad;
pspDebugScreenSetXY(0, 2);
sceCtrlReadBufferPositive(&pad, 1);
Refresh(RGB(0, 0, 0));

Here is the code that goes within the game loop. This part handles movements and bouncing off the walls. (In the VB.NET and Java version, it works the excact same way!)

int j;
   for (j = 0; j < squaresin; j ++){
       if (arrowDirs[j] == 0){
    squares[j].x = squares[j].x - 1;
      squares[j].x = squares[j].x + 1;
      if (squares[j].x <= 5){
      arrowDirs[j] = 1;
      if (squares[j].x >= (480 - squares[j].width)){
      arrowDirs[j] = 0;
int k;
   for (k = 0; k < Hsquaresin; k ++){
       if (HarrowDirs[k] == 0){
    Hsquares[k].y = Hsquares[k].y - 1;
      Hsquares[k].y = Hsquares[k].y + 1;
      if (Hsquares[k].y <= 5){
      HarrowDirs[k] = 1;
      if (Hsquares[k].y >= (272 - Hsquares[k].height)){
      HarrowDirs[k] = 0;

Heres a quick example on adding new shapes by clicking the O button:

 if (pad.Buttons & PSP_CTRL_CROSS){

Finally, finish the game loop with:


This tutorial may be a bit hard to follow. When the game is fully done the full source and a longer tutorial will be posted. In a few days a tutorial will be up on getting your development environment set up to begin programming for the PSP.

I will like to thank thecobra over at the forums for helping me get started in PSP programming!


Add a Comment

required, use real name
required, will not be published
optional, your blog address

Previose Post: