137 linhas
2.7 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_SDL && defined USE_D3D9
  25. HWND g_hwnd = NULL;
  26. extern IDirect3DDevice9 *g_d3ddevice;
  27. #endif
  28. namespace lol
  29. {
  30. /*
  31. * SDL App implementation class
  32. */
  33. class SdlAppData
  34. {
  35. friend class SdlApp;
  36. private:
  37. int unused;
  38. };
  39. /*
  40. * Public SdlApp class
  41. */
  42. SdlApp::SdlApp(char const *title, ivec2 res, float fps) :
  43. data(new SdlAppData())
  44. {
  45. #if defined USE_SDL
  46. /* Initialise SDL */
  47. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
  48. {
  49. Log::Error("cannot initialise SDL: %s\n", SDL_GetError());
  50. exit(EXIT_FAILURE);
  51. }
  52. # if defined USE_D3D9
  53. SDL_Surface *video = SDL_SetVideoMode(res.x, res.y, 16, 0);
  54. SDL_SysWMinfo wminfo;
  55. SDL_VERSION(&wminfo.version);
  56. SDL_GetWMInfo(&wminfo);
  57. g_hwnd = wminfo.window;
  58. # else
  59. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  60. SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
  61. SDL_Surface *video = SDL_SetVideoMode(res.x, res.y, 0, SDL_OPENGL);
  62. # endif
  63. if (!video)
  64. {
  65. Log::Error("cannot create rendering window: %s\n", SDL_GetError());
  66. SDL_Quit();
  67. exit(EXIT_FAILURE);
  68. }
  69. SDL_WM_SetCaption(title, NULL);
  70. /* Initialise everything */
  71. Ticker::Setup(fps);
  72. Video::Setup(ivec2(video->w, video->h));
  73. Audio::Setup(2);
  74. /* Autoreleased objects */
  75. new SdlInput();
  76. #endif
  77. }
  78. void SdlApp::ShowPointer(bool show)
  79. {
  80. #if defined USE_SDL
  81. SDL_ShowCursor(show ? 1 : 0);
  82. #endif
  83. }
  84. void SdlApp::Run()
  85. {
  86. while (!Ticker::Finished())
  87. {
  88. #if defined USE_SDL && defined USE_D3D9
  89. HRESULT hr;
  90. hr = g_d3ddevice->BeginScene();
  91. if (FAILED(hr))
  92. Abort();
  93. #endif
  94. /* Tick the renderer, show the frame and clamp to desired framerate. */
  95. Ticker::TickDraw();
  96. #if defined USE_SDL
  97. # if defined USE_D3D9
  98. hr = g_d3ddevice->EndScene();
  99. if (FAILED(hr))
  100. Abort();
  101. hr = g_d3ddevice->Present(NULL, NULL, NULL, NULL);
  102. if (FAILED(hr))
  103. Abort();
  104. # else
  105. SDL_GL_SwapBuffers();
  106. # endif
  107. #endif
  108. }
  109. }
  110. SdlApp::~SdlApp()
  111. {
  112. #if defined USE_SDL
  113. SDL_Quit();
  114. #endif
  115. delete data;
  116. }
  117. } /* namespace lol */