A bit verbose, but here's a complete example using only the code shown...
#include "DarkGDK.h"
// Globals for illustration...
const int BUILDING_ID=1;
const int MISSILE_ID=2;
const int NUMBER_OF_MISSILES=10;
const int BUILDING_X=200;
const int BUILDING_Y=200;
const int MAX_COLLISIONS=3;
// Forward declarations...
void CreateResources(void);
void FreeResources(void);
void DarkGDK(void)
{
bool key_pressed=false;
char fps[20]="";
char collision_text[20]="";
int collisions=0;
dbSyncOn();
dbSyncRate(0);
dbDrawSpritesLast();
// Create the sprites...
CreateResources();
while (LoopGDK())
{
//dbCLS(0);
// Handle keyboard input...
if (dbSpaceKey())
{
if (!key_pressed)
{
// Check all missiles to see if one is available for use...
for (int x=0;x<NUMBER_OF_MISSILES;x++)
{
if (dbSpriteVisible(x+MISSILE_ID))
continue; // If it's already in use, try the next...
//******************************************************
// We found an available missile, shoot it now...
//******************************************************
// Create a new missile...
int posx, posy;
while (true)
{
posx=dbRND(dbScreenWidth()-1);
posy=dbRND(dbScreenHeight()-1);
dbSprite(x+MISSILE_ID,posx,posy,MISSILE_ID);
// If we haven't created a shot already hitting the building, then we're done...
if (!dbSpriteCollision(BUILDING_ID,x+MISSILE_ID))
break;
}
// Point the missile at the building and show it...
float angle=dbWrapValue(dbATANFULL(dbABS((float)posx-dbSpriteX(BUILDING_ID)),
dbABS((float)posy-dbSpriteY(BUILDING_ID))));
dbRotateSprite(x+MISSILE_ID, angle);
dbShowSprite(x+MISSILE_ID);
key_pressed=true;
break; // Missiles away!
}
}
}
else if (key_pressed)
{
key_pressed=false;
}
// Update the existing missiles...
for (int x=0;x<NUMBER_OF_MISSILES;x++)
{
int missile=x+MISSILE_ID;
int curx=dbSpriteX(missile);
int cury=dbSpriteY(missile);
if (dbSpriteVisible(missile))
dbMoveSprite(missile, 1.0f);
if (curx<0 || curx>dbScreenWidth() ||
cury<0 || cury>dbScreenHeight())
dbHideSprite(missile);
if (dbSpriteCollision(BUILDING_ID, missile))
{
// Remove the missile that hit the building...
dbSprite(missile,0,0,MISSILE_ID);
dbHideSprite(missile);
// Max collisions detected...?
if (++collisions>=MAX_COLLISIONS)
{
// reset for next round...
collisions=0;
dbHideSprite(BUILDING_ID);
for (int y=0;y<NUMBER_OF_MISSILES;y++)
dbHideSprite(y+MISSILE_ID);
dbText(0, 40, "BUILDING DESTROYED");
dbText(0, 60, "Press any key to continue...");
dbSync();
dbWaitKey();
dbShowSprite(BUILDING_ID);
}
}
}
// Display the frame-rate for visual reference...
sprintf_s(fps, 20, "FPS: %d", dbScreenFPS());
dbText(0,0,fps);
sprintf_s(collision_text,20,"Colisions: %d",collisions);
dbText(0,20,collision_text);
dbSync();
}
// Delete the sprites...
FreeResources();
return;
}
void CreateResources(void)
{
int width, height, size;
int color, pos;
// Create the Building...
width=50;
height=50;
size=sizeof(DWORD) + // width...
sizeof(DWORD) + // height...
sizeof(DWORD) + // depth...
(sizeof(DWORD) * width * height);
color=dbRGB(255,0,255); // magenta (pink)...
pos=0;
dbMakeMemblock(BUILDING_ID, size);
dbWriteMemblockDword(BUILDING_ID, pos, width); pos+=sizeof(DWORD);
dbWriteMemblockDword(BUILDING_ID, pos, height); pos+=sizeof(DWORD);
dbWriteMemblockDword(BUILDING_ID, pos, 32); pos+=sizeof(DWORD);
for (;pos<size;pos+=sizeof(DWORD))
dbWriteMemblockDword(BUILDING_ID, pos, color);
dbMakeImageFromMemblock(BUILDING_ID, BUILDING_ID);
dbSprite(BUILDING_ID, BUILDING_X, BUILDING_Y, BUILDING_ID);
// Create the Missiles...
width=10;
height=10;
size=sizeof(DWORD) + // width...
sizeof(DWORD) + // height...
sizeof(DWORD) + // depth...
(sizeof(DWORD) * width * height);
color=dbRGB(255,255,255); // white...
pos=0;
dbMakeMemblock(MISSILE_ID, size);
dbWriteMemblockDword(MISSILE_ID, pos, width); pos+=sizeof(DWORD);
dbWriteMemblockDword(MISSILE_ID, pos, height); pos+=sizeof(DWORD);
dbWriteMemblockDword(MISSILE_ID, pos, 32); pos+=sizeof(DWORD);
for (;pos<size;pos+=sizeof(DWORD))
dbWriteMemblockDword(MISSILE_ID, pos, color);
dbMakeImageFromMemblock(MISSILE_ID, MISSILE_ID);
for (int x=0; x<NUMBER_OF_MISSILES; x++)
{
dbSprite(x+MISSILE_ID, 0, 0, MISSILE_ID);
dbHideSprite(x+MISSILE_ID);
}
}
void FreeResources(void)
{
int pos, max_check=1+NUMBER_OF_MISSILES;
for (pos=1;pos<=max_check;pos++)
{
if (dbImageExist(pos))
dbDeleteImage(pos);
if (dbSpriteExist(pos))
dbDeleteSprite(pos);
if (dbMemblockExist(pos))
dbDeleteMemblock(pos);
}
}
This sample creates its own sprites so nothing else needs to be included. Simply: copy, paste, run and trace through the code to get a better understanding.
Regards,
JTK