473,597 Members | 2,413 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Deleting Animations

I'm making a game. I need to maintain a bunch of objects, in this
case animations (explosions and stuff). I've decided to use an STL
vector. From my understanding, I need to declare it as
vector<Animatio n*so that I can insert new animations with something
like myAnimations.pu sh_back(new Animation(...)) ; So that's all fine
and dandy, but what I can't decide on is how to delete this animation
after it's done playing (the explosion should be deleted and removed
from the vector when it's done cycling through all it's frames). I've
been told it's a bad idea to get an object to delete itself, because
then the caller won't know whether or not it still exists, and it's
just a bad design decision. So, would it be best to have some sort of
boolean in my animation object that says something like
"animationCompl ete", and when set to true the iterator would then
remove it? If so, what would my iterator look like?

for( vector<Animatio n*>::iterator current = myAnimations.be gin();
current != myAnimations.en d(); current++ )
{
(*current)->step(); // tells the animation to change frames
and do whatever it needs to do
if( (*current)->animationCompl ete )
{
current = projectile.eras e(current);
delete (*current); // <-- is this right?
continue;
}
(*current)->draw(); // draw it
}

Code looks kind of ugly... is that really the best way of doing it?

Apr 27 '07 #1
14 1634
On 27 Apr, 05:53, Mark <mnbaya...@gmai l.comwrote:
I'm making a game. I need to maintain a bunch of objects, in this
case animations (explosions and stuff). I've decided to use an STL
vector. From my understanding, I need to declare it as
vector<Animatio n*so that I can insert new animations with something
like myAnimations.pu sh_back(new Animation(...)) ; So that's all fine
and dandy, but what I can't decide on is how to delete this animation
after it's done playing (the explosion should be deleted and removed
from the vector when it's done cycling through all it's frames). I've
been told it's a bad idea to get an object to delete itself, because
then the caller won't know whether or not it still exists, and it's
just a bad design decision. So, would it be best to have some sort of
boolean in my animation object that says something like
"animationCompl ete", and when set to true the iterator would then
remove it? If so, what would my iterator look like?

for( vector<Animatio n*>::iterator current = myAnimations.be gin();
current != myAnimations.en d(); current++ )
{
(*current)->step(); // tells the animation to change frames
and do whatever it needs to do
if( (*current)->animationCompl ete )
{
current = projectile.eras e(current);
delete (*current); // <-- is this right?
continue;
}
(*current)->draw(); // draw it
}

Code looks kind of ugly... is that really the best way of doing it?
Is there any special reason why you don't just declare it as
vector<Animatio nand add new elements like this:
'myAnimations.p ush_back(Animat ion(...))', notice the lack of new?

Then all you have to do is to use erase() on the vector to delete
them, I think it's possible to add a predicate to erase so that it
only erases those where animationComple te is true.

--
Erik Wikström

Apr 27 '07 #2
mos

"Mark" <mn*******@gmai l.com>
??????:11****** *************** *@r3g2000prh.go oglegroups.com. ..
I'm making a game. I need to maintain a bunch of objects, in this
case animations (explosions and stuff). I've decided to use an STL
vector. From my understanding, I need to declare it as
vector<Animatio n*so that I can insert new animations with something
like myAnimations.pu sh_back(new Animation(...)) ; So that's all fine
and dandy, but what I can't decide on is how to delete this animation
after it's done playing (the explosion should be deleted and removed
from the vector when it's done cycling through all it's frames). I've
been told it's a bad idea to get an object to delete itself, because
then the caller won't know whether or not it still exists, and it's
just a bad design decision. So, would it be best to have some sort of
boolean in my animation object that says something like
"animationCompl ete", and when set to true the iterator would then
remove it? If so, what would my iterator look like?

for( vector<Animatio n*>::iterator current = myAnimations.be gin();
current != myAnimations.en d(); current++ )
{
(*current)->step(); // tells the animation to change frames
and do whatever it needs to do
if( (*current)->animationCompl ete )
{
current = projectile.eras e(current);
delete (*current); // <-- is this right?
continue;
}
(*current)->draw(); // draw it
}

Code looks kind of ugly... is that really the best way of doing it?
I suppose it should be :

delete (*current); // first
current = projectile.eras e(current); // second

and I almost use list as
for (it = list.begin(); it != list.end(); )
{
if ((*it).die())
{
delete *it;
it = list.erase(it)
}
else
++it;
}
Apr 27 '07 #3
mos
Hi
sorry for posting unfinished mail.
I mean you'd better choose list to do it than vector.

