Advertisement
Guest User

main.cpp

a guest
Sep 17th, 2013
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. #include <Windows.h>
  2. #include <d3d9.h>
  3. #include "SpriteBatcher.h"
  4.  
  5. #pragma comment (lib, "d3d9.lib")
  6.  
  7. #define SCREEN_WIDTH 800
  8. #define SCREEN_HEIGHT 600
  9. #define CUSTOMFVF (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)
  10.  
  11. //Global Vars
  12. HINSTANCE hInstance;
  13. HWND winHandle;
  14.  
  15. LPDIRECT3D9 d3d;
  16. LPDIRECT3DDEVICE9 device;
  17.  
  18. //---------------------------------
  19.  
  20. SpriteBatcher batcher;
  21. float x;
  22.  
  23. //Prototypes
  24. LRESULT CALLBACK winProc(HWND winHandle, UINT message, WPARAM wParam, LPARAM lParam);
  25. bool initWindow(HINSTANCE hInstance);
  26. bool initDirectX();
  27. void render();
  28. void cleanUp();
  29.  
  30. //Create the main entry point
  31. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
  32. {
  33. //Create the window
  34. if(!(initWindow(hInstance)))
  35. return false;
  36.  
  37. //Init Direct X
  38. if(!(initDirectX()))
  39. return false;
  40.  
  41. //Prepare the game loop
  42. MSG msg;
  43. ZeroMemory(&msg, sizeof(MSG));
  44.  
  45. //While there is no quit message run the game loop
  46. while(msg.message != WM_QUIT)
  47. {
  48. //Handle window messages
  49. while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  50. {
  51. TranslateMessage(&msg);
  52. DispatchMessage(&msg);
  53. }
  54.  
  55. //Game Code
  56. render();
  57. }
  58.  
  59. //Clean up clean up
  60. cleanUp();
  61.  
  62. return msg.wParam;
  63. }
  64.  
  65. #pragma region Window Creation and call back
  66. bool initWindow(HINSTANCE hInstance)
  67. {
  68. //Set up the window properties
  69. WNDCLASSEX wc;
  70. ZeroMemory(&wc, sizeof(WNDCLASSEX));
  71.  
  72. //Window properties
  73. wc.cbSize = sizeof(WNDCLASSEX);
  74. wc.style = CS_HREDRAW | CS_VREDRAW;
  75. wc.lpfnWndProc = winProc;
  76. wc.cbClsExtra = 0;
  77. wc.cbWndExtra = 0;
  78. wc.hInstance = hInstance;
  79. wc.hIcon = 0;
  80. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  81. wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
  82. wc.lpszMenuName = NULL;
  83. wc.lpszClassName = "window";
  84. wc.hIconSm = 0;
  85.  
  86. //Register the window class
  87. RegisterClassEx(&wc);
  88.  
  89. //Create the window
  90. winHandle = CreateWindowEx(NULL,
  91. "window",
  92. "My Window",
  93. WS_OVERLAPPEDWINDOW,
  94. CW_USEDEFAULT,
  95. CW_USEDEFAULT,
  96. SCREEN_WIDTH,
  97. SCREEN_HEIGHT,
  98. NULL,
  99. NULL,
  100. hInstance,
  101. NULL);
  102.  
  103. if(winHandle == false)
  104. return false;
  105.  
  106. //Show the window
  107. ShowWindow(winHandle, SW_SHOW);
  108.  
  109. return true;
  110. }
  111.  
  112. //Call back funciton for the window
  113. LRESULT CALLBACK winProc(HWND winHandle, UINT message, WPARAM wParam, LPARAM lParam)
  114. {
  115. switch(message)
  116. {
  117. case WM_DESTROY:
  118. {
  119. PostQuitMessage(0);
  120. }
  121. break;
  122. }
  123.  
  124. return DefWindowProc(winHandle, message, wParam, lParam);
  125. }
  126. #pragma endregion
  127.  
  128. //Setup direct x
  129. bool initDirectX()
  130. {
  131. //Set up all the direct x properties
  132. d3d = NULL;
  133. device = NULL;
  134.  
  135. if(NULL == (d3d = Direct3DCreate9(D3D_SDK_VERSION)))
  136. return false;
  137.  
  138. D3DPRESENT_PARAMETERS d3dpp;
  139. ZeroMemory(&d3dpp, sizeof(d3dpp));
  140.  
  141. d3dpp.Windowed = true;
  142. d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  143. d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
  144. d3dpp.BackBufferCount = 1;
  145. d3dpp.BackBufferWidth = SCREEN_WIDTH;
  146. d3dpp.BackBufferHeight = SCREEN_HEIGHT;
  147. d3dpp.hDeviceWindow = winHandle;
  148.  
  149. //Create the direct x device
  150. if(FAILED(d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, winHandle, D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &device)))
  151. return false;
  152.  
  153. //device->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);
  154.  
  155. //Finalize stuff
  156. x = 10.0f;
  157.  
  158. return true;
  159. }
  160.  
  161. //Draw the things that need to be drawn
  162. void render()
  163. {
  164. x++;
  165.  
  166. //Draw
  167. batcher.beginBatch();
  168. batcher.draw(x, 50.0f, 64.0f, 64.0f, D3DCOLOR_XRGB(0,255,255));
  169. batcher.endBatch(device);
  170. }
  171.  
  172. //Clean up
  173. void cleanUp()
  174. {
  175.  
  176. //Clean up the D3D device
  177. if(device != NULL)
  178. {
  179. device->Release();
  180. device = NULL;
  181. }
  182.  
  183. //Clean up the D3D object
  184. if(d3d != NULL)
  185. {
  186. d3d->Release();
  187. d3d = NULL;
  188. }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement