• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
bd36

Sprite Rotation

6 posts in this topic

I currently have a small sprite that I am trying to rotate dynamically. I have look around on google and it doesn't seem like there are very many good solutions without switching to opengl or something. I am now using SDL to render my 2d game. Up to this point I have been able to get away with just making a sprite sheet with different directions. However, I want to be able to rotate to track a moving target to shoot at it so pre-drawing all to possible angles no longer seems like the right solution. Looking on Google it seems like one option is http://www.ferzkopp.net/joomla/content/view/19/14/ but it also look like a lot of people aren't recommending this because its slow or something. Im not really sure what I should do to fix this problem. It seems like a relatively common task in a game that i surprisingly haven't found a good answer to yet. Any suggestions would be greatly appreciated.
0

Share this post


Link to post
Share on other sites
Creating your own rotation code is not that hard: First, rotate the rectangle that contains the sprite and learn how rasterize it (loop over scan lines). Then learn how to look up a pixel in the original sprite, applying the inverse of the rotation. If you implement this in a low-level language (e.g., C or C++), it should be pretty fast.
0

Share this post


Link to post
Share on other sites
Im not really sure where to even begin with that so I decided to just try the sdl_gfx to see if I could just get something to run so I can make some progress. I can change it later. However I am having some problems implementing it. My class for image that is being rotated has a SDL_Surface* image that is storing the image that needs to be displayed. In my update() method i am trying to do the follow:[code] SDL_Surface * rotation= rotozoomSurface(image, 10.0, 1, 1);
image = rotation;
SDL_FreeSurface(rotation);[/code]

I know that the problems is in the image=rotation line. I'm fairly certain it is because they are both pointers and thus point to the address in memory of rotation, which is then being assigned to image so they both now point to the same location. That location is then subsequently released. Later when I try to draw to image the memory has been released resulting in the heap corruption error that is thrown. I think that to solve this I need to set image equal to a copy of rotation. However, I do not know how to do that. I am still trying to figure pointers and how they work so if any of the above logic is wrong please let me know. Is this what I need to do/ how do I make a copy?
0

Share this post


Link to post
Share on other sites
I would ask why you are trying to free/copy the rotated image at this point?

Might I recommend the following instead:
[code]
SDL_FreeSurface(image);
image = rotation;
[/code]

What this code does is frees the original image and sets the image SDL_Surface pointer to the address of the rotated image. My only worry with this code is the loss of image quality over multiple rotations (I am not positive that image quality would be lost, but it seems reasonable to me). It might instead be better to have something more like this:


[code]
if(oldRotation != newRotation)
{
if(displayImage != NULL && displayImage != original_Image) SDL_FreeSurface(displayImage);
displayImage = rotozoomSurface(original_Image, newRotation, 1, 1);
oldRotation = newRotation;
}
[/code]

This code makes new rotation images based off the originally loaded image, and only does so when the rotation has changed.
1

Share this post


Link to post
Share on other sites
I was trying to free the rotated image so that I didn't end up with a lot of extra rotations in memory that I don't need. I just needed it to display and rotate the image. So at the end of the call I would only have one SDL_Surface to hold on to. At least that was what I was thinking.

I used the code that Zael suggested and it works, kind of. Adding 10 to newRotation everytime the update function is called causes the image to rotate in a circle as desired and expected. However, it wobbles like crazy and jumps back and forth as is spins as if it were vibrating on a sifter or something. Is this a problem with the rotozoomSurface() or the way I am using it. The images are all circles so ideal as the rotate their position in space should remain constant but the features on the circle move around.

[code] if(oldRotation != newRotation)
{
if(image != NULL && image != original_Image)
SDL_FreeSurface(image);
image = rotozoomSurface(original_Image, newRotation, 1.0, 1);
oldRotation = newRotation;
}[/code]

Also out of curiosity, based on the link provided by alvaro I see that the way to make a copy of SDL_Surface* is by using [code]SDL_Surface *Srfc2 = SDL_ConvertSurface(Srfc1, Srfc1->format, Srfc1->flags);[/code] Does this mean there is no generic way to make a copy of an object that is pointed to by an object?
0

Share this post


Link to post
Share on other sites
bd36, I was going through this myself. I eventually got SDL_gfx to work, and I could rotate my sprite, but it was SLOOOOWWWWW. It uses software to rotate, and, I couldn't cope with it.

That's when I tried SFML. SFML is similar to SDL, except it sits on top of OpenGL, and all it's graphics API's are done in hardware, including rotations and scaling. It's Audio functions sit on top of OpenAL, so there's some really good audio API's available too.

Please take a peek at [url="http://sfml-dev.org"]http://sfml-dev.org[/url] you'll be glad you did. (BTW, my old blog (in my sig) details a game I made using SFML, and I rotate all my sprites with that game).
0

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  
Followers 0