Sign in to follow this  
Byron

static and generic programming (C++)

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Quote:
Original post by Deyja
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.


Definitions for static members of class templates can actually (and should!) be placed in a header file, even though it is not possible for non-templates.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this