Advertisement
noodleBowl

main.cpp

Sep 26th, 2013
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.83 KB | None | 0 0
  1. #include <Windows.h>
  2. #include <d3d9.h>
  3. #include <d3dx9.h>
  4. #include <iostream>
  5. #include <stdio.h>
  6. #include <io.h>
  7. #include <fcntl.h>
  8. #include <string>
  9.  
  10. #include "SpriteBatcher.h"
  11.  
  12. #pragma comment (lib, "d3d9.lib")
  13. #pragma comment (lib, "d3dx9.lib")
  14.  
  15. #define SCREEN_WIDTH 800
  16. #define SCREEN_HEIGHT 600
  17.  
  18. //Global Vars
  19. HINSTANCE hInstance;
  20. HWND winHandle;
  21. RECT windowSize;
  22.  
  23. LPDIRECT3D9 d3d;
  24. LPDIRECT3DDEVICE9 device;
  25.  
  26. //---------------------------------
  27.  
  28. LPDIRECT3DTEXTURE9 tex;
  29. LPDIRECT3DTEXTURE9 tex2;
  30. SpriteBatcher batcher;
  31. float x;
  32.  
  33. //Prototypes
  34. LRESULT CALLBACK winProc(HWND winHandle, UINT message, WPARAM wParam, LPARAM lParam);
  35. void createDebugConsole();
  36. bool initWindow(HINSTANCE hInstance);
  37. bool initDirectX();
  38. void render();
  39. void cleanUp();
  40.  
  41. //Create the main entry point
  42. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
  43. {
  44. //Set up the console
  45. createDebugConsole();
  46.  
  47. //Create the window
  48. if(!(initWindow(hInstance)))
  49. return false;
  50.  
  51. //Init Direct X
  52. if(!(initDirectX()))
  53. return false;
  54.  
  55. //Prepare the game loop
  56. MSG msg;
  57. ZeroMemory(&msg, sizeof(MSG));
  58.  
  59. //While there is no quit message run the game loop
  60. while(msg.message != WM_QUIT)
  61. {
  62. //Handle window messages
  63. while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  64. {
  65. TranslateMessage(&msg);
  66. DispatchMessage(&msg);
  67. }
  68.  
  69. //Game Code
  70. render();
  71. }
  72.  
  73. //Clean up clean up
  74. cleanUp();
  75.  
  76. return msg.wParam;
  77. }
  78.  
  79. #pragma region Window Creation and call back
  80.  
  81. bool initWindow(HINSTANCE hInstance)
  82. {
  83. //Set up the window properties
  84. WNDCLASSEX wc;
  85. ZeroMemory(&wc, sizeof(WNDCLASSEX));
  86.  
  87. //Window properties
  88. wc.cbSize = sizeof(WNDCLASSEX);
  89. wc.style = CS_HREDRAW | CS_VREDRAW;
  90. wc.lpfnWndProc = winProc;
  91. wc.cbClsExtra = 0;
  92. wc.cbWndExtra = 0;
  93. wc.hInstance = hInstance;
  94. wc.hIcon = 0;
  95. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  96. wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
  97. wc.lpszMenuName = NULL;
  98. wc.lpszClassName = "window";
  99. wc.hIconSm = 0;
  100.  
  101. //Register the window class
  102. RegisterClassEx(&wc);
  103.  
  104. //Set the save/size of the window
  105. windowSize.top = 0;
  106. windowSize.left = 0;
  107. windowSize.right = SCREEN_WIDTH;
  108. windowSize.bottom = SCREEN_HEIGHT;
  109. AdjustWindowRectEx(&windowSize, WS_OVERLAPPEDWINDOW, false, NULL);
  110.  
  111. //Create the window
  112. winHandle = CreateWindowEx(NULL,
  113. "window",
  114. "My Window",
  115. WS_OVERLAPPEDWINDOW,
  116. 500,
  117. 150,
  118. windowSize.right - windowSize.left,
  119. windowSize.bottom - windowSize.top,
  120. NULL,
  121. NULL,
  122. hInstance,
  123. NULL);
  124.  
  125. if(winHandle == false)
  126. return false;
  127.  
  128. //Show the window
  129. ShowWindow(winHandle, SW_SHOW);
  130.  
  131. return true;
  132. }
  133.  
  134. //Call back funciton for the window
  135. LRESULT CALLBACK winProc(HWND winHandle, UINT message, WPARAM wParam, LPARAM lParam)
  136. {
  137. switch(message)
  138. {
  139. case WM_DESTROY:
  140. {
  141. PostQuitMessage(0);
  142. }
  143. break;
  144. }
  145.  
  146. return DefWindowProc(winHandle, message, wParam, lParam);
  147. }
  148.  
  149. #pragma endregion
  150.  
  151. //Setup direct x
  152. bool initDirectX()
  153. {
  154. //Set up all the direct x properties
  155. d3d = NULL;
  156. device = NULL;
  157.  
  158. if(NULL == (d3d = Direct3DCreate9(D3D_SDK_VERSION)))
  159. return false;
  160.  
  161. D3DPRESENT_PARAMETERS d3dpp;
  162. ZeroMemory(&d3dpp, sizeof(d3dpp));
  163.  
  164. RECT rect;
  165. GetClientRect(winHandle, &rect);
  166.  
  167. std::cout<<"Win Width:"<<rect.right - rect.left<<std::endl;
  168. std::cout<<"Win Height: "<<rect.bottom - rect.top<<std::endl;
  169.  
  170. d3dpp.Windowed = true;
  171. d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  172. d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
  173. d3dpp.BackBufferCount = 1;
  174. d3dpp.BackBufferWidth = rect.right - rect.left;
  175. d3dpp.BackBufferHeight = rect.bottom - rect.top;
  176. d3dpp.hDeviceWindow = winHandle;
  177.  
  178. //Create the direct x device
  179. if(FAILED(d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, winHandle, D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &device)))
  180. return false;
  181.  
  182. //Texture items
  183. device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
  184. device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
  185. device->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
  186.  
  187. std::string texture = "img.png";
  188. std::string texture2 = "img2.png";
  189. HRESULT hr = D3DXCreateTextureFromFile(device, texture.c_str(), &tex);
  190. if(FAILED(hr))
  191. std::cout<<"Failed to load texture! "<<texture<<std::endl;
  192.  
  193. std::cout<<"Texture 1:"<<tex<<std::endl;
  194.  
  195.  
  196. hr = D3DXCreateTextureFromFile(device, texture2.c_str(), &tex2);
  197. if(FAILED(hr))
  198. std::cout<<"Failed to load texture2! "<<texture2<<std::endl;
  199.  
  200. std::cout<<"Texture 2:"<<tex2<<std::endl;
  201.  
  202. //Set the device to use for the sprite batcher
  203. batcher.setBatcherDevice(device);
  204.  
  205. x = 10.0f;
  206.  
  207. return true;
  208. }
  209.  
  210. //Draw the things that need to be drawn
  211. void render()
  212. {
  213.  
  214. //Clear the screen
  215. device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 40, 100), 1.0f, 0);
  216. device->BeginScene();
  217.  
  218. //Draw
  219. batcher.beginBatch();
  220. batcher.draw(250.0f, 50.0f, 64.0f, 64.0f, D3DCOLOR_XRGB(0,0,255), tex);
  221. batcher.draw(200.0f, 200.0f, 64.0f, 64.0f, D3DCOLOR_XRGB(0,0,255), tex);
  222. batcher.endBatch();
  223.  
  224. device->EndScene();
  225. device->Present(NULL, NULL, NULL, NULL);
  226. }
  227.  
  228. //Clean up
  229. void cleanUp()
  230. {
  231.  
  232. //Clean up the D3D device
  233. if(device != NULL)
  234. {
  235. device->Release();
  236. device = NULL;
  237. }
  238.  
  239. //Clean up the D3D object
  240. if(d3d != NULL)
  241. {
  242. d3d->Release();
  243. d3d = NULL;
  244. }
  245. }
  246.  
  247. void createDebugConsole()
  248. {
  249. AllocConsole();
  250.  
  251. HANDLE handle_out = GetStdHandle(STD_OUTPUT_HANDLE);
  252. int hCrt = _open_osfhandle((long) handle_out, _O_TEXT);
  253. FILE* hf_out = _fdopen(hCrt, "w");
  254. setvbuf(hf_out, NULL, _IONBF, 1);
  255. *stdout = *hf_out;
  256.  
  257. HANDLE handle_in = GetStdHandle(STD_INPUT_HANDLE);
  258. hCrt = _open_osfhandle((long) handle_in, _O_TEXT);
  259. FILE* hf_in = _fdopen(hCrt, "r");
  260. setvbuf(hf_in, NULL, _IONBF, 128);
  261. *stdin = *hf_in;
  262. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement