#include "CRM32Pro.h"
#define EXAMPLE_VERSION "Example 8: Sprites Collision"
#define GFX_RESOURCE "data/gfx.dpf" // DPF with graphic resources
void RenderGraphics(int);
void RenderGraphicsGL(int);
void PrintHelp();
SDL_Surface *bg, *info, *backinfo, *surfCursor;
SDL_Rect inf, rCursor;
Uint32 last = 0, lastinfo = 0;
Uint8 bFlagCollision = 1;
int main(int argc,char *argv[])
{
Uint8 done = 0;
int DirX = 1, DirY = 1, SprX = 100, SprY = 100;
SDL_Event event;
PrintHelp();
if(CRM32Pro.
Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER) < 0)
{
ILogSystem.
Msg(
LOG_NORMAL,
" · [LOG] - Couldnīt initialize CRM32Pro: %s\n",SDL_GetError());
return 1;
}
CRM32Pro.
Config.
Title = EXAMPLE_VERSION;
CRM32Pro.Config.
Icon = IImage->
Load(GFX_RESOURCE,
"icono");
{
return 1;
}
bg = IImage->
Load(GFX_RESOURCE,
"background2");
info = IImage->
Load(GFX_RESOURCE,
"info");
inf.x = 6;inf.y = 6;inf.w = info->w;inf.h = info->h;
backinfo = IImage->
Load(GFX_RESOURCE,
"info");
fInfo->
Load(GFX_RESOURCE,
"InfoFont");
sprRabbit->
Load(GFX_RESOURCE,
"rabbit");
surfCursor = IImage->
Load(GFX_RESOURCE,
"Smiley");
if(surfCursor == NULL) exit(1);
rCursor.x = 100;rCursor.y = 110;rCursor.w = surfCursor->w;rCursor.h = surfCursor->h;
bFlagCollision = 1;
done = 0;
while(!done)
{
SprX = SprX + (2 * DirX);
SprY = SprY + (2 * DirY);
if((SprX + 64) > CRM32Pro.
screen->w) DirX = DirX * (-1);
if((SprY + 64) > CRM32Pro.
screen->h) DirY = DirY * (-1);
if(SprX < 2) DirX = DirX * (-1);
if(SprY < 2) DirY = DirY * (-1);
while(CRM32Pro.
Update(&event))
{
switch(event.type)
{
case SDL_KEYDOWN:
if(event.key.keysym.sym == SDLK_ESCAPE) done = 1;
if(event.key.keysym.sym == SDLK_F1) bFlagCollision = bFlagCollision * (-1);
break;
case SDL_MOUSEMOTION:
rCursor.x = event.motion.x - rCursor.w / 2;
rCursor.y = event.motion.y - rCursor.h / 2;
break;
case SDL_QUIT:
done = 1;
break;
break;
default:
break;
}
}
}
delete fInfo;
delete sprRabbit;
return 0;
}
void RenderGraphics(int bLogicUpdate)
{
char sTmp[16];
int isCollision = 0, nCollisions = 0;
SDL_BlitSurface(bg, NULL, CRM32Pro.
screen, NULL);
nCollisions = 0;
if(bFlagCollision == 1)
{
isCollision = sprRabbit->
Collision(surfCursor,&rCursor,-1,-1,1);
if(isCollision) nCollisions++;
}
SDL_BlitSurface(surfCursor, NULL, CRM32Pro.
screen, &rCursor);
SDL_BlitSurface(info,NULL,CRM32Pro.
screen,&inf);
if((lastinfo + 250) < ITimeSystem->
GetTime())
{
SDL_BlitSurface(backinfo,NULL,info,NULL);
if(bFlagCollision == 1) fInfo->
PutString(info,62,7,
"ON");
else
sprintf(sTmp,"%d",nCollisions);
}
}
void RenderGraphicsGL(int bLogicUpdate)
{
char sTmp[16];
int isCollision = 0, nCollisions = 0;
SDL_BlitSurface(bg, NULL, CRM32Pro.
screen, NULL);
nCollisions = 0;
if(bFlagCollision == 1)
{
isCollision = sprRabbit->
Collision(surfCursor,&rCursor,-1,-1,1);
if(isCollision) nCollisions++;
}
SDL_BlitSurface(surfCursor, NULL, CRM32Pro.
screen, &rCursor);
SDL_BlitSurface(backinfo,NULL,CRM32Pro.
screen,&inf);
sprintf(sTmp,"%d",nCollisions);
}
void PrintHelp()
{
char *sMsg;
sMsg = new char[1024];
strcpy(sMsg,"-----------------------------------------------------------\n");
strcat(sMsg," ");
strcat(sMsg,EXAMPLE_VERSION);
strcat(sMsg,"\n-----------------------------------------------------------\n\n");
strcat(sMsg," Press [ESCAPE] key to exit.\n");
strcat(sMsg," Press [F1] key to enable/disable collision detection.\n");
strcat(sMsg,"\n Runtime information is logged into 'SpriteCollision.log' file.\n");
strcat(sMsg,"\n");
#ifdef _WINDOWS
MessageBox(NULL,sMsg,"CRM32Pro SDK - HelpScreen",MB_OK);
#endif
#ifdef _LINUX
printf("CRM32Pro SDK - HelpScreen\n%s",sMsg);
#endif
delete sMsg;
}