How can I put images in display?

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

Moderators: cheriff, TyRaNiD

Post Reply
kecC
Posts: 17
Joined: Mon Jul 04, 2005 11:07 pm

How can I put images in display?

Post by kecC »

Hi!

I'm learning to develop to PSP (and also learning C) and I want to push one button, and see one image in the display.

I have the image in C, and I've tried to make a function for do it, but I haven't get it.

Can anybody help me with a very simple function?

Thanks.
Bye.
Shine
Posts: 728
Joined: Fri Dec 03, 2004 12:10 pm
Location: Germany

Re: How can I put images in display?

Post by Shine »

kecC wrote:Hi!

I have the image in C, and I've tried to make a function for do it, but I haven't get it.
What do you mean with "in C"? Do you have an array of u32 values of your image? Then you could do this:

Code: Select all

#include <pspkernel.h>
#include <pspdisplay.h>
#include <pspctrl.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>

/* Define the module info section */
PSP_MODULE_INFO&#40;"PAINTIMAGE", 0, 1, 1&#41;;

/* Define the main thread's attribute value &#40;optional&#41; */
PSP_MAIN_THREAD_ATTR&#40;THREAD_ATTR_USER | THREAD_ATTR_VFPU&#41;;

/* Exit callback */
int exit_callback&#40;int arg1, int arg2, void *common&#41;
&#123;
	sceKernelExitGame&#40;&#41;;

	return 0;
&#125;

/* Callback thread */
int CallbackThread&#40;SceSize args, void *argp&#41;
&#123;
	int cbid;

	cbid = sceKernelCreateCallback&#40;"Exit Callback", &#40;void *&#41; exit_callback, NULL&#41;;
	sceKernelRegisterExitCallback&#40;cbid&#41;;

	sceKernelSleepThreadCB&#40;&#41;;

	return 0;
&#125;

/* Sets up the callback thread and returns its thread id */
int SetupCallbacks&#40;void&#41;
&#123;
	int thid = 0;

	thid = sceKernelCreateThread&#40;"update_thread", CallbackThread, 0x11, 0xFA0, 0, 0&#41;;
	if&#40;thid >= 0&#41;
	&#123;
		sceKernelStartThread&#40;thid, 0, 0&#41;;
	&#125;

	return thid;
&#125;

#define PSP_SCREEN_WIDTH 480
#define PSP_SCREEN_HEIGHT 272
#define PSP_LINE_SIZE 512
#define PSP_PIXEL_FORMAT 3

int main&#40;void&#41;
&#123;
	int i, x, y, oldButtons;
	SceCtrlData pad;
	u32* image;
	u32* vram = &#40;u32*&#41; &#40;0x40000000 | 0x04000000&#41;;

	SetupCallbacks&#40;&#41;;

	// some demo image
	image = malloc&#40;480*272*4&#41;;
	for &#40;y = 0; y < 272; y++&#41; &#123;
		for &#40;x = 0; x < 480; x++&#41; &#123;
			image&#91;x + y*480&#93; = x * y;
		&#125;
	&#125;
	
	// init graphics
	sceDisplaySetMode&#40;0, PSP_SCREEN_WIDTH, PSP_SCREEN_HEIGHT&#41;;
	sceDisplaySetFrameBuf&#40;&#40;void *&#41; vram, 512, 3, 1&#41;;
	
	// clear screen
	for &#40;i = 0; i < 512 * 480; i++&#41; vram&#91;i&#93; = 0;

	// wait for key
	sceCtrlSetSamplingCycle&#40;0&#41;;
	sceCtrlSetSamplingMode&#40;PSP_CTRL_MODE_DIGITAL&#41;;
	sceCtrlReadBufferPositive&#40;&pad, 1&#41;;
	oldButtons = pad.Buttons;
	while &#40;1&#41; &#123;
		sceDisplayWaitVblankStart&#40;&#41;;
		sceCtrlReadBufferPositive&#40;&pad, 1&#41;;
		if &#40;oldButtons != pad.Buttons&#41; break;
	&#125;
	
	// draw image
	for &#40;y = 0; y < 272; y++&#41; &#123;
		for &#40;x = 0; x < 480; x++&#41; &#123;
			vram&#91;x + y * 512&#93; = image&#91;x + y*480&#93;;
		&#125;
	&#125;

	sceKernelSleepThread&#40;&#41;;

	return 0;
&#125;
Instead of creating the demo image, you can use your image array.

BTW: with my Lua Player the full source code for the same, but loading a PNG image from memory stick included, looks like this:

Code: Select all

image = loadImage&#40;"background.png"&#41;
oldButtons = ctrlRead&#40;&#41;
while true do
	waitVblankStart&#40;&#41;
	buttons = ctrlRead&#40;&#41;
	if buttons ~= oldButtons then
		break
	end
end
blitImage&#40;0, 0, image&#41;
flipScreen&#40;&#41;
kecC
Posts: 17
Joined: Mon Jul 04, 2005 11:07 pm

Post by kecC »

Thanks a lot!

But I want to show a image like this type:

Code: Select all

const unsigned short image&#91;&#93; = &#123;
0x735a,0x6f39,0x6b39,0x6f39,0x6b39,0x6f39,...
&#125;
How can I do it?

Thanks again!
nevyn
Posts: 136
Joined: Sun Jul 31, 2005 5:05 pm
Location: Sweden
Contact:

Post by nevyn »

kecC wrote:Thanks a lot!

But I want to show a image like this type:

Code: Select all

const unsigned short image&#91;&#93; = &#123;
0x735a,0x6f39,0x6b39,0x6f39,0x6b39,0x6f39,...
&#125;
How can I do it?

Thanks again!
Dude, that's what the code he gave you does.
hexperience
Posts: 19
Joined: Tue Jul 05, 2005 7:35 am

Post by hexperience »

Thanks for the samples guys. It worked, but the colors are messed up.

My orginal pic was 24 bit color. Is there something I need to change in the init of the screen buffer?

Thanks.
Hex.

ps.
You know when you used to play Doom too long when you were a kid... and your head felt rather football shaped and there was pain somewhere about a half inch behind your eyes? I have that from reading C primers and PSP samples... but I'm getting there! 8)
hexperience
Posts: 19
Joined: Tue Jul 05, 2005 7:35 am

Post by hexperience »

I figured it out, but I don't know why it worked. Still reading up on my C.

I changed the define for vram from u32 to u16.
And I changed the PixelMode from 3 to 1 which means that the tool that converted my picture created a 16bit array in the .c file rather than a 32bit array which the sample code above was expecting. (I'm guessing).

This thread was what got me going in the right direction if you are still looking for help with this...

http://forums.ps2dev.org/viewtopic.php?t=2899

Cheers
Post Reply