another SDL question

Discussion in 'Game Development (Technical)' started by esrix, Aug 16, 2005.

  1. esrix

    Original Member

    Joined:
    Aug 11, 2005
    Messages:
    143
    Likes Received:
    0
    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?
     
  2. soniCron

    Indie Author

    Joined:
    May 4, 2005
    Messages:
    3,664
    Likes Received:
    0
    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.
     
  3. ggambett

    Moderator Original Member Indie Author

    Joined:
    Jul 26, 2004
    Messages:
    1,982
    Likes Received:
    6
    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.
     
  4. mahlzeit

    Original Member

    Joined:
    Sep 26, 2004
    Messages:
    852
    Likes Received:
    0
    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.
     
  5. sparkyboy

    Original Member

    Joined:
    Mar 3, 2005
    Messages:
    346
    Likes Received:
    0
    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.
     
  6. Bad Sector

    Original Member

    Joined:
    May 28, 2005
    Messages:
    2,742
    Likes Received:
    5
    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 :).
     
  7. esrix

    Original Member

    Joined:
    Aug 11, 2005
    Messages:
    143
    Likes Received:
    0
    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.
     
  8. ggambett

    Moderator Original Member Indie Author

    Joined:
    Jul 26, 2004
    Messages:
    1,982
    Likes Received:
    6
    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...
     
  9. illume

    Original Member

    Joined:
    Jul 27, 2004
    Messages:
    206
    Likes Received:
    0
    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.
     
  10. ggambett

    Moderator Original Member Indie Author

    Joined:
    Jul 26, 2004
    Messages:
    1,982
    Likes Received:
    6
    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 :(
     
  11. PeterM

    Original Member

    Joined:
    Aug 5, 2004
    Messages:
    343
    Likes Received:
    0
    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.
     
  12. ggambett

    Moderator Original Member Indie Author

    Joined:
    Jul 26, 2004
    Messages:
    1,982
    Likes Received:
    6
    Based on my previous post, do you have any idea who could that be? ;)
     
  13. PeterM

    Original Member

    Joined:
    Aug 5, 2004
    Messages:
    343
    Likes Received:
    0
    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
     
  14. Omega

    Original Member

    Joined:
    Sep 2, 2004
    Messages:
    265
    Likes Received:
    0
    I agree.....
     
  15. ggambett

    Moderator Original Member Indie Author

    Joined:
    Jul 26, 2004
    Messages:
    1,982
    Likes Received:
    6
    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.
     
  16. PeterM

    Original Member

    Joined:
    Aug 5, 2004
    Messages:
    343
    Likes Received:
    0
    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! :p

    Pete
     

Share This Page

  • About Indie Gamer

    When the original Dexterity Forums closed in 2004, Indie Gamer was born and a diverse community has grown out of a passion for creating great games. Here you will find over 10 years of in-depth discussion on game design, the business of game development, and marketing/sales. Indie Gamer also provides a friendly place to meet up with other Developers, Artists, Composers and Writers.
  • Buy us a beer!

    Indie Gamer is delicately held together by a single poor bastard who thankfully gets help from various community volunteers. If you frequent this site or have found value in something you've learned here, help keep the site running by donating a few dollars (for beer of course)!

    Sure, I'll Buy You a Beer