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