View Single Post
 
Old 01-26-2007, 08:37 PM
PSdonkey's Avatar
PSdonkey PSdonkey is offline
Squirrel Admin
PSP Titan
 
Join Date: Mar 2006
Posts: 9,117
PSdonkey has a reputation beyond reputePSdonkey has a reputation beyond reputePSdonkey has a reputation beyond reputePSdonkey has a reputation beyond reputePSdonkey has a reputation beyond reputePSdonkey has a reputation beyond reputePSdonkey has a reputation beyond reputePSdonkey has a reputation beyond reputePSdonkey has a reputation beyond reputePSdonkey has a reputation beyond reputePSdonkey has a reputation beyond repute
Default

Alright, here is lesson 2. I will show you how to use the PSP's buttons and I will go more into printing text onto your screen.


Lesson 2: PSP controller inputs

As you know, the PSP has several buttons. There are 4 buttons on the right hand side which are: cross, square, circle and triangle. There is also the L1 and R1 buttons on the top. Plus there is the analog stick which you can use in your programs. Here is a simple program which will print a text on your screen and depending on the button that you push, a different message will appear for each button. It will also show the movement of the analog stick when you move it left/right (X-axis) or up/down (Y-axis).

Here is the Makefile for this Lesson 2:

Code:
TARGET = Donkey_Lesson2
OBJS = main.o  

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

LIBDIR =
LDFLAGS =
STDLIBS= -losl -lpng -lz \
  -lpspsdk -lpspctrl -lpspumd -lpsprtc -lpsppower -lpspgu -lpspaudiolib -lpspaudio -lm
LIBS=$(STDLIBS)$(YOURLIBS)


EXTRA_TARGETS = EBOOT.PBP
PSP_EBOOT_TITLE = Donkey_Lesson2


PSPSDK=$(shell psp-config --pspsdk-path)
include $(PSPSDK)/lib/build.mak
Again, this Makefile is the exact same as the Makefile used in Lesson 1 except I changed the Target name to Donkey_Lesson2 and I changed the EBOOT's program title to Donkey_Lesson2.

Now here is the source code:
Code:
//OSlib header file
#include <oslib/oslib.h>

//Necessary to create eboot
PSP_MODULE_INFO("OSLib Sample", 0, 1, 1);
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU);

int main()
{
    //Initialization of the Oslib library
    oslInit(0);

    //Initialization of the graphics mode
    oslInitGfx(OSL_PF_8888, 1);

    //Initialization of the text console
    oslInitConsole();

    //Main loop
    while (!osl_quit)
    {
        //To be able to draw on the screen
        oslStartDrawing();
        
        //initiate the PSP's buttons
        oslReadKeys();
        
        //clears the screen
        oslCls();
  
        oslPrintf_xy(20,20,"What is your favorite psp site?");
        oslPrintf_xy(20,35,"If you like psp-hacks.com, push the X button");
oslPrintf_xy(20,45,"If you like psp-downgrades.com, push the square button");
oslPrintf_xy(20,55,"If you like psp-haxors.com, push the circle button");
oslPrintf_xy(20,65,"If you like zingaburga.co.nr, push the triangle button");

        if (osl_keys->held.cross) oslPrintf_xy(20,85,"You voted for greg's site");
        if (osl_keys->held.square) oslPrintf_xy(20,85,"You voted for power's site");
        if (osl_keys->held.circle) oslPrintf_xy(20,85,"You voted for Chaos_Zero's site");
        if (osl_keys->held.triangle) oslPrintf_xy(20,85,"You voted for ZingaBurga's site");

        oslPrintf_xy(20,100,"Value of joystick X : %d",osl_keys->analogX);
        oslPrintf_xy(20,110,"Value of joystick Y : %d",osl_keys->analogY);

        //Ends drawing mode
        oslEndDrawing();

        //Synchronizes the screen 
        oslSyncFrame();        
    }
    
    //Terminate the program
    oslEndGfx();
    oslQuit();
    return 0;
}
Explanations:

1) #include <oslib/oslib.h>
You must include this in all of your programs that use OSlib.

2) PSP_MODULE_INFO (“OSLib Sample”, 0, 1, 1);
PSP_MAIN_THREAD_ATTR (THREAD_ATTR_USER | THREAD_ATTR_VFPU);
This code is necessary for the PSPSDK to run the EBOOT that will be made by our program.

