Most efficient cursor method

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

Moderators: cheriff, TyRaNiD

Post Reply
OmahaStylee
Posts: 14
Joined: Mon Aug 29, 2005 5:00 pm
Location: Los Angeles, CA
Contact:

Most efficient cursor method

Post by OmahaStylee »

I was wondering what the most effecient and effective way to make a moveable cursor would be. I did one with a simple plot image script that shine used in his snake game, but I cant seem to get it to work perfectly. Well, his script works perfectly, but my part doesnt. I can control the cursor, but i cant get it to look right. Either it trails and leaves markings all over the background, or i refresh the background before plotting its movement and it flickers.

If anyone has some sample code i could use, that'd be great. I want to do this the most effecient way possible for where we are at right now (the method might get outdated later). I think that might be using the GPU (GU?)

Code: Select all

//Function For Printing An Image to the Screen
//Created by Yeldarb (with help from other code) on August 21, 2005

#include <pspkernel.h>
#include <pspdebug.h>
#include <pspdisplay.h>
#include <pspctrl.h>

/* Define the module info section */
PSP_MODULE_INFO&#40;"xxxxxxx", 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;;

//External Files &#40;Images&#41;
extern u32 image_start&#91;&#93;;
extern u32 image1_start&#91;&#93;;
u32* images&#91;&#93; = &#123; image_start, image1_start &#125;;

//Color "Functions" &#40;Conversion and Test&#41;
#define RGB&#40;r, g, b&#41; &#40;&#40;r&#41;|&#40;&#40;g&#41;<<8&#41;|&#40;&#40;b&#41;<<16&#41;&#41;
#define IS_ALPHA&#40;value&#41; &#40;&#40;&#40;value&#41;&0xff000000&#41;==0&#41;

//constants
#define SCREEN_WIDTH 480
#define SCREEN_HEIGHT 272

#define	PSP_PIXEL_FORMAT 3  // 32 bit RGBA
#define	PSP_LINE_SIZE 512   // in u32

//480*272 = 60*38
#define CMAX_X 60
#define CMAX_Y 38
#define CMAX2_X 30
#define CMAX2_Y 19
#define CMAX4_X 15
#define CMAX4_Y 9


u32* pg_vramtop = &#40;u32*&#41;&#40;0x04000000+0x40000000&#41;;
long pg_screenmode;
long pg_showframe;
long pg_drawframe;

int exit_callback&#40;void&#41; &#123;
	sceKernelExitGame&#40;&#41;;

	return 0;
&#125;

void CallbackThread&#40;void *arg&#41; &#123;
	int cbid;

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

	sceKernelSleepThreadCB&#40;&#41;;
&#125;

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;

void pgWaitVn&#40;unsigned long count&#41; &#123;
	for &#40;; count>0; --count&#41; &#123;
		sceDisplayWaitVblankStart&#40;&#41;;
	&#125;
&#125;

void pgWaitV&#40;&#41; &#123;
	sceDisplayWaitVblankStart&#40;&#41;;
&#125;

u32* pgGetVramAddr&#40;unsigned long x,unsigned long y&#41; &#123;
	return pg_vramtop+&#40;y * PSP_LINE_SIZE&#41;+x+&#40;pg_drawframe?PSP_LINE_SIZE*SCREEN_HEIGHT&#58;0&#41;;
&#125;

void pgScreenFrame&#40;long mode,long frame&#41; &#123;
	pg_screenmode=mode;
	frame=&#40;frame?1&#58;0&#41;;
	pg_showframe=frame;
	if &#40;mode==0&#41; &#123;
		//screen off
		pg_drawframe=frame;
		sceDisplaySetFrameBuf&#40;0,0,0,1&#41;;
	&#125; else if &#40;mode==1&#41; &#123;
		//show/draw same
		pg_drawframe=frame;
		sceDisplaySetFrameBuf&#40;&#40;char*&#41;&#40;pg_vramtop+&#40;pg_drawframe?PSP_LINE_SIZE*SCREEN_HEIGHT&#58;0&#41;&#41;,PSP_LINE_SIZE,PSP_PIXEL_FORMAT,1&#41;;
	&#125; else if &#40;mode==2&#41; &#123;
		//show/draw different
		pg_drawframe=&#40;frame?0&#58;1&#41;;
		sceDisplaySetFrameBuf&#40;&#40;char*&#41;&#40;pg_vramtop+&#40;pg_drawframe?PSP_LINE_SIZE*SCREEN_HEIGHT&#58;0&#41;&#41;,PSP_LINE_SIZE,PSP_PIXEL_FORMAT,1&#41;;
	&#125;
&#125;

