forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Most efficient cursor method

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
OmahaStylee



Joined: 29 Aug 2005
Posts: 14
Location: Los Angeles, CA

PostPosted: Tue Aug 30, 2005 10:47 am    Post subject: Most efficient cursor method Reply with quote

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:

//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("xxxxxxx", 0, 1, 1);

/* Define the main thread's attribute value (optional) */
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU);

//External Files (Images)
extern u32 image_start[];
extern u32 image1_start[];
u32* images[] = { image_start, image1_start };

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

//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 = (u32*)(0x04000000+0x40000000);
long pg_screenmode;
long pg_showframe;
long pg_drawframe;

int exit_callback(void) {
   sceKernelExitGame();

   return 0;
}

void CallbackThread(void *arg) {
   int cbid;

   cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
   sceKernelRegisterExitCallback(cbid);

   sceKernelSleepThreadCB();
}

int SetupCallbacks(void) {
   int thid = 0;

   thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11, 0xFA0, 0, 0);
   if(thid >= 0) {
      sceKernelStartThread(thid, 0, 0);
   }

   return thid;
}

void pgWaitVn(unsigned long count) {
   for (; count>0; --count) {
      sceDisplayWaitVblankStart();
   }
}

void pgWaitV() {
   sceDisplayWaitVblankStart();
}

u32* pgGetVramAddr(unsigned long x,unsigned long y) {
   return pg_vramtop+(y * PSP_LINE_SIZE)+x+(pg_drawframe?PSP_LINE_SIZE*SCREEN_HEIGHT:0);
}

void pgScreenFrame(long mode,long frame) {
   pg_screenmode=mode;
   frame=(frame?1:0);
   pg_showframe=frame;
   if (mode==0) {
      //screen off
      pg_drawframe=frame;
      sceDisplaySetFrameBuf(0,0,0,1);
   } else if (mode==1) {
      //show/draw same
      pg_drawframe=frame;
      sceDisplaySetFrameBuf((char*)(pg_vramtop+(pg_drawframe?PSP_LINE_SIZE*SCREEN_HEIGHT:0)),PSP_LINE_SIZE,PSP_PIXEL_FORMAT,1);
   } else if (mode==2) {
      //show/draw different
      pg_drawframe=(frame?0:1);
      sceDisplaySetFrameBuf((char*)(pg_vramtop+(pg_drawframe?PSP_LINE_SIZE*SCREEN_HEIGHT:0)),PSP_LINE_SIZE,PSP_PIXEL_FORMAT,1);
   }
}

void pgInit() {
   sceDisplaySetMode(0,SCREEN_WIDTH,SCREEN_HEIGHT);
   pgScreenFrame(0,0);
}

void pgScreenFlip() {
   pg_showframe=(pg_showframe?0:1);
   pg_drawframe=(pg_drawframe?0:1);
   sceDisplaySetFrameBuf((char*)(pg_vramtop+(pg_showframe?PSP_LINE_SIZE*SCREEN_HEIGHT:0)),PSP_LINE_SIZE,PSP_PIXEL_FORMAT,0);
}

void pgScreenFlipV() {
   pgWaitV();
   pgScreenFlip();
}

void pgFlipDrawFrame() {
   pg_drawframe=(pg_drawframe?0:1);
}

void pgFlipShowFrameV() {
   pgWaitV();
   pg_showframe=(pg_showframe?0:1);
   sceDisplaySetFrameBuf((char*)(pg_vramtop+(pg_showframe?PSP_LINE_SIZE*SCREEN_HEIGHT:0)),PSP_LINE_SIZE,PSP_PIXEL_FORMAT,0);
}

void plotImage(int x, int y, u32* image, int sizeX, int sizeY) {
   u32* vram = pgGetVramAddr(x, y);
   int xo, yo;
   for (yo = 0; yo < sizeY; yo++) {
      for (xo = 0; xo < sizeX; xo++) {
         if (!IS_ALPHA(*image)) {
            vram[xo + yo * PSP_LINE_SIZE] = *image;
         } else {
                //Uncomment this Line if You Want a Background Image (And Don't Clear the Screen)
            //vram[xo + yo * PSP_LINE_SIZE] = background_start[x + xo + (y + yo) * SCREEN_WIDTH];
         }
         image++;
      }
   }
}

int main() {
   SetupCallbacks();

   pgInit();
   pgScreenFrame(1, 1);
   
   int x=0;
   int y=0;
   int cursorWidth = 15;
   int cursorHeight = 25
   int speed = 5;
   
   SceCtrlData pad;
   int changed = 1;
   while(1) {
        sceDisplayWaitVblankStart();
       
      sceCtrlReadBufferPositive(&pad, 1);

      if (pad.Buttons & PSP_CTRL_LEFT) {
            if(x-speed >= 0) {
                x -= speed;
                changed = 1;
            }
        }
        if(pad.Buttons & PSP_CTRL_RIGHT) {
            if(x+speed+50 <= 480) {
                x += speed;
                changed = 1;
            }
        }
        if(pad.Buttons & PSP_CTRL_UP) {
            if(y-speed >= 0) {
                y -= speed;
                changed = 1;
            }
        }
        if(pad.Buttons & PSP_CTRL_DOWN) {
            if(y+speed+50 <= 272) {
                y += speed;
                changed = 1;
            }
        }
        if(changed) {

            plotImage(0, 0, images[0], 480, 272);
            plotImage(x, y, images[1], 15, 25);
            changed = 0;
        }   
    }
   
    return 0;
}

_________________
Back to top
View user's profile Send private message Visit poster's website AIM Address
elevationsnow



Joined: 10 Sep 2005
Posts: 22

PostPosted: Sun Sep 11, 2005 5:15 am    Post subject: Reply with quote

where would you place your cursor image and what would you name it?
sorry im new to c and stuff
Back to top
View user's profile Send private message
Shine



Joined: 03 Dec 2004
Posts: 728
Location: Germany

PostPosted: Sun Sep 11, 2005 5:54 am    Post subject: Re: Most efficient cursor method Reply with quote

This code is really old, try graphics.c and graphics.h from http://svn.ps2dev.org/listing.php?repname=pspware&path=%2Ftrunk%2FLuaPlayer%2Fsrc%2F&rev=0&sc=0 which uses GU to do fast blitting of images with transparent pixels.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group