In this chapter, we will see how to create a new project using the library. However, that supposed that you are already a bit familiar with the PSPSDK (basic use of cygwin and compilation of a simple project). I know it's annoying to use it when you are accustomed to the simplicity of Windows, but once you'll have worked a little bit with it, everything will be better ;-)
First of all, create a new directory. It will contain the files of your project. In this directory, we will create a makefile (it's a text file describing our project, in order to know which compiler to use, which files to compile, which libraries to use, which file to produce in the end for example).
The Makefile is a file named Makefile (without extension). Here is an example of the text it could contain (use this to begin):
TARGET = test OBJS = test.o YOURLIBS= 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 = OSL Sample PSPSDK=$(shell psp-config --pspsdk-path) include $(PSPSDK)/lib/build.mak
Let's comment the sections which constitute this file. First of all there is a line TARGET = test. It's in fact the name
of our project. Then, there is OBJS = test.o. That indicates object files to be produced, that mean files to be compiled.
Files to be produced have the extension .o (it is the result of the compilation of .c or .cpp files), it's why I put
test.o and not test.c.
The line YOURLIBS = defines your own libraries to be added to the project.
PSP_EBOOT_TITLE is the name which will appear to the PSP game selection menu.
For other lines, you do not need to change them, except when you want to use another library (in that case, refer to the
documentation of the library in question).
If you simply want to add OSLib to an already existing project, you just have to add -losl to your libraries list. However you have to make sure that all the system libraries on which OSLib depends are included, and placed in this order (otherwise you will get errors at linking stage):
-losl -lpng -lz -lpspsdk -lpspctrl -lpspumd -lpsprtc -lpsppower -lpspgu -lpspaudiolib -lpspaudio -lm
To compile the project, start cygwin, move in the directory in question and type "make". For any additional informations, see the official documentation on makefiles.
Now that the library is correctly included to your project, you should be able to use it without any problem. Let's see at first the structure of a project created with OSLib.
#include <oslib/oslib.h> PSP_MODULE_INFO("Module name", 0, 1, 1); PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU); int main(int argc, char* argv[]) { //Initialize the different parts of the library oslInit(0); oslInitGfx(OSL_PF_8888, 1); oslInitConsole(); //Main loop while (!osl_quit) { //Display code oslStartDrawing(); //Red screen oslClearScreen(RGB(255,0,0)); //End of display code oslEndDrawing(); //Synchronization oslSyncFrame(); } //Game terminated (HOME -> quit) oslEndGfx(); oslQuit(); return 0; }
If you are experimented, you can modify lines PSP_MODULE_INFO and PSP_MAIN_THREAD_ATTR, but the attributes THREAD_ATTR_USER and THREAD_ATTR_VFPU must always be present in the thread attributes.
If you are a beginner, do not change it, just know that they are required by the PSPSDK (so nothing to do with OSLib).
Also note that with OSLib, you don't have to care about the callback initialization, as long as the value 0 is passed in oslInit.
If you want to use your own callbacks (if you do not know what that is, do not change it, of course), pass 1.
First of all, it is necessary to initialize the library. The various parts of the library (as the sound or the graphics) are separately initialized just to be able to limit as much as possible useless resources. But before initializing the parts, it is necessary to initialize the library itself. It is done by a call to oslInit, to which you pass zero by default.
void oslInit(int useOwnCallbacks);
Then, you can initialize the graphic part, by specifying which pixelformat you want to use for the screen, as well as if you wish to use the double buffering. We shall see later what are pixelFormats, for the moment simply pass OSL_PF_8888 for a 32-bit screen (16'777'216 colors) or OSL_PF_5650 for a 16-bit screen (65'536 colors) as an argument for pixelFormat.
void oslInitGfx(int pixelFormat, int bDoubleBuffer);
Thanks to the double buffering you to have two screens, one on which you can draw and one which is shown on the screen. You draw on a screen when the other one is shown, so the user will see nothing until you exchange screens. So he has directly the completely drawn screen, blinkings are not visible if you erase the screen and then redraw on it (as it is usual in games). The buffer swapping is done by calling the function oslSwapBuffers, or simply oslSyncFrame. That oslSyncFrame is also waiting for the VSync and takes charge of the frameskip.
Warning: The call to oslInitGfx resets the graphic system, and all the images in VRAM will be also reset at the same time!
Now you just have to initialize the console, to be able to draw some text (the standard Sony font is loaded at this moment). For that, simply call:
void oslInitConsole();
Finally, to initialize the audio system:
void oslInitAudio();
But we will take a look on audio later.
To begin to draw, you have to understand that it is necessary to call oslStartDrawing(), and oslEndDrawing() when you have finished, as in the example upwards. If you try to draw without calling oslStartDrawing, it won't work. Then, everything will be simple, follow the following pages of the guide and let's go!