Vertex Buffer Error

Started by
3 comments, last by deathknight2005 17 years, 4 months ago
I am attempting (so far with not too much luck) to make a Pacman game with DirectX and I am trying to add backgrounds but I keep hitting problem after problem, right now the screen is completely black and windowed mode isn't working. Also this is what the results of my debug are: 'Pacman.exe': Loaded 'C:\Documents and Settings\Compaq_Owner\My Documents\Visual Studio 2005\Projects\Pacman\debug\Pacman.exe', Symbols loaded. 'Pacman.exe': Loaded 'C:\WINDOWS\system32\ntdll.dll', No symbols loaded. 'Pacman.exe': Loaded 'C:\WINDOWS\system32\kernel32.dll', No symbols loaded. 'Pacman.exe': Loaded 'C:\WINDOWS\system32\user32.dll', No symbols loaded. 'Pacman.exe': Loaded 'C:\WINDOWS\system32\gdi32.dll', No symbols loaded. 'Pacman.exe': Loaded 'C:\WINDOWS\system32\d3d9.dll', No symbols loaded. 'Pacman.exe': Loaded 'C:\WINDOWS\system32\d3d8thk.dll', No symbols loaded. 'Pacman.exe': Loaded 'C:\WINDOWS\system32\msvcrt.dll', No symbols loaded. 'Pacman.exe': Loaded 'C:\WINDOWS\system32\advapi32.dll', No symbols loaded. 'Pacman.exe': Loaded 'C:\WINDOWS\system32\rpcrt4.dll', No symbols loaded. 'Pacman.exe': Loaded 'C:\WINDOWS\system32\version.dll', No symbols loaded. 'Pacman.exe': Loaded 'C:\WINDOWS\system32\winmm.dll', No symbols loaded. 'Pacman.exe': Loaded 'C:\WINDOWS\system32\d3dx9_30.dll', No symbols loaded. 'Pacman.exe': Loaded 'C:\WINDOWS\system32\dinput8.dll', No symbols loaded. 'Pacman.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.VC80.DebugCRT_1fc8b3b9a1e18e3b_8.0.50727.42_x-ww_f75eb16c\msvcr80d.dll', No symbols loaded. 'Pacman.exe': Loaded 'C:\WINDOWS\system32\uxtheme.dll', No symbols loaded. 'Pacman.exe': Loaded 'C:\WINDOWS\system32\MSCTF.dll', No symbols loaded. 'Pacman.exe': Loaded 'C:\WINDOWS\system32\d3d9d.dll', No symbols loaded. 'Pacman.exe': Loaded 'C:\WINDOWS\system32\d3dx9d_30.dll', No symbols loaded. Direct3D9: (INFO) :Direct3D9 Debug Runtime selected. D3D9 Helper: Enhanced D3DDebugging disabled; Application was not compiled with D3D_DEBUG_INFO Direct3D9: (INFO) :======================= Hal SWVP device selected Direct3D9: (INFO) :HalDevice Driver style 9 Direct3D9: (INFO) :Failed to create driver indexbuffer 'Pacman.exe': Loaded 'C:\WINDOWS\system32\hid.dll', No symbols loaded. 'Pacman.exe': Loaded 'C:\WINDOWS\system32\setupapi.dll', No symbols loaded. 'Pacman.exe': Loaded 'C:\WINDOWS\system32\wintrust.dll', No symbols loaded. 'Pacman.exe': Loaded 'C:\WINDOWS\system32\crypt32.dll', No symbols loaded. 'Pacman.exe': Loaded 'C:\WINDOWS\system32\msasn1.dll', No symbols loaded. 'Pacman.exe': Loaded 'C:\WINDOWS\system32\imagehlp.dll', No symbols loaded. Direct3D9: (WARN) :Stream 0 stride and vertex size, computed from the current vertex declaration or FVF, are different, which might not work with pre-DX8 drivers Direct3D9: (ERROR) :Stream 0 does not have required number of vertices First-chance exception at 0x7c812a5b in Pacman.exe: Microsoft C++ exception: long at memory location 0x0012facc.. Direct3D9: (ERROR) :DrawPrimitive failed. The program '[2952] Pacman.exe: Native' has exited with code 0 (0x0). Here is the code:

// include the basic windows header files and the Direct3D header file
#include <windows.h>
#include <windowsx.h>
#include <d3d9.h>
#include <d3dx9.h>
#include <dinput.h>
// define the screen resolution and keyboard macros
#define SCREEN_WIDTH  640
#define SCREEN_HEIGHT 480
#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
#define SAFE_RELEASE(n) if(n != NULL) {n->Release(); n = NULL;}
#define CUSTOMFVF (D3DFVF_XYZRHW | D3DFVF_DIFFUSE) 
#define MB(n, m) MessageBox(NULL, n,m, 0); 
// include the Direct3D Library file
#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")
#pragma comment (lib, "dinput.lib")
#pragma comment (lib, "dinput8.lib")
#pragma comment (lib, "dxguid.lib")

// global declarations
LPDIRECT3D9 d3d; // the pointer to our Direct3D interface
LPDIRECT3DDEVICE9 d3ddev; // the pointer to the device class
LPD3DXSPRITE PacSprite;
LPDIRECT3DTEXTURE9 PacTexture = NULL;
LPDIRECT3DTEXTURE9 BackTexture = NULL;
LPDIRECT3DVERTEXBUFFER9 t_buffer = NULL;    // the pointer to the vertex buffer
LPDIRECT3DSURFACE9 z_buffer = NULL;
LPDIRECTINPUT8 din;    // the pointer to our DirectInput interface
LPDIRECTINPUTDEVICE8 dinkeyboard;
struct CUSTOMVERTEX {FLOAT X, Y, Z; DWORD COLOR;};

FLOAT posX, posY, posZ;
FLOAT speed;
FLOAT sChange = 0;
FLOAT sChange2 = 0;
bool movingLeft, movingRight, movingUp, movingDown;
bool fullscreen = true;
bool firstTime = true;
HWND hWnd;
RECT spriteType;
  CUSTOMVERTEX t_vert[] = 
    {
        { 0.0f, 0.0f, 50.0f, D3DCOLOR_XRGB(0, 0, 255), },
        { 0.0f, 480.0f, 50.0f, D3DCOLOR_XRGB(0, 255, 0), },
        { 640.f, 0.0f, 50.0f, D3DCOLOR_XRGB(255, 0, 0), },
		{ 640.f, 480.0f, 50.0f, D3DCOLOR_XRGB(255, 0, 0), },
    };
// function prototypes
void initD3D(HWND hWnd); // sets up and initializes Direct3D
void render_frame(void); // renders a single frame
void cleanD3D(void); // closes Direct3D and releases memory
void initDInput(HINSTANCE hInstance, HWND hWnd);    // sets up and initializes DirectInput
bool detect_keys(BYTE DIKVal);    // gets the current keys being pressed
void cleanDInput(void);
void wrapRoom();
void switchMode();
void setLogic(void);
void movementLogic(void);
void TranslationLogic(void);
void switchPacPics(void);
void init_graphics(void); 
void draw_background(void);
// the WindowProc function prototype
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);


// the entry point for any Windows program
int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
{
	
    WNDCLASSEX wc;

    ZeroMemory(&wc, sizeof(WNDCLASSEX));

    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = (WNDPROC)WindowProc;
    wc.hInstance = hInstance;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    // wc.hbrBackground = (HBRUSH)COLOR_WINDOW;    // not needed any more
    wc.lpszClassName = L"WindowClass1";

    RegisterClassEx(&wc);

    hWnd = CreateWindowEx(NULL,
                          L"WindowClass1",
                          L"Our Direct3D Program",
                          WS_EX_TOPMOST | WS_POPUP,    // fullscreen values
                          0, 0,    // the starting x and y positions should be 0
                          SCREEN_WIDTH, SCREEN_HEIGHT,    // set the window to 640 x 480
                          NULL,
                          NULL,
                          hInstance,
                          NULL);

    ShowWindow(hWnd, nCmdShow);

    // set up and initialize Direct3D
    initD3D(hWnd);
    initDInput(hInstance, hWnd);
   
    // enter the main loop:

    MSG msg;

    while(TRUE)
    {
        DWORD starting_point = GetTickCount();

        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            if (msg.message == WM_QUIT)
                break;

            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }

        render_frame();
        if(detect_keys(DIK_SPACE)) 
			switchMode();
        // check the 'escape' key
        if(KEY_DOWN(VK_ESCAPE))
            PostMessage(hWnd, WM_DESTROY, 0, 0);

        while ((GetTickCount() - starting_point) < 25);
    }

    // clean up DirectX and COM
    cleanD3D();
    cleanDInput();
    return msg.wParam;
}


// this is the main message handler for the program
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
        case WM_DESTROY:
            {
                PostQuitMessage(0);
                return 0;
            } break;
    }

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


// this function initializes and prepares Direct3D for use
void initD3D(HWND hWnd)
{
    d3d = Direct3DCreate9(D3D_SDK_VERSION); // create the Direct3D interface

    D3DPRESENT_PARAMETERS d3dpp; // create a struct to hold various device information

    ZeroMemory(&d3dpp, sizeof(d3dpp));    // clear out the struct for use
    d3dpp.Windowed = FALSE;    // program fullscreen, not windowed
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;    // discard old frames
    d3dpp.hDeviceWindow = hWnd;    // set the window to be used by Direct3D
    d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;    // set the back buffer format to 32-bit
    d3dpp.BackBufferWidth = SCREEN_WIDTH;    // set the width of the buffer
    d3dpp.BackBufferHeight = SCREEN_HEIGHT;    // set the height of the buffer
    d3dpp.EnableAutoDepthStencil = TRUE;
    d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
     // create a device class using this information and the info from the d3dpp stuct
    d3d->CreateDevice(D3DADAPTER_DEFAULT,
                      D3DDEVTYPE_HAL,
                      hWnd,
                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                      &d3dpp,
                      &d3ddev);
	
	D3DXIMAGE_INFO d3dxImageInfo;

	D3DXCreateTextureFromFileEx(d3ddev, L"pacman.bmp", 
		                         335, 60,
								 1, 
		                         D3DPOOL_DEFAULT,
                                 D3DFMT_UNKNOWN,
                                 D3DPOOL_DEFAULT,
                                 D3DX_DEFAULT,
                                 D3DX_DEFAULT,
                                 D3DCOLOR_COLORVALUE(0.0f,0.0f,0.0f,1.0f),
                                 &d3dxImageInfo,
                                 NULL,
                                 &PacTexture);
    D3DXIMAGE_INFO d3dxImageInfo2;
  
	D3DXCreateTextureFromFileEx(d3ddev, L"PacBack.bmp", 
		                         640, 480,
								 1, 
		                         D3DPOOL_DEFAULT,
                                 D3DFMT_UNKNOWN,
                                 D3DPOOL_DEFAULT,
                                 D3DX_DEFAULT,
                                 D3DX_DEFAULT,
                                 D3DCOLOR_COLORVALUE(0.0f,0.0f,0.0f,1.0f),
                                 &d3dxImageInfo2,
                                 NULL,
                                 &BackTexture);
	d3ddev->CreateDepthStencilSurface(SCREEN_WIDTH,    // the z-buffer width
                                  SCREEN_HEIGHT,    // the z-buffer height
                                  D3DFMT_D16,    // 16-bit pixel format for the z-buffer
                                  D3DMULTISAMPLE_NONE,    // no multisampling
                                  0,    // no multisampling quality
                                  TRUE,    // discard old buffer data
                                  &z_buffer,    // the address of the buffer
                                  NULL);    // reserved...we don't use this.
								  
    d3ddev->SetRenderState(D3DRS_ZENABLE, TRUE);
	d3ddev->SetRenderState(D3DRS_LIGHTING, FALSE);
	init_graphics();    
	D3DXCreateSprite(d3ddev, &PacSprite);
	
    return;
}