mos.
Apr 27 '07 #4
On 26 Apr 2007 23:22:17 -0700, =?iso-8859-1?q?Erik_Wikstr =F6m?= wrote:
>Is there any special reason why you don't just declare it as
vector<Animati onand add new elements like this:
'myAnimations. push_back(Anima tion(...))', notice the lack of new?
I guess the reason simply is that he deosn't want to duplicate the
Animation objects. The reason why the original code looks so 'ugly' is
that STL was designed only for values, not for (pointers to) objects.
--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
Apr 27 '07 #5
"Mark" <mn*******@gmai l.comwrote in message
news:11******** **************@ r3g2000prh.goog legroups.com...
I'm making a game. I need to maintain a bunch of objects, in this
case animations (explosions and stuff). I've decided to use an STL
vector. From my understanding, I need to declare it as
vector<Animatio n*so that I can insert new animations with something
like myAnimations.pu sh_back(new Animation(...)) ; So that's all fine
and dandy, but what I can't decide on is how to delete this animation
after it's done playing (the explosion should be deleted and removed
from the vector when it's done cycling through all it's frames). I've
been told it's a bad idea to get an object to delete itself, because
then the caller won't know whether or not it still exists, and it's
just a bad design decision. So, would it be best to have some sort of
boolean in my animation object that says something like
"animationCompl ete", and when set to true the iterator would then
remove it? If so, what would my iterator look like?

for( vector<Animatio n*>::iterator current = myAnimations.be gin();
current != myAnimations.en d(); current++ )
{
(*current)->step(); // tells the animation to change frames
and do whatever it needs to do
if( (*current)->animationCompl ete )
{
current = projectile.eras e(current);
delete (*current); // <-- is this right?
continue;
}
(*current)->draw(); // draw it
}

Code looks kind of ugly... is that really the best way of doing it?
The way I'm doing it:

// Update ALL the -On-The-Fly- or Fired 'Beams' to NEW positions.
for ( BeamEffectsType ::iterator it = Client.BeamEffe cts.begin(); it !=
Client.BeamEffe cts.end(); )
{
if ( !(*it).second->Update() )
{
delete (*it).second;
it = Client.BeamEffe cts.erase(it);
}
else
++it;
}

Although I'm using a map instead of a vector, the same logic should apply.

The only real difference between mine and yours is I have the function call
itself return a boolean. So you could have your step() return bool whether
to delete it or not. Also, your code is not removing the element from the
vector, which it needs to.
Apr 27 '07 #6
On 27 Apr, 11:23, rpbg...@yahoo.c om (Roland Pibinger) wrote:
On 26 Apr 2007 23:22:17 -0700, =?iso-8859-1?q?Erik_Wikstr =F6m?= wrote:
Is there any special reason why you don't just declare it as
vector<Animatio nand add new elements like this:
'myAnimations.p ush_back(Animat ion(...))', notice the lack of new?

I guess the reason simply is that he deosn't want to duplicate the
Animation objects. The reason why the original code looks so 'ugly' is
that STL was designed only for values, not for (pointers to) objects.
I asked since in his original post he said that he wanted to use it
like this:

myAnimations.pu sh_back(new Animation(...)) ;

notice that he creates the object in the push_back, in which case my
solution would be just as good (or better).

--
Erik Wikström

Apr 27 '07 #7
On 27 Apr 2007 02:36:38 -0700, =?iso-8859-1?q?Erik_Wikstr =F6m?= wrote:
>I asked since in his original post he said that he wanted to use it
like this:

myAnimations.p ush_back(new Animation(...)) ;

