Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

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