1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
#include <windows.h>
#include <d3d9.h>
//windows variables
HWND hWnd;
//directx-functions
bool initData();
void releaseResources();
void changeDisplay();
bool testDevice();
//directx-variables
IDirect3D9* pD3D=NULL;
D3DPRESENT_PARAMETERS d3dpp;
IDirect3DDevice9* pDevice;
IDirect3DVertexBuffer9* vBufferObject;
void*vBuffer;
bool appWindowed=false;
struct mvertex{float x,y,z,rhw;DWORD color;};
const DWORD d3dfvf=D3DFVF_XYZRHW|D3DFVF_DIFFUSE;
const mvertex data[]={//middelste driehoek
{0,0,1.0f,1.0f,0xFFFF0000},
{256,0,1.0f,1.0f,0xFF00FF00},
{128,256,1.0f,1.0f,0xFF0000FF},
};
int countPrimitives=(sizeof(data)/sizeof(mvertex))/3;
bool InitD3D(){
HRESULT hr;
if((pD3D = Direct3DCreate9(D3D_SDK_VERSION))==NULL){
return false;
}
D3DDISPLAYMODE d3ddm;
pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm);
ZeroMemory(&d3dpp,sizeof(d3dpp));
d3dpp.Windowed=appWindowed;
d3dpp.BackBufferFormat = d3ddm.Format;
d3dpp.BackBufferHeight=768;
d3dpp.BackBufferWidth=1024;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
if(FAILED(pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice))){
return false;
}
initData();
return true;
}
void RenderD3DScene(){
HRESULT hr;
pDevice->Clear(0,NULL,D3DCLEAR_TARGET,0x00000000,1.0f,0);
pDevice->BeginScene();
hr=pDevice->SetStreamSource(0,vBufferObject,0,sizeof(mvertex));
pDevice->SetFVF(d3dfvf);
pDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,countPrimitives);
pDevice->EndScene();
pDevice->Present(NULL,NULL,NULL,NULL);
}
void changeDisplay()
{HRESULT hr;
if(appWindowed)
{appWindowed=false;
}
else
{appWindowed=true;
}
d3dpp.Windowed=appWindowed;
hr=pDevice->Reset(&d3dpp);
if(FAILED(hr))
{if(hr==D3DERR_DEVICELOST)
{MessageBox(0,"D3DERR_DEVICELOST:device cannot be reset","DirectX ERROR",MB_OK);
}
else if(hr==D3DERR_DEVICENOTRESET)
{MessageBox(0,"D3DERR_DEVICENOTRESET","DirectX ERROR",MB_OK);
}
else if(hr==D3DERR_DRIVERINTERNALERROR)
{MessageBox(0,"D3DERR_INTERNALDRIVERERROR","DirectX ERROR",MB_OK);
}
}
}
bool testDevice()
{ HRESULT hr;
hr=pDevice->TestCooperativeLevel();
//device is lost, and cannot be reset at the moment (wait until the focus returns)
if(hr==D3D_OK){return true;}
if(hr == D3DERR_DEVICELOST)
{return false;
}
//device is lost, but the focus has returned and the device can now be reset
else if(hr==D3DERR_DEVICENOTRESET)
{//release the vertex-buffer on the video-driver
releaseResources();
InitD3D();
return true;
}
}
bool initData()
{HRESULT hr;
hr=pDevice->CreateVertexBuffer(sizeof(data),D3DUSAGE_WRITEONLY,d3dfvf,D3DPOOL_MANAGED,&vBufferObject,NULL);
if(FAILED(hr)){MessageBox(hWnd,"pDevice->CreateVertexBuffer failed","DirectX ERROR",MB_OK);return false;}
hr=vBufferObject->Lock(0,0,&vBuffer,0);
if(FAILED(hr)){MessageBox(hWnd,"pDevice->Lock failed","DirectX ERROR",MB_OK);return false;}
if(memcpy(vBuffer,data,sizeof(data))==false){MessageBox(hWnd,"memcpy failed","c++ error",MB_OK);return false;};
hr=vBufferObject->Unlock();
if(FAILED(hr)){MessageBox(hWnd,"pDevice->Unlock failed","DirectX ERROR",MB_OK);return false;}
return true;
}
void releaseResources()
{vBufferObject->Release();
vBufferObject=NULL;
vBuffer=NULL;
}
void MessageLoop(){
MSG msg;
if(testDevice()==true){RenderD3DScene();}
while(GetMessage(&msg,hWnd,0,0))
{TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
LRESULT CALLBACK WndProc( HWND hWnd , UINT message ,
WPARAM wParam , LPARAM lParam){
switch(message){
case WM_CLOSE:
PostQuitMessage(0);
return 0;
break;
case WM_RBUTTONDOWN:
PostQuitMessage(0);
break;
case WM_LBUTTONDOWN:
changeDisplay();
break;
default:
return DefWindowProc(hWnd,message,wParam,lParam);
break;
}
}
bool CreateSimpWindow(int width, int height){
// Create A Window Class Structure
WNDCLASSEX wc;
wc.cbClsExtra = 0;
wc.cbSize = sizeof(wc);
wc.cbWndExtra = 0;
wc.hbrBackground = NULL;
wc.hCursor = NULL;
wc.hIcon = NULL;
wc.hIconSm = NULL;
wc.hInstance = GetModuleHandle(NULL);
wc.lpfnWndProc = WndProc;
wc.lpszClassName = "X3D";
wc.lpszMenuName = NULL;
wc.style = 0;
// Register Window Class
RegisterClassEx(&wc);
// Create Window
hWnd = CreateWindowEx(0,
"X3D", "X3D Direct3D Tutorial 1",
WS_OVERLAPPEDWINDOW|WS_VISIBLE, 0,0,width,height,
NULL,NULL,wc.hInstance,0);
return true;
}
INT WINAPI WinMain( HINSTANCE , HINSTANCE , LPSTR , INT ){
if(!CreateSimpWindow(1024, 768))return 1;
if(!InitD3D())return 2;
MessageLoop();
return 0;
}
| |