[SDL Problem] Mirrored sprite blitting

Started by
6 comments, last by PnP Bios 18 years, 8 months ago
Hi, I'm starting with SDL and I have a question. How can I perform a fast blit of a sprite mirrored?
Advertisement
You can always just create a simple mirrored image in a paint program. Just as quick and easy.
There's no such function in current SDL API, and it's not even planned in the future versions, as David Olofson explains:

Quote:The problem is that anything added to the SDL API must also be
supported on all other backends - not just the glSDL-on-steroids of
the future SDL 1.3. That means software fallbacks, that are either
too slow for any form of real time use, or that use shortcuts that
make some artwork look horrible, or both. Adding just a few of these
features means opening up a gigantic can of worms with quality/speed
conflicts, countless special cases to optimize - and still, all you
get is a slow and/or horrible looking fallback for users who don't
have accelerated OpenGL or Direct3D/DirectGraphics.

Viable or not? Well, it's going to be more work than adding native s/w
rendering to a single game... (Can't cut any corners, since we have
no idea what kind of data people will throw at us.) It would be a
cool thing to have, but who will hack, debug and optimize all that
s/w blitter code? :-)


So, if you want to have mirrored sprites:

1. Do what Ekim_Gram suggested, or
2. Use OpenGL

If you must mirror those sprites in realtime... than, AFAIK, OpenGL is the only way.
Instead of blitting, you could always reverse a surface and then blit it.

SDL_Surface contains:
SDL_PixelFormat *format; /* Read-only */
int w, h; /* Read-only */
void *pixels; /* Read-write */

It would be simple to access *pixel, reverse each line and then blit the reversed surface. swap() is already built into the stl and pretty fast. If speed is a concern, you should probably load two of the same image during loadtime and the mirrored one.

The only way I know to do this in realtime is to set *srcrect in SDL_Blit() one pixel wide and your entire surface high and move the *srcrect in along the x axis in a positive direction while moving *dstrect in the negitive direction along the x axis. If you rarely mirror a sprite, this might be work out for you, although I'd prefer the my first suggestion.
I'm making an asteroid/trader game with LOTS of spinning objects. I tried the library SGE, which might suit your purposes, but it is kind of difficult to use, I found, and relatively slow. I wound up using OpenGL/SDL, which allows me to utilize hardware rotations. It wound up being a faster, more elegant solution that allows me to do more with my game. That's my suggestion.
my siteGenius is 1% inspiration and 99% perspiration
Well, maybe loading a mirrored sprite is the most easy solution.
Thanks to everybody for the help :)
If you want to mirror an image, take a look at my post here. I'm in the process of updating my webpages, so that image no longer exists, but you should be able to test it out yourself and see if it's what you are after. Note that you will not want to mirror in real time, rather mirror once, and store that image and just use that thoughout. Note that you will need to SDL_FreeSurface the surface returned by those functions.
If you want to use OpenGL, but don't feel like totaly destroying your codebase, take a look at my library, hxRender. It uses OpenGL ontop of an SDL Context, so you can keep all your input and sound code. Check it out.

All you would have to do for a mirrored sprite is define a negitive width and height for the w and h parameters of the destination rectangle.
HxRender | Cornerstone SDL TutorialsCurrently picking on: Hedos, Programmer One

This topic is closed to new replies.

Advertisement