Hi, I'm just trying to make a simple window with Direct3D 9. I get no errors, but the window made isn't a valid window. It's just a window that is always the same size, and any key exits (Not just escape) Here is the code without the resource and header files.
Windows.cpp
Code:
// INCLUDES ///////////////////////////////////////////////
#define WIN32_LEAN_AND_MEAN
#include "header.h"
// GLOBAL VARIABLES ///////////////////////////////
HWND g_hwnd = NULL; // save the window handle
LPDIRECT3D9 g_pD3D = NULL; // Direct3D 9 interface object pointer
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; // Direct3D 9 rendering component pointer
const char g_szClassName[] = "myWindowClass";
// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_KEYDOWN:
{
switch(wParam)
{
case VK_ESCAPE:
PostQuitMessage(0);
break;
}
}
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
MSG Msg;
//Step 1: Registering the Window Class
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_DIRECTX_ICON);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(hInstance, (LPCTSTR)IDI_DIRECTX_ICON);
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
// Step 2: Creating the Window
g_hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
WINDOW_TITLE,
WS_POPUP | WS_SYSMENU | WS_VISIBLE,
0, 0, WINDOW_WIDTH, WINDOW_HEIGHT,
NULL, NULL, hInstance, NULL);
if(g_hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(g_hwnd, nCmdShow);
UpdateWindow(g_hwnd);
// perform all game console specific initialization
init();
// enter main event loop
while(1)
{
if (PeekMessage(&Msg,NULL,0,0,PM_REMOVE))
{
// test if this is a quit
if (Msg.message == WM_QUIT)
break;
// translate any accelerator keys
TranslateMessage(&Msg);
// send the message to the window proc
DispatchMessage(&Msg);
} // end if
// main game processing goes here
render();
} // end while
// shutdown game and release all resources
shutdown();
// return to Windows like this
return(Msg.wParam);
} // end WinMain
Directx.cpp
Code:
#include "header.h"
void init()
{
g_pD3D = Direct3DCreate9( D3D_SDK_VERSION );
if( g_pD3D == NULL )
return;
D3DDISPLAYMODE d3ddm;
if( FAILED( g_pD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &d3ddm ) ) )
return;
HRESULT hr;
if( FAILED( hr = g_pD3D->CheckDeviceFormat( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
d3ddm.Format, D3DUSAGE_DEPTHSTENCIL,
D3DRTYPE_SURFACE, D3DFMT_D16 ) ) )
{
if( hr == D3DERR_NOTAVAILABLE )
// POTENTIAL PROBLEM: We need at least a 16-bit z-buffer!
return;
}
//
// Do we support hardware vertex processing? if so, use it.
// If not, downgrade to software.
//
D3DCAPS9 d3dCaps;
if( FAILED( g_pD3D->GetDeviceCaps( D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL, &d3dCaps ) ) )
return;
DWORD dwBehaviorFlags = 0;
if( d3dCaps.VertexProcessingCaps != 0 )
dwBehaviorFlags |= D3DCREATE_HARDWARE_VERTEXPROCESSING;
else
dwBehaviorFlags |= D3DCREATE_SOFTWARE_VERTEXPROCESSING;
//
// Everything checks out - create a simple, windowed device.
//
D3DPRESENT_PARAMETERS d3dpp;
memset(&d3dpp, 0, sizeof(d3dpp));
d3dpp.BackBufferFormat = d3ddm.Format;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.Windowed = TRUE;
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, g_hwnd,
dwBehaviorFlags, &d3dpp, &g_pd3dDevice ) ) )
return;
}
void shutdown()
{
if( g_pd3dDevice != NULL )
g_pd3dDevice->Release();
if( g_pD3D != NULL )
g_pD3D->Release();
}
void render()
{
g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
D3DCOLOR_COLORVALUE(0.0f,0.0f,0.0f,1.0f), 1.0f, 0 );
g_pd3dDevice->BeginScene();
// Render geometry here...
g_pd3dDevice->EndScene();
g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}
Thanks for the help.