Why doesn't the following work?

Hiya guys,

For some reason in the following code, the square doesn't show up no matter what I do! So could someone please point out my problem?

[code]
#include
#include
#include
#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")
#define ScreenW 640
#define ScreenH 480
#define CustomFVF (D3DFVF_XYZ | D3DFVF_TEX1)

const char *ClsName = "BasicApp";
const char *WndName = "DirectX";

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
VOID InitD3D();
VOID InitGraphics();
VOID Display();
VOID CleanProgram();

struct CustomV
{

FLOAT x, y, z;
FLOAT u, v;

};

LPDIRECT3D9 D3D;
LPDIRECT3DDEVICE9 D3DDev;
LPDIRECT3DVERTEXBUFFER9 SquareB;
LPDIRECT3DTEXTURE9 TileT;
D3DXMATRIX ViewM, ProjectionM;
D3DPRESENT_PARAMETERS D3DPP;
HINSTANCE hInstance;
HWND hWnd;
DWORD StartingCount;
VOID* VertMemLoc;
CustomV Square[] =
{

{3.0f, 3.0f, 1.0f, 0, 0, },
{-3.0f, 3.0f, 1.0f, 1, 0, },
{3.0f, -3.0f, 1.0f, 0, 1, },
{-3.0f, -3.0f, 1.0f, 1, 1, },

};

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{

MSG Msg;
WNDCLASSEX WndClsEx;

// Create the application window
WndClsEx.cbSize = sizeof(WNDCLASSEX);
WndClsEx.style = CS_HREDRAW | CS_VREDRAW;
WndClsEx.lpfnWndProc = WndProc;
WndClsEx.cbClsExtra = 0;
WndClsEx.cbWndExtra = 0;
WndClsEx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndClsEx.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClsEx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
WndClsEx.lpszMenuName = NULL;
WndClsEx.lpszClassName = ClsName;
WndClsEx.hInstance = hInstance;
WndClsEx.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

// Register the application
RegisterClassEx(&WndClsEx);

// Create the window object
hWnd = CreateWindowEx(WS_EX_TOPMOST,
ClsName,
WndName,
WS_POPUP,
0,
0,
ScreenW,
ScreenH,
NULL,
NULL,
hInstance,
NULL);

// Find out if the window was created
if( !hWnd ) // If the window was not created,
return 0; // stop the application

// Display the window to the user
ShowWindow(hWnd, SW_SHOWNORMAL);
UpdateWindow(hWnd);
InitD3D();

// Decode and treat the messages
// as long as the application is running
while(true)
{

StartingCount = GetTickCount();

if (PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE))
{

if (Msg.message == WM_QUIT) break;

TranslateMessage(&Msg);
DispatchMessage(&Msg);

}

Display();

while((GetTickCount() - StartingCount) < 25);

}

CleanProgram();

return Msg.wParam;

}

LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{

switch (Msg)
{

case WM_DESTROY :

PostQuitMessage(WM_QUIT);

break;

case WM_CLOSE :

DestroyWindow(hWnd);

break;

case WM_KEYUP :

switch(wParam)
{

case VK_ESCAPE :

DestroyWindow(hWnd);

break;

}

break;

}

return DefWindowProc(hWnd, Msg, wParam, lParam);

}

VOID InitD3D()
{

D3D = Direct3DCreate9(D3D_SDK_VERSION);

ZeroMemory(&D3DPP, sizeof(D3DPP));

D3DPP.Windowed = false;
D3DPP.SwapEffect = D3DSWAPEFFECT_DISCARD;
D3DPP.hDeviceWindow = hWnd;
D3DPP.BackBufferWidth = ScreenW;
D3DPP.BackBufferHeight = ScreenH;
D3DPP.BackBufferFormat = D3DFMT_X8R8G8B8;
D3DPP.EnableAutoDepthStencil = true;
D3DPP.AutoDepthStencilFormat = D3DFMT_D16;

D3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &D3DPP, &D3DDev);

InitGraphics();

}

VOID InitGraphics()
{

D3DDev->CreateVertexBuffer(4 * sizeof(CustomV), NULL, CustomFVF, D3DPOOL_MANAGED, &SquareB, NULL);
SquareB->Lock(NULL, NULL, &VertMemLoc, NULL);

memcpy(VertMemLoc, Square, sizeof(Square));

SquareB->Unlock();
D3DDev->SetRenderState(D3DRS_LIGHTING, false);
D3DDev->SetRenderState(D3DRS_ZENABLE, true);

D3DXCreateTextureFromFile(D3DDev, "C:\Tile.bmp", &TileT);

}

