Creating a Class variable Based on the Constructor?

Again, sorry for the bad title, but hopefully it'll be understandable. Basically, I'm creating a class for animation and instead of limiting myself to just using OpenGL for more power I would also implement software rendering with the SDL library. First off lets start with the class I have:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class AnimationStrip
{
public:
    AnimationStrip(string FramePrefix, int TotalFrames, float x, float y);
    float GetFrameWidth();
    float GetFrameHeight();
    float GetX();
    float GetY();
    void UpdateAnimation(int DelayBetweenFrames);
    void Render(Screen scr);
private:
    float X, Y, CurrentFrameWidth, CurrentFrameHeight;
    int NumberOfFrames;
};


I haven't actually created the constructor yet, but basically instead of creating two arrays of the images I'll be loading in the constructor which for OpenGL would be an array of GLuints and in SDL it would be an array of SDL_Surfaces, I want to know how to create one or the other depending on which is currently being used.

The problem is if I were to create one of these arrays it would have to be in the constructor once I determine if OpenGL is being used or not, but then the rest of the class wouldn't be able to use the variable because it'd be limited to the scope of the constructor. I also can't create the array without knowing how many frames (images) I'm actually loading which is determined by what is entered into the constructor.

So bottom line, is it possible to create variables in the constructor of a class that aren't limited to the scope of that constructor?
Last edited on
777th Post!
You can make the base class a virtual class, and make two derived classes:
One SDLAnimationStrip and one OGLAnimationStrip like follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class AnimationStrip {
    public:
        virtual ~AnimationStrip() {} // This line is REALLY IMPORTANT!
        // If you delete it, your derivated classes will not be able to call destructors!

        virtual void Render(Screen&) = 0; // <- The derivated classes
        // will create a Render function different from the base one.
        // The base one isn't "associated" to SDL or OpenGL, make it
        // undeclarable by itself.

        // Also, add non-driver-dependent things like position, size and frames count here.
};
class SDLAnimationStrip : public AnimationStrip {
    public:
        virtual void Render(Screen& scr) {
            // SDL Render Code goes Here.
        }
    // You can add SDL-dependent things here
};
class OGLAnimationStrip : public AnimationStrip {
    public:
        virtual void Render(Screen& scr) {
            // OGL Render Code
        }
    // You can add OGL-dependent things here
};

When you are going to use them, you should use them like:

1
2
3
4
5
6
7
8
9
10
11
int main()
{
    SDLAnimationStrip sdlstrip;
    OGLAnimationStrip oglstrip;
    // AnimationStrip strip; !! ERROR !!
    // But you can:
    AnimationStrip * CurrentStrip = 0;
    if(UseSDL) CurrentStrip = &sdlstrip;
    else CurrentStrip = &oglstrip;
    CurrentStrip->Render( (Screen) );
}


Also you need to change from 'private' to 'protected' in your code snip, or you will not be able to access variables from derived classes.
Last edited on
I love this place! It's hard not to get answers. Thank you, I'll try this out now....
Last thing: Into "Screen" struct/class you must hold BOTH the SDL's Screen Informations and OGL's Screen Informations.
Topic archived. No new replies allowed.