This post is a portion of my mini-series on Object Oriented Game Development using the Allegro 5 library. You can see all of the posts by going to http://fixbyproximity.com and clicking the “OOP Game Development” button at the top of this site.
Full Game Source Here
Videos in the Series:
- Object Oriented Game Development – Intro
- Object Oriented Game Development – Part 1
- Object Oriented Game Development – Part 2
- Object Oriented Game Development – Part 3
- Object Oriented Game Development – Part 4
- Object Oriented Game Development – Part 5
- Object Oriented Game Development – Part 6
- Object Oriented Game Development – Part 7
- Object Oriented Game Development – Part 8
I got an Heart attack at 5:40
Wow, I didn’t notice that when recording. I hate that dog.
Mmm, in ubuntu i use
void TakeLife() __attribute__((cdecl));
but at the moment of compile, it says
class SpaceShip dont have a member named ‘TakeLife’
Ops, my bad, put different name in the functions
Can you compile on Ubuntu? I can’t because of that cdecl, it doesn’t work 🙁
Use g++ to compile and
void function() __attribute__ ((cdecl));
Solved simply replacing cdecl with fastcall !
To keep the muster of the OO style, I should make a suggestion: I managed to fix the bug you described at around 14:00 simply by taking the function out of the base class, putting it in the Comet’s deconstructor (I put in empty deconstructors for the hell of it), and putting it in the ship’s destroy method (and obviously nowhere in the Bullet class since it doesn’t have an image).
I’m the one taking the course here so obviously I don’t have answers but my guess, if I’m learning this correctly, is that since there’s only really one Comet object and all the other comets are just illusions of a copy, all of their data is destroyed at the exact same time so there is no missing bitmap for any of the comet instances on screen. Since the copied comets are also being pushed into the list (true? I don’t know how lists work yet, their inclusion in the series was a complete surprise) when you iterate through each object’s Destroy methods the pointer cometImage still exists (and whatever pointer in the class) but the actual bitmap isn’t loaded – because of this, when the next comet is told to destroy itself the if(!blah==NULL) doesn’t bypass the destruction of an image that doesn’t actually exist in the memory. Because the deconstructor of the comet is called just before the main returns 0 and happens only once, there is no error.
Actually I just tried something else that proves my explanation completely wrong:
Putting the al_destroy_bitmap bit in the GameObject deconstructor caused an error the moment a comet collided with anything meaning their deconstructors must be called individually by copy.
This means that the fix I described is just some miracle.
You are correct. While they are all their own object, the share the reference of the image. If the image gets destroyed, you will have errors in the other objects.
I was wondering, since you had mentioned using a static flag in the Destroy method, would this be an acceptable way to do so?
static bool destroyed = false;
if (!destroyed)
{
GameObject::Destroy();
destroyed = true;
}
If my reasoning is correct, this should work correctly, but specifically for this program. If the enemies had more than one image for enemies, it would only destroy the first bitmap in the list, while letting the rest in the list stay in memory. At least, I think, haha.
By the way, thanks a ton for these tutorials. I’m learning a lot of funky things about C++ everyday with these videos.
You can, but then you need to be careful (as you said) if two objects use two different images
Thanks for the simple fix. Makes sense to destroy the image outside the loop seeing there is only one instance of it.
I have a question: How is this “cdecl” method different from simple “Pointers to functions” as is explained at the end of this page:
http://www.cplusplus.com/doc/tutorial/pointers/
?
I mean what does this cdecl thing really do? Make the pointer global?
Hey Mike, I’ve been following your invaluable resource for months now, learning C++ on the Raspberry Pi, I’ve hit a snag though, it seems that ARM processors don’t support the ‘cdecl’ method, its i386 only. Is there an alternative method I can use which works in the same manner?
Thanks for all the time spent putting these vids together, I’ve learnt more about C programming in 3 months here than I did in my first year at Uni and had more fun doing it. They’re amazing.
Arm and C++ don’t play nice with each other (or so I’ve heard). I believe this will be fixed in C++ 14 which will be out next year. I may be completely wrong.
That’s a shame, cdecl is the only thing that’s caused me any issues, everything else has been perfect. Thanks for getting back to me 🙂
Hey Mike ! Could you explain me just one thing ? Im having a problem with AI in my game. It works good when Im updating just one, but when I make 2 or more AIs, each new takes the update of the previous one. It might be because Im calculating distance, angle and so on, and not just simple x+= VelX * DirX or so. The same type of AI that we used in tutorial 13.3. So, again. The problem is that I cant make an update for each object (to work on its own), but only for all of them, and it updates all of the object according to update of the last one created.