Skip to main content
gamedev.net
🔒 Locked

Quad class problem

Started by Fuzztrek Oct 6, 2002 at 1:16 PM 1 replies 250+ views
Original Post
Fuzztrek
Fuzztrek
Hey all, I didn''t know what to put for the subject, so I just put that (although it probably doesn''t really imply the problem that I have.) Anyway, I have quad class. Basically this quad class can create and draw either a flat (untextured) quad, or a textured quad. I let the programmer decide on what type they would like to create by letting them set a flag when they call the function. Anyway, I have an if statement that checks the flag''s value (type) and then creates veticies based on that type of structure (which i have defined in a header file). However, later on when i try to lock and copy the vertices into the vertex buffer, i get a compiler error saying that my verticies are undeclared. Does that mean that variables in the if statement have a specific scope? I thought it was only for functions and such. anyway.. here is my code:
  
// Init everything

HRESULT D3D_quad::Init(QUAD_TYPE type, float width, float height, float z, D3D_material * material, D3D_texture * texture){

	Free();

	DWORD tempFVF = 0;

	if((pro_lpGMain == NULL) || (pro_lpGMain->GetDevice == NULL))
		return E_FAIL;


	// Find the type of quad

	if(type == QUAD_TEX3D){						// Textured (3D)

		
		// Get vertex size

		pro_VertSize = sizeof(TEX3D);

		// Get number of vertices

		pro_NumVerts = 4 * pro_VertSize;

		// Create vertices

		TEX3D Verts[4] = {
			{0.0f, 0.0f, z, 0.0f, 0.0f},
			{width, 0.0f, z, 1.0f, 0.0f},
			{0.0f, height, z, 0.0f, 1.0f},
			{width, height, z, 1.0f, 1.0f}
		};

		// Get FVF

		tempFVF =  FVF_TEX3D;

	}else if(type == QUAD_FLAT3D){				// Flat (no texture) (3D)


		// Get vertex size

		pro_VertSize = sizeof(FLAT3D);

		// Get number of vertices

		pro_NumVerts = 4 * pro_VertSize;

		// Create vertices

		FLAT3D Verts[4] = {
			{0.0f, 0.0f, z},
			{width, 0.0f, z},
			{0.0f, height, z},
			{width, height, z}
		};
		
		// Get FVF

		tempFVF =  FVF_FLAT3D;
	
	}else
		return E_FAIL;
		

	// Create the vertex buffer

	if(FAILED(pro_lpGMain->GetDevice()->CreateVertexBuffer(pro_NumVerts, 0, tempFVF, D3DPOOL_DEFAULT, &pro_lpVB)))
		return E_FAIL;

	// Lock

	if(FAILED(Lock()))
		return E_FAIL;

	else{

		// Load vertices into buffer (error occures here)

		memcpy(pro_Ptr, Verts, sizeof(Verts));
	
		// Unlock

		if(FAILED(Unlock()))
			return E_FAIL;
	}

	// Clear temp FVF, set loaded to true and set class FVF to temp FVF.

	tempFVF	   = 0;
	pro_Loaded = true;
	pro_FVF    = tempFVF;

	return S_OK;
}
  
Thanks in advance!! ¬_¬
Krunk
Krunk
Yes, automatic variables are scoped within the enclosing {} block. By the time you reach the memcpy, whichever Verts array you made has gone out of scope.

[edited by - Krunk on October 6, 2002 3:34:51 PM]
Fuzztrek
Fuzztrek
hm.. so is the only solution to insert the vertex buffer locking code into the if statements? So I have it twice? I don''t mind doing that but i just thought that if i didn''t have to repeat something twice it would cut down on the overall file size. Guess its not going to make that big of a difference.

Thanks for replying tho!!


Fuzztrek

¬_¬

Topic Locked

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

Sign in to reply to this topic.