Background image when showing the psp OSK

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
cr_17
Posts: 2
Joined: Wed Nov 19, 2008 6:59 am
Location: i don´t know

Background image when showing the psp OSK

Post by cr_17 »

Hi!

Does anybody knows how to show the psp OSK with a background image?

I have found some examples with custom libraries, but i need the source code to do it by myself.

Thanks.
psPea
Posts: 60
Joined: Sat Sep 01, 2007 12:51 pm

Post by psPea »

In the sdk samples you'll find the osk sample where a rotating cube is drawn in the background, so instead of drawing the cube just change it so an image image is drawn.
cr_17
Posts: 2
Joined: Wed Nov 19, 2008 6:59 am
Location: i don´t know

Post by cr_17 »

Hi psPea, can you please show me an example?.

I use lib graphics.h to show images but when I tried to use it with the sdk example it always crashed.

Thanks.
Kreationz
Posts: 52
Joined: Sun May 18, 2008 11:01 am

Post by Kreationz »

If you can show how your using it in your code that will go a long way toward helping or any answer is a blind guess.
psPea
Posts: 60
Joined: Sat Sep 01, 2007 12:51 pm

Post by psPea »

My bad, the osk sample doesn't have the cube its the save data sample
I use lib graphics.h to show images but when I tried to use it with the sdk example it always crashed.
Yeah so much for the easy way, try this quick mod of the osk sample, check out the DrawStuff function.An image img.png(png format width <= 512 height <= 512) in the same folder as the eboot is needed.


Code: Select all

#include <pspkernel.h>
#include <pspdisplay.h>
#include <pspdebug.h>
#include <pspgu.h>
#include <string.h>
#include <psputility.h>
#include <psptypes.h>
#include "graphics.h"

PSP_MODULE_INFO&#40;"OSK Sample", 0, 1, 1&#41;;
PSP_MAIN_THREAD_ATTR&#40;THREAD_ATTR_USER&#41;;
PSP_HEAP_SIZE_KB&#40;15*1024&#41;;

static int done = 0;

int exit_callback&#40;int arg1, int arg2, void *common&#41;
&#123;
	done = 1;
	
	return 0;
&#125;

int CallbackThread&#40;SceSize args, void *argp&#41;
&#123;
	int cbid = sceKernelCreateCallback&#40;"Exit Callback", exit_callback, NULL&#41;;
	sceKernelRegisterExitCallback&#40;cbid&#41;;
	sceKernelSleepThreadCB&#40;&#41;;
	
	return 0;
&#125;

int SetupCallbacks&#40;void&#41;
&#123;
	int thid = sceKernelCreateThread&#40;"update_thread", CallbackThread, 0x11, 0xFA0, PSP_THREAD_ATTR_USER, 0&#41;;
	
	if&#40;thid >= 0&#41;
		sceKernelStartThread&#40;thid, 0, 0&#41;;

	return thid;
&#125;
Image *img;
static unsigned int __attribute__&#40;&#40;aligned&#40;16&#41;&#41;&#41; list&#91;262144&#93;;

#define BUF_WIDTH	&#40;512&#41;
#define SCR_WIDTH	&#40;480&#41;
#define SCR_HEIGHT	&#40;272&#41;
#define PIXEL_SIZE	&#40;4&#41;
#define FRAME_SIZE	&#40;BUF_WIDTH * SCR_HEIGHT * PIXEL_SIZE&#41;
#define ZBUF_SIZE	&#40;BUF_WIDTH SCR_HEIGHT * 2&#41;

#define NUM_INPUT_FIELDS	&#40;3&#41;
#define TEXT_LENGTH			&#40;128&#41;

typedef struct&#123;

	ScePspSVector2 texture;

	ScePspSVector3 position;

&#125; TPSVertex;


void DrawImage&#40;Image *img, int xPos, int yPos&#41;&#123;if &#40;!img&#41; return;

     sceGuTexImage&#40;0, img->textureWidth, img->textureHeight, img->textureWidth, &#40;void*&#41; img->data&#41;;

 

     TPSVertex *vertices = &#40;TPSVertex*&#41; sceGuGetMemory&#40;2 * sizeof&#40;TPSVertex&#41;&#41;;

     vertices&#91;0&#93;.texture.x = 0;

     vertices&#91;0&#93;.texture.y = 0;

     vertices&#91;0&#93;.position.x = xPos;

     vertices&#91;0&#93;.position.y = yPos;

     vertices&#91;0&#93;.position.z = 0;

 

     vertices&#91;1&#93;.texture.x = img->imageWidth;

     vertices&#91;1&#93;.texture.y = img->imageHeight;

     vertices&#91;1&#93;.position.x = xPos+img->imageWidth;

     vertices&#91;1&#93;.position.y = yPos+img->imageHeight;

     vertices&#91;1&#93;.position.z = 0;

 

     sceGuTexMode&#40;GU_PSM_8888, 0, 0, 0&#41;;

     sceGuTexFunc&#40;GU_TFX_REPLACE, GU_TCC_RGBA&#41;;

     sceGuTexFilter&#40;GU_NEAREST, GU_NEAREST&#41;;

 

