So I have yet another question… surprise! So what I’m attempting to do out
of boredom is some simple 2d bump mapping effects but I seem to be having some
problems with copying surfaces (and I’m probably going about this all wrong
though it worked for 8 bit surfaces). Basically I have a 24bit surface,
‘surface’, created from SetVideoMode and a 24 bit surface, ‘color_map’,
created from IMG_Load (and is a png). I’ll be creating the bump map on a pixel
by pixel basis so I need to be able to copy single pixels to ‘surface’. I had
some problems so I decided to try and do just a pixel by pixel copy from
’color_map’ to ‘surface’, Flip the surface, and repeat. Both surfaces are
320x200 and here’s the behavior I’ve seen.
After 1 full pass of both surfaces, color_map is successfully copied to
surface and displayed correctly.
After the second pass, it seems that memory is being accessed that shouldn’t
be and garbage gets displayed about a third down the image and then the image
is fine. This is how the image stays for all the rest of the passes.
OR
The program segfaults at row 150. (I have two versions that are almost
identical and one continues drawing the other segfaults. I’m including the
segfaulting one).
Below I’ve included my code. The printfs are to determine where things go
screwy. Basically it prints out the pixel data for ‘surface’ and 'color_map’
before and after the copy. On the first pass ‘surface’ should be zero before
the copy and the same as ‘color_map’ after the copy. On all other passes,
‘surface’ should equal ‘color_map’ before and after the copy. Things seem to
go screwy around row 150 where ‘surface’ before the copy starts having data
which shouldn’t happen the first pass. And at row 200, which would be row 0 of
the second pass, both surfaces seem to have no data. The printf with "Frame:"
in it, just prints which row it’s currently on. Sorry for the drawn out
details, but I figured it’d help. Thanks!
#include <iostream.h>
#include <stdlib.h>
#include <math.h>
#include “SDL.h”
#include “SDL_image.h”
SDL_Surface *surface;
int main( )
{
int videoFlags;
if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
return 1;
videoFlags = SDL_DOUBLEBUF;
videoFlags |= SDL_HWPALETTE;
videoFlags |= SDL_HWSURFACE;
videoFlags |= SDL_HWACCEL;
surface = SDL_SetVideoMode( 320, 200, 24, videoFlags );
if ( !surface )
return 1;
SDL_Surface *color_map = IMG_Load( "ged3.png" );
Uint32 frameCount = 0;
if ( SDL_MUSTLOCK( surface ) )
SDL_LockSurface( surface );
Uint32 *surface_p = ( Uint32* )surface->pixels;
if ( SDL_MUSTLOCK( color_map ) )
SDL_UnlockSurface( color_map );
if ( SDL_MUSTLOCK( color_map ) )
SDL_LockSurface( color_map );
Uint32 *color_p = ( Uint32* )color_map->pixels;
if ( SDL_MUSTLOCK( surface ) )
SDL_UnlockSurface( surface );
bool done = false;
while ( !done )
{
for ( int j = 0; j < 200; j++ )
{
printf( "Frame: %u\n", frameCount );
if ( SDL_MUSTLOCK( surface ) )
SDL_LockSurface( surface );
for ( int i = 0; i < 320; i++ )
{
unsigned int pos = j * 320 + i;
printf( "Before s: %u c: %u", surface_p[pos],
color_p[pos] );
surface_p[pos] = color_p[pos];
printf( " After s: %u c: %u i: %d j: %d\n",
surface_p[pos], color_p[pos], i, j );
}
if ( SDL_MUSTLOCK( surface ) )
SDL_UnlockSurface( surface );
SDL_Flip( surface );
frameCount++;
SDL_Event event;
while ( SDL_PollEvent( &event ) )
{
switch ( event.type )
{
case SDL_QUIT:
done = true;
break;
case SDL_KEYDOWN:
if ( event.key.keysym.sym == SDLK_ESCAPE )
{
done = true;
printf( "DONE!\n" );
}
break;
}
}
}
}
SDL_FreeSurface( color_map );
SDL_Quit( );
return 0;
}–
Ti Leggett
leggett at eecs.tulane.edu