Skip to main content
GameDev.net gamedev.net
🔒 Locked

returning pointer to customvertex buffer

Started by Nuklear Sep 27, 2006 at 10:59 AM 1 replies 1.5k views
Original Post
Nuklear
Nuklear
What iI'm trying to achieve is to create a class that will create its own vertex buffer for the object it describes (a square in this case), then to return pointer to that buffer so I can memcpy it to my main vertex buffer and then render it. This is my block.h:
/* Defines a single block */
class cBlock
{
  public:
  // Our basic functions for initialization/drawing/destroying
  void Create();
  void Destroy();

  // Constructor & Destructor
  //cBlock();
  //virtual ~cBlock();

  // Get/Set procedures for block position
  int GetPosX();
  int GetPosY();
  CUSTOMVERTEX &vertbuffer();

  void SetPosX(int x);
  void SetPosY(int y);

  private:
  // Block position on the game’ grid
  int m_iPosX, m_iPosY;
  RECT rBlock;
};
And my block.cpp:
int cBlock::GetPosX()
{
	return m_iPosX;
}

int cBlock::GetPosY()
{
	return m_iPosY;
}

int * cBlock::vertbuffer()
{
	// Calculate block rect using pre-defined variables
	rBlock.left = m_iPosX * (BLOCK_SIZE + BLOCK_SPACING) + BLOCK_SPACING;
	rBlock.right = rBlock.left + BLOCK_SIZE - BLOCK_SPACING;
	rBlock.bottom = m_iPosY * (BLOCK_SIZE + BLOCK_SPACING) - BLOCK_SPACING;
	rBlock.top = rBlock.bottom - BLOCK_SIZE + BLOCK_SPACING;
	
	//Fill out vertex buffer for block
	CUSTOMVERTEX vertbuffer[4] =
	{
		{rBlock.left, rBlock.bottom, 5.0f, 1.0f, float(D3DCOLOR_XRGB(255, 255, 255))},
		{rBlock.left, rBlock.top, 5.0f, 1.0f, float(D3DCOLOR_XRGB(255, 255, 255))},
		{rBlock.right, rBlock.bottom, 5.0f, 1.0f, float(D3DCOLOR_XRGB(255, 255, 255))},
		{rBlock.right, rBlock.top, 5.0f, 1.0f, float(D3DCOLOR_XRGB(255, 255, 255))},
	};
	
	//Return pointer to vertex buffer
	CUSTOMVERTEX * pVertbuffer = &vertbuffer[0];
	return *pVertbuffer;
}

void cBlock::SetPosX(int x)
{
	m_iPosX = x;
}

void cBlock::SetPosY(int y)
{
	m_iPosY = y;
}
However when I try to compile it (VC++ 2005) i get the following errors:
e:\documents and settings\adnan\my documents\visual studio 2005\projects\tetris\tetris\block.h(38) : error C2556: 'int *cBlock::vertbuffer(void)' : overloaded function differs only by return type from 'CUSTOMVERTEX &cBlock::vertbuffer(void)'
        e:\documents and settings\adnan\my documents\visual studio 2005\projects\tetris\tetris\block.h(16) : see declaration of 'cBlock::vertbuffer'
e:\documents and settings\adnan\my documents\visual studio 2005\projects\tetris\tetris\block.h(38) : error C2372: 'cBlock::vertbuffer' : redefinition; different types of indirection
        e:\documents and settings\adnan\my documents\visual studio 2005\projects\tetris\tetris\block.h(16) : see declaration of 'cBlock::vertbuffer'
Any suggestions of where I'm going wrong?
ext
ext
In your class definition you define the vertbuffer()-function to return a reference to CUSTOMVERTEX and in your implementation you define the vertbuffer function to return an pointer to int.
In C++ it's not allowed to overload functions by return-type (only), so the compiler sees your different function-prototypes and causes this error.
To solve this you have to change the return-type to be the same in your class definition and your function implementation.
Namethatnobodyelsetook
Namethatnobodyelsetook
Besides the compile errors, there are some big problems with the code. The data is not guaranteed to exist once the function exits, as it's made on the stack. Never return pointers to, or references to, data created on the stack.

Topic Locked

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

Sign in to reply to this topic.