Skip to main content
gamedev.net
🔒 Locked

CreateVertexBuffer()

Started by shanelaffond13 Aug 1, 2006 at 8:59 AM 4 replies 1.5k views
Original Post
shanelaffond13
shanelaffond13
Hi. Im trying to create a vertex buffer so I can draw textures in 2D. Im using the Code::Blocks IDE with the dev-pack for DirectX. Here is my code: Main.cpp

/**************************************************
Main.cpp

DirectX Dice by Shane Laffond

Required libraries:
  D3D9.LIB and D3DX9.LIB
**************************************************/

// Include files
#include <windows.h>
#include <stdio.h>
#include "Graphics.h"

// Window handles, class and caption text
cMain Main;
cTexture Texture;
cVertexBuffer V;

BYTE *PTR;

IDirect3DTexture9 *one=NULL;
IDirect3DTexture9 *two=NULL;
IDirect3DTexture9 *three=NULL;
IDirect3DTexture9 *four=NULL;
IDirect3DTexture9 *five=NULL;

IDirect3DVertexBuffer9 *VBone;

sVertex Vone[1] = {
    { 0.0f, 0.0f, 0.0f, 0.0f,  }};
    //{ 32.0f, 0.0f, 0.0f },
    //{ 64.0f, 0.0f, 0.0f },
    //{ 96.

// Window handles, class and caption text
HWND          hWnd;
HINSTANCE     g_hInst;
static char   g_szClass[]   = "Dice";
static char   g_szCaption[] = "Dice - a Rolling Numbers Game";

// Function prototypes
int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev,                             LPSTR szCmdLine, int nCmdShow);
long FAR PASCAL WindowProc(HWND hWnd, UINT uMsg,                                         WPARAM wParam, LPARAM lParam);

BOOL DoInit();
BOOL DoShutdown();
BOOL DoFrame();

int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev,                             LPSTR szCmdLine, int nCmdShow)
{
  WNDCLASSEX wcex;
  MSG        Msg;

  g_hInst = hInst;

  // Create the window class here and register it
  wcex.cbSize        = sizeof(wcex);
  wcex.style         = CS_CLASSDC;
  wcex.lpfnWndProc   = WindowProc;
  wcex.cbClsExtra    = 0;
  wcex.cbWndExtra    = 0;
  wcex.hInstance     = hInst;
  wcex.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
  wcex.hCursor       = LoadCursor(NULL, IDC_ARROW);
  wcex.hbrBackground = NULL;
  wcex.lpszMenuName  = NULL;
  wcex.lpszClassName = g_szClass;
  wcex.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
  if(!RegisterClassEx(&wcex))
    return FALSE;

  // Create the Main Window
  hWnd = CreateWindow(g_szClass, g_szCaption,
        WS_CAPTION | WS_SYSMENU,
        0, 0, 400, 400,
        NULL, NULL,
        hInst, NULL );
  if(!hWnd)
    return FALSE;
  ShowWindow(hWnd, SW_NORMAL);
  UpdateWindow(hWnd);

  // Run init function and return on error
  if(DoInit() == FALSE)
    return FALSE;

  // Start message pump, waiting for signal to quit
  ZeroMemory(&Msg, sizeof(MSG));
  while(Msg.message != WM_QUIT) {
    if(PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE)) {
      TranslateMessage(&Msg);
      DispatchMessage(&Msg);
    }
    if(DoFrame() == FALSE)
      break;
  }

  // Run shutdown function
  DoShutdown();

  UnregisterClass(g_szClass, hInst);

  return Msg.wParam;
}

