Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
4.0 KiB

  1. #include "stdafx.h"
  2. #include "sqplusWin32.h"
  3. #include "DXSquirrel_Binds.h"
  4. extern WNDCLASSEX gWC;
  5. extern IDirect3D9 * gpD3D;
  6. _MEMBER_FUNCTION_IMPL(Device,constructor)
  7. {
  8. StackHandler sa(v);
  9. //PARSES the params
  10. SquirrelObject params = sa.GetObjectHandle(2);
  11. D3DPRESENT_PARAMETERS d3dpp;
  12. ZeroMemory( &d3dpp, sizeof(d3dpp) );
  13. //defaults
  14. d3dpp.Windowed = TRUE;
  15. d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  16. d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
  17. d3dpp.EnableAutoDepthStencil = TRUE;
  18. d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
  19. d3dpp.BackBufferWidth = 640;
  20. d3dpp.BackBufferHeight = 480;
  21. //windowed
  22. if(params.Exists(_T("Windowed"))) {
  23. d3dpp.Windowed = params.GetBool(_T("Windowed"))? TRUE : FALSE;
  24. }
  25. if(params.Exists(_T("SwapEffect"))) {
  26. d3dpp.SwapEffect = (D3DSWAPEFFECT)params.GetInt(_T("SwapEffect"));
  27. }
  28. if(params.Exists(_T("BackBufferFormat"))) {
  29. d3dpp.BackBufferFormat = (D3DFORMAT)params.GetInt(_T("BackBufferFormat"));
  30. }
  31. if(params.Exists(_T("EnableAutoDepthStencil"))) {
  32. d3dpp.EnableAutoDepthStencil = params.GetBool(_T("EnableAutoDepthStencil"))? TRUE : FALSE;
  33. }
  34. if(params.Exists(_T("AutoDepthStencilFormat"))) {
  35. d3dpp.AutoDepthStencilFormat = (D3DFORMAT)params.GetInt(_T("AutoDepthStencilFormat"));
  36. }
  37. if(params.Exists(_T("BackBufferWidth"))) {
  38. d3dpp.BackBufferWidth = params.GetInt(_T("BackBufferWidth"));
  39. }
  40. if(params.Exists(_T("BackBufferHeight"))) {
  41. d3dpp.BackBufferHeight = params.GetInt(_T("BackBufferHeight"));
  42. }
  43. //create the window
  44. HWND hWnd = CreateWindow( _T("DXSquirrel"), _T("DXSquirrel"),
  45. 0, CW_USEDEFAULT, CW_USEDEFAULT, d3dpp.BackBufferWidth, d3dpp.BackBufferHeight,
  46. NULL, NULL, gWC.hInstance, NULL );
  47. if(!hWnd) return sa.ThrowError(_T("Error creating the window"));
  48. EnableWindow(hWnd,TRUE);
  49. ShowWindow(hWnd,SW_SHOW);
  50. //d3dpp.hDeviceWindow = hWnd;
  51. //PARSES the params
  52. IDirect3DDevice9 *pDev;
  53. HRESULT hr;
  54. if(FAILED(hr = gpD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
  55. D3DCREATE_SOFTWARE_VERTEXPROCESSING,
  56. &d3dpp, &pDev))) {
  57. return sa.ThrowError(_T("Error initializing the device"));
  58. }
  59. return construct_RefCounted(pDev);
  60. }
  61. _MEMBER_FUNCTION_IMPL(Device,BeginScene)
  62. {
  63. StackHandler sa(v);
  64. _CHECK_SELF(IDirect3DDevice9,Device);
  65. if(FAILED(self->BeginScene())) {
  66. return sa.ThrowError(_T("BeginScene failed"));
  67. }
  68. return 0;
  69. }
  70. _MEMBER_FUNCTION_IMPL(Device,EndScene)
  71. {
  72. StackHandler sa(v);
  73. _CHECK_SELF(IDirect3DDevice9,Device);
  74. if(FAILED(self->EndScene())) {
  75. return sa.ThrowError(_T("EndScene failed"));
  76. }
  77. return 0;
  78. }
  79. //params (this,flags,[color],[z],[stencil])
  80. _MEMBER_FUNCTION_IMPL(Device,Clear)
  81. {
  82. StackHandler sa(v);
  83. _CHECK_SELF(IDirect3DDevice9,Device);
  84. FLOAT z = 1.0f;
  85. DWORD color = 0xFF000000;
  86. DWORD stencil = 0;
  87. INT params = sa.GetParamCount();
  88. if(params > 2) {
  89. color = (DWORD)sa.GetInt(3);
  90. }
  91. if(params > 3) {
  92. z = sa.GetFloat(4);
  93. }
  94. if(params > 4) {
  95. stencil = (DWORD)sa.GetInt(4);
  96. }
  97. HRESULT hr;
  98. if(FAILED(hr = self->Clear(0,NULL,(DWORD)sa.GetInt(2),color,z,stencil)))
  99. {
  100. return sa.ThrowError(_T("Clear failed"));
  101. }
  102. return 0;
  103. }
  104. _MEMBER_FUNCTION_IMPL(Device,Present)
  105. {
  106. StackHandler sa(v);
  107. _CHECK_SELF(IDirect3DDevice9,Device);
  108. self->Present(NULL,NULL,NULL,NULL);
  109. return 0;
  110. }
  111. _MEMBER_FUNCTION_IMPL(Device,SetTransform)
  112. {
  113. StackHandler sa(v);
  114. _CHECK_SELF(IDirect3DDevice9,Device);
  115. _CHECK_INST_PARAM(mat,3,D3DXMATRIX,Matrix);
  116. self->SetTransform((D3DTRANSFORMSTATETYPE)sa.GetInt(2),mat);
  117. return 0;
  118. }
  119. _MEMBER_FUNCTION_IMPL(Device,SetRenderState)
  120. {
  121. StackHandler sa(v);
  122. _CHECK_SELF(IDirect3DDevice9,Device);
  123. self->SetRenderState((D3DRENDERSTATETYPE)sa.GetInt(2),(DWORD)sa.GetInt(3));
  124. return 0;
  125. }
  126. _BEGIN_CLASS(Device)
  127. _MEMBER_FUNCTION(Device,constructor,2,_T("xt"))
  128. _MEMBER_FUNCTION(Device,BeginScene,0,NULL)
  129. _MEMBER_FUNCTION(Device,EndScene,0,NULL)
  130. _MEMBER_FUNCTION(Device,Present,0,NULL)
  131. _MEMBER_FUNCTION(Device,Clear,-2,_T("xnnnn"))
  132. _MEMBER_FUNCTION(Device,SetTransform,3,_T("xnx"))
  133. _MEMBER_FUNCTION(Device,SetRenderState,3,_T("xnn|b"))
  134. _END_CLASS(Device)