Code: Select all
#include <oslib/oslib.h>
PSP_MODULE_INFO("Desktop", 0, 1, 0);
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU);
PSP_HEAP_SIZE_KB(20*1024);
int initOSLib()
{
oslInit(0);
oslInitGfx(OSL_PF_8888, 1);
oslSetQuitOnLoadFailure(1);
oslSetKeyAutorepeatInit(40);
oslSetKeyAutorepeatInterval(10);
return 0;
}
int endOSLib()
{
oslEndGfx();
osl_quit = 1;
sceKernelExitGame();
return 0;
}
int collision(int x0, int y0, int x1, int y1, int w0, int h0, int w1, int h1)
{
int hit = 0;
if(x0+w0 > x1 && x0 < x1+w1 && y0+h0 > y1 && y0 < y1+h1)hit = 1;
return hit;
}
int main()
{
initOSLib();//Call the function made before
//Set item starting positions
int item1X = 50;
int item1Y = 50;
int item2X = 50;
int item2Y = 70;
int item3X = 50;
int item3Y = 90;
//Set cursors starting position
int cursorX = 0;
int cursorY = 0;
//Load the background and cursor
OSL_IMAGE* BG = oslLoadImageFilePNG("background.png", OSL_IN_RAM|OSL_SWIZZLED, OSL_PF_8888);
OSL_IMAGE* cursor = oslLoadImageFilePNG("cursor.png", OSL_IN_RAM|OSL_SWIZZLED, OSL_PF_8888);
while(!osl_quit)//Main loop
{
oslStartDrawing();//Start drawing
oslReadKeys();//Read the PSP's keys
oslDrawImage(BG);//Draw the background
oslDrawFillRect(item1X, item1Y, item1X+10, item1Y+10, RGB(255, 0, 0));//Draw item 1
oslDrawFillRect(item2X, item2Y, item2X+10, item2Y+10, RGB(0, 255, 0));//Draw item 2
oslDrawFillRect(item3X, item3Y, item3X+10, item3Y+10, RGB(0, 0, 255));//Draw item 3
oslDrawImageXY(cursor, cursorX, cursorY);//Draw the cursor
if(osl_keys->held.cross)//If cross is pressed
{
if(collision(cursorX, cursorY, item1X, item1Y, 10, 10, 10, 10) && !(collision(cursorX, cursorY, item2X, item2Y, 10, 10, 10, 10)) && !(collision(cursorX, cursorY, item3X, item3Y, 10, 10, 10, 10)))//If the cursor is on the first item and not on the other two
{
//Set the items X & Y to the cursors
item1X = cursorX;
item1Y = cursorY;
}
if(collision(cursorX, cursorY, item2X, item2Y, 10, 10, 10, 10) && !(collision(cursorX, cursorY, item1X, item1Y, 10, 10, 10, 10)) && !(collision(cursorX, cursorY, item3X, item3Y, 10, 10, 10, 10)))//If the cursor is on the second item and not on the other two
{
//Set the items X & Y to the cursors
item2X = cursorX;
item2Y = cursorY;
}
if(collision(cursorX, cursorY, item3X, item3Y, 10, 10, 10, 10) && !(collision(cursorX, cursorY, item1X, item1Y, 10, 10, 10, 10)) && !(collision(cursorX, cursorY, item2X, item2Y, 10, 10, 10, 10)))//If the cursor is on the third item and not on the other two
{
//Set the items X & Y to the cursor's
item3X = cursorX;
item3Y = cursorY;
}
}
//Controls for the cursor
if(osl_keys->held.left)cursorX -= 2;
if(osl_keys->held.right)cursorX += 2;
if(osl_keys->held.up)cursorY -= 2;
if(osl_keys->held.down)cursorY += 2;
//Exit if start is pressed
if(osl_keys->pressed.start)endOSLib();
//End drawing, frame .etc...
oslEndDrawing();
oslEndFrame();
oslSyncFrame();
}//End of main loop
return 1;
}