since i saw a lot of posts say that directx has to do with games, so ima post this here. all this does is inits directx. let me know what u think. i dont wanna get started with more directx stuff if the basics isnt good.
Code:#include <windows.h>
#include <d3d9.h>
//d3d9.lib d3dx9.lib winmm.lib mental note: always link
HWND MainWindowHandle = 0;
D3DCAPS9 caps;
IDirect3D9 *d3d9 = 0;
int Run()
{
MSG msg;
::ZeroMemory(&msg,sizeof(MSG));
while(::GetMessage(&msg,0,0,0))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
return (INT)msg.wParam;
}
LRESULT CALLBACK WndProc(HWND windowHandle, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_KEYDOWN:
{
if(wParam == VK_ESCAPE)
::DestroyWindow(MainWindowHandle);
/*if(wParam == VK_RETURN)
MessageBox(0,"Hello Steph","You Swamper",MB_OK);
if(wParam == 'M')
MessageBox(0,"Button M pressed","Button hit",MB_OK);
if(wParam == 'N')
MessageBox(0,"Button n pressed","Button hit",MB_OK);
else
MessageBox(0,"Unknown Command","Button hit",MB_OK);*/
break;
}
case WM_DESTROY:
{
::PostQuitMessage(0);
break;
}
}
return ::DefWindowProc(windowHandle,msg,wParam,lParam);
}
bool InitWindowsApp(HINSTANCE instanceHandle, int nCmdShow)
{
WNDCLASS wc; //windows class
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = instanceHandle;
wc.hCursor = ::LoadCursor(0, IDC_ARROW);
wc.hIcon = ::LoadIcon(0, IDI_APPLICATION);
wc.hbrBackground=
static_cast<HBRUSH>(::GetStockObject(WHITE_BRUSH));
wc.lpszMenuName = 0;
wc.lpszClassName= "Test";
if(!::RegisterClass(&wc))
{
::MessageBox(0,"Regester class failed", "FAILED1", MB_OK);
return false;
}
MainWindowHandle = ::CreateWindow("Test",
"Hello World",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
0,
0,
instanceHandle,
0);
if(MainWindowHandle == 0)
{
MessageBox(0,"Create Window failed", "FAILED", MB_OK);
return false;
}
::ShowWindow(MainWindowHandle,nCmdShow);
::UpdateWindow(MainWindowHandle);
return true;
}
bool initDirect3d()
{
d3d9 = Direct3DCreate9(D3D_SDK_VERSION); //pointer to direct3d interface
if(d3d9 == 0)
{
MessageBox(0,"error - direct3dcreate","error",MB_OK);
return false;
}
d3d9->GetDeviceCaps(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,&caps);
//hardware/software vertex processing check
int vp;
if(caps.DevCaps && D3DDEVCAPS_HWTRANSFORMANDLIGHT)
vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;
else
vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
D3DPRESENT_PARAMETERS d3dpp;
d3dpp.BackBufferWidth = 800;
d3dpp.BackBufferHeight = 600;
d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8;
d3dpp.BackBufferCount = 1;
d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
d3dpp.MultiSampleQuality= 0;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = MainWindowHandle;
d3dpp.Windowed = false;
d3dpp.EnableAutoDepthStencil = true;
d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
d3dpp.Flags = 0;
d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
IDirect3DDevice9 *device = 0;
HRESULT hr = d3d9->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,MainWindowHandle,vp,
&d3dpp,&device);
if(FAILED(hr))
{
MessageBox(0,"createdevice failed","error",MB_OK);
return false;
}
return true;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
//init the window
InitWindowsApp(hInstance, nCmdShow);
if(!initDirect3d())
{
MessageBox(0,"Direct3d init error","Error!",MB_OK);
return 0;
}
Run();
d3d9->Release();
return 0;
}