[SDL] SDL and KDevlop

Fredrik persson perrascout at linux.nu
Sun Sep 29 12:27:01 PDT 2002


Now that Sam has posted his nifty SDL->OpenGL surface
converter, the only remaining piece of the puzzle is
the image inverter.

For me, Ortho2D isn't always available since I'm
usually in 3D mode, and I frequently use pieces of
code that already assume normal OpenGL texture
coordinate conventions. (I've had some very
frightening results after integrating a Quake MD2
model loader into my stuff and the textures mapped to
all the wrong places.)

So here's the code I use to flip the surfaces. I'd be
happy to see any bug corrections or speed optimations
:)

-Eric


/* Will modify the image array so it is vertically
flipped.
 * This is intened to be a helper function to comply
with
 * OpenGL's y-coordinate system.
 * Returns -1 on failure, 0 on success
 * Warning: The original data will be modified.
 * Sample Usage: invert_image(image->pitch, image->h,
&image->pixels)
  */
static int invert_image(int pitch, int height, void*
image_pixels)
{
	int index;
	void* temp_row;
	int height_div_2;

	temp_row = (void *)malloc(pitch);
	if(NULL == temp_row)
	{
		SDL_SetError("Not enough memory for image
inversion");
		return -1;
	}
	/* if height is odd, don't need to swap middle row */
	height_div_2 = (int) (height * .5);
	for(index = 0; index < height_div_2; index++) 	{
		/* uses string.h */
		memcpy((Uint8 *)temp_row,
			(Uint8 *)(image_pixels) +
			pitch * index,
			pitch);

		memcpy(
			(Uint8 *)(image_pixels) + 
			pitch * index,
			(Uint8 *)(image_pixels) + 
			pitch * (height - index-1),
			pitch);
		memcpy(
			(Uint8 *)(image_pixels) + 
			pitch * (height - index-1),
			temp_row,
			pitch);
	}
	free(temp_row);
	return 0;
}

/* Convenience wrapper for invert_image */
int SDL_InvertSurface(SDL_Surface* image)
{
	if(NULL == image)
	{
		SDL_SetError("Surface is NULL");
		return -1;
	}
	return( invert_image(image->pitch, image->h, 
			image->pixels) );
}




__________________________________________________
Do you Yahoo!?
Yahoo! News - Today's headlines
http://news.yahoo.com




More information about the SDL mailing list