notice that he creates the object in the push_back, in which case my
solution would be just as good (or better).
Not quite. Your solution copies the new Animation object into the
vector and may copy all other objects in the vector due to relocation.
The pointer solution never copies Animation objects, just pointers.
Moreover, since Animation seems to be an object in the OO sense it may
be semantically problematic to duplicate these objects, i.e. the copy
constructor probably should private.
--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
Apr 27 '07 #8
On Apr 26, 11:53 pm, Mark <mnbaya...@gmai l.comwrote:
I'm making a game. I need to maintain a bunch of objects, in this
case animations (explosions and stuff). I've decided to use an STL
vector. From my understanding, I need to declare it as
vector<Animatio n*so that I can insert new animations with something
like myAnimations.pu sh_back(new Animation(...)) ; So that's all fine
and dandy, but what I can't decide on is how to delete this animation
after it's done playing (the explosion should be deleted and removed
from the vector when it's done cycling through all it's frames).
Why? Do you only use the explosion once in the entire game? You're
going to need a Cray to run your game if you allocate/load/delete
animations every time you play them.
Apr 27 '07 #9
On Apr 26, 11:22 pm, Erik Wikström <eri...@student .chalmers.sewro te:
On 27 Apr, 05:53, Mark <mnbaya...@gmai l.comwrote:
I'm making a game. I need to maintain a bunch of objects, in this
case animations (explosions and stuff). I've decided to use an STL
vector. From my understanding, I need to declare it as
vector<Animatio n*so that I can insert new animations with something
like myAnimations.pu sh_back(new Animation(...)) ; So that's all fine
and dandy, but what I can't decide on is how to delete this animation
after it's done playing (the explosion should be deleted and removed
from the vector when it's done cycling through all it's frames). I've
been told it's a bad idea to get an object to delete itself, because
then the caller won't know whether or not it still exists, and it's
just a bad design decision. So, would it be best to have some sort of
boolean in my animation object that says something like
"animationCompl ete", and when set to true the iterator would then
remove it? If so, what would my iterator look like?
for( vector<Animatio n*>::iterator current = myAnimations.be gin();
current != myAnimations.en d(); current++ )
{
(*current)->step(); // tells the animation to change frames
and do whatever it needs to do
if( (*current)->animationCompl ete )
{
current = projectile.eras e(current);
delete (*current); // <-- is this right?
continue;
}
(*current)->draw(); // draw it
}
Code looks kind of ugly... is that really the best way of doing it?

Is there any special reason why you don't just declare it as
vector<Animatio nand add new elements like this:
'myAnimations.p ush_back(Animat ion(...))', notice the lack of new?

Then all you have to do is to use erase() on the vector to delete
them, I think it's possible to add a predicate to erase so that it
only erases those where animationComple te is true.

--
Erik Wikström
heh. I was just thinking about that, and I asked my friend if it was
possible, but he said it was necessary to use "new". Shows how much
he knows :)
This will help, thank you!

Apr 27 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1522
by: Michel Gaudry | last post by:
Hello, I want to create a svg with multiple animations. It starts with <path> describing a Triangle and it's moved with a series of <animate>. An example : <path ...> <animate .../> <animate .../> <animate .../>
1
1262
by: Garry Gaves | last post by:
Hi @all, I'm currently writing a VB.NET project and I want to show some animations to the user (e.g. GIF, or any other format). How can I do that? I couldn't find a appropriate control for this!!!! Garry
1
1290
by: dzemo | last post by:
I want to make some simple animations in program (for example when showing some piece of form or some information on click) but I don't know how. I can change left property of form but there are no animations. Any ideas? timer, ....? thx -- ----------------------------------------------------------------------------------------------------------------------- Dzemal Tipura (Dzemo) - MCP
1
2851
by: DrXavier | last post by:
Well I have not programmed in C++ for some length of time, so I will need a reffresher. On that note, I have never really written a real world app in any language. I would like to make a platform independant graphic animation software. What I'd like to do is to start, with a mathematical plane, and allow the user to use the functions within the object to disort the surface. Then Id like the computer to animate , in smooth 3d, the process...
4
2350
by: Fla | last post by:
Good morning. I'd like to know if I can use for free the animations, the bitmaps and the icons inside the archive VS2005ImageLibrary.zip in the installation path of VB2005 C:\Program Files\Microsoft Visual Studio 8\Common7\VS2005ImageLibrary, for developing my application, without any further royalty nor license, beyond the licensed copy of VB2005. Thanks
2
1408
by: Mike Stolkes | last post by:
I have the task to make 2 or 3 simple games, probably some shot them up game (space invaders or so) and a poker game. For this I need to move things around very quickly on the screen. So that is my question.. What is the easiest way to achieve fast animations in vb.net? I know I can write it myself but is there a ready package or control to buy? What do other people use?
7
12597
by: kvnsmnsn | last post by:
I've got two GIF files named "Tigger.gif" and "Angry_barbarian.gif" that contain animations that I want to display on a web page. I've written the following XHTML code which works for a fraction of a sec- ond but no more. That is, the animations run for a very short period of time and then stop. My guess is that they're running through their short animated period and then stopping. How can I display animations like this that keep...
5
6096
by: vitaminB18 | last post by:
I want to make my forms unique with animations...i want the best codes for my main menu forms and other forms to make my database projects excellent. Im a student and the best design will receive an award in graduation..Anybody can help me to design a form?what should i put there to make it look best? IT is the Docket Inventory System for Civil and Criminal Cases.thanks
0
8263
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8379
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6677
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5842
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5421
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3876
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2393
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1226
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.