SDL_windowsframebuffer.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "../../SDL_internal.h"
  19. #if SDL_VIDEO_DRIVER_WINDOWS
  20. #include "SDL_windowsvideo.h"
  21. int WIN_CreateWindowFramebuffer(_THIS, SDL_Window * window, Uint32 * format, void ** pixels, int *pitch)
  22. {
  23. SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
  24. size_t size;
  25. LPBITMAPINFO info;
  26. HBITMAP hbm;
  27. /* Free the old framebuffer surface */
  28. if (data->mdc) {
  29. DeleteDC(data->mdc);
  30. }
  31. if (data->hbm) {
  32. DeleteObject(data->hbm);
  33. }
  34. /* Find out the format of the screen */
  35. size = sizeof(BITMAPINFOHEADER) + 256 * sizeof (RGBQUAD);
  36. info = (LPBITMAPINFO)SDL_stack_alloc(Uint8, size);
  37. SDL_memset(info, 0, size);
  38. info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  39. /* The second call to GetDIBits() fills in the bitfields */
  40. hbm = CreateCompatibleBitmap(data->hdc, 1, 1);
  41. GetDIBits(data->hdc, hbm, 0, 0, NULL, info, DIB_RGB_COLORS);
  42. GetDIBits(data->hdc, hbm, 0, 0, NULL, info, DIB_RGB_COLORS);
  43. DeleteObject(hbm);
  44. *format = SDL_PIXELFORMAT_UNKNOWN;
  45. if (info->bmiHeader.biCompression == BI_BITFIELDS) {
  46. int bpp;
  47. Uint32 *masks;
  48. bpp = info->bmiHeader.biPlanes * info->bmiHeader.biBitCount;
  49. masks = (Uint32*)((Uint8*)info + info->bmiHeader.biSize);
  50. *format = SDL_MasksToPixelFormatEnum(bpp, masks[0], masks[1], masks[2], 0);
  51. }
  52. if (*format == SDL_PIXELFORMAT_UNKNOWN)
  53. {
  54. /* We'll use RGB format for now */
  55. *format = SDL_PIXELFORMAT_RGB888;
  56. /* Create a new one */
  57. SDL_memset(info, 0, size);
  58. info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  59. info->bmiHeader.biPlanes = 1;
  60. info->bmiHeader.biBitCount = 32;
  61. info->bmiHeader.biCompression = BI_RGB;
  62. }
  63. /* Fill in the size information */
  64. *pitch = (((window->w * SDL_BYTESPERPIXEL(*format)) + 3) & ~3);
  65. info->bmiHeader.biWidth = window->w;
  66. info->bmiHeader.biHeight = -window->h; /* negative for topdown bitmap */
  67. info->bmiHeader.biSizeImage = window->h * (*pitch);
  68. data->mdc = CreateCompatibleDC(data->hdc);
  69. data->hbm = CreateDIBSection(data->hdc, info, DIB_RGB_COLORS, pixels, NULL, 0);
  70. SDL_stack_free(info);
  71. if (!data->hbm) {
  72. return WIN_SetError("Unable to create DIB");
  73. }
  74. SelectObject(data->mdc, data->hbm);
  75. return 0;
  76. }
  77. int WIN_UpdateWindowFramebuffer(_THIS, SDL_Window * window, const SDL_Rect * rects, int numrects)
  78. {
  79. SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
  80. BitBlt(data->hdc, 0, 0, window->w, window->h, data->mdc, 0, 0, SRCCOPY);
  81. return 0;
  82. }
  83. void WIN_DestroyWindowFramebuffer(_THIS, SDL_Window * window)
  84. {
  85. SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
  86. if (!data) {
  87. /* The window wasn't fully initialized */
  88. return;
  89. }
  90. if (data->mdc) {
  91. DeleteDC(data->mdc);
  92. data->mdc = NULL;
  93. }
  94. if (data->hbm) {
  95. DeleteObject(data->hbm);
  96. data->hbm = NULL;
  97. }
  98. }
  99. #endif /* SDL_VIDEO_DRIVER_WINDOWS */
  100. /* vi: set ts=4 sw=4 expandtab: */