[SDL] Scale and delete an image?

Started by
2 comments, last by Auriya 16 years, 1 month ago
I google'd, searched the forums and various tutorials, but I can't figure out how to delete an image. I have a character on screen, and when I press space, a circle forms around it... Here's some code from void Player::shoot()

	Uint32 reload = 0; //resets the timer
	alpha -= 140; //makes the circle transparent
	apply_surface( x - camera.x, y - camera.y, circle, human_ship ); //Applies circle to player
	reload = SDL_GetTicks(); //starts the timer
	if (reload = 300) //after .3 seconds
	{
		SDL_FreeSurface(circle); //ERROR in runtime when I press space
	}
Right now I'm using a png circle... How can I scale the circle (over .3 seconds) and delete it? I figure a while loop should do it... but I don't know what SDL command to use. It has to be deleted; alpha = 0 won't work because of collision detection for it. Freeing the surface gives me an error at runtime in Main.cpp with this line
SDL_SetAlpha( circle, SDL_SRCALPHA, alpha );
But not if SDL_FreeSurface(circle); is commented out. Thanks a lot! [Edited by - nibbuler on March 2, 2008 5:01:43 PM]
Advertisement
This isn't exactly how you're expected to use SDL. SDL_GetTicks, for one, doesn't start a timer. It returns the amount of time that has gone by since your program started.

Example:
int main() //<-- SDL_GetTicks is set to 0 here, and starts counting up.{   SDL_Init(SDL_INIT_EVERYTHING); //Or possibly starts here, I'm honestly not too sure. =)      ...      Uint32 time = SDL_GetTicks();   //'time' is now equal to however many millaseconds has passed since your program started when you hit this particular line of code.}


You can use SDL_GetTicks to time things, and create timers, but not how you have it in your code snippet. This tutorial explains it fairly well.

A SDL_Surface is a image only, not a object that has collision detection or anything like that built into it.

When you create your SDL_Surface by loading a image, SDL gives you a image only. It doesn't manage the image for you like a game engine might, you have to do it yourself(which is good).

When you delete your SDL_Surface with SDL_FreeSurface, the image ceases to exist, and your call SDL_SetAlpha is called on a image that no longer exists.

What could be done is you'd load your image at the beggining of your game, and free it at the end. Then you'd keep a bool also, and when you set it to 'true' it draws the image, and when you set it to false, it doesn't draw it.



Also, in C/C++, you can't say:
if (reload = 300)

Because that sets 'reload' to be equal to 300. It doesn't check if reload is equal to 300. Use '==' instead of '=' when in 'if' or 'while' or 'for' statements.
if (reload == 300)

In C/C++ the '=' symbol means 'make this equal to' and the '==' symbol means 'if this is equal to'. (Basicly)

Try something like this:
//In the Player class, have a bool 'CanFire' (start it as 'true')//Have a bool named 'DrawCircle' (start it at false)//Also, have a Uint32 'FireTime' (start it as 0)void Player::shoot() //Call this when the player shoots.{    ... //<--- Whatever else you have in this function        if(CanFire == true)    {        CanFire = false; //To stop the player from firing again, until he has reloaded.        FireTime = SDL_GetTicks(); //The player fired at 'xxx' millaseconds since the program started.        DrawCircle = true; //Set DrawCircle to true, so we know to draw the circle.                    }        ...}void Player::draw() //Call this every single frame.{    //Draw the player.    apply_surface(/*your x location*/, (/*your y location*/, human_ship, screen); //Draws the player on the screen.        if(DrawCircle == true)    {        apply_surface(/*your x location*/, (/*your y location*/, circle, screen); //Draws the circle on top of the place where we draw the player.        //But only if 'DrawCircle' is equal to 'true'. =)    }}void Player::update() //Call this every frame also.{    if(CanFire == false) //We are currently reloading    {        Uint32 currentTime = SDL_GetTicks(); //Checks the current time.        if(currentTime > (FireTime + 300)) //If the current time, is more than 300 millaseconds past the firing time.        {            CanFire = true; //We're done reloading.            DrawCircle = false; //Tell ourselves to stop drawing the circle.        }    }}


Reading through this 'Game Loops' article might be of some help, if you used to make games with GameMaker or something simular.

In fact, the entire Lazyfoo's SDL tutorials are a very good way to learn SDL, I suggest reading through them while working on your game. [smile]

Also, Futurama rocks, if your name is a reference to the show. [grin]
I got it to work great... couldn't have done it without you. :)

Any tips on scaling the image?
I haven't looked into this myself as it's still on my to-do list but I've read and heard that for image managing you can use SDL_Gfx which you can find here:

http://www.ferzkopp.net/joomla/content/view/19/14/


I tried getting it to work with Xcode on OSX platform, but as I couldn't find a direct how-to for it I left it alone till I have time to work on it again, please notify me if it's handy! ;p

I hope this'll help out.

Cheers
------Future C.A.R.D. Game Technologies-----

This topic is closed to new replies.

Advertisement