// this is the function used to render a single frame
void render_frame(void)
{
    // clear the window to a deep blue
    d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 40, 100), 1.0f, 0);
    d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
    d3ddev->BeginScene();    // begins the 3D scene
    draw_background();
    PacSprite->Begin(D3DXSPRITE_ALPHABLEND);
   
    setLogic();
    movementLogic();
	TranslationLogic();
	switchPacPics();
	D3DXVECTOR3 vPosition(posX, posY, posZ);

    PacSprite->Draw(PacTexture, &spriteType, NULL, &vPosition, D3DCOLOR_COLORVALUE(1.0f,1.0f,1.0f,1.0f));
    

	PacSprite->End();
    
    d3ddev->EndScene();    // ends the 3D scene

    d3ddev->Present(NULL, NULL, NULL, NULL);   // displays the created frame on the screen

    return;
}


// this is the function that cleans up Direct3D and COM
void cleanD3D(void)
{
    SAFE_RELEASE(d3ddev); // close and release the 3D device
    SAFE_RELEASE(d3d); // close and release Direct3D
    SAFE_RELEASE(PacSprite);
	
	//SAFE_RELEASE(PacTexture);
	//SAFE_RELEASE(BackTexture);
	SAFE_RELEASE(z_buffer);
	
    return;
}
void initDInput(HINSTANCE hInstance, HWND hWnd)
{
    // create the DirectInput interface
    DirectInput8Create(hInstance,    // the handle to the application
                       DIRECTINPUT_VERSION,    // the compatible version
                       IID_IDirectInput8,    // the DirectInput interface version
                       (void**)&din,    // the pointer to the interface
                       NULL);    // COM stuff, so we'll set it to NULL

    // create the keyboard device
    din->CreateDevice(GUID_SysKeyboard,    // the default keyboard ID being used
                      &dinkeyboard,    // the pointer to the device interface
                      NULL);    // COM stuff, so we'll set it to NULL

    dinkeyboard->SetDataFormat(&c_dfDIKeyboard); // set the data format to keyboard format

    // set the control you will have over the keyboard
    dinkeyboard->SetCooperativeLevel(hWnd,
                                     DISCL_NONEXCLUSIVE | DISCL_BACKGROUND);

      return;    // return to WinMain()
}
bool detect_keys(BYTE DIKVal)
{
    static BYTE keystate[256];    // create a static storage for the key-states

    dinkeyboard->Acquire();    // get access if we don't have it already

    dinkeyboard->GetDeviceState(256, (LPVOID)keystate);    // fill keystate with values

    return (keystate[DIKVal] & 0x80);    // if the 'A' key was pressed...
}
void cleanDInput(void)
{
    dinkeyboard->Unacquire();    // make sure the keyboard is unacquired
    SAFE_RELEASE(din);    // close DirectInput before exiting

    return;
}
void switchMode(void)
{
	if(fullscreen == true)
	{
PacSprite->OnLostDevice();
SAFE_RELEASE(PacTexture);
SAFE_RELEASE(BackTexture);
SAFE_RELEASE(t_buffer);
D3DPRESENT_PARAMETERS d3dpp; // create a struct to hold various device information
    ZeroMemory(&d3dpp, sizeof(d3dpp));    // clear out the struct for use
    d3dpp.Windowed = TRUE;    // program fullscreen, not windowed
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;    // discard old frames
    d3dpp.hDeviceWindow = hWnd;    // set the window to be used by Direct3D
    d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;    // set the back buffer format to 32-bit
    d3dpp.BackBufferWidth = SCREEN_WIDTH;    // set the width of the buffer
    d3dpp.BackBufferHeight = SCREEN_HEIGHT;    // set the height of the buffer
    d3dpp.EnableAutoDepthStencil = TRUE;
    d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
	d3ddev->Reset(&d3dpp);
PacSprite->OnResetDevice();
D3DXIMAGE_INFO d3dxImageInfo;

	D3DXCreateTextureFromFileEx(d3ddev, L"pacman.bmp", 
		                         335, 60,
								 1, 
		                         D3DPOOL_DEFAULT,
                                 D3DFMT_UNKNOWN,
                                 D3DPOOL_DEFAULT,
                                 D3DX_DEFAULT,
                                 D3DX_DEFAULT,
                                 D3DCOLOR_COLORVALUE(0.0f,0.0f,0.0f,1.0f),
                                 &d3dxImageInfo,
                                 NULL,
                                 &PacTexture);
	
 D3DXIMAGE_INFO d3dxImageInfo2;

	D3DXCreateTextureFromFileEx(d3ddev, L"PacBack.bmp", 
		                         640, 480,
								 1, 
		                         D3DPOOL_DEFAULT,
                                 D3DFMT_UNKNOWN,
                                 D3DPOOL_DEFAULT,
                                 D3DX_DEFAULT,
                                 D3DX_DEFAULT,
                                 D3DCOLOR_COLORVALUE(0.0f,0.0f,0.0f,1.0f),
                                 &d3dxImageInfo2,
                                 NULL,
                                 &BackTexture);
d3ddev->CreateVertexBuffer(4*sizeof(CUSTOMVERTEX),
                               0,
                               CUSTOMFVF,
                               D3DPOOL_MANAGED,
                               &t_buffer,
                               NULL);
VOID* pVoid;    // a void pointer

    // lock t_buffer and load the vertices into it
    t_buffer->Lock(0, 0, (void**)&pVoid, 0);
    memcpy(pVoid, t_vert, sizeof(t_vert));
    t_buffer->Unlock();
fullscreen = false;
}
	else
	{
PacSprite->OnLostDevice();

SAFE_RELEASE(PacTexture);
SAFE_RELEASE(BackTexture);
SAFE_RELEASE(t_buffer);
 D3DPRESENT_PARAMETERS d3dpp; // create a struct to hold various device information

    ZeroMemory(&d3dpp, sizeof(d3dpp));    // clear out the struct for use
    d3dpp.Windowed = FALSE;    // program fullscreen, not windowed
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;    // discard old frames
    d3dpp.hDeviceWindow = hWnd;    // set the window to be used by Direct3D
    d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;    // set the back buffer format to 32-bit
    d3dpp.BackBufferWidth = SCREEN_WIDTH;    // set the width of the buffer
    d3dpp.BackBufferHeight = SCREEN_HEIGHT;    // set the height of the buffer
d3ddev->Reset(&d3dpp);
PacSprite->OnResetDevice();

D3DXIMAGE_INFO d3dxImageInfo;

	D3DXCreateTextureFromFileEx(d3ddev, L"pacman.bmp", 
		                         335, 60,
								 1, 
		                         D3DPOOL_DEFAULT,
                                 D3DFMT_UNKNOWN,
                                 D3DPOOL_DEFAULT,
                                 D3DX_DEFAULT,
                                 D3DX_DEFAULT,
                                 D3DCOLOR_COLORVALUE(0.0f,0.0f,0.0f,1.0f),
                                 &d3dxImageInfo,
                                 NULL,
                                 &PacTexture);
	D3DXIMAGE_INFO d3dxImageInfo2;

	D3DXCreateTextureFromFileEx(d3ddev, L"PacBack.bmp", 
		                         640, 480,
								 1, 
		                         D3DPOOL_DEFAULT,
                                 D3DFMT_UNKNOWN,
                                 D3DPOOL_DEFAULT,
                                 D3DX_DEFAULT,
                                 D3DX_DEFAULT,
                                 D3DCOLOR_COLORVALUE(0.0f,0.0f,0.0f,1.0f),
                                 &d3dxImageInfo2,
                                 NULL,
                                 &BackTexture);

	d3ddev->CreateVertexBuffer(4*sizeof(CUSTOMVERTEX),
                               0,
                               CUSTOMFVF,
                               D3DPOOL_MANAGED,
                               &t_buffer,
                               NULL);

    VOID* pVoid;    // a void pointer

    // lock t_buffer and load the vertices into it
    t_buffer->Lock(0, 0, (void**)&pVoid, 0);
    memcpy(pVoid, t_vert, sizeof(t_vert));
    t_buffer->Unlock();
	fullscreen = true;
	}

	DWORD dwstyle = fullscreen ? WS_POPUP : WS_OVERLAPPEDWINDOW;
SetWindowLong(hWnd, GWL_STYLE, dwstyle);
SetWindowPos(hWnd, NULL, 0, 0, 0, 0,SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED);

}
void setLogic(void)
{
 if (firstTime == true)
	{
	posX = 200.0f;
	posY = 300.0f;
	posZ = 1.0f;
	 movingRight = false;
	movingLeft = false;
	movingUp = false;
	movingDown = false;
	firstTime = false;
	speed = 3.0f;
	}
   
if(detect_keys(DIK_LEFT))
	{
    movingRight = false;
	movingLeft = true;
	movingUp = false;
	movingDown = false;
    
	}
  else if(detect_keys(DIK_RIGHT))
	{
    movingRight = true;
	movingLeft = false;
	movingUp = false;
	movingDown = false;
	}
  else if(detect_keys(DIK_UP))
	{
     movingRight = false;
	movingLeft = false;
	movingUp = true;
	movingDown = false;
	}
  else if(detect_keys(DIK_DOWN))
	{
    movingRight = false;
	movingLeft = false;
	movingUp = false;
	movingDown = true;
	}
}
void movementLogic(void)
{
if(movingRight == true)
	{
posX += speed;
	}
	else if (movingLeft == true)
	{
  posX -= speed;
	}
	else if (movingUp == true)
	{
    posY -= speed;
	}
	else if (movingDown == true)
	{
    posY += speed;
	}
	
}
void TranslationLogic(void)
{

  if (posX < -70.0f)
  {
	posX = SCREEN_WIDTH;
  }
  else if (posX > SCREEN_WIDTH)
  {
    posX = -70.0f;
  }
  else if (posY < -70.0f)
  {
   posY = SCREEN_HEIGHT;
  }
  else if (posY > SCREEN_HEIGHT)
  {
  posY = -70.0f;
  }
}
void switchPacPics(void)
{
    if (movingRight == true)
	{
		spriteType.left = 0;
		spriteType.right = 67;
		spriteType.top = 0;
		spriteType.bottom = 60;
    }
	else if (movingLeft == true)
	{
		spriteType.left = 67;
		spriteType.right = 134;
		spriteType.top = 0;
		spriteType.bottom = 60;

	}
	else if (movingUp == true)
	{
		spriteType.left = 134;
		spriteType.right = 201;
		spriteType.top = 0;
		spriteType.bottom = 60;

	}
	else if (movingDown == true)
	{
		spriteType.left = 201;
		spriteType.right = 268;
		spriteType.top = 0;
		spriteType.bottom = 60;
   }
	else {
		spriteType.left = 268;
		spriteType.right = 335;
		spriteType.top = 0;
		spriteType.bottom = 60;
    
	}
}
void init_graphics(void)
{

    // create a vertex buffer interface called t_buffer
    d3ddev->CreateVertexBuffer(4*sizeof(CUSTOMVERTEX),
                               0,
                               CUSTOMFVF,
                               D3DPOOL_MANAGED,
                               &t_buffer,
                               NULL);

    VOID* pVoid;    // a void pointer

    // lock t_buffer and load the vertices into it
    t_buffer->Lock(0, 0, (void**)&pVoid, 0);
    memcpy(pVoid, t_vert, sizeof(t_vert));
    t_buffer->Unlock();

    return;
}
void draw_background(void){
d3ddev->SetFVF(CUSTOMFVF);

d3ddev->SetStreamSource(0, t_buffer, 0, sizeof(CUSTOMVERTEX));
d3ddev->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);
}

