Sorry to be bugging you guys with another question so soon... I've managed to get the sprite up and moving around the screen at 30 fps. My question now is how would I mirror the sprite? Instead of having him face the right, I want him to face the left. I've looked at the SDL documentation over a few times and attempted to try something with their putpixel and getpixel functions, but instead got a highly discolored sprite. Any ideas?
Wrong Forum It's ok, but if you're going to keep asking technical questions, then please put your future threads in the Game Development and Technical forum, ok? Thanks.
Fix your code There's no way to flip a surface vertically or horizontally, but it's trivial to implement with putpixel() and getpixel(). BTW, I'm not entirely sure about the relevance of this post in Indie Basics (it IS technical, but it's also very basic). But please use a more descriptive title for the thread. "Another SDL question" is almost meaningless. "How to mirror surfaces in SDL" is much better.
Mirror the image in your paint program instead, so you simply have two sprites: one facing left, the other facing right. It will double your file size, but you shouldn't have to worry about that until the game is finished.
There you go! With the copious amounts of memory in PC's you shouldn't really have a problem.Alternatively, use one of the many graphics libraries that are listed on the SDL site. All that work will already be done for you and much more.Why re-invent the wheel!! All the best Mark.
Assuming that you use a 32bit mode with 32bit sprites and 32bit stuff this will create a new surface with a horizontally and/or vertically flipped image: Code: SDL_Surface *newFlippedSurface(SDL_Surface *src, int fliph, int flipv) { int x, y; unsigned int *srcbuff, *dstbuff; /* assuming unsigned int is 32bit */ SDL_Surface *flipped = SDL_CreateRGBSurface(SDL_SWSURFACE, src->w, src->h, 32, src->format->Rmask, src->format->Gmask, src->format->Bmask, src->format->Amask); SDL_LockSurface(src); SDL_LockSurface(flipped); srcbuff = (unsigned int*)src->pixels; dstbuff = (unsigned int*)flipped->pixels; for (y=0;y<src->h;y++) for (x=0;x<src->w;x++) dstbuff[y*src->w + x] = srcbuff[(flipv?(src->h-y-1):y)*src->w + (fliph?(src->w-x-1):x)]; SDL_UnlockSurface(flipped); SDL_UnlockSurface(src); return flipped; } Passing 1 to fliph flips it horizontally and passing 1 to flipv flips it vertically (passing 1 in both flips it in both directions). Example (copy/pasted from the test app i wrote for the above function): Code: /* return the image flipped vertically */ flipped = newFlippedSurface(image, 1, 0); I hope that this little function helped you. It works by replacing pixel (x, y) in the destination image with pixel (sx, sy) from the source image. sx is width-x-1 if horizontal flipping is enabled or x if it is disabled and sy is height-y-1 if vertical flipping is enabled or y if it is disabled. Note: if you use 16bit mode with 16bit stuff, etc, just replace unsigned int with unsigned short and the number 32 in the call to SDL_CreateRGBSurface, to 16 and hope that it'll work since i haven't tested it, but i don't see why this won't work .
Sorry for posting in the wrong area. I'll be sure to make sure I'm in the right place before doing so again < Thanks for the responses. This really helps with the project I'm working on. You guys are great.
You'd be surprised at how many people with 128 MB or even 64 MB of RAM try to play Wild West Wendy and get an "out of memory" message. Not every casual player has a reasonable, let alone a copious, amount of memory. In fact I began implementing a flipped blitter for SDL so you don't even have to store a mirrored image in RAM...
Great idea! You should be able to do it just as fast as a normal blit too Also rotate 180 should be able to be done just as fast.
In fact I did implement vflip and hflip for 24 and 32 bit images, as fast as normal blits. I got bored before implementing 16 bit blits
There was talk on the SDL mailing list about someone building in flipped blitting into SDL. I don't know if it made it into CVS or not though.
I thought it might be, but I wasn't sure. How much work would it be to complete it and send the patch to the list? Pete
Making it work for 24/32 bits was easy - I offset the destination pointer and adjust the row skip and pixel skip to draw "backwards". This works with minor changes to all the blitters, including the MMX blitters. The problem with the 16 bit blitters is that they copy 32 bits at a time (2 pixels), so you'd have to not only reverse the dest pointer, but also swap the 16 bit halves. It's not terribly complicated, but I didn't feel like doing it. I can send you my current code if you want to complete it.
I can see where you're coming from. If doing it for 16 & 24 was enough for my project I wouldn't have dg too deep into the SDL source either. Thanks, but no it's OK, I've got IGE to work on! Pete