Seg fault problem

hello, sorry about the last question. well i have the g_game() function
that faults and sets off the damned parachute. I think it has something
to do
with the pollevent be
cause it faults when i hit a direction button. here do it be:

void g_game(void) {
while(!quit) {
while(SDL_PollEvent(&event)) {
switch(event.type) {
case SDL_KEYDOWN:
switch(event.key.keysym.sym) {
case SDLK_LEFT:
x_vel = -1;
break;
case SDLK_RIGHT:
x_vel = 1;
break;
case SDLK_UP:
y_vel = -1;
break;
case SDLK_DOWN:
y_vel = 1;
break;
default:
break;
}
break;
case SDL_KEYUP:
switch( event.key.keysym.sym ){
case SDLK_LEFT:
if( x_vel < 0 )
x_vel = 0;
break;
case SDLK_RIGHT:
if( x_vel > 0 )
x_vel = 0;
break;
case SDLK_UP:
if( y_vel < 0 )
y_vel = 0;
break;
case SDLK_DOWN:
if( y_vel > 0 )
y_vel = 0;
break;
default:
break;
}
break;
case SDL_QUIT:
quit=1;
break;
default:
break;
}
}
player.rect.x += x_vel;
player.rect.y += y_vel;
SDL_BlitSurface(tile_image[tile[ox][oy].image], NULL,
screen, &tile[ox][oy].rect);
SDL_BlitSurface(player.image, NULL, screen, &player.rect);
ox = player.rect.x;
oy = player.rect.y;
}

so long and thanks for all the fish.

player.rect.x += x_vel;
player.rect.y += y_vel;
SDL_BlitSurface(tile_image[tile[ox][oy].image], NULL,
  screen, &tile[ox][oy].rect);
SDL_BlitSurface(player.image, NULL, screen, &player.rect);
ox = player.rect.x;
oy = player.rect.y;

}

Make sure that your image is being blitted entirely into the screen
surface. If you have a (hypothetical) screen surface of 10 pixels by 10
pixels, and you blit an image of 5x5 pixels to coordinates (6, 6), then
you’ve written past the edge of the surface, which may distort your
blitted image, or may cause a segfault, depending on where you wrote to
memory.

Perhaps change this:

player.rect.x += x_vel;
player.rect.y += y_vel;

To this:

 if (player.rect.x + x_vel < screen->w)
     player.rect.x += x_vel;

 if (player.rect.y + y_vel < screen->h)
     player.rect.y += y_vel;

…if you want to move the image partially off of the screen, it gets a
little more complex.

–ryan.

Make sure that your image is being blitted entirely into the screen
surface. If you have a (hypothetical) screen surface of 10 pixels by 10
pixels, and you blit an image of 5x5 pixels to coordinates (6, 6), then
you’ve written past the edge of the surface, which may distort your
blitted image, or may cause a segfault, depending on where you wrote to
memory.

no need - BlitSurface always clips to the surface boundaries (or to the
destination surface clip rectangle, whichever is smaller)

no need - BlitSurface always clips to the surface boundaries (or to the
destination surface clip rectangle, whichever is smaller)

It does?

Uh, disregard what I said. :slight_smile:

–ryan.

It doesn’t look like it’s correct. You have player.rect apparently tracking
the player’s screen coordinates. You have tile_image[tile[ox][oy].image],
which looks like a tile-based system. But is each tile only 1 pixel big?
Probably not, but it looks as though it would have to be with that code. If
you are using a standard fixed-size grid style tile based system, then you
aren’t going about it correctly. I can help, though, if you want. Email me
directly, though, since that would be off-topic for here.

Also, your code is very reckless as it is. Your tile double arrays have some
finite range, but you aren’t doing any checking whatsoever to ensure that ox
and oy are within the acceptable range. This could be the source of your seg
faults.On Monday 21 May 2001 09:45, you wrote:

player.rect.x += x_vel;
player.rect.y += y_vel;
SDL_BlitSurface(tile_image[tile[ox][oy].image], NULL,
  screen, &tile[ox][oy].rect);
SDL_BlitSurface(player.image, NULL, screen, &player.rect);
ox = player.rect.x;
oy = player.rect.y;

}

— “Ryan C. Gordon” wrote:

no need - BlitSurface always clips to the surface
boundaries (or to the
destination surface clip rectangle, whichever is
smaller)

It does?

Uh, disregard what I said. :slight_smile:

From what I’ve noticed, it does - however something
that’s bit me a few times is that SDL_UpdateRects()
/doesn’t/ clip to the edge of the screen. I’ve run
into problems when I thought it did :slight_smile: Nothing too
difficult to deal with, but something to keep in mind.

Then again, I’ve been without internet access since
just after 1.2.0 came out, and it’s quite possible
everything’s changed since to the point where SDL is a
Personal Finance program, so my warning may be
irrelevant :)=====

-Roger Ostrander


Do You Yahoo!?
Yahoo! Auctions - buy the things you want at great prices
http://auctions.yahoo.com/

no need - BlitSurface always clips to the surface
boundaries (or to the
destination surface clip rectangle, whichever is
smaller)

It does?

Uh, disregard what I said. :slight_smile:

From what I’ve noticed, it does - however something
that’s bit me a few times is that SDL_UpdateRects()
/doesn’t/ clip to the edge of the screen. I’ve run
into problems when I thought it did :slight_smile: Nothing too
difficult to deal with, but something to keep in mind.

BlitSurface clips, UpdateRects does not. This is on purpose, so that
you don’t have to clip twice, since a blit is often followed immediately
by an update.

Then again, I’ve been without internet access since
just after 1.2.0 came out, and it’s quite possible
everything’s changed since to the point where SDL is a
Personal Finance program, so my warning may be
irrelevant :slight_smile:

Heheh. SDL 1.3, Personal Finance Multimedia Terminal Library

See ya!
-Sam Lantinga, Lead Programmer, Loki Software, Inc.

From what I’ve noticed, it does - however something
that’s bit me a few times is that SDL_UpdateRects()
/doesn’t/ clip to the edge of the screen. I’ve run
into problems when I thought it did :slight_smile: Nothing too
difficult to deal with, but something to keep in mind.

right. I’ll add a note of this to the docs