The text console

OSLib includes its own text console, independent from the Sony one. However, it is completely identical at first sight, the only difference is that it takes into account the double buffering, which means that if you draw some text, you will not see the modifications before having swapped buffers, which is made by a call to oslSyncFrame or oslSwapBuffers. In simple buffer mode, nothing changes and the modifications are directly visible on the screen. So, it's this mode you should choose if you realize a console-based game. However, if you use double buffering, here is an example:

oslInitGfx(OSL_PF_8888, 1);
oslPrintf("Hello world!");
oslSwapBuffers();
oslWaitKey();
oslPrintf("Second test");
oslSwapBuffers();
oslWaitKey();
oslQuit();

Another possibility to draw directly on the screen is to redefine the drawbuffer to the currently displayed screen (secondary buffer).

oslInitGfx(OSL_PF_8888, 1);
oslSetDrawBuffer(OSL_SECONDARY_BUFFER);
oslPrintf("Hello world!");
oslWaitKey();
oslPrintf("Second test");
oslWaitKey();
oslQuit();

The available functions are the following:

oslPrintf(format...);			//Printf
oslCls();					//Clears the screen
oslMoveTo(x,y)				//Moves the cursor
oslPrintf_xy(x,y, format...)		//Printf at a specific position

There is also the following function to display the result of the benchmark (do not trust it too much because of we said in the chapter about the CPU cache):

void oslSysBenchmarkDisplay();

There are also functions which can show message boxes for the user. It looks like Windows 98 and it also works in double buffering mode.

void oslDebug(format...);

Displays a debug message with the source line, file and function are in the window title.

void oslWarning(format...);

Displays a warning message to the user.

void oslFatalError(format...);

Displays a fatal error message to the user and quits the application immediately after the user selected the default option.

void oslAssert(condition);

Verifies that the condition passed as a parameter is true. Else, it does warn the user. Useful for debugging purposes. Here is an example:

image = oslLoadImageFile("test.png", OSL_IN_RAM, OSL_PF_8888);
oslAssert(image);

We make sure the loaded image is not NULL (that means it was correctly loaded) and we continue the program execution only in this case.

oslSetTextColor(OSL_COLOR color);

Sets the current text color. If the current alpha mode is OSL_FX_RGBA (it's the default mode), colors can be defined in RGBA, that means that the last 8 bits define the transparency. For example, you can define a semi-transparent red text like this:

oslSetTextColor(RGBA(255,0,0,128));

oslSetBkColor(OSL_COLOR color);

Defines the background color used for text. You can make it transparent like this:

oslSetBkColor(RGBA(0,0,0,0));

unsigned int oslMessageBox(const char *text, const char *title, unsigned int flags);

Shows a message from which you specify the text (caption), the title and the supplementary button flags, and returns the value corresponding to the user choice. To define buttons, use the function named oslMake3Buttons.

oslMake3Buttons(b1,a1,b2,a2,b3,a3);

Creates the flag value for the oslMessageBox function with the specified buttons. For example:

oslMessageBox("Test", "Title", oslMake3Buttons(OSL_KEY_CROSS,OSL_MB_OK,OSL_KEY_TRIANGLE,OSL_MB_QUIT,0,0));

This will show a message where the first button will have text OK, associated to the cross button, and the other one the text Quit, associated to the triangle button. The possible values are the following ones: OSL_MB_OK, OSL_MB_CANCEL, OSL_MB_YES, OSL_MB_NO, OSL_MB_QUIT, and for the keys: OSL_KEY_SELECT, OSL_KEY_START, OSL_KEY_UP, OSL_KEY_RIGHT, OSL_KEY_DOWN, OSL_KEY_LEFT, OSL_KEY_L, OSL_KEY_R, OSL_KEY_TRIANGLE, OSL_KEY_CIRCLE, OSL_KEY_CROSS, OSL_KEY_SQUARE, OSL_KEY_HOME, OSL_KEY_HOLD, OSL_KEY_NOTE. The OSL_MB_QUIT value is special, because if the user chooses it, the game is quit immediately. For all the others, the choice of the user is returned and the game continues.

Finally, as the console supports double buffering, you can use it in any graphic game, but rather with oslPrintf_xy so you are sure of the place where you draw your text. Here is an example:

	int x = 0;
	oslInit(0);
	oslInitGfx(OSL_PF_8888, 1);
	oslInitConsole();
	
	//Main loop
	while (!osl_quit)
	{
		oslStartDrawing();
		oslClearScreen(0);
		oslPrintf_xy(0, 0, "x = %i", x);				//Position (x,y) is at pixel, unlike oslMoveTo
		oslEndDrawing();
		oslSyncFrame();
		x++;
	}
	oslQuit();