/computer_graphics/lab5/Src/Library/cglD3D.cpp

https://github.com/smi13/semester07 · C++ · 152 lines · 64 code · 26 blank · 62 comment · 15 complexity · 7fd076980c3b3f5c410921d52a3163d5 MD5 · raw file

  1. /*
  2. * CVS Revision:$Id: cglD3D.cpp,v 1.0 10 Sep 2005 ã. Bvs Exp $
  3. *
  4. * Contact Person: Sang Oak WOO, James D.K. KIM
  5. *
  6. * This software module was originally developed by SAMSUNG Electronics
  7. * Co., Ltd. in the course of development of Direct3D Mobile Device
  8. * Driver.
  9. *
  10. * Copyright is not released for non SAMSUNG Electronics products. SAMSUNG
  11. * Electronics Co., Ltd. retains full right to use the code for his/her
  12. * own purpose, assign or donate the code to a third party and to inhibit
  13. * third parties from using the code for non SAMSUNG Electronics products.
  14. * This copyright notice must be included in all copies or derivative
  15. * works.
  16. *
  17. * Copyright (C) 2005, SAMSUNG Electronics Co., Ltd. All Rights Reserved.
  18. *
  19. *
  20. * For more information or to receive an up to date version of this
  21. * module, you may contact Sang Oak WOO at sowoo@samsung.com and James
  22. * D. K. KIM at jamesdk.kim@samsung.com.
  23. */
  24. /**
  25. @file
  26. @brief @documentme
  27. @date Created on 10/09/2005
  28. @project D3DBase
  29. @author Bvs
  30. */
  31. // *******************************************************
  32. // compiler specific
  33. // *******************************************************************
  34. // includes
  35. #include <windows.h>
  36. // this system
  37. #include "cglD3D.h"
  38. // *******************************************************************
  39. // defines & constants
  40. // *******************************************************************
  41. // static data
  42. // *******************************************************************
  43. // methods
  44. // *******************************************************************
  45. // functions
  46. cglD3D::FormatRecord cglD3D::m_formats[BPP_LAST] =
  47. { { D3DFMT_R5G6B5, D3DFMT_D16 },
  48. { D3DFMT_R8G8B8, D3DFMT_D24X8 },
  49. { D3DFMT_X8R8G8B8, D3DFMT_D24S8 } };
  50. // *******************************************************************
  51. // functions
  52. /**
  53. * This method creates Direct3D8, Direct3D8Device
  54. */
  55. cglD3D::cglD3D(const CreateParams &params)
  56. {
  57. D3DPRESENT_PARAMETERS ppParams;
  58. HRESULT hRes;
  59. DWORD nBehaviour;
  60. // Create interface
  61. m_lpD3D9 = Direct3DCreate9(D3D_SDK_VERSION);
  62. if (m_lpD3D9 == NULL)
  63. return;
  64. // A bit of checking: check adapters count
  65. if (m_lpD3D9->GetAdapterCount() < 1)
  66. return;
  67. // Here we simply assume that we ALWAYS start adapter with 0 index.
  68. // Commonly there is only one adapter, but in case of additional PCI video cards
  69. // we gonna miss them.
  70. // Get caps for future use
  71. hRes = m_lpD3D9->GetDeviceCaps(0, D3DDEVTYPE_HAL, &m_caps);
  72. // Simply fill presentation parameters
  73. memset(&ppParams, 0, sizeof(D3DPRESENT_PARAMETERS));
  74. ppParams.Windowed = TRUE; // We choose windowed mode
  75. ppParams.BackBufferCount = 1;
  76. ppParams.MultiSampleType = D3DMULTISAMPLE_NONE;
  77. ppParams.SwapEffect = D3DSWAPEFFECT_DISCARD;
  78. ppParams.EnableAutoDepthStencil = TRUE; // We want to create depth (z-buffer)
  79. ppParams.AutoDepthStencilFormat = m_formats[params.nBPP].fmtDepthBuffer;
  80. ppParams.hDeviceWindow = HWND(params.hWnd);
  81. ppParams.BackBufferWidth = params.nWidth;
  82. ppParams.BackBufferHeight = params.nHeight;
  83. ppParams.BackBufferFormat = m_formats[params.nBPP].fmtBackBuffer;
  84. // Determine the behaviour
  85. if ((m_caps.DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT) != 0)
  86. nBehaviour = D3DCREATE_HARDWARE_VERTEXPROCESSING;
  87. else
  88. nBehaviour = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
  89. // Create the device
  90. hRes = m_lpD3D9->CreateDevice(0, D3DDEVTYPE_HAL, ppParams.hDeviceWindow, nBehaviour,
  91. &ppParams, &m_lpD3D9Device);
  92. if (hRes != D3D_OK)
  93. {
  94. return;
  95. }
  96. }
  97. cglD3D::~cglD3D()
  98. {
  99. // Kill device if was created
  100. if (m_lpD3D9Device != NULL)
  101. m_lpD3D9Device->Release();
  102. // Kill interface if was created
  103. if (m_lpD3D9 != NULL)
  104. m_lpD3D9->Release();
  105. }
  106. bool cglD3D::isFailed()
  107. {
  108. return (m_lpD3D9 == NULL || m_lpD3D9Device == NULL);
  109. }
  110. bool cglD3D::beginRender()
  111. {
  112. // Begin rendering
  113. HRESULT hRes = m_lpD3D9Device->BeginScene();
  114. return (hRes == D3D_OK);
  115. }
  116. void cglD3D::endRender()
  117. {
  118. // Finish rendering
  119. m_lpD3D9Device->EndScene();
  120. // Present rendering results (performs coping from back to front buffer)
  121. m_lpD3D9Device->Present(NULL, NULL, NULL, NULL);
  122. }
  123. void cglD3D::clear(unsigned int nColor)
  124. {
  125. m_lpD3D9Device->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, nColor, 1.0f, 0);
  126. }