Note: The program was working fine when the only features I had was the picture changed when I moved the pacman, it wrapped around the room and when it could change from fullscreen to windowed and vice versa.
Advertisement
Some points:
  • You have a memory leak there - You're telling D3D to create a depth-stencil surface, and then you go and create one and replace the pointer D3D has. Setting the EnableAutoDepthStencil member of the presentation parameters will cause D3D to create a depth-stencil surface for you, you don't need to create one as well.

  • You should really be using D3DPOOL_MANAGED for your textures. D3DPOOL_DEFAULT tells D3D that you want to manage memory yourself, usually if you're creating render targets or other more advanced stuff.

  • You're not checking return values for anything. You should especially be checking the return values for functions that create other resources. If CreateVertexBuffer() fails, you're going to access a duff pointer and your app will blow up with a nice Windows "The application has performed an illegal operation" error.

  • The error you describe is because you're only putting 4 vertices into the buffer, but you're telling D3D to draw two triangles, which is 6 vertices. D3D won't like that, for obvious reasons [smile]. Either put in 6 vertices for two triangles, or draw the 4 vertices as a triangle strip. I'd recommend going for the two seperate triangles.

  • The warning in the debug output is because you're telling D3D that your vertex format has X, Y, Z cooridnates, then a RHW value, then a colour. But the struct you pass only has X, Y, Z and colour. You need to add another float after the Z coordinate for RHW. More details can be found in the SDK docs under "using transformed vertices" or something like that.

  • You might as well add D3D_DEBUG_INFO to your preprocessor defines. That'll let you look at some member variables of D3D interfaces, which may help with debugging.

