GP2X Sprite Shows Up In Windows, But Not On Gp2x?!


motorollin

Member
Joined
Jul 31, 2007
Messages
163
I'm having problems with one of my enemy classes. When I run my game in Windows the enemy shows up. When I run it on the GP2X the enemy doesn't show. The game responds to collisions with the enemy even though the enemy is not visible.

As a test, I added a line of code to blit the enemy's sprite to the screen and this did show, so I know the file is being loaded correctly. I just can't figure this out! Can anyone shed any light?

CODE
class Green
{
private:
int frame, dx, dy;
bool run;

public:
int col_l, col_r, col_t, col_b, x, y, xVel, yVel;
Green();
void move();
void show();
void runaway( int x1, int y1, int x2, int y2 );
};

Green::Green()
{
x=rand() % 600 + 20;
y=rand() % 440 + 20;
xVel=greenspeed;
yVel=greenspeed;
run=false;
}

void Green::show()
{
frame++;

//Loop the animation
if( frame >= 6 )
{
frame = 0;
}

//Show the enemy
apply_surface( x - camera.x, y - camera.y, green, screen, &greenClips[ frame ] );
}

void Green::runaway( int x1, int y1, int x2, int y2 )
{
run=true;
int ddx = x1-x-(x1-x2)*4;
int ddy = y1-y-(y1-y2)*4;
int bdx = x1-x2;
int bdy = y1-y2;
int distd = sqrt(ddx*ddx + ddy*ddy)+0.001;
int distb = sqrt(bdx*bdx + bdy*bdy)+0.001;
if( distd < 100 )
{
ddx = -ddx/distd*30;
ddy = -ddy/distd*30;
ddx :+ bdx/distb*30;
ddy :+ bdy/distb*30;
dx = dx + ddx;
dy = dy + ddy;
}
}

vector<Green> greens;

void Bullet::move()
{

//The bullet gets moved
...

//Make the green enemies run away from the bullet
for ( int i=1; i<greens.size(); i++ )
{
int distx = x-greens.x;
int disty = y-greens.y;
double dist = sqrt( (distx*distx) + (disty*disty) );
if ( dist < 50 )
{
greens.runaway( x, y, dx, dy );
}
}
}

void Green::move()
{
if( !run )
{

if( x < playerx-(PLAYER_WIDTH/2) ) dx = greenspeed;
if( x > playerx-(PLAYER_WIDTH/2) ) dx = -greenspeed;
if( y < playery+(PLAYER_HEIGHT/2)-7 ) dy = greenspeed;
if( y > playery+(PLAYER_HEIGHT/2)-7 ) dy = -greenspeed;

}

run=false;

x+=dx;
y+=dy;

col_l = x - (GREEN_WIDTH / 2);
col_r = col_l + GREEN_WIDTH;
col_t = y - (GREEN_HEIGHT / 2);
col_b = col_t + GREEN_HEIGHT;
}



Extract from main loop:

CODE
for ( int i = 1; i<greens.size(); i++ )
{
greens.move();
}

for ( int i = 1; i < greens.size(); i++ )
{
if ( collision( myPlayer.col_l , greens.col_l, myPlayer.col_r , greens.col_r, myPlayer.col_t , greens.col_t, myPlayer.col_b , greens.col_b ) )
{
status=2;
loop=false;
}
for ( int j = 1; j<bullets.size(); j++ )
{
if ( collision( bullets[j].col_l , greens.col_l, bullets[j].col_r , greens.col_r, bullets[j].col_t , greens.col_t, bullets[j].col_b , greens.col_b ) )
{
greens.erase(greens.begin()+i);
bullets.erase(bullets.begin()+j);
}
}
}

for ( int i = 1; i < greens.size(); i++ ) greens.show();
 
Well this is weird. Through a laborious process of trial and error I have tracked the problem down to the "frame" variable in the Green::show() function. If I change the apply_surface() command to "apply_surface( x - camera.x, y - camera.y, green, screen, &greenClips[ 0 ] );" then it works on the GP2X - but obviously the sprite is not animated. I'll look in to this more, though I'm really surprised this problem affects the GP2X build but not the Windows build, from the same source! Also, there are other enemy classes with their own show functions, and they work fine with their own frame variable.
 
Ok, sorted. I added "frame=0;" in to Green:green() and it works on the GP2X now. Weird that I didn't need to do this for any of my other enemy classes :blink:
 
motorollin said:
Ok, sorted. I added "frame=0;" in to Green:green() and it works on the GP2X now. Weird that I didn't need to do this for any of my other enemy classes :blink:
You really MUST initialize any variable you use in your C/C++ code on any platform. Otherwise, the initial value may be anyone. I suppose that your Windows machine is plenty of memory, and it allocates space for the variable 'frame' in a not-previously-used memory position, but it may not be the case of the Gp2x.

Initialize your variables, Luke.
 
Last edited by a moderator:
As it was explained you forgot to initialise the variable. On the GP2X it could be any value unless you initialise it. On the PC it could be also any value. It depends on the compiler I think.
 
0 is a number just like any other, there's no reason any unitialized variable will have this value!
unless you use calloc of course ;)
 
Back
Top