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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include "glsdlscreen.h"
  2. GLSDLScreen::GLSDLScreen() {
  3. width = 0;
  4. height = 1;
  5. bpp = 16;
  6. znear = 1.0f;
  7. zfar = 100.0f;
  8. opengl = false;
  9. glsetup = true;
  10. resizable = false;
  11. fullscreen = false;
  12. extraglparam = 0;
  13. }
  14. int GLSDLScreen::getFlags() {
  15. const SDL_VideoInfo *videoInfo = SDL_GetVideoInfo();
  16. int videoflags = 0;
  17. if(videoInfo->hw_available)
  18. videoflags |= SDL_HWSURFACE;
  19. else
  20. videoflags |= SDL_SWSURFACE;
  21. if(videoInfo->blit_hw)
  22. videoflags |= SDL_HWACCEL;
  23. if(opengl) {
  24. videoflags |= SDL_OPENGL;
  25. videoflags |= SDL_GL_DOUBLEBUFFER;
  26. videoflags |= SDL_HWPALETTE;
  27. }
  28. return videoflags;
  29. }
  30. void GLSDLScreen::enableOpenGL(bool w) {
  31. opengl = w;
  32. }
  33. void GLSDLScreen::enableResizable(bool w) {
  34. resizable = w;
  35. }
  36. void GLSDLScreen::enableFullscreen(bool w) {
  37. fullscreen = w;
  38. }
  39. void GLSDLScreen::setGLNearFar(float _znear, float _zfar) {
  40. znear = _znear;
  41. zfar = _zfar;
  42. }
  43. void GLSDLScreen::enableGLSetup(bool w) {
  44. glsetup = w;
  45. }
  46. void GLSDLScreen::setExtraGLParamFunc(void (*extrafunc)()) {
  47. extraglparam = extrafunc;
  48. }
  49. void GLSDLScreen::setVideoMode(int _width, int _height, int _bpp) {
  50. width = _width;
  51. height = _height;
  52. bpp = _bpp;
  53. if(height==0)
  54. height = 1;
  55. }
  56. bool GLSDLScreen::isOK() {
  57. return SDL_VideoModeOK(width, height, bpp, getFlags());
  58. }
  59. bool GLSDLScreen::apply() {
  60. if(!SDL_WasInit(SDL_INIT_VIDEO)) {
  61. if(SDL_Init(SDL_INIT_VIDEO)==-1) {
  62. return false;
  63. }
  64. }
  65. screen = SDL_SetVideoMode(width, height, bpp, getFlags());
  66. if(!screen) {
  67. return false;
  68. }
  69. if(opengl) {
  70. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  71. // Setup GL
  72. if(glsetup) {
  73. glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  74. glClearDepth(1.0f);
  75. glDepthFunc(GL_LEQUAL);
  76. glShadeModel(GL_SMOOTH);
  77. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  78. glEnable(GL_ALPHA_TEST);
  79. glAlphaFunc(GL_GREATER, 0.1);
  80. }
  81. // Projection
  82. glViewport(0, 0, (GLsizei)width, (GLsizei)height);
  83. glMatrixMode(GL_PROJECTION);
  84. glLoadIdentity();
  85. gluPerspective(45.0f, (GLfloat)width/(GLfloat)height, znear, zfar);
  86. if(extraglparam) {
  87. extraglparam();
  88. }
  89. glMatrixMode(GL_MODELVIEW);
  90. // Texturen neuladen, eigentlich nur für Windows. Aber egal.
  91. GLTexture::reloadAll();
  92. }
  93. return true;
  94. }