Jump to content
  • Advertisement
Sign in to follow this  
X Abstract X

SDL Surface Pixel Data Access Violation

This topic is 2989 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

I'm trying to write a function to vertically flip an SDL_Surface but I get an access violation as soon as I try to read or write to the pixel data of the surface. Can anyone see the reason for this?

edit: removed bad code, problem has been solved.

Thanks.

[Edited by - X Abstract X on July 16, 2010 6:40:28 AM]

Share this post


Link to post
Share on other sites
Advertisement
1. The size of the surface->pixels array is actually
surface->w * surface->h * surface->format->BytesPerPixel bytes. Your code assumes that BytesPerPixel is 4, since your casting to unsigned int. If your using anything other than a 32 bit image format, the code will crash.
2. Also, by running the loop up to maxIndex you end up flipping the image twice, since for any i to maxIndex-i swap, you're also doing an maxIndex-i to i swap.

Try something like this instead

unsigned int temp = 0;
unsigned int bpp = surface->format->BytesPerPixel;

unsigned int maxIndex = (surface->w * surface->h * bpp) - bpp;
unsigned char* pixels = (unsigned char*)(surface->pixels);

for (unsigned int i = 0; i < (maxIndex +1)/2; i+= bpp) {
memcpy(&temp, &pixels, bpp);
memcpy(&pixels, &pixels[maxIndex - i], bpp);
memcpy(&pixels[maxIndex-i], &temp, bpp);
}



3. Also, your code doesn't just flip the image vertically, but insteat it flips the image around its center point. In order to make a vertical flip, you must swap whole lines instead of individual pixels. I'll let you figure out how to implement it.

Share this post


Link to post
Share on other sites
Hey there. I really appreciate you taking your time to help me out but I managed to fix this problem 2 days ago. I should have posted when I solved it, I suppose.

Like you said, I was trying to operate on a 24 bit surface and my algorithm was also wrong.

+Rating because your post was amazing and would have solved all my problems at the time.

Share this post


Link to post
Share on other sites
You should never delete your code just because you might think your problem has been solved: It might still be there. But even if this is not the case, others can't learn from your problem anymore.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!