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 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* libsegl - Sebas Extended GL Library
  2. * Collection of Opengl/3D-Math helpers
  3. *
  4. * Copyright (c) 2008 by Sebastian Lohff, seba@seba-geek.de
  5. * http://www.seba-geek.de
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Library General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Library General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Library General Public
  18. * License along with this library; if not, write to the
  19. * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
  20. * Boston, MA 02110-1301, USA.
  21. */
  22. #include "glsdlscreen.h"
  23. namespace segl {
  24. GLSDLScreen::GLSDLScreen() {
  25. width = 0;
  26. height = 1;
  27. bpp = 16;
  28. znear = 1.0f;
  29. zfar = 100.0f;
  30. opengl = false;
  31. glsetup = true;
  32. resizable = false;
  33. fullscreen = false;
  34. extraglparam = 0;
  35. }
  36. int GLSDLScreen::getFlags() {
  37. const SDL_VideoInfo *videoInfo = SDL_GetVideoInfo();
  38. int videoflags = 0;
  39. if(videoInfo->hw_available)
  40. videoflags |= SDL_HWSURFACE;
  41. else
  42. videoflags |= SDL_SWSURFACE;
  43. if(videoInfo->blit_hw)
  44. videoflags |= SDL_HWACCEL;
  45. if(opengl) {
  46. videoflags |= SDL_OPENGL;
  47. videoflags |= SDL_GL_DOUBLEBUFFER;
  48. videoflags |= SDL_HWPALETTE;
  49. }
  50. if(fullscreen)
  51. videoflags |= SDL_FULLSCREEN;
  52. return videoflags;
  53. }
  54. void GLSDLScreen::enableOpenGL(bool w) {
  55. opengl = w;
  56. }
  57. void GLSDLScreen::enableResizable(bool w) {
  58. resizable = w;
  59. }
  60. void GLSDLScreen::enableFullscreen(bool w) {
  61. fullscreen = w;
  62. }
  63. void GLSDLScreen::setGLNearFar(float _znear, float _zfar) {
  64. znear = _znear;
  65. zfar = _zfar;
  66. }
  67. void GLSDLScreen::enableGLSetup(bool w) {
  68. glsetup = w;
  69. }
  70. void GLSDLScreen::setExtraGLParamFunc(void (*extrafunc)()) {
  71. extraglparam = extrafunc;
  72. }
  73. void GLSDLScreen::setVideoMode(int _width, int _height, int _bpp) {
  74. width = _width;
  75. height = _height;
  76. bpp = _bpp;
  77. if(height==0)
  78. height = 1;
  79. }
  80. void GLSDLScreen::getVideoRes(std::string vidstr, int *_w, int *_h) {
  81. if(_w)
  82. *_w = atoi(vidstr.substr(0, vidstr.find("x")).c_str());
  83. if(_h)
  84. *_h = atoi(vidstr.substr(vidstr.find("x")+1, (vidstr.length()-vidstr.find("x")-1)).c_str());
  85. }
  86. bool GLSDLScreen::isOK() {
  87. return SDL_VideoModeOK(width, height, bpp, getFlags());
  88. }
  89. bool GLSDLScreen::apply() {
  90. if(!SDL_WasInit(SDL_INIT_VIDEO)) {
  91. if(SDL_Init(SDL_INIT_VIDEO)==-1) {
  92. std::cerr << "Konnte SDL_VIDEO nicht initialisieren" << std::endl;
  93. return false;
  94. }
  95. }
  96. screen = SDL_SetVideoMode(width, height, bpp, getFlags());
  97. if(!screen) {
  98. return false;
  99. }
  100. if(opengl) {
  101. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  102. // Setup GL
  103. if(glsetup) {
  104. glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  105. glClearDepth(1.0f);
  106. glDepthFunc(GL_LEQUAL);
  107. glShadeModel(GL_SMOOTH);
  108. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  109. glEnable(GL_ALPHA_TEST);
  110. glAlphaFunc(GL_GREATER, 0.1);
  111. glEnable(GL_DEPTH_TEST);
  112. }
  113. // Projection
  114. glViewport(0, 0, (GLsizei)width, (GLsizei)height);
  115. glMatrixMode(GL_PROJECTION);
  116. glLoadIdentity();
  117. gluPerspective(45.0f, (GLfloat)width/(GLfloat)height, znear, zfar);
  118. if(extraglparam) {
  119. extraglparam();
  120. }
  121. // Texturen neuladen, eigentlich nur f�r Windows. Aber egal.
  122. GLTexture::reloadAll();
  123. glMatrixMode(GL_TEXTURE);
  124. glLoadIdentity();
  125. glMatrixMode(GL_MODELVIEW);
  126. }
  127. return true;
  128. }
  129. int GLSDLScreen::getWidth() {
  130. return width;
  131. }
  132. int GLSDLScreen::getHeight() {
  133. return height;
  134. }
  135. void GLSDLScreen::clearScreen() {
  136. glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  137. glLoadIdentity();
  138. }
  139. void GLSDLScreen::renderScreen() {
  140. SDL_GL_SwapBuffers();
  141. }
  142. } // namespace segl