ImageDisplaySDL.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #ifdef NICE_USELIB_SDL
  2. #include <stdio.h>
  3. #include <iostream>
  4. #include <SDL/SDL.h>
  5. #if defined(Q_WS_X11)
  6. #include <X11/Xlib.h>
  7. #endif
  8. #include "core/imagedisplay/ImageDisplaySDL.h"
  9. using namespace std;
  10. namespace NICE {
  11. bool ImageDisplaySDL::sdlDisplayActive = false;
  12. ImageDisplaySDL::ImageDisplaySDL(QWidget *parent, const char *name)
  13. : QWidget(parent, name), screen(NULL), colorImage(NULL) {
  14. if (!sdlDisplayActive) {
  15. atexit(SDL_Quit);
  16. }
  17. }
  18. ImageDisplaySDL::~ImageDisplaySDL() {
  19. if (sdlDisplayActive) {
  20. SDL_QuitSubSystem(SDL_INIT_VIDEO);
  21. sdlDisplayActive = false;
  22. }
  23. }
  24. void ImageDisplaySDL::resizeEvent(QResizeEvent *) {
  25. // is SDL display already usend by another ImageDisplaySDL?
  26. if (screen == NULL && sdlDisplayActive) {
  27. return;
  28. }
  29. // We could get a resize event at any time, so clean previous mode
  30. screen = NULL;
  31. SDL_QuitSubSystem(SDL_INIT_VIDEO);
  32. sdlDisplayActive = false;
  33. // init SDL, do a little hack to get SDL in this Qt widget
  34. char variable[64];
  35. sprintf(variable, "SDL_WINDOWID=0x%lx", winId());
  36. putenv(variable);
  37. if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) {
  38. cerr << "Unable to init SDL: " << SDL_GetError() << endl;
  39. return;
  40. }
  41. // this holds some info about our display
  42. const SDL_VideoInfo* videoInfo = SDL_GetVideoInfo();
  43. // the flags to pass to SDL_SetVideoMode()
  44. int videoFlags = 0;
  45. int bitDepth = 0;
  46. if (videoInfo == NULL) {
  47. // FIXME throw an exception
  48. cerr << "Video query failed: " << SDL_GetError() << endl;
  49. } else {
  50. videoFlags = SDL_GL_DOUBLEBUFFER;
  51. //#define _OPENGL_
  52. #ifdef _OPENGL_
  53. bitDepth = 32;
  54. // use OpenGL
  55. videoFlags |= SDL_OPENGL;
  56. //videoFlags |= SDL_OPENGLBLIT;
  57. // color depth
  58. SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, bitDepth);
  59. SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);//Use at least 5 bits of Red
  60. SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);//Use at least 5 bits of Green
  61. SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);//Use at least 5 bits of Blue
  62. SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);//Use at least 16 bits for the depth buffer
  63. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);//Enable double buffering
  64. #else
  65. bitDepth = 24;
  66. #endif
  67. videoFlags |= SDL_HWPALETTE; // Store the palette in hardware
  68. videoFlags |= SDL_RLEACCEL; // use compression to speedup blit-transfer
  69. videoFlags |= SDL_ANYFORMAT; // simply use whatever format the hardware can accomodate
  70. videoFlags |= SDL_ASYNCBLIT;
  71. //videoFlags |= SDL_RESIZABLE; // Enable window resizing
  72. // This checks if hardware blits can be done
  73. if (videoInfo->blit_hw) {
  74. videoFlags |= SDL_HWACCEL;
  75. cerr << "SDL: blit_hw" << endl;
  76. }
  77. // This checks to see if surfaces can be stored in memory
  78. if (videoInfo->hw_available) {
  79. videoFlags |= SDL_HWSURFACE;
  80. cerr << "SDL: hw_available" << endl;
  81. } else {
  82. videoFlags |= SDL_SWSURFACE;
  83. }
  84. }
  85. // Set the new video mode with the new window size
  86. //screen = SDL_SetVideoMode(width(), height(), 0, 0);
  87. screen = SDL_SetVideoMode(width(), height(),
  88. bitDepth, videoFlags);
  89. if (screen == NULL) {
  90. cerr << "Unable to set video mode: " << SDL_GetError() << endl;
  91. return;
  92. } else {
  93. sdlDisplayActive = true;
  94. }
  95. if (!SDL_WM_ToggleFullScreen(screen)) {
  96. cerr << "SDL: SDL_WM_ToggleFullScreen(screen) failed." << endl;
  97. }
  98. }
  99. void ImageDisplaySDL::paintEvent(QPaintEvent *) {
  100. #if defined(Q_WS_X11)
  101. // Make sure we're not conflicting with drawing from the Qt library
  102. XSync(QPaintDevice::x11Display(), FALSE);
  103. #endif
  104. if (screen != NULL && colorImage != NULL) {
  105. // Lock surface if needed
  106. // if (SDL_MUSTLOCK(screen)) {
  107. // if (SDL_LockSurface(screen) < 0) {
  108. // return;
  109. // }
  110. // }
  111. SDL_Surface* sdlImage
  112. = SDL_CreateRGBSurfaceFrom(colorImage->getPixelPointer(),
  113. colorImage->width(),
  114. colorImage->height(),
  115. 24,
  116. colorImage->getStepsize(),
  117. #if SDL_BYTEORDER == SDL_LIL_ENDIAN
  118. 0x000000ff, 0x0000ff00, 0x00ff0000, 0);
  119. #else
  120. 0xff000000, 0x00ff0000, 0x0000ff00, 0);
  121. #endif
  122. if (sdlImage) {
  123. SDL_Rect dst;
  124. dst.x = (screen->w - sdlImage->w) / 2;
  125. dst.y = (screen->h - sdlImage->h) / 2;
  126. dst.w = sdlImage->w;
  127. dst.h = sdlImage->h;
  128. SDL_BlitSurface(sdlImage, NULL, screen, &dst);
  129. }
  130. // Unlock if needed
  131. // if (SDL_MUSTLOCK(gScreen)) {
  132. // SDL_UnlockSurface(gScreen);
  133. // }
  134. // Tell SDL to update the whole gScreen
  135. //SDL_UpdateRect(screen, 0, 0, m_iWidth, m_iHeight);
  136. SDL_Flip(screen);
  137. }
  138. }
  139. void ImageDisplaySDL::setImage(image::ColorImage* _image,
  140. const bool copy) {
  141. colorImage = _image;
  142. resize(colorImage->width(), colorImage->height());
  143. //updateGL();
  144. update();
  145. }
  146. } // namespace
  147. #else // NICE_USELIB_SDL
  148. #include "core/imagedisplay/ImageDisplaySDL.h"
  149. namespace NICE {
  150. bool ImageDisplaySDL::sdlDisplayActive = false;
  151. ImageDisplaySDL::ImageDisplaySDL(QWidget */*parent*/, const char */*name*/) {}
  152. ImageDisplaySDL::~ImageDisplaySDL() {}
  153. void ImageDisplaySDL::resizeEvent(QResizeEvent *) {}
  154. void ImageDisplaySDL::paintEvent(QPaintEvent *) {}
  155. void ImageDisplaySDL::setImage(ColorImage* /*_image*/, const bool /*copy*/) {}
  156. } // namespace
  157. #endif // NICE_USELIB_SDL