145 行
3.0 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2011 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 USE_SDL
  14. # include <SDL.h>
  15. # if defined USE_D3D9
  16. # include <d3d9.h>
  17. # include <SDL_syswm.h>
  18. # endif
  19. #endif
  20. #include "core.h"
  21. #include "lolgl.h"
  22. #include "platform/sdl/sdlapp.h"
  23. #include "platform/sdl/sdlinput.h"
  24. #if defined USE_D3D9
  25. # include "platform/d3d9/d3d9input.h"
  26. #endif
  27. #if defined USE_SDL && defined USE_D3D9
  28. HWND g_hwnd = NULL;
  29. extern IDirect3DDevice9 *g_d3ddevice;
  30. #endif
  31. namespace lol
  32. {
  33. /*
  34. * SDL App implementation class
  35. */
  36. class SdlAppData
  37. {
  38. friend class SdlApp;
  39. private:
  40. int unused;
  41. };
  42. /*
  43. * Public SdlApp class
  44. */
  45. SdlApp::SdlApp(char const *title, ivec2 res, float fps) :
  46. data(new SdlAppData())
  47. {
  48. #if defined USE_SDL
  49. /* Initialise SDL */
  50. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
  51. {
  52. Log::Error("cannot initialise SDL: %s\n", SDL_GetError());
  53. exit(EXIT_FAILURE);
  54. }
  55. # if defined USE_D3D9
  56. SDL_Surface *video = SDL_SetVideoMode(res.x, res.y, 16, 0);
  57. SDL_SysWMinfo wminfo;
  58. SDL_VERSION(&wminfo.version);
  59. SDL_GetWMInfo(&wminfo);
  60. g_hwnd = wminfo.window;
  61. # else
  62. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  63. SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
  64. SDL_Surface *video = SDL_SetVideoMode(res.x, res.y, 0, SDL_OPENGL);
  65. # endif
  66. if (!video)
  67. {
  68. Log::Error("cannot create rendering window: %s\n", SDL_GetError());
  69. SDL_Quit();
  70. exit(EXIT_FAILURE);
  71. }
  72. SDL_WM_SetCaption(title, NULL);
  73. /* Initialise everything */
  74. Ticker::Setup(fps);
  75. Video::Setup(ivec2(video->w, video->h));
  76. Audio::Setup(2);
  77. /* Autoreleased objects */
  78. # if defined USE_D3D9
  79. /* Prefer D3d9 for joysticks on Windows, because the X360 pads are not
  80. * advertised with the proper number of axes. */
  81. new D3d9Input();
  82. # endif
  83. new SdlInput();
  84. #endif
  85. }
  86. void SdlApp::ShowPointer(bool show)
  87. {
  88. #if defined USE_SDL
  89. SDL_ShowCursor(show ? 1 : 0);
  90. #endif
  91. }
  92. void SdlApp::Run()
  93. {
  94. while (!Ticker::Finished())
  95. {
  96. #if defined USE_SDL && defined USE_D3D9
  97. HRESULT hr;
  98. hr = g_d3ddevice->BeginScene();
  99. if (FAILED(hr))
  100. Abort();
  101. #endif
  102. /* Tick the renderer, show the frame and clamp to desired framerate. */
  103. Ticker::TickDraw();
  104. #if defined USE_SDL
  105. # if defined USE_D3D9
  106. hr = g_d3ddevice->EndScene();
  107. if (FAILED(hr))
  108. Abort();
  109. hr = g_d3ddevice->Present(NULL, NULL, NULL, NULL);
  110. if (FAILED(hr))
  111. Abort();
  112. # else
  113. SDL_GL_SwapBuffers();
  114. # endif
  115. #endif
  116. }
  117. }
  118. SdlApp::~SdlApp()
  119. {
  120. #if defined USE_SDL
  121. SDL_Quit();
  122. #endif
  123. delete data;
  124. }
  125. } /* namespace lol */