Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

179 строки
3.9 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. /* This instructs our headers to let SDL override the "main"
  14. * symbol using its macros. */
  15. #define LOL_DONT_DIVERT_MAIN 1
  16. #if defined USE_SDL
  17. # if defined HAVE_SDL_SDL_H
  18. # include <SDL/SDL.h>
  19. # else
  20. # include <SDL.h>
  21. # endif
  22. # if defined USE_D3D9
  23. # include <d3d9.h>
  24. # include <SDL_syswm.h>
  25. # endif
  26. #endif
  27. #include "core.h"
  28. #include "lolgl.h"
  29. #include "platform/sdl/sdlapp.h"
  30. #include "platform/sdl/sdlinput.h"
  31. #if defined USE_XINPUT
  32. # include "platform/d3d9/d3d9input.h"
  33. #endif
  34. #if defined USE_SDL && defined USE_D3D9
  35. HWND g_hwnd = NULL;
  36. extern IDirect3DDevice9 *g_d3ddevice;
  37. #endif
  38. #if defined main
  39. # if defined _MSC_VER
  40. int lol_sdl_main();
  41. int lol_sdl_main(int argc, char **argv);
  42. int lol_sdl_main(int argc, char **argv, char **envp);
  43. # define WRAPPER lol_sdl_main_msvc
  44. # else
  45. int lol_sdl_main() __attribute__((weak));
  46. int lol_sdl_main(int argc, char **argv) __attribute__((weak));
  47. int lol_sdl_main(int argc, char **argv, char **envp) __attribute__((weak));
  48. # define WRAPPER lol_sdl_main
  49. # endif
  50. /* One of these wrappers will be overridden by the user's version */
  51. int WRAPPER() { return 0; }
  52. int WRAPPER(int argc, char **argv) { return 0; }
  53. int WRAPPER(int argc, char **argv, char **envp) { return 0; }
  54. int main(int argc, char *argv[])
  55. {
  56. int ret = 0;
  57. ret += lol_sdl_main();
  58. ret += lol_sdl_main(argc, argv);
  59. ret += lol_sdl_main(argc, argv, NULL);
  60. return ret;
  61. }
  62. #endif
  63. namespace lol
  64. {
  65. /*
  66. * SDL App implementation class
  67. */
  68. class SdlAppData
  69. {
  70. friend class SdlApp;
  71. private:
  72. int unused;
  73. };
  74. /*
  75. * Public SdlApp class
  76. */
  77. SdlApp::SdlApp(char const *title, ivec2 res, float fps) :
  78. data(new SdlAppData())
  79. {
  80. #if defined USE_SDL
  81. /* Initialise SDL */
  82. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
  83. {
  84. Log::Error("cannot initialise SDL: %s\n", SDL_GetError());
  85. exit(EXIT_FAILURE);
  86. }
  87. # if defined USE_D3D9
  88. SDL_Surface *video = SDL_SetVideoMode(res.x, res.y, 16, 0);
  89. SDL_SysWMinfo wminfo;
  90. SDL_VERSION(&wminfo.version);
  91. SDL_GetWMInfo(&wminfo);
  92. g_hwnd = wminfo.window;
  93. # else
  94. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  95. SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
  96. SDL_Surface *video = SDL_SetVideoMode(res.x, res.y, 0, SDL_OPENGL);
  97. # endif
  98. if (!video)
  99. {
  100. Log::Error("cannot create rendering window: %s\n", SDL_GetError());
  101. SDL_Quit();
  102. exit(EXIT_FAILURE);
  103. }
  104. SDL_WM_SetCaption(title, NULL);
  105. /* Initialise everything */
  106. Ticker::Setup(fps);
  107. Video::Setup(ivec2(video->w, video->h));
  108. Audio::Setup(2);
  109. /* Autoreleased objects */
  110. # if defined USE_XINPUT
  111. /* Prefer D3d9 for joysticks on Windows, because the X360 pads are not
  112. * advertised with the proper number of axes. */
  113. new D3d9Input();
  114. # endif
  115. new SdlInput();
  116. #endif
  117. }
  118. void SdlApp::ShowPointer(bool show)
  119. {
  120. #if defined USE_SDL
  121. SDL_ShowCursor(show ? 1 : 0);
  122. #endif
  123. }
  124. void SdlApp::Tick()
  125. {
  126. #if defined USE_SDL && defined USE_D3D9
  127. HRESULT hr;
  128. hr = g_d3ddevice->BeginScene();
  129. if (FAILED(hr))
  130. Abort();
  131. #endif
  132. /* Tick the renderer, show the frame and clamp to desired framerate. */
  133. Ticker::TickDraw();
  134. #if defined USE_SDL
  135. # if defined USE_D3D9
  136. hr = g_d3ddevice->EndScene();
  137. if (FAILED(hr))
  138. Abort();
  139. hr = g_d3ddevice->Present(NULL, NULL, NULL, NULL);
  140. if (FAILED(hr))
  141. Abort();
  142. # else
  143. SDL_GL_SwapBuffers();
  144. # endif
  145. #endif
  146. }
  147. SdlApp::~SdlApp()
  148. {
  149. #if defined USE_SDL
  150. SDL_Quit();
  151. #endif
  152. delete data;
  153. }
  154. } /* namespace lol */