Code:
#include <oslib/oslib.h>
#include <stdio.h>
//Necessary to create eboot
PSP_MODULE_INFO("OSLib Sample", 0, 1, 1);
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU);
//declaration of the pointers of our images
OSL_IMAGE *background,*crosshair,*bg,*target[5],*gun,*hud;
//variables
#define RIGHT 0
float j=0,sty,collision=0,collide=0;
int counter,target_choice=0,stretchx,stretchy,width,height;
int gun_march=0,gun_position;
int i;
int p;
void Hud();
void GAME();
void keys();
void target_AI();
void GunAnimate();
bool collisionHS( float targetposX, float targetposY, float crosshairposX, float crosshairposY)
{
int collision,collide=0;
collision = 0;
int *strhx=&stretchx;
target[i]->y=targetposY;
target[i]->x=targetposX;
crosshair->x=crosshairposX;
crosshair->y=crosshairposY;
float targetwidth = 35+*strhx;
float targetheight = sty;
float crosshairwidth = 10;
float crosshairheight = 10;
if ((targetposX + targetwidth > crosshairposX) &&
(targetposX < crosshairposX + crosshairwidth) &&
(targetposY + targetheight+20 > crosshairposY) &&
(targetposY < crosshairposY + crosshairheight))
{
collision = 1; collide=2;
}
return collision;
}
bool collisionBD( float targetposX1, float targetposY1, float crosshairposX1, float crosshairposY1)
{
int collision1;
collision1 = 0;
int *strhx1=&stretchx;
target[i]->y=targetposY1;
target[i]->x=targetposX1;
crosshair->x=crosshairposX1;
crosshair->y=crosshairposY1;
float targetwidth1 = 35+*strhx1;
float targetheight1 = sty;
float crosshairwidth1 = 10;
float crosshairheight1 = 10;
if((targetposX1 + targetwidth1 > crosshairposX1) &&
(targetposX1 < crosshairposX1 + crosshairwidth1) &&
(targetposY1+25 + targetheight1+50 > crosshairposY1) &&
(targetposY1+25 < crosshairposY1 + crosshairheight1)){ collision1 = 1;}
return collision1;
}
int GetRandomNum(int lo, int hi)
{
SceKernelUtilsMt19937Context ctx;
sceKernelUtilsMt19937Init(&ctx, time(NULL)); //SEED TO TIME
u32 rand_val = sceKernelUtilsMt19937UInt(&ctx);
rand_val = lo + rand_val % hi;
return (int)rand_val;
}
void keys(){
//crosshair movement
if(osl_keys->held.right)crosshair->x+=4;
if(osl_keys->held.left)crosshair->x-=4;
if(osl_keys->held.up)crosshair->y-=4;
if(osl_keys->held.down)crosshair->y+=4;
//
if (osl_keys->held.circle){ target[i]->stretchX++;
target[i]->stretchY++;
stretchx++;
stretchy++;
sty+=.80;} //zoom target
if (osl_keys->held.cross){ target[i]->stretchX--;
target[i]->stretchY--;
stretchx--;
stretchy--;
sty-=.80;} //zoom target
}
void GAME(){
///darw images
oslDrawImage(bg);
oslDrawImage(target[0]);
oslDrawImage(target[1]);
oslDrawImage(crosshair);
oslDrawImage(hud);
oslDrawImage(gun);
////keys
keys();
////
GunAnimate();
//
Hud();
//
target_AI();
}
void target_AI(){
//
if (collisionHS( target[i]->x , target[i]->y, crosshair->x, crosshair->y ))oslPrintf_xy(250,60,"headshot");
if (collisionBD( target[i]->x , target[i]->y, crosshair->x, crosshair->y ))oslPrintf_xy(250,70,"bodyshot");
}
void GunAnimate()
{
//Moves the sprite constantly with no static position
oslSetImageTileSize(gun,(gun_march * 36),gun_position,36,60);
//resets the sprite movement in that row
if(crosshair->x>145 && crosshair->x<345 && gun->y>86)gun_march = 0;
if(crosshair->y<86 && crosshair->x<145)gun_march = 1;
if(crosshair->x>345 && crosshair->y<86)gun_march = 2;
if(crosshair->x<145 && crosshair->y>86)gun_march = 3;
if(crosshair->x>345 && crosshair->y>86)gun_march = 4;
}
void Hud(){
//
if(hud->x<0)hud->x+=8;
}
int main(int argc, char **argv)
{
//Initialization of the Oslib library
oslInit(0);
//Initialization of the graphics mode
oslInitGfx(OSL_PF_8888, 1);
oslInitConsole(); //Text
oslSetTransparentColor(RGB(255,0,255));
//loads our images into memory
crosshair = oslLoadImageFile("crosshair.png", OSL_IN_RAM, OSL_PF_5551);
bg = oslLoadImageFile("bg.png", OSL_IN_RAM, OSL_PF_5551);
target[0] = oslLoadImageFile("target.png", OSL_IN_RAM, OSL_PF_5551);
if(target[0]==NULL) oslFatalError("t didn't load in");
target[1] = oslLoadImageFile("target1.png", OSL_IN_RAM, OSL_PF_5551);
if(target[1]==NULL) oslFatalError("t1 didn't load in");
target[2] = oslLoadImageFile("target2.png", OSL_IN_RAM, OSL_PF_5551);
if(target[2]==NULL) oslFatalError("t2 didn't load in");
target[3] = oslLoadImageFile("target3.png", OSL_IN_RAM, OSL_PF_5551);
if(target[3]==NULL) oslFatalError("t3 didn't load in");
target[4] = oslLoadImageFile("target4.png", OSL_IN_RAM, OSL_PF_5551);
if(target[4]==NULL) oslFatalError("t4 didn't load in");
target[5] = oslLoadImageFile("target5.png", OSL_IN_RAM, OSL_PF_5551);
if(target[5]==NULL) oslFatalError("t5 didn't load in");
gun = oslLoadImageFile("gun.png", OSL_IN_RAM, OSL_PF_5551);
hud = oslLoadImageFile("hud.png", OSL_IN_RAM, OSL_PF_5551);
//positions
crosshair->x =100;
crosshair->y =150;
target[0]->x =200;
target[0]->y =60;
target[1]->x =250;
target[1]->y =120;
gun->x =300;
gun->y =220;
hud->x =-200;
hud->y =237;
//for loop
for(p=0; p < 6; p++ )i=p;
//main while loop
while (!osl_quit)
{
//To be able to draw on the screen
oslStartDrawing();
//Clear the screen
oslCls();
//initiate the PSP's buttons
oslReadKeys();
///
GAME();
//Ends drawing mode
oslEndDrawing();
//Synchronizes the screen
oslSyncFrame();
}
//Terminate the program
oslEndGfx();
oslQuit();
return 0;
}
I tried this and the collision only worked for one of my targets(target[1])and not target[0]