SDL_windowsframebuffer.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2026 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 defined(SDL_VIDEO_DRIVER_WINDOWS) && !defined(SDL_PLATFORM_XBOXONE) && !defined(SDL_PLATFORM_XBOXSERIES)
  20. #include "SDL_windowsvideo.h"
  21. bool WIN_CreateWindowFramebuffer(SDL_VideoDevice *_this, SDL_Window *window, SDL_PixelFormat *format, void **pixels, int *pitch)
  22. {
  23. SDL_WindowData *data = window->internal;
  24. bool isstack;
  25. size_t size;
  26. LPBITMAPINFO info;
  27. HBITMAP hbm;
  28. int w, h;
  29. SDL_GetWindowSizeInPixels(window, &w, &h);
  30. // Free the old framebuffer surface
  31. if (data->mdc) {
  32. DeleteDC(data->mdc);
  33. }
  34. if (data->hbm) {
  35. DeleteObject(data->hbm);
  36. }
  37. // Find out the format of the screen
  38. size = sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD);
  39. info = (LPBITMAPINFO)SDL_small_alloc(Uint8, size, &isstack);
  40. if (!info) {
  41. return false;
  42. }
  43. SDL_memset(info, 0, size);
  44. info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  45. // The second call to GetDIBits() fills in the bitfields
  46. hbm = CreateCompatibleBitmap(data->hdc, 1, 1);
  47. GetDIBits(data->hdc, hbm, 0, 0, NULL, info, DIB_RGB_COLORS);
  48. GetDIBits(data->hdc, hbm, 0, 0, NULL, info, DIB_RGB_COLORS);
  49. DeleteObject(hbm);
  50. // Check if a transparent channel is required
  51. bool need_alpha = (window->flags & SDL_WINDOW_TRANSPARENT) != 0;
  52. *format = SDL_PIXELFORMAT_UNKNOWN;
  53. if (info->bmiHeader.biCompression == BI_BITFIELDS) {
  54. int bpp;
  55. Uint32 *masks;
  56. bpp = info->bmiHeader.biPlanes * info->bmiHeader.biBitCount;
  57. masks = (Uint32 *)((Uint8 *)info + info->bmiHeader.biSize);
  58. *format = SDL_GetPixelFormatForMasks(bpp, masks[0], masks[1], masks[2], 0);
  59. }
  60. if (*format == SDL_PIXELFORMAT_UNKNOWN || need_alpha) {
  61. // We'll use RGB or BGRA32 format for now
  62. *format = need_alpha ? SDL_PIXELFORMAT_BGRA32 : SDL_PIXELFORMAT_XRGB8888;
  63. // Create a new one
  64. SDL_memset(info, 0, size);
  65. info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  66. info->bmiHeader.biPlanes = 1;
  67. info->bmiHeader.biBitCount = 32;
  68. info->bmiHeader.biCompression = need_alpha ? BI_BITFIELDS : BI_RGB;
  69. if (need_alpha) {
  70. int tmpbpp;
  71. Uint32 *bgr32masks = (Uint32 *)((Uint8 *)info + info->bmiHeader.biSize);
  72. SDL_GetMasksForPixelFormat(SDL_PIXELFORMAT_BGRA32, &tmpbpp, &bgr32masks[0], &bgr32masks[1], &bgr32masks[2], &bgr32masks[3]);
  73. }
  74. }
  75. // Fill in the size information
  76. *pitch = (((w * SDL_BYTESPERPIXEL(*format)) + 3) & ~3);
  77. info->bmiHeader.biWidth = w;
  78. info->bmiHeader.biHeight = -h; // negative for topdown bitmap
  79. info->bmiHeader.biSizeImage = (DWORD)h * (*pitch);
  80. data->mdc = CreateCompatibleDC(data->hdc);
  81. data->hbm = CreateDIBSection(data->hdc, info, DIB_RGB_COLORS, pixels, NULL, 0);
  82. SDL_small_free(info, isstack);
  83. if (!data->hbm) {
  84. return WIN_SetError("Unable to create DIB");
  85. }
  86. SelectObject(data->mdc, data->hbm);
  87. return true;
  88. }
  89. bool WIN_UpdateWindowFramebuffer(SDL_VideoDevice *_this, SDL_Window *window, const SDL_Rect *rects, int numrects)
  90. {
  91. SDL_WindowData *data = window->internal;
  92. int i;
  93. for (i = 0; i < numrects; ++i) {
  94. BitBlt(data->hdc, rects[i].x, rects[i].y, rects[i].w, rects[i].h,
  95. data->mdc, rects[i].x, rects[i].y, SRCCOPY);
  96. }
  97. return true;
  98. }
  99. void WIN_DestroyWindowFramebuffer(SDL_VideoDevice *_this, SDL_Window *window)
  100. {
  101. SDL_WindowData *data = window->internal;
  102. if (!data) {
  103. // The window wasn't fully initialized
  104. return;
  105. }
  106. if (data->mdc) {
  107. DeleteDC(data->mdc);
  108. data->mdc = NULL;
  109. }
  110. if (data->hbm) {
  111. DeleteObject(data->hbm);
  112. data->hbm = NULL;
  113. }
  114. }
  115. #endif // SDL_VIDEO_DRIVER_WINDOWS