Sign in to follow this  

Unity Mirroring an SDL surface?

This topic is 3866 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This is some code I wrote years ago to load an image in SDL, mirror it on the x-axis, and make an OpenGL texture. Somehow it works fine except on images smaller than 256x256 or something.
GLuint loadTexture(string filename)
{
	SDL_Surface *imagesurface = IMG_Load(filename.c_str());
	GLubyte *pixels = (GLubyte *)imagesurface->pixels;
	GLuint texID;

	int width = imagesurface->w;
	int height = imagesurface->h;
	int bytesPerRow = imagesurface->format->BitsPerPixel / 8 * width;

	for (int y = 0; y < height / 2; y++)
	{
		for (int x = 0; x < bytesPerRow; x++)
		{
			int a = y * bytesPerRow + x;
			int b = (height - y) * bytesPerRow + x;

			GLubyte temp = pixels[a];
			pixels[a] = pixels[b];
			pixels[b] = temp;
		}
	}

	glGenTextures(1, &texID);
	glBindTexture(GL_TEXTURE_2D, texID);
	if (imagesurface->format->BitsPerPixel == 24)
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
	else
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);	

	SDL_FreeSurface(imagesurface);

	return texID;
}
EDIT: I are gud at internet code tags Can anyone see a problem I'm missing? I've had problems with this before, and some hero in the IRC channel showed me this library of helpful SDL functions he made. Unfortunately I can't remember who this helpful stranger (actually, I'm pretty sure he's a regular helpful awesome member of the community, I just can't remember him) is or what I did with the library, so I'm stuck with my own junk for now.

Share this post


Link to post
Share on other sites
I could do that, but I'd rather not have to if I can avoid it.

EDIT: Just realised I can mirror surfaces with SDL_gfx. Still, I'd like to know where I went wrong with trying to flip it manually.

[Edited by - Stukkm on May 19, 2007 8:23:33 AM]

Share this post


Link to post
Share on other sites

This topic is 3866 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this