Small OpenGL based c++ rendering library
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

glsdlscreen.cpp 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #include "glsdlscreen.h"
  2. namespace segl {
  3. GLSDLScreen::GLSDLScreen() {
  4. width = 0;
  5. height = 1;
  6. bpp = 16;
  7. znear = 1.0f;
  8. zfar = 100.0f;
  9. opengl = false;
  10. glsetup = true;
  11. resizable = false;
  12. fullscreen = false;
  13. extraglparam = 0;
  14. }
  15. int GLSDLScreen::getFlags() {
  16. const SDL_VideoInfo *videoInfo = SDL_GetVideoInfo();
  17. int videoflags = 0;
  18. if(videoInfo->hw_available)
  19. videoflags |= SDL_HWSURFACE;
  20. else
  21. videoflags |= SDL_SWSURFACE;
  22. if(videoInfo->blit_hw)
  23. videoflags |= SDL_HWACCEL;
  24. if(opengl) {
  25. videoflags |= SDL_OPENGL;
  26. videoflags |= SDL_GL_DOUBLEBUFFER;
  27. videoflags |= SDL_HWPALETTE;
  28. }
  29. if(fullscreen)
  30. videoflags |= SDL_FULLSCREEN;
  31. return videoflags;
  32. }
  33. void GLSDLScreen::enableOpenGL(bool w) {
  34. opengl = w;
  35. }
  36. void GLSDLScreen::enableResizable(bool w) {
  37. resizable = w;
  38. }
  39. void GLSDLScreen::enableFullscreen(bool w) {
  40. fullscreen = w;
  41. }
  42. void GLSDLScreen::setGLNearFar(float _znear, float _zfar) {
  43. znear = _znear;
  44. zfar = _zfar;
  45. }
  46. void GLSDLScreen::enableGLSetup(bool w) {
  47. glsetup = w;
  48. }
  49. void GLSDLScreen::setExtraGLParamFunc(void (*extrafunc)()) {
  50. extraglparam = extrafunc;
  51. }
  52. void GLSDLScreen::setVideoMode(int _width, int _height, int _bpp) {
  53. width = _width;
  54. height = _height;
  55. bpp = _bpp;
  56. if(height==0)
  57. height = 1;
  58. }
  59. void GLSDLScreen::getVideoRes(std::string vidstr, int *_w, int *_h) {
  60. if(_w)
  61. *_w = atoi(vidstr.substr(0, vidstr.find("x")).c_str());
  62. if(_h)
  63. *_h = atoi(vidstr.substr(vidstr.find("x")+1, (vidstr.length()-vidstr.find("x")-1)).c_str());
  64. }
  65. bool GLSDLScreen::isOK() {
  66. return SDL_VideoModeOK(width, height, bpp, getFlags());
  67. }
  68. bool GLSDLScreen::apply() {
  69. if(!SDL_WasInit(SDL_INIT_VIDEO)) {
  70. if(SDL_Init(SDL_INIT_VIDEO)==-1) {
  71. std::cerr << "Konnte SDL_VIDEO nicht initialisieren" << std::endl;
  72. return false;
  73. }
  74. }
  75. screen = SDL_SetVideoMode(width, height, bpp, getFlags());
  76. if(!screen) {
  77. return false;
  78. }
  79. if(opengl) {
  80. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  81. // Setup GL
  82. if(glsetup) {
  83. glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  84. glClearDepth(1.0f);
  85. glDepthFunc(GL_LEQUAL);
  86. glShadeModel(GL_SMOOTH);
  87. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  88. glEnable(GL_ALPHA_TEST);
  89. glAlphaFunc(GL_GREATER, 0.1);
  90. glEnable(GL_DEPTH_TEST);
  91. }
  92. // Projection
  93. glViewport(0, 0, (GLsizei)width, (GLsizei)height);
  94. glMatrixMode(GL_PROJECTION);
  95. glLoadIdentity();
  96. gluPerspective(45.0f, (GLfloat)width/(GLfloat)height, znear, zfar);
  97. if(extraglparam) {
  98. extraglparam();
  99. }
  100. // Texturen neuladen, eigentlich nur f�r Windows. Aber egal.
  101. GLTexture::reloadAll();
  102. glMatrixMode(GL_TEXTURE);
  103. glLoadIdentity();
  104. glMatrixMode(GL_MODELVIEW);
  105. }
  106. return true;
  107. }
  108. int GLSDLScreen::getWidth() {
  109. return width;
  110. }
  111. int GLSDLScreen::getHeight() {
  112. return height;
  113. }
  114. void GLSDLScreen::clearScreen() {
  115. glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  116. glLoadIdentity();
  117. }
  118. void GLSDLScreen::renderScreen() {
  119. SDL_GL_SwapBuffers();
  120. }
  121. } // namespace segl