fork(2) download
  1. /* Triangle.cpp
  2.  
  3. Demonstration code showing how to draw a simple triangle using Direct3D.
  4. See the site for details of each stage.
  5.  
  6. Written by Keith Ditchburn 2006
  7. */
  8.  
  9. #define VC_EXTRALEAN
  10. #include <windows.h>
  11. #include <d3d9.h> // Main Direct3D header
  12. #include <d3dx9.h> // D3DX helper functions
  13.  
  14.  
  15. // Forward declarations
  16. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
  17. bool InitialiseDirect3D(HWND hWnd);
  18. void CloseDownDirect3D();
  19. void RenderScene();
  20. void DrawSprite();
  21.  
  22.  
  23. // Globals
  24. LPDIRECT3D9 gD3dObject=NULL;
  25. LPDIRECT3DDEVICE9 gD3dDevice=NULL;
  26. LPDIRECT3DTEXTURE9 gTexture = NULL;
  27. LPD3DXSPRITE sprite = NULL;
  28.  
  29.  
  30. // The main entry point of our Windows program
  31. int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  32. {
  33. // Create and fill out a window class structure
  34. WNDCLASS wcex;
  35. ZeroMemory(&wcex,sizeof(WNDCLASS));
  36.  
  37. // Provide the address of the function Windows should call with messages
  38. wcex.lpfnWndProc= (WNDPROC)WndProc;
  39. wcex.style= CS_HREDRAW | CS_VREDRAW;
  40. wcex.hInstance= hInstance;
  41. wcex.hCursor= LoadCursor(NULL, IDC_ARROW);
  42. wcex.hbrBackground= (HBRUSH)(COLOR_WINDOW+1);
  43. wcex.lpszClassName= "MyWindowClass";
  44.  
  45. // Register my new window class with the Windows API
  46. RegisterClass(&wcex);
  47.  
  48. // Create the window
  49. HWND hWnd = CreateWindow("MyWindowClass", "D3D Triangle Demo",
  50. WS_OVERLAPPEDWINDOW, 100, 100, 800, 600,
  51. NULL, NULL, hInstance, NULL);
  52.  
  53. if (hWnd==0)
  54. return -1;
  55.  
  56. // Set up Direct3D
  57. if (!InitialiseDirect3D(hWnd))
  58. return -1;
  59.  
  60. // Show the window for the first time
  61. ShowWindow(hWnd, nCmdShow);
  62. UpdateWindow(hWnd);
  63.  
  64. // Windows sends some messages directly to your callback function but others are placed in
  65. // a queue. These tend to be keyboard messages or other input that may need translating. We
  66. // enter a loop that checks for queued messages. If one is found it is translated and dispatched
  67. // to our windows procedure. Once the application is closed a WM_QUIT message is received.
  68. // When there are no messages to handle we draw our scene. This makes our program 'badly behaved'
  69. // as we are hogging system resources so I tend to put in a sleep(0) that lets other threads have
  70. // at least some time.
  71. MSG msg;
  72. ZeroMemory( &msg, sizeof(msg) );
  73. while( msg.message!=WM_QUIT )
  74. {
  75. if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
  76. {
  77. TranslateMessage( &msg );
  78. DispatchMessage( &msg );
  79. }
  80. else
  81. {
  82. RenderScene();
  83. Sleep(0);
  84. }
  85. }
  86.  
  87. // Clean up before exiting
  88. CloseDownDirect3D();
  89.  
  90. return (int)msg.wParam;
  91. }
  92.  
  93. // Callback message handler function for our window
  94. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  95. {
  96. switch(message)
  97. {
  98. case WM_DESTROY:
  99. PostQuitMessage(0);
  100. break;
  101. default:
  102. // We do not want to handle this message so pass it back to Windows
  103. // to handle it in a default way
  104. return DefWindowProc(hWnd, message, wParam, lParam);
  105. break;
  106. }
  107.  
  108. return 0;
  109. }
  110.  
  111. // Set up Direct3D
  112. // Requires creation of the Direct3D object and the Direct3D device
  113. bool InitialiseDirect3D(HWND hWnd)
  114. {
  115. // Create the main Direct3D object. This object is used to initialise Direct3D
  116. // and to provide us with a device interface object
  117. gD3dObject=Direct3DCreate9(D3D_SDK_VERSION);
  118. if (gD3dObject==0)
  119. return false;
  120.  
  121. // Fill out the parameters for our required device
  122. D3DPRESENT_PARAMETERS presParams;
  123. ZeroMemory(&presParams,sizeof(presParams));
  124.  
  125. presParams.Windowed=TRUE;
  126. presParams.SwapEffect=D3DSWAPEFFECT_DISCARD;
  127. presParams.BackBufferFormat=D3DFMT_UNKNOWN;
  128. presParams.PresentationInterval=D3DPRESENT_INTERVAL_ONE;
  129.  
  130. // Get the Direct3D object to give us a pointer to a device interface object
  131. // this device represents the graphics capabilities of the PC
  132. HRESULT hr=gD3dObject->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,
  133. D3DCREATE_HARDWARE_VERTEXPROCESSING,&presParams,&gD3dDevice);
  134. if (FAILED(hr))
  135. {
  136. // If we could not get a hardware vertex processing device fallback to a software one
  137. hr=gD3dObject->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,
  138. D3DCREATE_SOFTWARE_VERTEXPROCESSING,&presParams,&gD3dDevice);
  139. if (FAILED(hr))
  140. return false;
  141. }
  142.  
  143. // Since we are providing a colour component per vertex we do not
  144. // want Direct3D lighting to be on
  145. gD3dDevice->SetRenderState(D3DRS_LIGHTING,FALSE);
  146.  
  147. D3DXCreateTextureFromFile(gD3dDevice, "sprite_devil.bmp", &gTexture);
  148. D3DXCreateSprite(gD3dDevice, &sprite);
  149.  
  150.  
  151.  
  152. return true;
  153. }
  154.  
  155. // Release all the Direct3D interfaces in reverse order to their creation
  156. // All Direct3D objects implement a Release interface
  157. void CloseDownDirect3D()
  158. {
  159. if (gD3dDevice)
  160. {
  161. gD3dDevice->Release();
  162. gD3dDevice=NULL;
  163. }
  164. if (gD3dObject)
  165. {
  166. gD3dObject->Release();
  167. gD3dObject=NULL;
  168. }
  169. }
  170.  
  171. /* There are 3 matrices using by Direct3D
  172. World Matrix - Transforms 3D data from Model Space into World Space.
  173. You need to set this before rendering every entity in your world.
  174. View Matrix - Transforms from World Space into View Space.
  175. You need to set this each time your camera changes position
  176. Projection Matrix - Transforms from View Space into Screen Space.
  177. You normally set this just once during initialization.
  178. */
  179.  
  180. // Draw the scene
  181. void RenderScene()
  182. {
  183. // Clear the back buffer to black
  184. gD3dDevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0f,0);
  185.  
  186. // Note: all drawing must occur between the BeginScene and EndScene calls
  187. if (SUCCEEDED(gD3dDevice->BeginScene()))
  188. {
  189. // Draw out triangle
  190. DrawSprite();
  191.  
  192. // Tell Direct3D that we have finished sending it 3D data
  193. gD3dDevice->EndScene();
  194. }
  195.  
  196. // Indicate that the scene should be shown
  197. gD3dDevice->Present(0,0,0,0);
  198. }
  199.  
  200. // Draw the triangle
  201. void DrawSprite()
  202. {
  203. //D3DXVECTOR3 pos;
  204.  
  205. //pos.x=384.0f;
  206. //pos.y=284.0f;
  207. //pos.z=0.0f;
  208.  
  209. //
  210.  
  211. //sprite->Begin(D3DXSPRITE_ALPHABLEND);
  212. //sprite->Draw(gTexture,NULL,NULL,&pos,0xFFFFFFFF);
  213. //sprite->End();
  214.  
  215.  
  216. sprite->Begin(D3DXSPRITE_ALPHABLEND);
  217.  
  218. // Texture being used is 32 by 32:
  219. //D3DXVECTOR2 spriteCentre=D3DXVECTOR2(16.0f,16.0f);
  220.  
  221. // Screen position of the sprite
  222. D3DXVECTOR2 trans=D3DXVECTOR2(384.0f,284.0f);
  223.  
  224. // Rotate based on the time passed
  225. //float rotation=0.0f;
  226.  
  227. // Build our matrix to rotate, scale and position our sprite
  228. D3DXMATRIX mat;
  229.  
  230. //D3DXVECTOR2 scaling(1.0f,1.0f);
  231.  
  232. // out, scaling centre, scaling rotation, scaling, rotation centre, rotation, translation
  233. D3DXMatrixTransformation2D(&mat,NULL,NULL,NULL,NULL,NULL,&trans);
  234.  
  235. // Tell the sprite about the matrix
  236. sprite->SetTransform(&mat);
  237.  
  238. // Draw the sprite
  239. sprite->Draw(gTexture,NULL,NULL,NULL,0xFFFFFFFF);
  240.  
  241. // Thats it
  242. sprite->End();
  243. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:10:21: error: windows.h: No such file or directory
prog.cpp:11:43: error: d3d9.h: No such file or directory
prog.cpp:12:45: error: d3dx9.h: No such file or directory
prog.cpp:16: error: ‘LRESULT’ does not name a type
prog.cpp:17: error: ‘HWND’ was not declared in this scope
prog.cpp:24: error: ‘LPDIRECT3D9’ does not name a type
prog.cpp:25: error: ‘LPDIRECT3DDEVICE9’ does not name a type
prog.cpp:26: error: ‘LPDIRECT3DTEXTURE9’ does not name a type
prog.cpp:27: error: ‘LPD3DXSPRITE’ does not name a type
prog.cpp:31: error: expected initializer before ‘WinMain’
stdout
Standard output is empty