Sign in to follow this  
zen_phoenix

SDL Surface Blit : Horizontal Flip

Recommended Posts

zen_phoenix    122
Hello there. I am currently using SDL for a project of mine and am stuck looking for the best solution to what I need. I am developing a fighting game, similar to the style of Mortal Kombat and am at the point where I am defining character movement. I have all the drawing I need except the case there a character is facing another direction. I would prefer not duplicating the images I have so that there are two sets: facing left and facing right. The SDL documentation seems to have no solutions for blitting tricks with the rectangles (the width and height of SDL rectangles are unsigned ints and so I cannot do that). My current solution would be to use SDL_gfx and do a zoom of a negatice unit scale on the x-axis which should flip it for me however I fear this may be an inefficient method. Any other lightweight solutions out there? Perhaps there is something I am missing that is already built into SDL. Anything would be appreciated. Thank you.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Not that I know of (its been a while since I used SDL on it's own).
Quote:
I would prefer not duplicating the images I have so that there are two sets: facing left and facing right

How do you mean? you don't want to open in an image editor and mirror them or you dont want two sets?
If you dont mind two sets, at load time reverse the bytes in the rows of the image making sure you don't mess the rgb/rgba values up, of course.

Share this post


Link to post
Share on other sites
zen_phoenix    122
I am aiming at having only one set of images and doing a flip whenever necessary. When I have done blits before using a different library, I was able to set the rectangles width and height with negative values so that the pixels were copied in a reversed order. This is not an option in SDL.

Falling back on creating two sets would inflate the memory footprint significantly.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Quote:
Falling back on creating two sets would inflate the memory footprint significantly.

Well do it the clever way then and have an image resource manager which holds the textures and uses something like a shared pointer, handing out copies when requested and if its not loaded then it loads it itself, rather than having many copies of the same image set. Thus reducing the memory footprint and doing away with any mirroring in a render loop.

Share this post


Link to post
Share on other sites
zen_phoenix    122
Quote:
Well do it the clever way then and have an image resource manager which holds the textures and uses something like a shared pointer, handing out copies when requested and if its not loaded then it loads it itself, rather than having many copies of the same image set. Thus reducing the memory footprint and doing away with any mirroring in a render loop.


However clever this approach would be, it comes with a significant flaw. This would cause loading time whenever a desired frame is not available in memory. So, imagine playing Street Fighter and the first minute or so of any fight you are confronted with pauses in the animation where the system is fetching frames from the hard disk until you explore all of the characters animation. Then again for when you jump over your opponent, at which point both characters are causing load time. This is not an acceptable approach. =(

What I am looking for is a way to flip an image stored in memory when it is time to do a blit. I am close to coming to the conclusion that I will either have to make my own blit function which will reverse the order that columns are drawn or fiddle around with zoomSurface from sdl_gfx and just take the hit in performance, or do both and compare my performances and go with the best solution.

Unless somebody else can come in and save the day? =D

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Quote:
This is not an acceptable approach. =(

Depending on the amount of images required for a level I would agree, but then I never said to load the images during the playing time did I?

Share this post


Link to post
Share on other sites
Foot Soldier    124
I've encountered the same problem, Zen, and have gotten around it by flipping the entire sprite sheet of the character right when I load it, then drawing from either the regular, or flipped frame depending on which way they face. Here is the tidbit of code I used to account for each cell being in a different X/Y position then the regular image:

if(direction == P_LEFT)
src.x = currentFrame->x;
if(direction == P_RIGHT)
src.x = image->w - currentFrame->x - currentFrame->w;


So when their direction is right, it subtracts the current cells width from the current frames x position, then from the images width to get the new x position of the cell in the sprite sheet.

I actually prefer flipping the entire image just once when loaded, because then the draw function doesn't need to play around with flipping images. If you store each cell in it's own file, the same principle still applies, just do it for each cell. The extra memory is negligible.

Share this post


Link to post
Share on other sites

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