317 line
7.9 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2012 Sam Hocevar <sam@hocevar.net>
  5. // This program is free software; you can redistribute it and/or
  6. // modify it under the terms of the Do What The Fuck You Want To
  7. // Public License, Version 2, as published by Sam Hocevar. See
  8. // http://sam.zoy.org/projects/COPYING.WTFPL for more details.
  9. //
  10. #if defined HAVE_CONFIG_H
  11. # include "config.h"
  12. #endif
  13. #include <cmath>
  14. #if defined _XBOX
  15. # include <xtl.h>
  16. # undef near /* Fuck Microsoft */
  17. # undef far /* Fuck Microsoft again */
  18. #elif defined _WIN32
  19. # if defined USE_D3D9
  20. # include <d3d9.h>
  21. # endif
  22. # define WIN32_LEAN_AND_MEAN
  23. # include <windows.h>
  24. # undef near /* Fuck Microsoft */
  25. # undef far /* Fuck Microsoft again */
  26. #endif
  27. #include "core.h"
  28. #include "lolgl.h"
  29. using namespace std;
  30. /* FIXME: g_d3ddevice should never be exported */
  31. #if defined USE_D3D9
  32. IDirect3DDevice9 *g_d3ddevice;
  33. # if defined USE_SDL
  34. extern HWND g_hwnd;
  35. # endif
  36. #elif defined _XBOX
  37. D3DDevice *g_d3ddevice;
  38. #endif
  39. namespace lol
  40. {
  41. class VideoData
  42. {
  43. friend class Video;
  44. private:
  45. static mat4 proj_matrix, view_matrix;
  46. static ivec2 saved_viewport;
  47. #if defined USE_D3D9
  48. static IDirect3D9 *d3d_ctx;
  49. static IDirect3DDevice9 *d3d_dev;
  50. static D3DCOLOR clear_color;
  51. #elif defined _XBOX
  52. static Direct3D *d3d_ctx;
  53. static D3DDevice *d3d_dev;
  54. static D3DCOLOR clear_color;
  55. #endif
  56. };
  57. mat4 VideoData::proj_matrix;
  58. mat4 VideoData::view_matrix;
  59. ivec2 VideoData::saved_viewport(0, 0);
  60. #if defined USE_D3D9
  61. IDirect3D9 *VideoData::d3d_ctx;
  62. IDirect3DDevice9 *VideoData::d3d_dev;
  63. D3DCOLOR VideoData::clear_color;
  64. #elif defined _XBOX
  65. Direct3D *VideoData::d3d_ctx;
  66. D3DDevice *VideoData::d3d_dev;
  67. D3DCOLOR VideoData::clear_color;
  68. #endif
  69. /*
  70. * Public Video class
  71. */
  72. void Video::Setup(ivec2 size)
  73. {
  74. #if defined USE_D3D9 || defined _XBOX
  75. VideoData::d3d_ctx = Direct3DCreate9(D3D_SDK_VERSION);
  76. if (!VideoData::d3d_ctx)
  77. {
  78. Log::Error("cannot initialise D3D\n");
  79. exit(EXIT_FAILURE);
  80. }
  81. HWND window = 0;
  82. D3DPRESENT_PARAMETERS d3dpp;
  83. memset(&d3dpp, 0, sizeof(d3dpp));
  84. # if defined USE_SDL
  85. window = g_hwnd;
  86. d3dpp.hDeviceWindow = g_hwnd;
  87. d3dpp.Windowed = TRUE;
  88. # elif defined _XBOX
  89. XVIDEO_MODE VideoMode;
  90. XGetVideoMode( &VideoMode );
  91. if (size.x > VideoMode.dwDisplayWidth)
  92. size.x = VideoMode.dwDisplayWidth;
  93. if (size.y > VideoMode.dwDisplayHeight)
  94. size.y = VideoMode.dwDisplayHeight;
  95. # endif
  96. VideoData::saved_viewport = size;
  97. VideoData::clear_color = D3DCOLOR_XRGB(26, 51, 77);
  98. d3dpp.BackBufferWidth = size.x;
  99. d3dpp.BackBufferHeight = size.y;
  100. d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
  101. d3dpp.BackBufferCount = 1;
  102. d3dpp.EnableAutoDepthStencil = TRUE;
  103. d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
  104. d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
  105. d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
  106. HRESULT hr = VideoData::d3d_ctx->CreateDevice(0, D3DDEVTYPE_HAL, window,
  107. D3DCREATE_HARDWARE_VERTEXPROCESSING,
  108. &d3dpp, &VideoData::d3d_dev);
  109. if (FAILED(hr))
  110. {
  111. Log::Error("cannot create D3D device\n");
  112. exit(EXIT_FAILURE);
  113. }
  114. g_d3ddevice = VideoData::d3d_dev;
  115. g_d3ddevice->SetRenderState(D3DRS_ALPHABLENDENABLE, 1);
  116. g_d3ddevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
  117. g_d3ddevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
  118. #else
  119. # if defined USE_GLEW && !defined __APPLE__
  120. /* Initialise GLEW if necessary */
  121. GLenum glerr = glewInit();
  122. if (glerr != GLEW_OK)
  123. {
  124. Log::Error("cannot initialise GLEW: %s\n", glewGetErrorString(glerr));
  125. exit(EXIT_FAILURE);
  126. }
  127. # endif
  128. /* Initialise OpenGL */
  129. glViewport(0, 0, size.x, size.y);
  130. VideoData::saved_viewport = size;
  131. glClearColor(0.1f, 0.2f, 0.3f, 1.0f);
  132. glClearDepth(1.0);
  133. # if defined HAVE_GL_2X && !defined __APPLE__
  134. glShadeModel(GL_SMOOTH);
  135. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  136. # endif
  137. #endif
  138. }
  139. void Video::SetFov(float theta)
  140. {
  141. vec2 size = GetSize();
  142. float near = -size.x - size.y;
  143. float far = size.x + size.y;
  144. #if defined __ANDROID__
  145. size = vec2(640.0f, 480.0f);
  146. #endif
  147. /* Set the projection matrix */
  148. if (theta < 1e-4f)
  149. {
  150. /* The easy way: purely orthogonal projection. */
  151. VideoData::proj_matrix = mat4::ortho(0, size.x, 0, size.y, near, far);
  152. }
  153. else
  154. {
  155. /* Compute a view that approximates the glOrtho view when theta
  156. * approaches zero. This view ensures that the z=0 plane fills
  157. * the screen. */
  158. float t1 = tanf(theta / 2);
  159. float t2 = t1 * size.y / size.y;
  160. float dist = size.x / (2.0f * t1);
  161. near += dist;
  162. far += dist;
  163. if (near <= 0.0f)
  164. {
  165. far -= (near - 1.0f);
  166. near = 1.0f;
  167. }
  168. mat4 proj = mat4::frustum(-near * t1, near * t1,
  169. -near * t2, near * t2, near, far);
  170. mat4 trans = mat4::translate(-0.5f * size.x, -0.5f * size.y, -dist);
  171. VideoData::proj_matrix = proj * trans;
  172. }
  173. VideoData::view_matrix = mat4(1.0f);
  174. }
  175. void Video::SetDepth(bool set)
  176. {
  177. #if defined USE_D3D9 || defined _XBOX
  178. # define STR0(x) #x
  179. # define STR(x) STR0(x)
  180. # pragma message(__FILE__ "(" STR(__LINE__) "): warning: Video::SetDepth() not implemented")
  181. #else
  182. if (set)
  183. glEnable(GL_DEPTH_TEST);
  184. else
  185. glDisable(GL_DEPTH_TEST);
  186. #endif
  187. }
  188. void Video::SetClearColor(vec4 color)
  189. {
  190. #if defined USE_D3D9 || defined _XBOX
  191. VideoData::clear_color = D3DCOLOR_XRGB((int)(color.r * 255.999f),
  192. (int)(color.g * 255.999f),
  193. (int)(color.b * 255.999f));
  194. #else
  195. glClearColor(color.r, color.g, color.b, color.a);
  196. #endif
  197. }
  198. void Video::Clear()
  199. {
  200. ivec2 size = GetSize();
  201. #if defined USE_D3D9 || defined _XBOX
  202. if (FAILED(VideoData::d3d_dev->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER
  203. | D3DCLEAR_STENCIL,
  204. VideoData::clear_color, 1.0f, 0)))
  205. Abort();
  206. #else
  207. glViewport(0, 0, size.x, size.y);
  208. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
  209. #endif
  210. SetFov(0.0f);
  211. }
  212. void Video::Destroy()
  213. {
  214. ;
  215. }
  216. void Video::Capture(uint32_t *buffer)
  217. {
  218. #if defined USE_D3D9 || defined _XBOX
  219. /* TODO */
  220. #else
  221. GLint v[4];
  222. # if defined __CELLOS_LV2__
  223. // FIXME: use psglCreateDeviceAuto && psglGetDeviceDimensions
  224. v[2] = 1920;
  225. v[3] = 1080;
  226. # else
  227. glGetIntegerv(GL_VIEWPORT, v);
  228. # endif
  229. int width = v[2], height = v[3];
  230. # if defined HAVE_GL_2X
  231. glPixelStorei(GL_PACK_ROW_LENGTH, 0);
  232. # endif
  233. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  234. # if defined GL_BGRA
  235. glReadPixels(0, 0, width, height, GL_BGRA, GL_UNSIGNED_BYTE, buffer);
  236. # else
  237. glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
  238. # endif
  239. for (int j = 0; j < height / 2; j++)
  240. for (int i = 0; i < width; i++)
  241. {
  242. uint32_t tmp = buffer[j * width + i];
  243. buffer[j * width + i] = buffer[(height - j - 1) * width + i];
  244. buffer[(height - j - 1) * width + i] = tmp;
  245. }
  246. #endif
  247. }
  248. ivec2 Video::GetSize()
  249. {
  250. #if defined USE_D3D9 || defined _XBOX
  251. return VideoData::saved_viewport;
  252. #elif 1
  253. /* GetSize() is called too often on the game thread; we cannot rely on
  254. * the GL context at this point */
  255. return VideoData::saved_viewport;
  256. #elif defined __CELLOS_LV2__
  257. // FIXME: use psglCreateDeviceAuto && psglGetDeviceDimensions
  258. #else
  259. GLint v[4];
  260. glGetIntegerv(GL_VIEWPORT, v);
  261. return ivec2(v[2], v[3]);
  262. #endif
  263. }
  264. mat4 const & Video::GetProjMatrix()
  265. {
  266. return VideoData::proj_matrix;
  267. }
  268. mat4 const & Video::GetViewMatrix()
  269. {
  270. return VideoData::view_matrix;
  271. }
  272. } /* namespace lol */