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

99 строки
1.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://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. #endif
  16. #include "core.h"
  17. #include "lolgl.h"
  18. #include "sdlapp.h"
  19. namespace lol
  20. {
  21. /*
  22. * SDL App implementation class
  23. */
  24. class SdlAppData
  25. {
  26. friend class SdlApp;
  27. private:
  28. int unused;
  29. };
  30. /*
  31. * Public SdlApp class
  32. */
  33. SdlApp::SdlApp(char const *title, vec2i res, float fps) :
  34. data(new SdlAppData())
  35. {
  36. #if defined USE_SDL
  37. /* Initialise SDL */
  38. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0)
  39. {
  40. Log::Error("cannot initialise SDL: %s\n", SDL_GetError());
  41. exit(EXIT_FAILURE);
  42. }
  43. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  44. SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
  45. SDL_Surface *video = SDL_SetVideoMode(res.x, res.y, 0, SDL_OPENGL);
  46. if (!video)
  47. {
  48. Log::Error("cannot create OpenGL screen: %s\n", SDL_GetError());
  49. SDL_Quit();
  50. exit(EXIT_FAILURE);
  51. }
  52. SDL_WM_SetCaption(title, NULL);
  53. SDL_ShowCursor(0);
  54. /* Initialise everything */
  55. Ticker::Setup(fps);
  56. Video::Setup(vec2i(video->w, video->h));
  57. Audio::Setup(2);
  58. #endif
  59. }
  60. void SdlApp::Run()
  61. {
  62. while (!Ticker::Finished())
  63. {
  64. /* Tick the game */
  65. Ticker::TickGame();
  66. /* Tick the renderer, show the frame and clamp to desired framerate. */
  67. Ticker::TickDraw();
  68. #if defined USE_SDL
  69. SDL_GL_SwapBuffers();
  70. #endif
  71. Ticker::ClampFps();
  72. }
  73. }
  74. SdlApp::~SdlApp()
  75. {
  76. #if defined USE_SDL
  77. SDL_Quit();
  78. #endif
  79. delete data;
  80. }
  81. } /* namespace lol */