Skip to main content
gamedev.net
🔒 Locked

Managed Vertex Buffer

Started by dave 1 reply 350+ views
Original Post
dave
dave
Is it a good idea to write a class which manages a single vertex buffer. I have written a full first version of this class as u can see below. I am aware that it doesn't do much, early days though.

#ifndef _WIN_DX_MANAGED_VERTEX_BUFFER_
#define _WIN_DX_MANAGED_VERTEX_BUFFER_

#include "win_dx_vrt_df.h"
#include <d3dx9.h>
#include <assert.h>

namespace COM_DAVESDUMP
{
	template <class T>
	class ManagedVB
	{
		public:
			//ManagedVB();
			ManagedVB(	unsigned int			numVerts,
						D3DPOOL					pool,
						LPDIRECT3DDEVICE9		device);
			~ManagedVB();

			void						Write(T& v);
			LPDIRECT3DVERTEXBUFFER9*	GetMVBPtr()			{return &vertexBuffer};
			unsigned int				GetMVBCapacity()	{return size;};
			unsigned int				GetMVBIterator()	{return numberWritten;};
			unsigned int				GetMVBMemCapacity()	{return size * sizeof(T);};
			unsigned int				GetMemIterator()	{return numberWritten * sizeof(T);};

			
		private:
			bool			isUsed;			// MVB must be closed before use, indicator
			unsigned int	size;			// Current size of MVB 
			unsigned int	numberWritten;	// Number of verts written to the buffer

			D3DPOOL			pool;

			void*			pV;				// pointer to the place locked in the buffer

			LPDIRECT3DDEVICE9 device;

			LPDIRECT3DVERTEXBUFFER9	vertexBuffer;

			void Create();
			void Open(	const T& v, 
						unsigned int offsetInBytes);
			void Close();
			void Cleanup();
	};
/*
	template<class T>
	ManagedVB<T>::ManagedVB()
	{
	}
*/
	template<class T>
	ManagedVB<T>::ManagedVB(	unsigned int			numVerts,
								D3DPOOL					pool,
								LPDIRECT3DDEVICE9		device)
	{
		assert(device != NULL);
		assert(size != 0);

		isUsed			= false;
		numberWritten	= 0;
		this->size		= numVerts;
		this->device	= device;
		this->pool		= pool;

		vertexBuffer = NULL;

		Create();
	}

	template<class T>
	ManagedVB<T>::~ManagedVB()
	{
		Cleanup();
	}

	template<class T>
	void ManagedVB<T>::Write(T& v)
	{
		if (numberWritten < size)
		{
			void* a;
			pV = NULL;
			// lock the buffer
			Open(v, (numberWritten * sizeof(v)) );

			a = memcpy(pV, &v, sizeof(v));

			Close();

			if (a != NULL)
			{
				numberWritten++;
			}
			else
			{
				assert(0);
			}
		}
		else
		{
			assert(0);
		}

	}

	template<class T>
	void ManagedVB<T>::Create()
	{
		device->CreateVertexBuffer( size * sizeof(T),
									0,
									D3DFVF_TRANSTEXT,
									pool,
									&vertexBuffer,
									NULL);

		assert(device != NULL);
	}

	template<class T>
	void ManagedVB<T>::Open(	const T& v, 
								unsigned int offsetInBytes)
	{
		vertexBuffer->Lock( offsetInBytes, 
							sizeof(v), 
							(void**)&pV, 
							0 );
	}

	template<class T>
	void ManagedVB<T>::Close()
	{
		vertexBuffer->Unlock();
	}

	template<class T>
	void ManagedVB<T>::Cleanup()
	{
		if (vertexBuffer)
		{
			vertexBuffer->Release();
		}
	}
}



#endif

What do you think. I am going to rewrite this several times, but this is going straight into my next engine. ace
Holy Fuzz
Holy Fuzz
There is absolutely nothing wrong with writing a wrapper class around a vertex buffer. Use whatever works for you.
dave
dave
bump, any more opinions on this idea?

ace

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.