Thanks a lot, windowed mode is actually working! Unfortunately I am still getting these debug errors:
The thread 'Win32 Thread' (0x660) has exited with code 0 (0x0).
Direct3D9: (INFO) :MemFini!
Direct3D9: (WARN) :Memory still allocated! Alloc count = 141
Direct3D9: (WARN) :Current Process (pid) = 00000694
Direct3D9: (WARN) :Memory Address: 00ab4aa8 lAllocID=1 dwSize=000047f8, (pid=00000694)
Direct3D9: (WARN) : Stack Back Trace
Direct3D9: (ERROR) : [0] : Address 00C7D4CB
Direct3D9: (ERROR) : [1] : Address 00C7D59B
Direct3D9: (ERROR) : [2] : Address 00C7D440
Direct3D9: (ERROR) : [3] : Address 00C71D44
Direct3D9: (ERROR) : [4] : Address 4FDFAF2E
Direct3D9: (ERROR) : [5] : Address 00413AC8
Direct3D9: (ERROR) : [6] : Address 00415FAE
Direct3D9: (ERROR) : [7] : Address 00414316
Direct3D9: (ERROR) : [8] : Address 0041407D
Direct3D9: (ERROR) : [9] : Address 7C816FD7
Direct3D9: (ERROR) : [10] : Address 00000000
Direct3D9: (ERROR) : [11] : Address 00000000
Direct3D9: (ERROR) : [12] : Address 00000000
Direct3D9: (ERROR) : [13] : Address 00000000
Direct3D9: (ERROR) : [14] : Address 00000000
Direct3D9: (ERROR) : [15] : Address 00000000
Direct3D9: (WARN) :Memory Address: 00abb040 lAllocID=9 dwSize=00000d14, (pid=00000694)
Direct3D9: (WARN) : Stack Back Trace
Direct3D9: (ERROR) : [0] : Address 00C74064
Direct3D9: (ERROR) : [1] : Address 00C74E4F
Direct3D9: (ERROR) : [2] : Address 00C75588
Direct3D9: (ERROR) : [3] : Address 00C71D65
Direct3D9: (ERROR) : [4] : Address 4FDFAF2E
Direct3D9: (ERROR) : [5] : Address 00413AC8
Direct3D9: (ERROR) : [6] : Address 00415FAE
Direct3D9: (ERROR) : [7] : Address 00414316
Direct3D9: (ERROR) : [8] : Address 0041407D
Direct3D9: (ERROR) : [9] : Address 7C816FD7
Direct3D9: (ERROR) : [10] : Address 00000000
Direct3D9: (ERROR) : [11] : Address 00000000
Direct3D9: (ERROR) : [12] : Address 00000000
Direct3D9: (ERROR) : [13] : Address 00000000
Direct3D9: (ERROR) : [14] : Address 00000000
Direct3D9: (ERROR) : [15] : Address 00000000
Direct3D9: (WARN) :Memory Address: 00abaed8 lAllocID=10 dwSize=00000008, (pid=00000694)
Direct3D9: (WARN) : Stack Back Trace
Direct3D9: (ERROR) : [0] : Address 00C74152
Direct3D9: (ERROR) : [1] : Address 00C74E4F
Direct3D9: (ERROR) : [2] : Address 00C75588
Direct3D9: (ERROR) : [3] : Address 00C71D65
Direct3D9: (ERROR) : [4] : Address 4FDFAF2E
Direct3D9: (ERROR) : [5] : Address 00413AC8
Direct3D9: (ERROR) : [6] : Address 00415FAE
Direct3D9: (ERROR) : [7] : Address 00414316
Direct3D9: (ERROR) : [8] : Address 0041407D
Direct3D9: (ERROR) : [9] : Address 7C816FD7
Direct3D9: (ERROR) : [10] : Address 00000000
Direct3D9: (ERROR) : [11] : Address 00000000
Direct3D9: (ERROR) : [12] : Address 00000000
Direct3D9: (ERROR) : [13] : Address 00000000
Direct3D9: (ERROR) : [14] : Address 00000000
Direct3D9: (ERROR) : [15] : Address 00000000
Direct3D9: (WARN) :Memory Address: 00abbdc8 lAllocID=11 dwSize=00000660, (pid=00000694)
Direct3D9: (WARN) : Stack Back Trace
And it is repeating that a lot.
Also my background still isn't showing.
You're not releasing at least one interface. Since D3D is based on COM, and COM objects are reference counted, holding on to one texture means that your device, backbuffer, and D3D interface won't get released.

As it is, you're not releasing your textures before exiting (I'm assuming you've fixed the depthbuffer double-allocation).

As for the background not showing, try disabling culling with:
d3ddev->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
If that causes the background to show, you're drawing your triangles the wrong way. D3D culls anti-clockwise triangles by default, and I think your vertices are specified in anti-clockwise order. Can we see how you're declaring your vertices now?
I am declaring them like this:
CUSTOMVERTEX t_vert[] =
{
{ 0.0f, 0.0f, 50.0f, 1.0f, D3DCOLOR_XRGB(0, 0, 255), },
{ 0.0f, 480.0f, 50.0f, 1.0f,D3DCOLOR_XRGB(0, 255, 0), },
{ 640.f, 0.0f, 50.0f,1.0f, D3DCOLOR_XRGB(255, 0, 0), },
{ 640.f, 480.0f, 50.0f,1.0f, D3DCOLOR_XRGB(255, 0, 0), },
};
And I tried it disabling this: d3ddev->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
but that isn't working either.
Edit: The SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
is working now, so I'll switch around the vertices.

[Edited by - deathknight2005 on November 29, 2006 10:40:28 AM]

This topic is closed to new replies.

Advertisement