   d3d basic problem
  I've just started learning direct3d (dx 8.1) and I'm learning with the tutorial that comes with the SDK and I practicing it into my code but I found a problem, it won't run! I'm sure that I've followed the tutorial without mistake or atleast I think I am. Here's my code can you tell me where my problem is? It keeps terminating before showing something on my window.#include <windows.h>#include <d3d8.h>#pragma comment (lib,"dxguid.lib")#pragma comment (lib,"d3d8.lib")const int RES_WIDTH  = 800;const int RES_HEIGHT = 600;struct CUSTOMVERTEX{float x, y, z, rhw ;DWORD color ;};#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE)CUSTOMVERTEX g_Vertices[] ={    { 150.0f,  50.0f, 0.5f, 1.0f, 0xffff0000, }, // x, y, z, rhw, color    { 250.0f, 250.0f, 0.5f, 1.0f, 0xff00ff00, },    {  50.0f, 250.0f, 0.5f, 1.0f, 0xff00ffff, },};WNDCLASSEX g_win;HWND g_hWnd;IDirect3D8 *g_pD3D ;IDirect3DDevice8 *g_p3DDevice ;IDirect3DVertexBuffer8 *g_pVB;bool initWindow(LPCTSTR,DWORD,WNDPROC);bool initDirect3D( void );bool initVertices( void );void cleanup( void );void draw( void );LRESULT CALLBACK wndProc(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam);int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd){MSG Msg;initDirect3D();initVertices();initWindow("MainWindow",WS_POPUP,wndProc);while(GetMessage(&Msg,NULL,0,0)>0){TranslateMessage(&Msg);DispatchMessage(&Msg);}return 0;}LRESULT CALLBACK wndProc(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam){switch(Msg){case WM_CREATE  :draw();break;case WM_QUIT:case WM_DESTROY :cleanup();PostQuitMessage(0);break;}return DefWindowProc(hWnd,Msg,wParam,lParam);}bool initWindow(LPCTSTR lpszClassName,DWORD dwStyle,WNDPROC lpfnWinProc){memset(&g_win,0,sizeof(WNDCLASSEX));g_win.cbSize = sizeof(WNDCLASSEX);g_win.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);g_win.hCursor = NULL;g_win.hIcon = NULL;g_win.hIconSm = NULL;g_win.hInstance = GetModuleHandle(NULL);g_win.lpszClassName = lpszClassName; g_win.lpfnWndProc = lpfnWinProc;if(!RegisterClassEx(&g_win))return false;g_hWnd = CreateWindowEx(0,lpszClassName,"RZ's",dwStyle,0,0,RES_WIDTH,RES_HEIGHT,NULL,NULL,GetModuleHandle(NULL),0);if(g_hWnd == NULL)return false;ShowWindow(g_hWnd,SW_SHOW);return true;}bool initDirect3D( void ){g_pD3D = Direct3DCreate8(D3D_SDK_VERSION) ;if(g_pD3D == NULL)return false ;D3DDISPLAYMODE d3ddm ;if(FAILED(g_pD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT , &d3ddm ) ) )return false ;D3DPRESENT_PARAMETERS d3dpp ;ZeroMemory( &d3dpp , sizeof(d3dpp) ) ;d3dpp.Windowed = TRUE ;d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD ;d3dpp.BackBufferFormat = d3ddm.Format ;if( FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,g_hWnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp,&g_p3DDevice) ))return false ;return true ;}bool initVertices( void ){if(FAILED( g_p3DDevice->CreateVertexBuffer( 3*sizeof(CUSTOMVERTEX),0,D3DFVF_CUSTOMVERTEX,D3DPOOL_DEFAULT,&g_pVB) ))return false;VOID* pVertices;if( FAILED( g_pVB->Lock( 0, sizeof(g_Vertices), (BYTE**)&pVertices, 0 ) ) )return false;memcpy( pVertices, g_Vertices, sizeof(g_Vertices) );g_pVB->Unlock();return true;}void cleanup( void ){if(g_p3DDevice)g_p3DDevice->Release();if(g_pD3D)g_pD3D->Release();}void draw( void ){g_p3DDevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,255),1.0f,0L);g_p3DDevice->BeginScene();g_p3DDevice->SetStreamSource( 0, g_pVB, sizeof(CUSTOMVERTEX) );g_p3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,1);g_p3DDevice->EndScene();g_p3DDevice->Present(NULL,NULL,NULL,NULL);}[edited by - zacharya on February 9, 2004 10:37:51 PM]
  You forgot to call:g_p3DDevice->SetVertexShader(D3DFVF_CUSTOMVERTEX);EDIT: you should also release your vertex buffer when you're done with it   [edited by - Zorbfish on February 9, 2004 10:51:07 PM];  You have to create your window before you create your D3D device.   Stay Casual,KenDrunken Hyena;  I''ve already did add the SetVertexShader to my code but it still terminate.....here''s my updated code#include <windows.h>#include <d3d8.h>#pragma comment (lib,"dxguid.lib")#pragma comment (lib,"d3d8.lib")const int RES_WIDTH  = 800;const int RES_HEIGHT = 600;struct CUSTOMVERTEX{float x, y, z, rhw ;DWORD color ;};#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE)CUSTOMVERTEX g_Vertices[] ={    { 150.0f,  50.0f, 0.5f, 1.0f, 0xffff0000, }, // x, y, z, rhw, color    { 250.0f, 250.0f, 0.5f, 1.0f, 0xff00ff00, },    {  50.0f, 250.0f, 0.5f, 1.0f, 0xff00ffff, },};WNDCLASSEX g_win;HWND g_hWnd;IDirect3D8 *g_pD3D ;IDirect3DDevice8 *g_p3DDevice ;IDirect3DVertexBuffer8 *g_pVB;bool initWindow(LPCTSTR,DWORD,WNDPROC);bool initDirect3D( void );bool initVertices( void );void cleanup( void );void draw( void );LRESULT CALLBACK wndProc(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam);int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd){MSG Msg;initWindow("MainWindow",WS_OVERLAPPEDWINDOW,wndProc);while(GetMessage(&Msg,NULL,0,0)>0){TranslateMessage(&Msg);DispatchMessage(&Msg);}return 0;}LRESULT CALLBACK wndProc(HWND hWnd,UINT Msg,WPARAM wParam,LPARAM lParam){switch(Msg){case WM_CREATE  :initDirect3D();initVertices();draw();break;case WM_QUIT:case WM_CHAR :cleanup();PostQuitMessage(0);break;}return DefWindowProc(hWnd,Msg,wParam,lParam);}bool initWindow(LPCTSTR lpszClassName,DWORD dwStyle,WNDPROC lpfnWinProc){memset(&g_win,0,sizeof(WNDCLASSEX));g_win.cbSize = sizeof(WNDCLASSEX);g_win.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);g_win.hCursor = NULL;g_win.hIcon = NULL;g_win.hIconSm = NULL;g_win.hInstance = GetModuleHandle(NULL);g_win.lpszClassName = lpszClassName; g_win.lpfnWndProc = lpfnWinProc;if(!RegisterClassEx(&g_win))return false;g_hWnd = CreateWindowEx(0,lpszClassName,"RZ''s",dwStyle,0,0,RES_WIDTH,RES_HEIGHT,NULL,NULL,GetModuleHandle(NULL),0);if(g_hWnd == NULL)return false;ShowWindow(g_hWnd,SW_SHOW);return true;}bool initDirect3D( void ){g_pD3D = Direct3DCreate8(D3D_SDK_VERSION) ;if(g_pD3D == NULL)return false ;D3DDISPLAYMODE d3ddm ;if(FAILED(g_pD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT , &d3ddm ) ) )return false ;D3DPRESENT_PARAMETERS d3dpp ;ZeroMemory( &d3dpp , sizeof(d3dpp) ) ;d3dpp.Windowed = TRUE ;d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD ;d3dpp.BackBufferFormat = d3ddm.Format ;if( FAILED(g_pD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,g_hWnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp,&g_p3DDevice) ))return false ;return true ;}bool initVertices( void ){if(FAILED( g_p3DDevice->CreateVertexBuffer( 3*sizeof(CUSTOMVERTEX),0,D3DFVF_CUSTOMVERTEX,D3DPOOL_DEFAULT,&g_pVB) ))return false;VOID* pVertices;if( FAILED( g_pVB->Lock( 0, sizeof(g_Vertices), (BYTE**)&pVertices, 0 ) ) )return false;memcpy( pVertices, g_Vertices, sizeof(g_Vertices) );g_pVB->Unlock();return true;}void cleanup( void ){if(g_p3DDevice)g_p3DDevice->Release();if(g_pD3D)g_pD3D->Release();}void draw( void ){g_p3DDevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,255),1.0f,0L);g_p3DDevice->BeginScene();g_p3DDevice->SetVertexShader( D3DFVF_CUSTOMVERTEX );g_p3DDevice->SetStreamSource( 0, g_pVB, sizeof(CUSTOMVERTEX) );g_p3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,1);g_p3DDevice->EndScene();g_p3DDevice->Present(NULL,NULL,NULL,NULL);}[\code]   ;  Well, I always thought you had to use ''PeekMessage'', not ''GetMessage''. That''s what I''ve always done and it works just fine for me. if (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE)) {// Send the messageTranslateMessage(&msg);DispatchMessage(&msg);}I don''t know for sure that that''s the problem, but it''s an attempt. ;  still not working.....I''m desperate....btw how do you write your post in a text area like that? ;  Well, it''s used for placing source code in your posts. Well, it''s supposed to be. Anyhow, first you put in [ source ] without spaces. Then, you can type/copy-paste your code, then close it with [ /source ] without spaces. ;int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd){MSG Msg;initDirect3D();initVertices();initWindow("MainWindow",WS_POPUP,wndProc);while(GetMessage(&Msg,NULL,0,0)>0){TranslateMessage(&Msg);DispatchMessage(&Msg);}return 0;}//should beint APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd){MSG Msg;initDirect3D();initVertices();initWindow("MainWindow",WS_POPUP,wndProc);while(GetMessage(&Msg,NULL,0,0)>0){TranslateMessage(&Msg);DispatchMessage(&Msg);}else{draw();}return 0;}// Also change this:case WM_CREATE  :draw();break;// to this:case WM_CREATE  :break;WM_CREATE is called when the window is created, so directX hasn't been created yet!PS: To display it source code in the editor use the source tagLeft bracket source right bracket.   /*I use DirectX 8.1 and C++ (Microsoft Visual C++ 6.0 Professional edition)*/   [edited by - Programmer16 on February 9, 2004 11:51:29 PM] [edited by - Programmer16 on February 9, 2004 11:53:38 PM];  It works!So when WM_CREATE is passed the window is not created yet but is about to be created.thanks for the help, guys.;quote:Original post by SimDemon  Well, I always thought you had to use ''PeekMessage'', not ''GetMessage''. That''s what I''ve always done and it works just fine for me. if (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE)) {// Send the messageTranslateMessage(&msg);DispatchMessage(&msg);}I don''t know for sure that that''s the problem, but it''s an attempt.  PeekMessage is a better choice because it doesn''t wait for a message like GetMessage does. So, if you''re going to make a game, then PeekMessage would be a better choice. /*I use DirectX 8.1 and C++ (Microsoft Visual C++ 6.0 Professional edition)*/ 