void pgInit&#40;&#41; &#123;
	sceDisplaySetMode&#40;0,SCREEN_WIDTH,SCREEN_HEIGHT&#41;;
	pgScreenFrame&#40;0,0&#41;;
&#125;

void pgScreenFlip&#40;&#41; &#123;
	pg_showframe=&#40;pg_showframe?0&#58;1&#41;;
	pg_drawframe=&#40;pg_drawframe?0&#58;1&#41;;
	sceDisplaySetFrameBuf&#40;&#40;char*&#41;&#40;pg_vramtop+&#40;pg_showframe?PSP_LINE_SIZE*SCREEN_HEIGHT&#58;0&#41;&#41;,PSP_LINE_SIZE,PSP_PIXEL_FORMAT,0&#41;;
&#125;

void pgScreenFlipV&#40;&#41; &#123;
	pgWaitV&#40;&#41;;
	pgScreenFlip&#40;&#41;;
&#125;

void pgFlipDrawFrame&#40;&#41; &#123;
	pg_drawframe=&#40;pg_drawframe?0&#58;1&#41;;
&#125;

void pgFlipShowFrameV&#40;&#41; &#123;
	pgWaitV&#40;&#41;;
	pg_showframe=&#40;pg_showframe?0&#58;1&#41;;
	sceDisplaySetFrameBuf&#40;&#40;char*&#41;&#40;pg_vramtop+&#40;pg_showframe?PSP_LINE_SIZE*SCREEN_HEIGHT&#58;0&#41;&#41;,PSP_LINE_SIZE,PSP_PIXEL_FORMAT,0&#41;;
&#125;

void plotImage&#40;int x, int y, u32* image, int sizeX, int sizeY&#41; &#123;
	u32* vram = pgGetVramAddr&#40;x, y&#41;;
	int xo, yo;
	for &#40;yo = 0; yo < sizeY; yo++&#41; &#123;
		for &#40;xo = 0; xo < sizeX; xo++&#41; &#123;
			if &#40;!IS_ALPHA&#40;*image&#41;&#41; &#123;
				vram&#91;xo + yo * PSP_LINE_SIZE&#93; = *image;
			&#125; else &#123;
                //Uncomment this Line if You Want a Background Image &#40;And Don't Clear the Screen&#41;
				//vram&#91;xo + yo * PSP_LINE_SIZE&#93; = background_start&#91;x + xo + &#40;y + yo&#41; * SCREEN_WIDTH&#93;;
			&#125;
			image++;
		&#125;
	&#125;
&#125;

int main&#40;&#41; &#123;
	SetupCallbacks&#40;&#41;;

	pgInit&#40;&#41;;
	pgScreenFrame&#40;1, 1&#41;;
	
	int x=0;
	int y=0;
	int cursorWidth = 15;
	int cursorHeight = 25
	int speed = 5;
	
	SceCtrlData pad;
	int changed = 1;
	while&#40;1&#41; &#123;
        sceDisplayWaitVblankStart&#40;&#41;;
        
		sceCtrlReadBufferPositive&#40;&pad, 1&#41;; 

		if &#40;pad.Buttons & PSP_CTRL_LEFT&#41; &#123;
            if&#40;x-speed >= 0&#41; &#123;
                x -= speed;
                changed = 1;
            &#125;
        &#125; 
        if&#40;pad.Buttons & PSP_CTRL_RIGHT&#41; &#123;
            if&#40;x+speed+50 <= 480&#41; &#123;
                x += speed;
                changed = 1;
            &#125;
        &#125;
        if&#40;pad.Buttons & PSP_CTRL_UP&#41; &#123;
            if&#40;y-speed >= 0&#41; &#123;
                y -= speed;
                changed = 1;
            &#125;
        &#125; 
        if&#40;pad.Buttons & PSP_CTRL_DOWN&#41; &#123;
            if&#40;y+speed+50 <= 272&#41; &#123;
                y += speed;
                changed = 1;
            &#125;
        &#125;
        if&#40;changed&#41; &#123;

            plotImage&#40;0, 0, images&#91;0&#93;, 480, 272&#41;;
            plotImage&#40;x, y, images&#91;1&#93;, 15, 25&#41;;
            changed = 0;
        &#125;    
    &#125;
    
    return 0;
&#125;
Image
elevationsnow
Posts: 22
Joined: Sat Sep 10, 2005 6:45 am

Post by elevationsnow »

where would you place your cursor image and what would you name it?
sorry im new to c and stuff
Shine
Posts: 728
Joined: Fri Dec 03, 2004 12:10 pm
Location: Germany

Re: Most efficient cursor method

Post by Shine »

This code is really old, try graphics.c and graphics.h from http://svn.ps2dev.org/listing.php?repna ... rev=0&sc=0 which uses GU to do fast blitting of images with transparent pixels.
Post Reply