PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/src/system/graphic/VideoManager.cpp

http://violetland.googlecode.com/
C++ | 116 lines | 82 code | 26 blank | 8 comment | 14 complexity | 2f52cd387a5ead4c40ab428c42b20725 MD5 | raw file
  1. #include "VideoManager.h"
  2. VideoManager::VideoManager(FileUtility* fileUtility) {
  3. m_fileUtility = fileUtility;
  4. std::cout << "SDL_GL_SetAttribute SDL_GL_DOUBLEBUFFER..." << std::endl;
  5. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  6. RegularText = NULL;
  7. SmallText = NULL;
  8. m_lastFrameTime = m_fpsCountingStart = SDL_GetTicks();
  9. m_framesCount = 0;
  10. // Seems that this code is supported only in windows.
  11. // std::cout << "SDL_GL_SetAttribute SDL_GL_SWAP_CONTROL..." << std::endl;
  12. // SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1);
  13. }
  14. void VideoManager::countFrame(int frameDelay) {
  15. m_framesCount++;
  16. int now = SDL_GetTicks();
  17. m_frameDeltaTime = now - m_lastFrameTime;
  18. m_lastFrameTime = now;
  19. if (now - m_fpsCountingStart > 5000) {
  20. m_fpsCountingStart = now;
  21. m_fps = m_framesCount / 5;
  22. m_framesCount = 0;
  23. }
  24. if (frameDelay > 0 && m_frameDeltaTime < frameDelay)
  25. SDL_Delay(frameDelay - m_frameDeltaTime);
  26. }
  27. bool VideoManager::isModeAvailable(int w, int h, int bpp, bool fullscreen,
  28. int* true_bpp) {
  29. Uint32 flags = SDL_OPENGL;
  30. if (fullscreen)
  31. flags = flags | SDL_FULLSCREEN;
  32. int r = SDL_VideoModeOK(w, h, bpp, flags);
  33. if (true_bpp)
  34. *true_bpp = r;
  35. return (r != 0);
  36. }
  37. std::vector<SDL_Rect> VideoManager::GetAvailableModes() {
  38. /* This way is better than SDL_ListModes because of
  39. * SDL_ListModes returns not all possible modes
  40. */
  41. // Number of possible modes
  42. int wL[] = { 400, 640, 800, 1024, 1280, 1280, 1280, 1280, 1366, 1600, 1600,
  43. 1680, 1920, 1920 };
  44. int hL[] = { 300, 480, 600, 768, 720, 768, 800, 1024, 768, 900, 1200, 1050,
  45. 1080, 1200 };
  46. // If the mode is supported, it will be added to the return list
  47. std::vector<SDL_Rect> modes;
  48. for (unsigned int i = 0; i < getStructSize(wL); i++) {
  49. if (isModeAvailable(wL[i], hL[i], 16, true, NULL)) {
  50. SDL_Rect r;
  51. r.w = wL[i];
  52. r.h = hL[i];
  53. modes.push_back(r);
  54. }
  55. }
  56. return modes;
  57. }
  58. void VideoManager::setMode(VideoMode mode, Camera* cam) {
  59. std::cout << "SDL_SetVideoMode " << mode.Width << 'x' << mode.Height << '('
  60. << (mode.Full ? 'f' : 'w') << ")..." << std::endl;
  61. m_videoMode = mode;
  62. SDL_Surface *screen = SDL_SetVideoMode(mode.Width, mode.Height, mode.Color,
  63. mode.Full ? SDL_OPENGL | SDL_FULLSCREEN : SDL_OPENGL);
  64. std::cout << "Calculating aspect size..." << std::endl;
  65. cam->setAspect((float) mode.Width / mode.Height);
  66. WK = (float) mode.Width / cam->getW();
  67. HK = (float) mode.Height / cam->getH();
  68. Scale = (float) mode.Width / 800;
  69. if (screen == NULL) {
  70. std::cerr << "Couldn't set video mode: " << SDL_GetError() << std::endl;
  71. exit(2);
  72. }
  73. std::cout << "glViewport..." << std::endl;
  74. glViewport(0, 0, mode.Width, mode.Height);
  75. if (RegularText != NULL) {
  76. delete RegularText;
  77. }
  78. if (SmallText != NULL) {
  79. delete SmallText;
  80. }
  81. std::cout << "Preparing fonts..." << std::endl;
  82. boost::filesystem::path fontPath = m_fileUtility->getFullPath(
  83. FileUtility::common, "fonts/archangelsk.ttf");
  84. RegularText = new TextManager(fontPath, 46 * WK);
  85. SmallText = new TextManager(fontPath, 30 * WK);
  86. }
  87. VideoManager::~VideoManager() {
  88. delete RegularText;
  89. delete SmallText;
  90. }