//     sceKernelDcacheWritebackAll&#40;&#41;;

     sceGuDrawArray&#40;GU_SPRITES, GU_TEXTURE_16BIT|GU_VERTEX_16BIT|GU_TRANSFORM_2D, 2, 0, vertices&#41;;

&#125;

static void DrawStuff&#40;void&#41;
&#123;

    sceGuStart&#40;GU_DIRECT,list&#41;;

    // Clear screen    
    sceGuClearColor&#40;0xff554433&#41;;
    sceGuClearDepth&#40;0&#41;;
    sceGuClear&#40;GU_COLOR_BUFFER_BIT|GU_DEPTH_BUFFER_BIT&#41;;

    DrawImage&#40;img, 0, 0&#41;;

    sceGuFinish&#40;&#41;;
    sceGuSync&#40;0,0&#41;;


&#125;



int main&#40;int argc, char* argv&#91;&#93;&#41;
&#123;
	SetupCallbacks&#40;&#41;;

//	initGraphics&#40;&#41;;
	img = loadImage&#40;"./img.png"&#41;;
	if &#40;img == NULL&#41; sceKernelExitGame&#40;&#41;;
	
	sceGuInit&#40;&#41;;
	sceGuStart&#40;GU_DIRECT,list&#41;;
	sceGuDrawBuffer&#40;GU_PSM_8888,&#40;void*&#41;0,BUF_WIDTH&#41;;
	sceGuDispBuffer&#40;SCR_WIDTH,SCR_HEIGHT,&#40;void*&#41;0x88000,BUF_WIDTH&#41;;
	sceGuDepthBuffer&#40;&#40;void*&#41;0x110000,BUF_WIDTH&#41;;
	sceGuOffset&#40;2048 - &#40;SCR_WIDTH/2&#41;,2048 - &#40;SCR_HEIGHT/2&#41;&#41;;
	sceGuViewport&#40;2048,2048,SCR_WIDTH,SCR_HEIGHT&#41;;
	sceGuDepthRange&#40;0xc350,0x2710&#41;;
	sceGuScissor&#40;0,0,SCR_WIDTH,SCR_HEIGHT&#41;;
	sceGuEnable&#40;GU_SCISSOR_TEST&#41;;
	sceGuDepthFunc&#40;GU_GEQUAL&#41;;
	sceGuEnable&#40;GU_DEPTH_TEST&#41;;
	sceGuFrontFace&#40;GU_CW&#41;;
	sceGuShadeModel&#40;GU_FLAT&#41;;
	sceGuEnable&#40;GU_CULL_FACE&#41;;
	sceGuEnable&#40;GU_TEXTURE_2D&#41;;
	sceGuEnable&#40;GU_CLIP_PLANES&#41;;
	sceGuFinish&#40;&#41;;
	sceGuSync&#40;0,0&#41;;
	sceDisplayWaitVblankStart&#40;&#41;;
	sceGuDisplay&#40;GU_TRUE&#41;;
	
	
	unsigned short intext&#91;NUM_INPUT_FIELDS&#93;&#91;TEXT_LENGTH&#93;;
	unsigned short outtext&#91;NUM_INPUT_FIELDS&#93;&#91;TEXT_LENGTH&#93;;
	unsigned short desc&#91;NUM_INPUT_FIELDS&#93;&#91;TEXT_LENGTH&#93;;
	
	memset&#40;&intext, 0, NUM_INPUT_FIELDS * TEXT_LENGTH * sizeof&#40;unsigned short&#41;&#41;;
	memset&#40;&outtext, 0, NUM_INPUT_FIELDS * TEXT_LENGTH * sizeof&#40;unsigned short&#41;&#41;;
	memset&#40;&desc, 0, NUM_INPUT_FIELDS * TEXT_LENGTH * sizeof&#40;unsigned short&#41;&#41;;
	
	int i;
	
	for&#40;i = 0;i < NUM_INPUT_FIELDS;i++&#41;
	&#123;
		desc&#91;i&#93;&#91;0&#93; = 'F';
		desc&#91;i&#93;&#91;1&#93; = 'i';
		desc&#91;i&#93;&#91;2&#93; = 'e';
		desc&#91;i&#93;&#91;3&#93; = 'l';
		desc&#91;i&#93;&#91;4&#93; = 'd';
		desc&#91;i&#93;&#91;5&#93; = ' ';
		desc&#91;i&#93;&#91;6&#93; = i + 48 + 1; // Convert i to ASCII value.
		desc&#91;i&#93;&#91;7&#93; = 0;
		
		intext&#91;i&#93;&#91;0&#93; = 'T';
		intext&#91;i&#93;&#91;1&#93; = 'e';
		intext&#91;i&#93;&#91;2&#93; = 'x';
		intext&#91;i&#93;&#91;3&#93; = 't';
		intext&#91;i&#93;&#91;4&#93; = ' ';
		intext&#91;i&#93;&#91;5&#93; = i + 48 + 1; // Convert i to ASCII value.
		intext&#91;i&#93;&#91;6&#93; = 0;
		
	&#125;

	SceUtilityOskData data&#91;NUM_INPUT_FIELDS&#93;;
	
	for&#40;i = 0; i < NUM_INPUT_FIELDS;i++&#41;
	&#123;
		memset&#40;&data&#91;i&#93;, 0, sizeof&#40;SceUtilityOskData&#41;&#41;;
		data&#91;i&#93;.language = PSP_UTILITY_OSK_LANGUAGE_DEFAULT; // Use system default for text input
		data&#91;i&#93;.lines = 1;
		data&#91;i&#93;.unk_24 = 1;
		data&#91;i&#93;.inputtype = PSP_UTILITY_OSK_INPUTTYPE_ALL; // Allow all input types
		data&#91;i&#93;.desc = desc&#91;i&#93;;
		data&#91;i&#93;.intext = intext&#91;i&#93;;
		data&#91;i&#93;.outtextlength = TEXT_LENGTH;
		data&#91;i&#93;.outtextlimit = 32; // Limit input to 32 characters
		data&#91;i&#93;.outtext = outtext&#91;i&#93;;
	&#125;
	
	SceUtilityOskParams params;
	memset&#40;&params, 0, sizeof&#40;params&#41;&#41;;
	params.base.size = sizeof&#40;params&#41;;
	sceUtilityGetSystemParamInt&#40;PSP_SYSTEMPARAM_ID_INT_LANGUAGE, &params.base.language&#41;;
	sceUtilityGetSystemParamInt&#40;PSP_SYSTEMPARAM_ID_INT_UNKNOWN, &params.base.buttonSwap&#41;;
	params.base.graphicsThread = 17;
	params.base.accessThread = 19;
	params.base.fontThread = 18;
	params.base.soundThread = 16;
	params.datacount = NUM_INPUT_FIELDS;
	params.data = data;

	sceUtilityOskInitStart&#40;&params&#41;;

	while&#40;!done&#41;
	&#123;
		DrawStuff&#40;&#41;;

		switch&#40;sceUtilityOskGetStatus&#40;&#41;&#41;
		&#123;
			case PSP_UTILITY_DIALOG_INIT&#58;
				break;
			
			case PSP_UTILITY_DIALOG_VISIBLE&#58;
				sceUtilityOskUpdate&#40;1&#41;;
				break;
			
			case PSP_UTILITY_DIALOG_QUIT&#58;
				sceUtilityOskShutdownStart&#40;&#41;;
				break;
			
			case PSP_UTILITY_DIALOG_FINISHED&#58;
				break;
				
			case PSP_UTILITY_DIALOG_NONE&#58;
				done = 1;
				
			default &#58;
				break;
		&#125;

		sceDisplayWaitVblankStart&#40;&#41;;
		sceGuSwapBuffers&#40;&#41;;
	&#125;

	pspDebugScreenInit&#40;&#41;;
	pspDebugScreenSetXY&#40;0, 0&#41;;
	
	int j;
	
	for&#40;i = 0; i < NUM_INPUT_FIELDS;i++&#41;
	&#123;
		pspDebugScreenPrintf&#40;"Field %d&#58; ", i+1&#41;;
		
		switch&#40;data&#91;i&#93;.result&#41;
		&#123;
			case PSP_UTILITY_OSK_RESULT_UNCHANGED&#58;
				pspDebugScreenPrintf&#40;"UNCHANGED&#58; "&#41;;
				break;
			
			case PSP_UTILITY_OSK_RESULT_CANCELLED&#58;
				pspDebugScreenPrintf&#40;"CANCELLED&#58; "&#41;;
				break;
				
			case PSP_UTILITY_OSK_RESULT_CHANGED&#58;
				pspDebugScreenPrintf&#40;"CHANGED&#58; "&#41;;
				break;
				
			default&#58;
				break;
		&#125;
		
		for&#40;j = 0; data&#91;i&#93;.outtext&#91;j&#93;; j++&#41;
		&#123;
			unsigned c = data&#91;i&#93;.outtext&#91;j&#93;;
			
			if&#40;32 <= c && c <= 127&#41; // print ascii only
				pspDebugScreenPrintf&#40;"%c", data&#91;i&#93;.outtext&#91;j&#93;&#41;;
		&#125;
		
		pspDebugScreenPrintf&#40;"\n"&#41;;
	&#125;

	done = 0;

	while&#40;!done&#41;
		sceKernelDelayThread&#40;1000&#41;;	

	sceGuTerm&#40;&#41;;
	
	sceKernelExitGame&#40;&#41;;

	return 0;
&#125;

Post Reply