long FAR PASCAL WindowProc(HWND hWnd, UINT uMsg,                                         WPARAM wParam, LPARAM lParam)
{
  switch(uMsg) {
    case WM_DESTROY:
      PostQuitMessage(0);
      return 0;

    case WM_KEYUP:
        switch(wParam)
        {
            case 0x1B:  //Escape Key
                DoShutdown();
                PostQuitMessage(0);
                break;
        }
        break;

  }

  return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

BOOL DoInit()
{

  CoInitialize(NULL);


  Main.Init();
  Main.SetMode(hWnd);
  ShowCursor(FALSE);

  //if(SUCCEEDED(V.Create(Main.m_pD3DDevice, VBone)))
  //{
  //V.Lock(PTR, VBone);
  //memcpy(PTR, Vone, sizeof(Vone));
  //V.Unlock(VBone);
  //}

  Texture.Load(Main.m_pD3DDevice, "Res/one.bmp", FALSE, D3DFMT_R8G8B8, one);
  //Texture.Draw(Main.m_pD3DDevice, one, VBone);

  return TRUE;
}

BOOL DoShutdown()
{
  //VBone->Release();
  //VBone=NULL;

  Main.Shutdown();
  CoUninitialize();
  return TRUE;
}

BOOL DoFrame()
{
return TRUE;
}



Graphics.h

/**************************************************
Graphics.cpp

Required libraries:
  D3D9.LIB and D3DX9.LIB
**************************************************/

#include "d3d9.h"
#include "d3dx9.h"
#include "objbase.h"

#define ReleaseCOM(x) if(x) { x->Release(); x = NULL; }
typedef struct {
  FLOAT x, y, z;     // 2-D coordinates
  FLOAT rhw;         // rhw
  FLOAT u, v;        // Texture coordinates
} sVertex;
#define FVF (D3DFVF_XYZRHW | D3DFVF_TEX1)

class cMain;
class cTexture;
class cFont;
class cVertexBuffer;

class cMain
{
	public:
        HWND                m_hWnd;
		IDirect3D9       	*m_pD3D;
		IDirect3DDevice9 	*m_pD3DDevice;

		D3DDISPLAYMODE   	m_d3ddm;

		D3DPRESENT_PARAMETERS m_d3dpp;

		D3DFORMAT		 	m_d3dFormat;

		BOOL 				Init();
		BOOL				Shutdown();


		BOOL				SetMode(HWND hWnd);

		BOOL				ClrScr();
};

class cTexture
{

    public:
    BOOL Load(IDirect3DDevice9 *Graphics, char *Filename, DWORD Transparent, D3DFORMAT Format, IDirect3DTexture9 *Texture);
	BOOL Draw( IDirect3DDevice9 *Graphics, IDirect3DTexture9 *Texture, IDirect3DVertexBuffer9 *Buffer );

};

class cFont
{

};


class cVertexBuffer
{
  public:

    BOOL cVertexBuffer::Create(IDirect3DDevice9 *Graphics, IDirect3DVertexBuffer9 *VB);

    BOOL cVertexBuffer::Lock(BYTE *Ptr, IDirect3DVertexBuffer9 *VB);
    BOOL  Unlock(IDirect3DVertexBuffer9 *VB);
};



and Graphics.cpp

#include "Graphics.h"

#include "rmxftmpl.h"
#include "rmxfguid.h"

BOOL cMain::Init()
{
    m_d3dFormat = D3DFMT_A8R8G8B8;

	if(FAILED(m_pD3D = Direct3DCreate9(D3D_SDK_VERSION)))
	{
		PostQuitMessage(0);
	}

	m_d3ddm.Width = 640;
	m_d3ddm.Height = 480;
	m_d3ddm.RefreshRate = 0;
	m_d3ddm.Format = m_d3dFormat;

}

BOOL cMain::SetMode(HWND hWnd)
{

	// Clear presentation structure
	ZeroMemory(&m_d3dpp, sizeof(D3DPRESENT_PARAMETERS));

	m_d3dpp.Windowed   = FALSE;
	m_d3dpp.SwapEffect = D3DSWAPEFFECT_FLIP;

	m_d3dpp.BackBufferWidth  = 640;
	m_d3dpp.BackBufferHeight = 480;
	m_d3dpp.FullScreen_RefreshRateInHz      = D3DPRESENT_RATE_DEFAULT;
	m_d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
	m_d3dpp.BackBufferFormat = m_d3ddm.Format;

	if(m_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &m_d3dpp, &m_pD3DDevice))
		{
			ReleaseCOM(m_pD3D);
			PostQuitMessage(0);
		}

}

BOOL cMain::Shutdown()
{
     ReleaseCOM(m_pD3DDevice);
     ReleaseCOM(m_pD3D);
}


BOOL cMain::ClrScr()
{
	m_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_RGBA(0,0,0,0), 1.0f, 0);
}

BOOL cTexture::Load(IDirect3DDevice9 *Graphics, char *Filename, DWORD Transparent, D3DFORMAT Format, IDirect3DTexture9 *Texture)
{


  if(FAILED(D3DXCreateTextureFromFileEx(Graphics,
    Filename, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT,
    0, Format, D3DPOOL_MANAGED,
    D3DX_FILTER_TRIANGLE, D3DX_FILTER_TRIANGLE,
    Transparent, NULL, NULL, &Texture)))
      return FALSE;


  return TRUE;
}

