static and generic programming (C++)

Started by
9 comments, last by Fruny 17 years, 6 months ago
There's a certain amount of code I end up coding over and over again. One of these is billboard rendering which is use for various effects such as particles, plants etc... What I would like is to create a single base class which has the billboard rendering code incorportated and simply inherit from this class for every use of billboards I have. However, I am using DirectX and I don't want 3 different instances of a class to have seperate vertex buffers but share a single instance of a vertex buffer. Normally I would make the vertex buffer a static but that isn't going to work if I am going to make the billboard class a base class since everything that inherits from it is going to share the same vertex buffer - which is bad because ideally I need a few mid-range sized vertex buffers not one allmighty large one since the lock operation I use creates a new copy each time and dumps the old one. So, if you understood that lot up there - what's my best option?
ByronBoxes
Advertisement
It looks like you contradicted yourself. First you said you want all sub classes to share the same vertex buffer, but then later on you say you want a couple of vertex buffers for them to use. Which one are you going for?

If I understand you correctly (which I probably don't), you want a group of vertex buffers (4 or 5 for example) to be shared across all billboards. If this is the case, then you could try a Singleton class that holds those vertex buffers.

Now that I've said 'Singleton', I can hear the mob outside my room. Uh oh.
I haven't done anything graphics wise, so forgive me if this is just way out there, but wouldn't it be easiest to just create your vertex buffer and pass it as a parameter to the billboards? Your base class could maintain a pointer to a vertex buffer, and each inherited billboard could be given a vertex to use in its constructor.
So you want one vertex buffer per type of subclass, NOT per subclass instance, and NOT just a shared one for all subclass types?
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
If a group of Billboard instances "share ownership" of a vertex buffer, then to control the lifetime of the vertex buffer you can use a reference counting scheme (perhaps simply a reference counted smart pointer), and then whatever objects should "share ownership" of the shared resource get a smart pointer member that points to it...
Let me see if I understand what you want.

You will have one parent class.

You will have several derived classes, each of which will have it's own static instance of a vertex buffer.

Correct?

class CommonBase{public:    virtual ~CommonBase() {}};template <typename T, typename Derived>class StaticMember : public CommonBase{public:   virtual T* get_static()   {      static T _static;      return &_static;   }};class ChildOne : public StaticMember<int,ChildOne> {};class ChildTwo : public StaticMember<int,ChildTwo> {};


With this setup, every instance of ChildOne shares a single int. Every instance of ChildTwo share an int. But ChildOne and ChildTwo have different ints. That second argument to StaticMember makes the distinction.
Have a vertex buffer manager that has no more than one instance of each vertex buffer you need. In the base billboard class, have a member pointer to a vertex buffer. In derived billboard classes' constructors, acquire a pointer to the relevant pointer from the manager, and assign the member pointer to it.

That would be my apporach.
Sorry for the delay in a response - forced absence from a computer due to an overwhelming need to re-landscape the front garden...

iMalc - correct!

Deyja - Looks like just what I am after. I wasn't sure of the semantics of creating a static in template.
ByronBoxes
It's challenging to create a real class-level static in a template because they have external linkage, and reguire a seperate declaration in a source file.

A function static, however, handily dodges the issue.
Deyja -

Out of curiosity, I'm not sure what the point of CommonBase is, though. Could you elaborate?

Edit: Nevermind, re-reading the OP's post I get the point of it.

[Edited by - okonomiyaki on October 9, 2006 2:34:31 PM]

This topic is closed to new replies.

Advertisement