3) oslInit (0);
void oslInit(int useOwnCallbacks);
This line of code takes care of all your PSP callbacks. Normally, without this library, you would have to setup and initialize your callbacks with every single PSP program. With this line of code, you don't need to worry about the callback initialization, as long as the value 0 is between the parenthesis (). If you want to use your own callbacks (if you do not know what that is, do not change it, of course), type 1 in the parenthesis.

4) oslInitGfx (OSL_PF_8888, 0);
void oslInitGfx(int pixelFormat, int bDoubleBuffer);
This is used to initialize the graphic part, by specifying which pixelFormat you want to use on the screen, as well as if you want to use double buffering. Default 0 is used for single buffering and 1 for double buffering. With this Lesson, we are using (1) for double buffereing. Simply use OSL_PF_8888 for a 32-bit screen (2^32 = 16,777,216 colors) (-not entirely correct, but there is 16.7mil colours for 32 bit -ZiNgABuRgA) or OSL_PF_5650 for a 16-bit screen (2^16 = 65,536 colors) as an argument for pixelFormat.

5) oslInitConsole ();
This initialize the text part of the PSP, to be able to draw some text (the standard Sony font is loaded at this moment).

6) while (!osl_quit)
This is our main while loop. Everything within the two braces {} after while (!osl_quit) will keep on repeating itself until the osl_quit() command is found.
It technically means "while the code does not osl_quit()". "!" means "does not"
Hint: since osl_quit() is no where in between the two {} braces of the while loop, this program will loop forever unless you press the home button. You can change this by adding something like this to your code:
if (osl_keys->pressed.start) osl_quit();
That way, if the user wants to exit the loop/game, he would just have to press the start button.


7) oslStartDrawing();
You have to call this function before drawing anything onto the screen.

8) oslReadKeys();
Reads the input of all the buttons and stores in the total structure osl_keys. Make sure to call this function before having the user press a key (button).

9) oslCls();
Simply clears the screen.

10) oslPrintf_xy(x,y,"");
This function prints text to your screen and also lets you decide where you would like the text to be placed on the PSP's screen. X,Y are the X and Y coordinates of the text. " " are the quotations where your text is placed in between these quotations.

11) if (osl_keys->held.cross)
This is what you would type to read input from the cross button. To read from a different button, you would just change "cross" to another PSP button.
Here is the list of the PSP's buttons to use with this library:
Up > up
Down > down
Left > left
Right > right
Cross > cross
Square > square
Triangle > triangle
Circle > circle
L1 > L
R1 > R
Home > home
Music note > note
Select > select
Start > start
Hold > hold


Also, you use held.cross] for when the user holds down the cross button. You would use pressed.cross for when the user just pushes the cross button once.

12) osl_keys->analogX
Same as #11 except this is for when the analog stick is used.
osl_keys->analogX is the the X-axis of the analog stick and
osl_keys->analogy is for the Y-axis of the analog stick.

13) oslEndDrawing();
This corresponds to oslStartDrawing. It is to be used with each frame and the end of all the drawing operations.

14) oslSyncFrame();
You need to call this after each frame. It will synchronize the screen, use it when you have finish all the drawings.

15) oslEndGfx();
This ends the graphics part of the library. You can also reset it with other parameters.

16) oslQuit ();
Quits the game immediately. You'll go back to the PSP game menu.

17) return 0;
Always used at the end of main()


So now you have the basics of printing text to the screen, where the text can be placed, how to use the PSP's buttons, how to do something if the buttons are pushed, and more relating to controller inputs.

In the next lesson, lesson3, I will show you how to load images onto your PSP. I will show you how to load a background image as a map and then load a sprite onto the map and move the sprite around.
__________________
Want to become a PSP Dev the easy way? Check out my tutorial for the PSP here www.psp-hacks.com/forums/viewtopic.php?id=65403
Want to learn how to create your own games on the PSP? Check out my tutorial here http://www.psp-hacks.com/forums/view...693884#p693884
Want to learn how to program C++ ? Check out that tutorial here www.psp-hacks.com/forums/viewtopic.php?id=28694
Reply With Quote