VOID Display()
{

D3DXMatrixLookAtLH(&ViewM, &D3DXVECTOR3(0.0f, 0.0f, 10.0f), &D3DXVECTOR3(0.0f, 0.0f, 0.0f), &D3DXVECTOR3(0.0f, 1.0f, 0.0f));
D3DXMatrixPerspectiveFovLH(&ProjectionM, D3DXToRadian(45), (FLOAT)ScreenW / (FLOAT)ScreenH, 1.0f, 100.0f);

D3DDev->Clear(NULL, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, NULL);
D3DDev->BeginScene();
D3DDev->SetTransform(D3DTS_VIEW, &ViewM);
D3DDev->SetTransform(D3DTS_PROJECTION, &ProjectionM);
D3DDev->SetFVF(CustomFVF);
D3DDev->SetStreamSource(NULL, SquareB, NULL, sizeof(Square));
D3DDev->SetTexture(NULL, TileT);
D3DDev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
D3DDev->EndScene();
D3DDev->Present(NULL, NULL, NULL, NULL);

}

VOID CleanProgram()
{

D3D->Release();
D3DDev->Release();
SquareB->Release();
TileT->Release();

}
[/code]

Any ideas?

Thanks.

Comments

  • It looks fine - try modifying at and eye vectors of the following.

    D3DXMatrixLookAtLH(&ViewM, &D3DXVECTOR3(0.0f, 0.0f, 10.0f), &D3DXVECTOR3(0.0f, 0.0f, 0.0f), &D3DXVECTOR3(0.0f, 1.0f, 0.0f));

    Also check if you get a triangle without rotating it and using triangles and not a strip.

    : Hiya guys,
    :
    : For some reason in the following code, the square doesn't show up no
    : matter what I do! So could someone please point out my problem?
    :
    : [code]:
    : #include
    : #include
    : #include
    : #pragma comment (lib, "d3d9.lib")
    : #pragma comment (lib, "d3dx9.lib")
    : #define ScreenW 640
    : #define ScreenH 480
    : #define CustomFVF (D3DFVF_XYZ | D3DFVF_TEX1)
    :
    : const char *ClsName = "BasicApp";
    : const char *WndName = "DirectX";
    :
    : LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
    : VOID InitD3D();
    : VOID InitGraphics();
    : VOID Display();
    : VOID CleanProgram();
    :
    : struct CustomV
    : {
    :
    : FLOAT x, y, z;
    : FLOAT u, v;
    :
    : };
    :
    : LPDIRECT3D9 D3D;
    : LPDIRECT3DDEVICE9 D3DDev;
    : LPDIRECT3DVERTEXBUFFER9 SquareB;
    : LPDIRECT3DTEXTURE9 TileT;
    : D3DXMATRIX ViewM, ProjectionM;
    : D3DPRESENT_PARAMETERS D3DPP;
    : HINSTANCE hInstance;
    : HWND hWnd;
    : DWORD StartingCount;
    : VOID* VertMemLoc;
    : CustomV Square[] =
    : {
    :
    : {3.0f, 3.0f, 1.0f, 0, 0, },
    : {-3.0f, 3.0f, 1.0f, 1, 0, },
    : {3.0f, -3.0f, 1.0f, 0, 1, },
    : {-3.0f, -3.0f, 1.0f, 1, 1, },
    :
    : };
    :
    : int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    : {
    :
    : MSG Msg;
    : WNDCLASSEX WndClsEx;
    :
    : // Create the application window
    : WndClsEx.cbSize = sizeof(WNDCLASSEX);
    : WndClsEx.style = CS_HREDRAW | CS_VREDRAW;
    : WndClsEx.lpfnWndProc = WndProc;
    : WndClsEx.cbClsExtra = 0;
    : WndClsEx.cbWndExtra = 0;
    : WndClsEx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    : WndClsEx.hCursor = LoadCursor(NULL, IDC_ARROW);
    : WndClsEx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    : WndClsEx.lpszMenuName = NULL;
    : WndClsEx.lpszClassName = ClsName;
    : WndClsEx.hInstance = hInstance;
    : WndClsEx.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
    :
    : // Register the application
    : RegisterClassEx(&WndClsEx);
    :
    : // Create the window object
    : hWnd = CreateWindowEx(WS_EX_TOPMOST,
    : ClsName,
    : WndName,
    : WS_POPUP,
    : 0,
    : 0,
    : ScreenW,
    : ScreenH,
    : NULL,
    : NULL,
    : hInstance,
    : NULL);
    :
    : // Find out if the window was created
    : if( !hWnd ) // If the window was not created,
    : return 0; // stop the application
    :
    : // Display the window to the user
    : ShowWindow(hWnd, SW_SHOWNORMAL);
    : UpdateWindow(hWnd);
    : InitD3D();
    :
    : // Decode and treat the messages
    : // as long as the application is running
    : while(true)
    : {
    :
    : StartingCount = GetTickCount();
    :
    : if (PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE))
    : {
    :
    : if (Msg.message == WM_QUIT) break;
    :
    : TranslateMessage(&Msg);
    : DispatchMessage(&Msg);
    :
    : }
    :
    : Display();
    :
    : while((GetTickCount() - StartingCount) < 25);
    :
    : }
    :
    : CleanProgram();
    :
    : return Msg.wParam;
    :
    : }
    :
    : LRESULT CALLBACK WndProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
    : {
    :
    : switch (Msg)
    : {
    :
    : case WM_DESTROY :
    :
    : PostQuitMessage(WM_QUIT);
    :
    : break;
    :
    : case WM_CLOSE :
    :
    : DestroyWindow(hWnd);
    :
    : break;
    :
    : case WM_KEYUP :
    :
    : switch(wParam)
    : {
    :
    : case VK_ESCAPE :
    :
    : DestroyWindow(hWnd);
    :
    : break;
    :
    : }
    :
    : break;
    :
    : }
    :
    : return DefWindowProc(hWnd, Msg, wParam, lParam);
    :
    : }
    :
    : VOID InitD3D()
    : {
    :
    : D3D = Direct3DCreate9(D3D_SDK_VERSION);
    :
    : ZeroMemory(&D3DPP, sizeof(D3DPP));
    :
    : D3DPP.Windowed = false;
    : D3DPP.SwapEffect = D3DSWAPEFFECT_DISCARD;
    : D3DPP.hDeviceWindow = hWnd;
    : D3DPP.BackBufferWidth = ScreenW;
    : D3DPP.BackBufferHeight = ScreenH;
    : D3DPP.BackBufferFormat = D3DFMT_X8R8G8B8;
    : D3DPP.EnableAutoDepthStencil = true;
    : D3DPP.AutoDepthStencilFormat = D3DFMT_D16;
    :
    : D3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &D3DPP, &D3DDev);
    :
    : InitGraphics();
    :
    : }
    :
    : VOID InitGraphics()
    : {
    :
    : D3DDev->CreateVertexBuffer(4 * sizeof(CustomV), NULL, CustomFVF, D3DPOOL_MANAGED, &SquareB, NULL);
    : SquareB->Lock(NULL, NULL, &VertMemLoc, NULL);
    :
    : memcpy(VertMemLoc, Square, sizeof(Square));
    :
    : SquareB->Unlock();
    : D3DDev->SetRenderState(D3DRS_LIGHTING, false);
    : D3DDev->SetRenderState(D3DRS_ZENABLE, true);
    :
    : D3DXCreateTextureFromFile(D3DDev, "C:\Tile.bmp", &TileT);
    :
    : }
    :
    : VOID Display()
    : {
    :
    : D3DXMatrixLookAtLH(&ViewM, &D3DXVECTOR3(0.0f, 0.0f, 10.0f), &D3DXVECTOR3(0.0f, 0.0f, 0.0f), &D3DXVECTOR3(0.0f, 1.0f, 0.0f));
    : D3DXMatrixPerspectiveFovLH(&ProjectionM, D3DXToRadian(45), (FLOAT)ScreenW / (FLOAT)ScreenH, 1.0f, 100.0f);
    :
    : D3DDev->Clear(NULL, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, NULL);
    : D3DDev->BeginScene();
    : D3DDev->SetTransform(D3DTS_VIEW, &ViewM);
    : D3DDev->SetTransform(D3DTS_PROJECTION, &ProjectionM);
    : D3DDev->SetFVF(CustomFVF);
    : D3DDev->SetStreamSource(NULL, SquareB, NULL, sizeof(Square));
    : D3DDev->SetTexture(NULL, TileT);
    : D3DDev->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
    : D3DDev->EndScene();
    : D3DDev->Present(NULL, NULL, NULL, NULL);
    :
    : }
    :
    : VOID CleanProgram()
    : {
    :
    : D3D->Release();
    : D3DDev->Release();
    : SquareB->Release();
    : TileT->Release();
    :
    : }
    : [/code]:
    :
    : Any ideas?
    :
    : Thanks.
    :
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion