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.
 
 
 

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