BOOL cTexture::Draw( IDirect3DDevice9 *Graphics, IDirect3DTexture9 *Texture, IDirect3DVertexBuffer9 *Buffer )
{
	Graphics->BeginScene();

	Graphics->SetTexture(0, Texture);

	Graphics->SetStreamSource(0, Buffer, 0, sizeof(sVertex));

	Graphics->SetFVF(FVF);

	Graphics->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 4);

	Graphics->EndScene();

	Graphics->SetTexture(0, NULL);

	Graphics->Present(NULL,NULL,NULL,NULL);
}


BOOL cVertexBuffer::Create(IDirect3DDevice9 *Graphics, IDirect3DVertexBuffer9 *VB)
{
    int bob = (sizeof(VB)/sizeof(sVertex)) * sizeof(sVertex);
  if(FAILED(Graphics->CreateVertexBuffer(bob, D3DUSAGE_WRITEONLY, FVF, D3DPOOL_MANAGED, &VB, NULL)))
    return FALSE;

  return TRUE;
}


BOOL cVertexBuffer::Lock(BYTE *Ptr, IDirect3DVertexBuffer9 *VB)
{
  if(FAILED(VB->Lock(0,
            0, (void**)&Ptr,
            0)))
    return FALSE;


  return TRUE;
}

BOOL cVertexBuffer::Unlock(IDirect3DVertexBuffer9 *VB)
{
  VB->Unlock();

  return TRUE;
}



I can't create a vertex buffer with CreateVertexBuffer. The program compiles, but it returns an error after trying to create the vertex buffer. It says that Dice DirectX needs to close. Im on windows, so the dialog asks if I want to send an error report. I don't know what I'm doing wrong. Please Help Me. Thanks in advance. [Edited by - shanelaffond13 on August 1, 2006 9:43:03 AM]
shanelaffond13
jollyjeffers
jollyjeffers
I've corrected your post, but for future reference these forums use [source] ... [/source] tags for posting code.

That aside, I'm tempted to close this thread as breaking 5 of the forum guidelines. Instead, please reply back (or edit your original post) to contain something useful. Help us to help you.

You say you're trying to create a vertex buffer. hmm... woohoo!... I presume you didn't create this thread just to tell us what you're doing today, in which case, what is your problem?

Compile errors? Runtime errors? If so, which line(s) and what are the exact messages?

What have you done to try and diagnose or fix the problem? Run with the reference rasterizer, the debug runtimes, stepped through with a debugger?


I dont like to sound bad tempered, but if you'd like myself or other forum members to volunteer their time to help you out the least you can do is help them by providing usable/helpful information [wink]

Cheers,
Jack
<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ |
Evil Steve
Evil Steve
int bob = (sizeof(VB)/sizeof(sVertex)) * sizeof(sVertex);
That's wrong for a start. sizeof(VB) is going to be 4, because it's a pointer (Assuming a 32-bit compiler). That line should probably be:
int bob = numVerts * sizeof(sVertex);
Where numVerts is the number of vertices you want to put into the vertex buffer.

I'm assuming this is just a silly mistake, but if it's not you really should get a good grasp of C++ before attempting DirectX. It just gets more complicated from here on...
DXnut
DXnut
Also, you can't change a pointer within a function and return it (as changed) to the caller, unless you pass a pointer to it (a pointer to a pointer). That is why so many DX interfaces take a pointer to a pointer in their parameter lists. When you just pass a pointer, the function can change what the pointer points at, but any changes to the pointer itself will be lost. It will create a temporary variable within your function for the pointer.

So you would have to do this:

BOOL cVertexBuffer::Create(IDirect3DDevice9 *Graphics, IDirect3DVertexBuffer9 **VB)
{
if(FAILED(Graphics->CreateVertexBuffer(bob, D3DUSAGE_WRITEONLY, FVF, D3DPOOL_MANAGED, VB, NULL)))
return FALSE;
}
--------------------------Most of what I know came from Frank D. Luna's DirectX books
shanelaffond13
shanelaffond13
Can you delete this post??? I have moved on. Thanks
shanelaffond13
Evil Steve
Evil Steve
It's best to leave topics in the forums, to help other people who come across the same problem. They don't take up much space in the database.

Topic Locked

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

Sign in to reply to this topic.