#include <windows.h>
#include <d3d9.h>
#include <d3dx9.h>
#pragma comment(lib,"d3d9.lib")
#pragma comment(lib,"d3dx9.lib")
LPDIRECT3D9 Stranded_D3D = NULL;
LPDIRECT3DDEVICE9 Stranded_D3DDevice = NULL;
bool InitializeDirect3D(HWND hwnd,bool fullscreen);
void Render();
#define STRANDED_ENGINE "The stranded Engine V1.0"
#define D3DFVF_TRIANGLE (D3DFVF_XYZ | D3DFVF_DIFFUSE)
struct VERTEX
{
int X,Y,Z;
DWORD Color;
};
VERTEX Verts[] =
{
{320.0f, 50.0f, 1.0f, D3DCOLOR_XRGB(0, 0, 255),},
{520.0f, 400.0f, 1.0f, D3DCOLOR_XRGB(0, 255, 0),},
{120.0f, 400.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 0),},
};
void CreateTriangle();
void DrawTriangle();
LPDIRECT3DVERTEXBUFFER9 TriangleBuffer;
LRESULT WINAPI Stranded_Proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch( message )
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message,wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nShowCmd)
{
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = (WNDPROC)Stranded_Proc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = NULL;
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.lpszMenuName = NULL;
wc.lpszClassName = STRANDED_ENGINE;
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.hIconSm = NULL;
RegisterClassEx(&wc);
HWND hwnd;
hwnd = CreateWindowA(
STRANDED_ENGINE,
STRANDED_ENGINE,
WS_OVERLAPPEDWINDOW,
10,
10,
800,
600,
NULL,
NULL,
hInstance,
NULL);
MSG messages;
if(InitializeDirect3D(hwnd, false))
{
ShowWindow(hwnd,nShowCmd);
UpdateWindow(hwnd);
CreateTriangle();
ZeroMemory(&messages,sizeof(MSG));
while(messages.message != WM_QUIT)
{
if(PeekMessage(&messages,NULL,0,0,PM_REMOVE))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
else
{
Render();
}
}
}
return messages.wParam;
}
bool InitializeDirect3D(HWND hwnd, bool fullscreen)
{
D3DDISPLAYMODE Display;
Stranded_D3D = Direct3DCreate9(D3D_SDK_VERSION);
if(Stranded_D3D == NULL)
{
return 0;
}
if(FAILED(Stranded_D3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&Display)))
{
return 0;
}
D3DPRESENT_PARAMETERS Stranded_Present;
ZeroMemory(&Stranded_Present,sizeof(Stranded_Present));
if(fullscreen == 1)
{
Stranded_Present.Windowed = false;
Stranded_Present.BackBufferWidth = 800;
Stranded_Present.BackBufferHeight = 600;
}
else
{
Stranded_Present.Windowed = true;
}
Stranded_Present.SwapEffect = D3DSWAPEFFECT_DISCARD;
Stranded_Present.BackBufferFormat = Display.Format;
if(FAILED(Stranded_D3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hwnd,D3DCREATE_HARDWARE_VERTEXPROCESSING,
&Stranded_Present,&Stranded_D3DDevice)))
{
return 0;
}
return true;
}
void Render()
{
Stranded_D3DDevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0f,1);
if(Stranded_D3DDevice->BeginScene())
{
DrawTriangle();
Stranded_D3DDevice->EndScene();
}
Stranded_D3DDevice->Present(NULL,NULL,NULL,NULL);
}
void CreateTriangle()
{
Stranded_D3DDevice->CreateVertexBuffer(
sizeof(VERTEX),
0,
D3DFVF_TRIANGLE,
D3DPOOL_DEFAULT,
&TriangleBuffer,
NULL);
VOID* Temp = NULL;
TriangleBuffer->Lock(0,0,(VOID**)&Temp,D3DLOCK_DISCARD);
memcpy(Temp,Verts,sizeof(Verts));
TriangleBuffer->Unlock();
}
void DrawTriangle()
{
Stranded_D3DDevice->SetStreamSource(0,TriangleBuffer,0,sizeof(VERTEX));
Stranded_D3DDevice->SetFVF(D3DFVF_TRIANGLE);
Stranded_D3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,1);
}
the Triangle wont render
For some reason, the triangle wont render when i used the code from U;timate Game Programming with DirectX. Do you see anything wrong with this code?
is there anything wrong with that code? if so please let me know. did anyone else have some problems with the code from that book?
Well, it seems that the device is failing to render. Cause i put a FAILED(hr) with Present in it and told it to quit if that didn't work and it did. I put the device to NULL what more does the code need? should I do the new opertor and allocate memory?
You should always check return codes. Every call from DirectX that returns a HRESULT, you need to check it. If it fails, report the error. Debugging a problem like this in a large program will be impossible if you don't, trust me.
Nothin' doing. i put the FAILED on almost every HRESULT and nothing shows up. I doubt it's my system...I wanna know how i can fix this so i'll never get this kind of problem again.
as you can see, i did check with almost everything. other then this i don't know what else to do.
#include <windows.h>#include <d3d9.h>#include <d3dx9.h>#pragma comment(lib,"d3d9.lib")#pragma comment(lib,"d3dx9.lib")LPDIRECT3D9 Stranded_D3D;LPDIRECT3DDEVICE9 Stranded_D3DDevice;LPDIRECT3DSURFACE9 BackBuffer;bool InitializeDirect3D(HWND hwnd,bool fullscreen);void Render();#define STRANDED_ENGINE "The stranded Engine V1.0"#define D3DFVF_TRIANGLE (D3DFVF_XYZ | D3DFVF_DIFFUSE)struct VERTEX{ int X,Y,Z; DWORD Color;};VERTEX Verts[] = { {320.0f, 50.0f, 1.0f, D3DCOLOR_XRGB(0, 0, 255),}, {520.0f, 400.0f, 1.0f, D3DCOLOR_XRGB(0, 255, 0),}, {120.0f, 400.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 0),},};void CreateTriangle();void DrawTriangle();LPDIRECT3DVERTEXBUFFER9 TriangleBuffer;LRESULT WINAPI Stranded_Proc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){ switch( message ) { case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hwnd, message,wParam, lParam);}int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd){ WNDCLASSEX wc; wc.cbSize = sizeof(WNDCLASSEX); wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = (WNDPROC)Stranded_Proc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = NULL; wc.hCursor = LoadCursor(NULL,IDC_ARROW); wc.lpszMenuName = NULL; wc.lpszClassName = STRANDED_ENGINE; wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); wc.hIconSm = NULL; RegisterClassEx(&wc); HWND hwnd; hwnd = CreateWindowA( STRANDED_ENGINE, STRANDED_ENGINE, WS_OVERLAPPEDWINDOW, 10, 10, 800, 600, NULL, NULL, hInstance, NULL);MSG messages; if(InitializeDirect3D(hwnd, false)) { ShowWindow(hwnd,nShowCmd); UpdateWindow(hwnd); CreateTriangle(); ZeroMemory(&messages,sizeof(MSG)); while(messages.message != WM_QUIT) { if(PeekMessage(&messages,NULL,0,0,PM_REMOVE)) { TranslateMessage(&messages); DispatchMessage(&messages); } else { Render(); } } } return messages.wParam;}bool InitializeDirect3D(HWND hwnd, bool fullscreen){ D3DDISPLAYMODE Display; Stranded_D3D = Direct3DCreate9(D3D_SDK_VERSION); if(Stranded_D3D == NULL) { return 0; } if(FAILED(Stranded_D3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&Display))) { return 0; } D3DPRESENT_PARAMETERS Stranded_Present; ZeroMemory(&Stranded_Present,sizeof(Stranded_Present)); if(fullscreen == 1) { Stranded_Present.Windowed = false; Stranded_Present.BackBufferWidth = 800; Stranded_Present.BackBufferHeight = 600; } else { Stranded_Present.Windowed = true; } Stranded_Present.SwapEffect = D3DSWAPEFFECT_DISCARD; Stranded_Present.BackBufferFormat = D3DFMT_X8R8G8B8; Stranded_Present.hDeviceWindow = hwnd; if(FAILED(Stranded_D3D->CheckDeviceType(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,Display.Format,Display.Format,false))) { return 0; } if(FAILED(Stranded_D3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hwnd,D3DCREATE_HARDWARE_VERTEXPROCESSING, &Stranded_Present,&Stranded_D3DDevice))) { } if(FAILED(Stranded_D3DDevice->Clear(0,0,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0f,1))) { return 0; } if(FAILED(Stranded_D3DDevice->GetBackBuffer(0,0,D3DBACKBUFFER_TYPE_MONO,&BackBuffer))) { return 0; } return true;}void Render(){ if(FAILED(Stranded_D3DDevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0f,1))) { return; } if(Stranded_D3DDevice->BeginScene()) { DrawTriangle(); if(FAILED(Stranded_D3DDevice->EndScene())) { return; } } if(FAILED(Stranded_D3DDevice->Present(NULL,NULL,NULL,NULL))) { return; } }void CreateTriangle(){ if(FAILED(Stranded_D3DDevice->CreateVertexBuffer( 3*sizeof(VERTEX), 0, D3DFVF_TRIANGLE, D3DPOOL_DEFAULT, &TriangleBuffer, NULL))) { return; } VOID* Temp = NULL; TriangleBuffer->Lock(0,0,(VOID**)&Temp,D3DLOCK_DISCARD); memcpy(Temp,Verts,sizeof(Verts)); TriangleBuffer->Unlock();}void DrawTriangle(){ if(FAILED(Stranded_D3DDevice->SetStreamSource(0,TriangleBuffer,0,sizeof(VERTEX)))) { return; } if(FAILED(Stranded_D3DDevice->SetFVF(D3DFVF_TRIANGLE))) { return; } if(FAILED(Stranded_D3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,1))) { return; }}
as you can see, i did check with almost everything. other then this i don't know what else to do.
1. Your vertex structure is wrong. X, Y, and Z are float, not int.
2. Your BeginScene check is acting like it's getting a bool instead of an HRESULT. S_OK, D3D_OK, etc, are 0. Your BeginScene likely succeeds, you think it fails. Present then fails because you're presenting without calling EndScene. THE DEBUG RUNTIMES WOULD HAVE TOLD YOU THIS.
3. You make your vertex buffer large enough for a single vertex, which most definately isn't enough to hold the 3 you're putting into it.
4. Your windowed mode window won't hold an 800x600 backbuffer. 800x600 as passed to CreateWindow includes titlebars, borders, etc. Use AdjustRect to find the the size your window will need to be on all versions of Windows.
5. Don't call CreateWindowA. CreateWindow will be mapped to CreateWindowA or CreateWindowW depending on whether you've compiled for unicode or not. Hardcoding for the A version means you may have to change the code at some point in the future. Leaving off the A makes it just work.
There may be more, I just took a quick glance at the code. Enable the debug runtimes before doing anything else.
2. Your BeginScene check is acting like it's getting a bool instead of an HRESULT. S_OK, D3D_OK, etc, are 0. Your BeginScene likely succeeds, you think it fails. Present then fails because you're presenting without calling EndScene. THE DEBUG RUNTIMES WOULD HAVE TOLD YOU THIS.
3. You make your vertex buffer large enough for a single vertex, which most definately isn't enough to hold the 3 you're putting into it.
4. Your windowed mode window won't hold an 800x600 backbuffer. 800x600 as passed to CreateWindow includes titlebars, borders, etc. Use AdjustRect to find the the size your window will need to be on all versions of Windows.
5. Don't call CreateWindowA. CreateWindow will be mapped to CreateWindowA or CreateWindowW depending on whether you've compiled for unicode or not. Hardcoding for the A version means you may have to change the code at some point in the future. Leaving off the A makes it just work.
There may be more, I just took a quick glance at the code. Enable the debug runtimes before doing anything else.
Well, it looks like i needed to put the triangle in the middle of Begin()endScene. but i tried it a long time ago to put a function that rebnders then put what ever i needed to render ontop of the function. where.
CreateVertex()Render()void Render(){ BeginScene() EndScene() Present()[/zource]so why didn't it work this time?
That would not have worked. You must have been mistaken.
The documentation clearly states:
The documentation clearly states:
Quote:DirectX SDK Docs
Applications must call IDirect3DDevice9::BeginScene before performing any rendering and must call IDirect3DDevice9::EndScene when rendering is complete and before calling IDirect3DDevice9::BeginScene again.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement