Advertisement

trying to make a class that makes quads

Started by August 22, 2002 01:33 PM
11 comments, last by Da cobra 22 years, 4 months ago
well as the title says I''m trying to make a class so I can make "infinite" quads but here''s my problem, how do I deal with the variables that always need a new name?!? I mean for each quad I need a new pointer to a IDirect3DVertexBuffer8, right. so how do I solve this? here''s my CQuad.h :
  
#include <d3d8.h>		// Direct3D header file  


//class definition of Quad :

class CQuad
{
private:
	struct my_vertex
	{
		FLOAT x, y, z, rhw; // The transformed position for the vertex.

		DWORD color;        // The vertex color.

	} ; // end of struct

	
public:
	void DrawQuad() ;
	void InitQuad(float fStartX, float fStartY, float fEndX, float fEndY, DWORD color) ;
	void ReleaseQuad() ;
} ; // end of class

  
and this is my CQuad.cpp :
  
#include "CQuad.h"

IDirect3DVertexBuffer8 *g_square=NULL ; // what to do with this one???


void CQuad::DrawQuad()
{
	#define D3D8T_CUSTOMVERTEX (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)
	
	// Vertex shaders are a complex topic, but you can do some amazing things with them

    // For this example we''re not creating one, so we tell Direct3D that we''re just

    // using a plain vertex format.

    g_lpDevice->SetVertexShader(D3D8T_CUSTOMVERTEX);

    // Set the active stream to be our square.

    // NOTE: Because of the offsets that DrawPrimitive takes, we could

    // have built the triangle & square into a single VB and still

    // drawn them seperately.

    g_lpDevice->SetStreamSource(0,g_square,sizeof(my_vertex));

    // Now we''re drawing a Triangle Strip, 4 vertices to draw 2 triangles.

    g_lpDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
}

void CQuad::InitQuad(float fStartX, float fStartY, float fEndX, float fEndY, DWORD color)
{
	// Declare a structure to hold a vertex with all the information that we need

	struct my_vertex
	{
		FLOAT x, y, z, rhw ;	// The transformed position for the vertex.

		DWORD color;			// The vertex color.

	} ; // end of struct


	// A handy little ''macro'' for our definition of the vertex.  When we use the vertex data

	// we have to tell D3D what data we''re passing it.  D3DFVF_DIFFUSE specifies that the

	// vertex will have a colour, the D3DFVF_XYZRHW specifies that the vertex will have

	// coordinate given in screen space.

	#define D3D8T_CUSTOMVERTEX (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)

	my_vertex g_square_vertices[] =
	{
		{ fStartX,  fEndY  ,  0.5f, 1.0f, color }, // x, y, z, rhw, color

		{ fStartX,  fStartY,  0.5f, 1.0f, color },
		{ fEndX  ,  fEndY  ,  0.5f, 1.0f, color },
		{ fEndX  ,  fStartY,  0.5f, 1.0f, color }
	} ; // end of my_vertex g_square_vertices[]

	

	unsigned char *vb_vertices;

	if(FAILED(g_lpDevice->CreateVertexBuffer(4*sizeof(my_vertex),//Size of memory to be allocated

                                                           // Number of vertices * size of a vertex

                                       D3DUSAGE_WRITEONLY, // We never need to read from it so

                                                           // we specify write only, it''s faster

                                       D3D8T_CUSTOMVERTEX, // Our custom vertex specifier (coordinates & a colour)

                                       D3DPOOL_MANAGED,    // Tell DirectX to manage the memory of this resource

                                       &g_square)))        // Pointer to our triangle, after this call

                                                           // It will point to a valid vertex buffer

	{
		Write2Log("Failed to initialize a square") ;
	} // end of if


	// Now we go through the same process to fill in our VB for the square.

    if(FAILED(g_square->Lock(0,				//Offset, we want to start at the beginning

							 0,				//SizeToLock, 0 means lock the whole thing

							 &vb_vertices,	//If successful, this will point to the data in the VB

							 0)))			//Flags, nothing special

	{
		Write2Log("Failed to lock Square") ;
    }
   
   memcpy(vb_vertices, g_square_vertices, sizeof(g_square_vertices)) ;

   g_square->Unlock() ;

} // end of InitASquare


void CQuad::ReleaseQuad() 
{
	if(g_square)
	{
		g_square->Release() ;
		g_square=NULL ;
	}
} // end of ReleaseSquare()

  
thanx in advance for any tips
Maybe I missed something obvious here ... but why not just add an IDirect3DVertexBuffer8 object to the class itself, instead of trying to use global variables?

ReactOS - an Open-source OS compatible with Windows NT apps and drivers
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
Advertisement
will I then be able to make more quads with this class by declaring new objects like this :

CQUAD quad1 ;
CQUAD quad2 ;

I just don''t have the concept of classes totally figured out yet.
well I added the IDirect3DVertexBuffer8 object to the class self as a private member, and yeah all still works fine but how can I make 2 quads now, if I try to make 2 quad, my PC reboots when I exit my application, probably because I use the same pointer for each quad. How do I solve this???
If you use the same vertex buffer for each quad then I would not give your quad class the abilty to release the vb (since it obviously didn''t create it in the first place, why should it delete it?). Also, make sure that you are adding your vertices to different offsets in the buffer so that one quad doesn''t overwrite the other.


The hackers must have gotten into the system through the hyperlink!!

Invader''s Realm
yeah but what can I do now to so I can use this class?
declare a pointer to a IDirect3DVertexBuffer8 manually and pass it on to the class, each time I want to create a quad?

edit => well I tried the above solution so now I create a vertexbuffer manually, and pass it on to my class, like so =>


  CQuad	Quad ;IDirect3DVertexBuffer8	*quad1_menu ;		// Pointer to a Vetex BufferQuad.InitQuad(quad1_menu, 312.0f, 334.0f, 400.0f, 100.0f, RGB(255, 255, 255)) ;Quad.DrawQuad(quad1_menu) ;Quad.ReleaseQuad(quad1_menu) ;   


now I get a LNK error 1000

Linking...
LINK : fatal error LNK1000: unknown error; consult documentation for technical support options
Error executing link.exe.

what could've gone wrong?!?

[edited by - da cobra on August 24, 2002 4:02:57 AM]
Advertisement
Everything looks ok to me. Here is a quote from MSDN that you might want to look into:

quote:
From MSDN on LNK1000

You may get this error if you mix standard header files (for example, dos.h) and your own files. #include the standard headers first, followed by your own header files.



The hackers must have gotten into the system through the hyperlink!!

Invader''s Realm
uhh where did you found that?

thanx will check my header files
nope that isn''t my problem

anyway I still haven''t really got a real answer to my question :

is it possible to use the same VertexBuffer for 2 objects that are at the same time at my screen if I put that VertexBuffer as a private member in my class, or do I have to declare a new VertexBuffer and then pass it on to my class?
I found that on MSDN by searching for "LNK 1000". Anyway, yes, you can use the same vb for multiple objects if a pointer to the vb is a private member. That way you can use 1 vb for multiple objects.

I give this option for my sprite class. I allow the user to assign an external vb for my sprites. They pass a pointer to it and I store the pointer. They also pass an offset in the vb that the sprites can write to so that the sprites don''t write over each other''s data.

A simple example could be:


  class foo{public:void AssignVB(IDirect3DVertexBuffer8 *new_vb){ the_vb = new_vb; }private:IDirect3DVertexBuffer8 *the_vb;};  


Of course, if you destroy the external vb and then try to do something in the class that has it then you''ll cause a lot of problems so you have to be careful in that respect. You could also have the member variable as a pointer to the pointer of the vb so that you could check if the external vb''s pointer has been set to NULL or not. (Of course, if you don''t set your pointers to NULL after you''ve made them invalid then you still have problems!)


The hackers must have gotten into the system through the hyperlink!!

Invader''s Realm

This topic is closed to new replies.

Advertisement