0

I'm building a simple generic engine for my true start in the making of games, and I am trying to be somehow organized and decent in the making of my engine, meaning I don't want it to be something I throw to the side once I make what I'm planning to.

I add objects to be displayed, drawObjects, and these can either move, not move, and have an animation, or not have one.

In case they DO have an animation, I want to initialize a single animationSet, and this animationSet will have xxx animationComp inside of it. As I'm trying to be neat and have worked abit on "optimizations" towards memory and cpu usage (such as sharing already-loaded image pointers, and whatever came across my mind), I wanted to not ask for possibly unused memory in arrays.

So I had animationSetS* animationSet = NULL; initially, planning to do a animationSet = animationSetS[spacesINEED]; after, only on the objects that needed animation that I added, being those that aren't animations a NULL and therefore not using memory (correct?). And then this question popped up! (title)

    struct animationComp {
        SDL_Rect* clip;
        int clipsize;
    };

    struct animationSetS {
        animationComp* animation;
        int currentFrame;
        int currentAnimation;
        int animationNumber;
    };

    struct drawObject { // Um objecto.
        char* name;
        SDL_Surface* surface;
        bool draw = true;
        float xPos;
        float yPos;
        bool willMove = false; // 0 - Won't move, 10 - Moves alot, TO IMPLEMENT
        bool isSprite = false;
        animationSetS* animationSet;
    };

I dabble alot in my questions, sorry for that. For any clarifications reply here, I'll reply within 10 minutes for the next... 1 hour perhaps? Or more. Thanks!

parreirat
  • 715
  • 3
  • 9
  • 22

2 Answers2

3

Setting the pointer to NULL means that you'll be able to add ASSERT(ptr != NULL); and KNOW that your pointer does not accidentally contain some rubbish value from whatever happens to be in the memory it was using.

So, if for some reason, you end up using the object before it's been properly set up, you can detect it.

It also helps if you sometimes don't use a field, you can still call delete stuff; [assuming it's allocated in the first place].

Note that leaving a variable uninitialized means that it can have ANY value within it's valid range [and for some types, outside the valid range - e.g. pointers and floating point values can be "values that are not allowed by the processor"]. This means that it's impossible to "tell" within the code if it has been initialized or not - but things will go horribly wrong if you don't initialize things!

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
3

If this should be really implemented in C++ (as you write), why don't you use the C++ Standard Library? Like

struct animationSetS {
    std::vector< std::shared_ptr<animationComp> > animation;
    // ...
}
Andreas Florath
  • 4,418
  • 22
  • 32
  • 1
    I agree, that is a much better solution! – Mats Petersson Dec 25 '12 at 23:14
  • As I'm inexperienced with C++, I'm trying to do it all with structs from the ground up without libraries just to getting used to using pointers and all of that. Is it a bad thing to do? – parreirat Dec 25 '12 at 23:16
  • @user1896797: It is not a bad thing - but why do you want to reimplement all the stuff which is already there? – Andreas Florath Dec 25 '12 at 23:18
  • For the same reason at colleges we build our own data structures throughout the years - because by coding it ourselves, realizing what obstacles and solutions there are, we get a true understand of it. Edit - And once we get a true understanding of it, or atleast grasp it well, then yes, we use (I will) already-done and well-coded libraries - why reinvent the wheel :) – parreirat Dec 25 '12 at 23:20
  • @user1896797: 1) Why are you using C++ and not C? 2) Is this homework? 3) Do you want to concentrate on `making of games` or on basic data structures and algorithms? – Andreas Florath Dec 25 '12 at 23:26
  • No reason for C++ instead of C, just wanted the language with more features that I'll use in the future, not homework (I wish we did fun stuff such as game engines at college), and since it's my first real tackle at a game, I suppose the first – parreirat Dec 25 '12 at 23:32
  • 2
    I'm pretty sure you'll still learn about pointers, and all manner of other things if you produce a (some degree of) real game-engine. Doing the EASY stuff the easy way is definitely always a better choice than doing it the hard way. All (but the simplest) programming projects give you hard stuff to do anyways - so make it simple for the stuff that doesn't have to be complicated, and learn in the complicated bits that you DON'T know how to do! – Mats Petersson Dec 25 '12 at 23:35
  • @Griwes: I'm from the 'old' school and read Scott Meyers 'Effective STL: 50 Specific Ways to Improve the Use of the Standard Template Library' - hope it's better now. ;-) – Andreas Florath Dec 25 '12 at 23:36
  • That's actually a better way to look at it, Mats. I'll look into changing all I've done into already done STL/stdlib structures then! Thanks for the 3rd or 4th time! – parreirat Dec 25 '12 at 23:41