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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
|
#include <windows.h>
#include <d3dx9.h>
//--------------------------------------GLOBAL-VARIABLES----------------------------------------//
// WinAPI global variables:
HWND hWnd = NULL; // Our main window;
MSG msg; // Creates a MSG variable, needed for handling the events-messages in the window.
HRESULT hr; // Is needed to quickly check if a function succeded (optional).
// DX global interfaces:
LPDIRECT3D9 g_pD3DObject = NULL; // Declaring the Direct3D interface pointer (this allows to operate with D3D API);
LPDIRECT3DDEVICE9 g_pD3DDevice = NULL; // Declaring the rendering device pointer (this allows our app to "see" the rendering hardware - GPU or CPU)
IDirect3DVertexBuffer9* gVB;
IDirect3DTexture9* g_pTexture;
struct vertex //Creating a temporary class for our vertices (needs to be changed).
{
float x, y, z;
// FLOAT nx, ny, nz; // Normal.
// D3DCOLOR diffuse; // Colour being diffused (most likely will not be used).
FLOAT u, v; // Texture coordinates.
};
#define VERTEXFVF (D3DFVF_XYZ|/*D3DFVF_DIFFUSE|*/D3DFVF_TEX1)
//----------------------------------GLOBAL-VARIABLES-FINSIHED------------------------------------//
// Functions needed to initialize, release or operate the WinAPI and DirectX modules.
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd, message, wParam, lParam);
}
HRESULT InitialiseDirectX(HWND hWnd)
{
// Create the D3D object
g_pD3DObject=Direct3DCreate9(D3D_SDK_VERSION);
if (g_pD3DObject==NULL)
return FALSE;
// Fill out some presentation parameters for running in a window
D3DPRESENT_PARAMETERS presParams;
ZeroMemory(&presParams,sizeof(presParams));
presParams.Windowed=TRUE;
presParams.SwapEffect=D3DSWAPEFFECT_DISCARD;
presParams.BackBufferFormat=D3DFMT_UNKNOWN;
presParams.BackBufferCount=1;
// Create the D3D device
HRESULT hr=g_pD3DObject->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,
D3DCREATE_HARDWARE_VERTEXPROCESSING, &presParams, &g_pD3DDevice);
if (FAILED(hr))
{
// Some cards may not do hardware vertex processing so fall back to software:
hr=g_pD3DObject->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING, &presParams, &g_pD3DDevice);
if (FAILED(hr))
return FALSE;
}
// Filling a temporary array of vertices with the data.
vertex vertices[] =
{
{-3, -4, 0, /* D3DCOLOR_RGBA(0, 0, 255, 255),*/ 0, 0},
{-3, 4, 0, /* D3DCOLOR_RGBA(0, 0, 255, 255),*/ 0, 1},
{ 3, -4, 0, /* D3DCOLOR_RGBA(0, 0, 255, 255),*/ 1, 0},
{ 3, 4, 0, /* D3DCOLOR_RGBA(0, 0, 255, 255),*/ 1, 1},
};
// Creating a vertex buffer
void* vb_vertices;
g_pD3DDevice->CreateVertexBuffer(6*sizeof(vertex), D3DUSAGE_WRITEONLY,
VERTEXFVF, D3DPOOL_DEFAULT,
&gVB,NULL);
// Filling the actual vertex buffer.
gVB->Lock(0,sizeof(vertices),(void**)&vb_vertices,0);
memcpy(vb_vertices,vertices,sizeof(vertices));
gVB->Unlock();
D3DXMATRIX mWorld;
// Specifying the rendering options.
g_pD3DDevice->SetRenderState(D3DRS_LIGHTING, false);
g_pD3DDevice->SetStreamSource(0, gVB, 0, sizeof(vertex));
g_pD3DDevice->SetFVF(VERTEXFVF);
// Filling a transposition matrix.
D3DXMatrixTranslation(&mWorld, 0, 0, 0);
// Showing the "World" matrix to the device.
g_pD3DDevice->SetTransform(D3DTS_WORLD, &mWorld);
D3DXCreateTextureFromFile(g_pD3DDevice, "Tree.bmp", &g_pTexture);
g_pD3DDevice->SetTexture(0, g_pTexture);
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++//
D3DXMATRIX matCam;
D3DXMatrixTranslation(&matCam, 0, 0, 100);
g_pD3DDevice->SetTransform(D3DTS_VIEW, &matCam);
D3DXMATRIX matProj;
D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI/4, 1.0f, 1.0f, 5000.0f);
g_pD3DDevice->SetTransform(D3DTS_PROJECTION, &matProj);
//+++++++++++++++ Other rendering options +++++++++++++++//
// Setting the rendering texture.
//g_pD3DDevice->SetTexture(0, g_pTexture);
return TRUE;
}
void CleanupDirectX()
{
if (g_pD3DObject)
g_pD3DObject->Release();
if (g_pD3DDevice)
g_pD3DDevice->Release();
}
HRESULT InitWinAPI(HINSTANCE& hInstance, int& nCmdShow)
/**************************************************************************************************
Desc: Creates and registers window calss as well as the window itself
**************************************************************************************************/
{
// Describe and register our window class
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_CLASSDC;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0L;
wcex.cbWndExtra = 0L;
wcex.hInstance = hInstance;
wcex.hIcon = 0;
wcex.hCursor = 0;
wcex.hbrBackground = 0;
wcex.lpszMenuName = 0;
wcex.lpszClassName = "SomeWindowClass";
wcex.hIconSm = 0;
if (!RegisterClassEx(&wcex))
return 0;
// Create the window
hWnd=CreateWindow("SomeWindowClass", "WindowName", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT, 800, 600, NULL, NULL, hInstance, NULL);
if (!hWnd)
return 0;
return TRUE;
}
//-----------------------------Section-for-the-User's-functions----------------------------------//
//--------------------------Section-for-the-User's-functions-finished----------------------------//
/**************************************************************************************************
Desc: This is the main entry point of the program.
**************************************************************************************************/
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow)
{
//---------------------------------Initialization-phase--------------------------------------//
// Cleaning the memory for the msg variable;
ZeroMemory(&msg, sizeof(msg));
// Initialize and create the window.
InitWinAPI(hInstance, nCmdShow);
// Before displaying our window create the D3D device.
if (!InitialiseDirectX(hWnd))
return 0;
// Display the window and force an initial paint.
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
//--------------------------------Initialization-finished------------------------------------//
while(msg.message!=WM_QUIT)
{
if(PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
g_pD3DDevice->Clear(0,NULL,D3DCLEAR_TARGET/* | D3DCLEAR_ZBUFFER*/,D3DCOLOR_XRGB(0,0,255),1.0f,0);
g_pD3DDevice->BeginScene();
g_pD3DDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP,0,2);
g_pD3DDevice->EndScene();
g_pD3DDevice->Present(NULL,NULL,NULL,NULL);
}
}
// Clean up DirectX
CleanupDirectX();
return msg.wParam;
}
|