Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.11 KB | None | 0 0
  1. #include <Windows.h>
  2. #include <d3d9.h>
  3. #include <d3dx9.h>
  4.  
  5. #pragma comment (lib, "d3d9.lib")
  6. #pragma comment (lib, "d3dx9.lib")
  7. #pragma comment (lib, "winmm.lib")
  8.  
  9. //Win32 Stuff
  10. LPCTSTR WndClassName = L"DirektSaft";
  11. HWND hwnd = NULL;
  12.  
  13. const int Width = 1024;
  14. const int Height = 768;
  15.  
  16. //--------DirectX Stuff
  17. IDirect3D9* d3d9 = NULL;
  18. IDirect3DDevice9* d3dev = NULL;
  19.  
  20. IDirect3DVertexBuffer9* VertexBuffer = 0;
  21. IDirect3DIndexBuffer9* IndexBuffer = 0;
  22.  
  23. struct Vertex
  24. {
  25. float x, y, z;
  26. float u,v;
  27. };
  28.  
  29. const DWORD VertexFVF = D3DFVF_XYZ | D3DFVF_TEX1;
  30.  
  31. // VALUES
  32.  
  33. D3DXMATRIX WorldMatrixL;
  34. D3DXMATRIX WorldMatrixR;
  35. IDirect3DTexture9* Texture = 0;
  36.  
  37. //DirectX Prototypes
  38. bool Setup();
  39. void Clean();
  40. bool Update(float deltaTime);
  41. bool Render(float deltaTime);
  42.  
  43. //Win32 Prototypes--------
  44. bool InitWindow(HINSTANCE hInstance, int ShowWnd, int width,
  45. int height, bool windowed, D3DDEVTYPE deviceType, IDirect3DDevice9** d3ddev);
  46. int messageloop(bool (*display) (float deltaTime));
  47. LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
  48.  
  49. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
  50. {
  51. if (!InitWindow(hInstance, nShowCmd, Width, Height, true,D3DDEVTYPE_HAL,&d3dev))
  52. {
  53. MessageBox(0, L"WIndow Init failed", L"ERROR", MB_OK);
  54. return 1;
  55. }
  56.  
  57. if (!Setup())
  58. {
  59. MessageBox(0, L"Setup Scene failed", L"ERROR", MB_OK);
  60. return 1;
  61. }
  62.  
  63. messageloop(Render);
  64. Clean();
  65. return 0;
  66.  
  67. }
  68.  
  69. bool InitWindow(HINSTANCE hInstance, int ShowWnd, int width, int height,
  70. bool windowed, D3DDEVTYPE deviceType, IDirect3DDevice9** d3ddev)
  71. {
  72. WNDCLASSEX wc;
  73.  
  74. wc.cbSize = sizeof(WNDCLASSEX);
  75. wc.style = CS_HREDRAW | CS_VREDRAW;
  76. wc.lpfnWndProc = WndProc;
  77. wc.cbWndExtra = NULL;
  78. wc.cbClsExtra = NULL;
  79. wc.hInstance = hInstance;
  80. wc.hIcon = LoadIcon(NULL, IDI_SHIELD);
  81. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  82. wc.hbrBackground = (HBRUSH) COLOR_WINDOW + 1;
  83. wc.lpszMenuName = NULL;
  84. wc.lpszClassName = WndClassName;
  85. wc.hIconSm = LoadIcon(NULL, IDI_SHIELD);
  86.  
  87. if (!RegisterClassEx(&wc))
  88. {
  89. MessageBox(0, L"Registering windows fauled", L"ERROR", MB_OK);
  90. return 1;
  91. }
  92.  
  93. hwnd = CreateWindowEx(
  94. NULL, WndClassName, L"DirektSaftDemo",
  95. WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
  96. width, height, NULL, NULL, hInstance, NULL);
  97.  
  98. if (!hwnd)
  99. {
  100. MessageBox(0, L"Create windowsEX faail", L"ERROR", MB_OK);
  101. return 1;
  102. }
  103.  
  104. ShowWindow(hwnd, ShowWnd);
  105. UpdateWindow(hwnd);
  106.  
  107. // DIRECTX INIT
  108.  
  109. d3d9 = Direct3DCreate9(D3D_SDK_VERSION);
  110. if (!d3d9)
  111. {
  112. MessageBox(0, L"Init D3D fail", L"ERROR", MB_OK);
  113. return 1;
  114. }
  115.  
  116. D3DCAPS9 caps;
  117. d3d9->GetDeviceCaps(D3DADAPTER_DEFAULT, deviceType, &caps);
  118. int vertexproc = NULL;
  119. if (caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
  120. vertexproc = D3DCREATE_HARDWARE_VERTEXPROCESSING;
  121. else
  122. vertexproc = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
  123.  
  124. D3DPRESENT_PARAMETERS d3dpp;
  125. d3dpp.BackBufferWidth = width;
  126. d3dpp.BackBufferHeight = height;
  127. d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8;
  128. d3dpp.BackBufferCount = 1;
  129. d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
  130. d3dpp.EnableAutoDepthStencil = true;
  131. d3dpp.Flags = NULL;
  132. d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
  133. d3dpp.hDeviceWindow = hwnd;
  134. d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
  135. d3dpp.MultiSampleQuality = NULL;
  136. d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
  137. d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  138. d3dpp.Windowed = windowed;
  139.  
  140. HRESULT hr = 0;
  141.  
  142. hr = d3d9->CreateDevice(D3DADAPTER_DEFAULT, deviceType, hwnd,
  143. vertexproc, &d3dpp, d3ddev);
  144. if (FAILED(hr))
  145. {
  146. MessageBox(0, L"Direct3D Device Failed", L"ERROR", MB_OK);
  147. d3d9->Release();
  148. return 1;
  149. }
  150.  
  151. d3d9->Release();
  152.  
  153. return true;
  154. }
  155.  
  156. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  157. {
  158. switch (msg)
  159. {
  160. case WM_KEYDOWN:
  161. if (wParam == VK_ESCAPE)
  162. {
  163. DestroyWindow(hwnd);
  164. }
  165. return 0;
  166.  
  167. case WM_DESTROY:
  168. PostQuitMessage(0);
  169. return 0;
  170. }
  171. return DefWindowProc(hwnd, msg, wParam, lParam);
  172. }
  173.  
  174. int messageloop(bool (*display) (float deltaTime))
  175. {
  176. MSG msg;
  177. ZeroMemory(&msg, sizeof(MSG));
  178.  
  179. static float lastTime = (float)timeGetTime();
  180.  
  181. while (true)
  182. {
  183. if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  184. {
  185. if (msg.message == WM_QUIT) break;
  186. TranslateMessage(&msg);
  187. DispatchMessage(&msg);
  188. }
  189. else
  190. {
  191. // GAME LOOP
  192. float curT = (float)timeGetTime();
  193. float deltaTime = (curT - lastTime) * 0.001f;
  194. Update(deltaTime);
  195. display( deltaTime);
  196. lastTime = curT;
  197. }
  198.  
  199. }
  200. return (int)msg.wParam;
  201. }
  202.  
  203. bool Setup()
  204. {
  205.  
  206. D3DXVECTOR3 CamPos(0.0f, 2.0f, -30.0f);
  207. D3DXVECTOR3 CamTarget(0.0f, 0.0f, 0.0f);
  208. D3DXVECTOR3 CamUp(0.0f, 1.0f, 0.0f);
  209.  
  210. D3DXMATRIX View;
  211. D3DXMatrixLookAtLH(&View, &CamPos, &CamTarget, &CamUp);
  212.  
  213. d3dev->SetTransform(D3DTS_VIEW, &View);
  214.  
  215. D3DXMATRIX Projection;
  216. D3DXMatrixPerspectiveFovLH(
  217. &Projection,
  218. D3DX_PI * 0.5f, //FOV Y
  219. (float)Width / (float)Height, //Aspect Ratio (zb 16:9
  220. 0.3f, // Near Plane
  221. 1000.0f); //Far Plane
  222.  
  223. d3dev->SetTransform(D3DTS_PROJECTION, &Projection);
  224.  
  225. //------------------------------VERTEX BUFFER
  226. d3dev->CreateVertexBuffer(
  227. 24 * sizeof(Vertex),
  228. D3DUSAGE_WRITEONLY,
  229. VertexFVF,
  230. D3DPOOL_MANAGED,
  231. &VertexBuffer,
  232. 0);
  233.  
  234. VOID* pVoid;
  235.  
  236. Vertex vertices[] =
  237. {
  238. { -3.0f, -3.0f, 3.0f, 1.0f, 1.0f, }, // side 1
  239. { 3.0f, -3.0f, 3.0f, 1.0f, 0.0f, },
  240. { -3.0f, 3.0f, 3.0f, 0.0f, 1.0f, },
  241. { 3.0f, 3.0f, 3.0f, 0.0f, 0.0f, },
  242.  
  243. { -3.0f, -3.0f, -3.0f, 1.0f, 1.0f, }, // side 2
  244. { -3.0f, 3.0f, -3.0f, 1.0f, 0.0f, },
  245. { 3.0f, -3.0f, -3.0f, 0.0f, 1.0f, },
  246. { 3.0f, 3.0f, -3.0f, 0.0f, 0.0f, },
  247.  
  248. { -3.0f, 3.0f, -3.0f, 1.0f, 1.0f, }, // side 3
  249. { -3.0f, 3.0f, 3.0f, 1.0f, 0.0f, },
  250. { 3.0f, 3.0f, -3.0f, 0.0f, 1.0f, },
  251. { 3.0f, 3.0f, 3.0f, 0.0f, 0.0f, },
  252.  
  253. { -3.0f, -3.0f, -3.0f, 1.0f, 1.0f, }, // side 4
  254. { 3.0f, -3.0f, -3.0f, 1.0f, 0.0f, },
  255. { -3.0f, -3.0f, 3.0f, 0.0f, 1.0f, },
  256. { 3.0f, -3.0f, 3.0f, 0.0f, 0.0f, },
  257.  
  258. { 3.0f, -3.0f, -3.0f, 1.0f, 1.0f, }, // side 5
  259. { 3.0f, 3.0f, -3.0f, 1.0f, 0.0f, },
  260. { 3.0f, -3.0f, 3.0f, 0.0f, 1.0f, },
  261. { 3.0f, 3.0f, 3.0f, 0.0f, 0.0f, },
  262.  
  263. { -3.0f, -3.0f, -3.0f,1.0f, 1.0f, }, // side 6
  264. { -3.0f, -3.0f, 3.0f, 1.0f, 0.0f, },
  265. { -3.0f, 3.0f, -3.0f, 0.0f, 1.0f, },
  266. { -3.0f, 3.0f, 3.0f, 0.0f, 0.0f, },
  267. };
  268. VertexBuffer->Lock(0, 0, (void**)& pVoid, 0);
  269. memcpy(pVoid, vertices, sizeof(vertices));
  270. VertexBuffer->Unlock();
  271.  
  272. //------------------------------------INDEX BUFFER
  273.  
  274. WORD indices[] =
  275. {
  276. 0, 1, 2, // side 1
  277. 2, 1, 3,
  278. 4, 5, 6, // side 2
  279. 6, 5, 7,
  280. 8, 9, 10, // side 3
  281. 10, 9, 11,
  282. 12, 13, 14, // side 4
  283. 14, 13, 15,
  284. 16, 17, 18, // side 5
  285. 18, 17, 19,
  286. 20, 21, 22, // side 6
  287. 22, 21, 23,
  288. };
  289.  
  290. d3dev->CreateIndexBuffer(36 * sizeof(WORD),0,D3DFMT_INDEX16,D3DPOOL_MANAGED,&IndexBuffer,NULL);
  291.  
  292.  
  293. IndexBuffer->Lock(0, 0, (void**)&pVoid, 0);
  294. memcpy(pVoid, indices, sizeof(indices));
  295. IndexBuffer->Unlock();
  296.  
  297. // TEXTURE
  298.  
  299. D3DXCreateTextureFromFile(d3dev, L"texture.jpg", &Texture);
  300.  
  301. d3dev->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
  302. d3dev->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
  303. d3dev->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
  304.  
  305. // RENDER STATES
  306.  
  307.  
  308. d3dev->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID);
  309. d3dev->SetRenderState(D3DRS_LIGHTING, false);
  310. d3dev->SetRenderState(D3DRS_SHADEMODE,D3DSHADE_GOURAUD);
  311.  
  312. //d3dev->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
  313.  
  314.  
  315. return true;
  316. }
  317. void Clean()
  318. {
  319. VertexBuffer->Release();
  320. IndexBuffer->Release();
  321. Texture->Release();
  322.  
  323. return;
  324. }
  325.  
  326. bool Update(float deltaTime)
  327. {
  328. D3DXMATRIX RotY;
  329. D3DXMATRIX RotX;
  330. D3DXMATRIX RotZ;
  331. D3DXMATRIX Scale;
  332.  
  333. D3DXMATRIX TransLeft;
  334. D3DXMATRIX TransRight;
  335. static float y = 0;
  336. static float x = 0;
  337. static float z = 0;
  338. static float s = 0;
  339.  
  340. D3DXMatrixRotationY(&RotY, y);
  341. D3DXMatrixRotationX(&RotX, x);
  342. D3DXMatrixRotationZ(&RotZ, z);
  343.  
  344. float scale = 1.5f + (sin(s) * 0.5f);
  345. D3DXMatrixScaling(&Scale, scale, scale, scale);
  346.  
  347. y += deltaTime * 1.5f;
  348. x += deltaTime * 1.7f;
  349. z += deltaTime * 1.3f;
  350. s += deltaTime * 2 * D3DX_PI;
  351.  
  352. D3DXMatrixTranslation(&TransLeft, 0, 0, 0);
  353. D3DXMatrixTranslation(&TransRight, 4, 0, 0);
  354.  
  355. WorldMatrixL = Scale * RotZ;
  356. WorldMatrixR = RotX * TransRight * RotY;
  357.  
  358. return true;
  359. }
  360.  
  361. bool Render(float deltaTime)
  362. {
  363. if (d3dev)
  364. {
  365. d3dev->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0x00355DFF, 1.0f, 0);
  366. d3dev->BeginScene();
  367.  
  368. d3dev->SetStreamSource(0, VertexBuffer, 0, sizeof(Vertex));
  369. d3dev->SetIndices(IndexBuffer);
  370. d3dev->SetFVF(VertexFVF);
  371. d3dev->SetTexture(0, Texture);
  372. // Linker WÜrfel
  373. d3dev->SetTransform(D3DTS_WORLD, &WorldMatrixL);
  374. d3dev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 8, 0, 12);
  375. //Rechter WÜrfel
  376. d3dev->SetTransform(D3DTS_WORLD, &WorldMatrixR);
  377. d3dev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 8, 0, 12);
  378.  
  379. d3dev->EndScene();
  380. d3dev->Present(0, 0, 0, 0);
  381. }
  382. return true;
  383. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement