-
many errors....bad....
I never thought implementing collision detection would cause this much trouble....I guess it does....
anyways, lets see if any of you can solve this problem.
I am making a Dragonball Z game in SDL, and while trying to implement collision detection, some problems have arisen.
Here is an excerpt from the main function of my code:
Code:
#include "sequence.h"
#include "map.h"
#include "player.h"
MAP_DTP *Map;
PLAYER_DTP *Player;
void LoadMap ( void )
{
Map = new MAP_DTP;
Map->LoadBackground ( "background_map.bmp" );
Map->LoadForeground ( "foreground_map.bmp" );
}
int main ( int argc, char *argv[] )
{
......................code..................
SDL_Event event;
Player = new PLAYER_DTP(GOKU);
LoadMap ();
while(1)
{
Map->DisplayMap(screen);
if ( Player->CheckForCollision(Map) ) {
Player->Act( STAND_STILL, Map );
}
Player->DisplayPlayer(screen);
SDL_Flip(screen);
SDL_FillRect(screen, 0, 0);
.......................code...............
}
}
Now, when I run this code, after I wait for a couple of seconds and it gives me this message:
"User breakpoint called from code at 0x77F7F570"
but i did not set any breakpoints...so i dont understand why its doing that....
Also, for some reason the map has also stopped displaying now...it wont display at all...i dont know why...
Here are an assortment of functions you might find helpfull in figuring this out:
Code:
void MAP_DTP::DisplayMap ( SDL_Surface *screen )
{
if(Background)
SDL_BlitSurface ( Background, Rect, screen, 0 );
if(Foreground)
SDL_BlitSurface ( Foreground, Rect, screen, 0 );
}
void MAP_DTP::LoadBackground ( char *filename )
{
Background = SDL_LoadBMP ( filename );
}
void MAP_DTP::LoadForeground ( char *filename )
{
Foreground = SDL_LoadBMP ( filename );
ForegroundBitMatrix = new int *[PLAYING_WIDTH];
for ( int x = 0; x < Rect->w; x++)
{
ForegroundBitMatrix[x] = new int[PLAYING_HEIGHT];
}
CONVERT_BOOLEAN ( Foreground, ForegroundBitMatrix );
//set the transparency value to RGB of (0,0,0).
SDL_SetColorKey ( Foreground, SDL_SRCCOLORKEY,
SDL_MapRGB ( Foreground->format, 0, 0, 0 ) );
}
PLAYER_DTP::PLAYER_DTP ( int characterName )
{
char *filearray[3];
CurAnimationIndex = 0;
CurPlayerX = 160;
CurPlayerY = 120;
CharacterAnimation = new ANIMATION_SEQUENCE_DTP *[ANIMATION_SEQUENCES_PER_CHAR];
for(int j = 0; j < ANIMATION_SEQUENCES_PER_CHAR; j++)
{
CharacterAnimation[j] = new ANIMATION_SEQUENCE_DTP;
}
switch (characterName)
{
case GOKU:
filearray[0] = "goku_f1.bmp";
filearray[1] = "goku_f2.bmp";
filearray[2] = "goku_f3.bmp";
CharacterAnimation[0]->Load(filearray, 3);
CharacterAnimation[0]->SetPosition(100, 100);
CharacterAnimation[0]->SetFrameRepeatRate(10);
CharacterAnimation[0]->SetTransparencyColor (0,0,0);
filearray[0] = "goku_f4.bmp";
filearray[1] = "goku_f5.bmp";
filearray[2] = "goku_f6.bmp";
CharacterAnimation[1]->Load(filearray, 3);
CharacterAnimation[1]->SetPosition(100, 100);
CharacterAnimation[1]->SetFrameRepeatRate(10);
CharacterAnimation[1]->SetTransparencyColor (0,0,0);
break;
}
}
void PLAYER_DTP::DisplayPlayer ( SDL_Surface *screen )
{
CharacterAnimation[CurAnimationIndex]->DrawCurrent(screen);
}
int PLAYER_DTP::CheckForCollision ( MAP_DTP *Map )
{
int **finalMatrix, **mapMatrix;
int cX = Map->GetX() + CurPlayerX; //get starting coords of rect
int cY = Map->GetY() + CurPlayerY;
int tempy = cY;
//size the matrix
mapMatrix = new int *[ MATRIX_WIDTH ];
for ( int x = 0; x < MATRIX_WIDTH; x++, cX++ )
{
mapMatrix[x] = new int [ MATRIX_HEIGHT ];
for ( int y = 0; y < MATRIX_HEIGHT; y++, cY++ )
{
mapMatrix[x][y] = Map->ForegroundBitMatrix[cX][cY];
}
cY = tempy;
}
// AND the matrixes together:
int isCollision = AND_DTP ( CharacterAnimation[CurAnimationIndex]->GetCurrentFrame()->bitMatrix,
mapMatrix, finalMatrix );
delete [] mapMatrix;
delete [] finalMatrix;
return isCollision;
}
void ANIMATION_SEQUENCE_DTP::Load ( char *filearray[], int arraySize )
{
Sequence = new GRAPHIC *[(const)arraySize];
numFrames = arraySize;
for(int x = 0; x < arraySize; x++)
{
Sequence[x] = new GRAPHIC;
Sequence[x]->Surface = SDL_LoadBMP ( filearray[x] );
Sequence[x]->bitMatrix = new int *[Sequence[x]->Surface->w];
for(int y = 0; y < Sequence[x]->Surface->w; y++)
{
Sequence[x]->bitMatrix[y] = new int[Sequence[x]->Surface->h];
}
CONVERT_BOOLEAN ( Sequence[x]->Surface,
Sequence[x]->bitMatrix );
//SDL_SetAlpha ( Sequence[x], SDL_SRCALPHA, 128);
}
}
void CONVERT_BOOLEAN ( SDL_Surface *surface, int ** &matrix )
{
matrix = new int *[surface->w];
for ( int x = 0; x < surface->w; x++)
{
matrix[x] = new int[surface->h];
for ( int y = 0; y < surface->h; y++)
{
if( getpixel(surface, x, y) > 0 )
matrix[x][y] = 1;
else matrix[x][y] = 0;
}
}
}
int AND_DTP ( int **lhs, int **rhs, int **myMatrix )
{
myMatrix = new int *[MATRIX_WIDTH];
int result = 0;
for ( int x = 0; x < MATRIX_WIDTH; x++)
{
myMatrix[x] = new int [MATRIX_HEIGHT];
for ( int y = 0; y < MATRIX_HEIGHT; y++)
{
//AND the bits together
myMatrix[x][y] = (lhs[x][y] & rhs[x][y]);
//if result = 0, get the latest ANDed bit as new result
if(!result) result = myMatrix[x][y];
}
}
return result;
}
If you want to see all the code, etc....email me...IM me....etc...thanx for any help you can give.
-
>I am making a Dragonball Z game
Copywrite Infraction?
-
I believe it's a DBZ clone of Liero, I've played Liero and it's cool. I'd like to see what this turns out like, :D