comparison src/video/windows/SDL_windowsframebuffer.c @ 5179:6d37aa594ecd

Use the exact format of the window if possible, for speed.
author Sam Lantinga <slouken@libsdl.org>
date Fri, 04 Feb 2011 13:47:02 -0800
parents 9b2e99ebd099
children 4191af605cb0
comparison
equal deleted inserted replaced
5178:9b2e99ebd099 5179:6d37aa594ecd
21 */ 21 */
22 #include "SDL_config.h" 22 #include "SDL_config.h"
23 23
24 #include "SDL_windowsvideo.h" 24 #include "SDL_windowsvideo.h"
25 25
26 #define HAVE_GETDIBITS
26 27
27 int WIN_CreateWindowFramebuffer(_THIS, SDL_Window * window, Uint32 * format, void ** pixels, int *pitch) 28 int WIN_CreateWindowFramebuffer(_THIS, SDL_Window * window, Uint32 * format, void ** pixels, int *pitch)
28 { 29 {
29 SDL_WindowData *data = (SDL_WindowData *) window->driverdata; 30 SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
30 BITMAPINFO info; 31 size_t size;
32 LPBITMAPINFO info;
33 #ifdef HAVE_GETDIBITS
34 HBITMAP hbm;
35 #endif
31 36
32 /* Free the old framebuffer surface */ 37 /* Free the old framebuffer surface */
33 if (data->mdc) { 38 if (data->mdc) {
34 DeleteDC(data->mdc); 39 DeleteDC(data->mdc);
35 } 40 }
36 if (data->hbm) { 41 if (data->hbm) {
37 DeleteObject(data->hbm); 42 DeleteObject(data->hbm);
38 } 43 }
39 44
40 /* We'll use RGB format for now */ 45 /* Find out the format of the screen */
41 *format = SDL_PIXELFORMAT_RGB888; 46 size = sizeof(BITMAPINFOHEADER) + 256 * sizeof (RGBQUAD);
47 info = (LPBITMAPINFO)SDL_stack_alloc(Uint8, size);
48
49 #ifdef HAVE_GETDIBITS
50 SDL_memset(info, 0, size);
51 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
52
53 /* The second call to GetDIBits() fills in the bitfields */
54 hbm = CreateCompatibleBitmap(data->hdc, 1, 1);
55 GetDIBits(data->hdc, hbm, 0, 0, NULL, info, DIB_RGB_COLORS);
56 GetDIBits(data->hdc, hbm, 0, 0, NULL, info, DIB_RGB_COLORS);
57 DeleteObject(hbm);
58
59 *format = SDL_PIXELFORMAT_UNKNOWN;
60 if (info->bmiHeader.biCompression == BI_BITFIELDS) {
61 Uint32 *masks;
62
63 masks = (Uint32*)((Uint8*)info + info->bmiHeader.biSize);
64 if (masks[0] == 0x00FF0000 && masks[2] == 0x000000FF) {
65 *format = SDL_PIXELFORMAT_RGB888;
66 } else if (masks[0] == 0x000000FF && masks[2] == 0x00FF0000) {
67 *format = SDL_PIXELFORMAT_BGR888;
68 } else if (masks[0] == 0xF800 && masks[2] == 0x001F) {
69 *format = SDL_PIXELFORMAT_RGB565;
70 } else if (masks[0] == 0x001F && masks[2] == 0xF800) {
71 *format = SDL_PIXELFORMAT_BGR565;
72 } else if (masks[0] == 0x7C00 && masks[2] == 0x001F) {
73 *format = SDL_PIXELFORMAT_RGB555;
74 } else if (masks[0] == 0x001F && masks[2] == 0x7C00) {
75 *format = SDL_PIXELFORMAT_BGR555;
76 }
77 }
78 if (*format == SDL_PIXELFORMAT_UNKNOWN)
79 #endif
80 {
81 /* We'll use RGB format for now */
82 *format = SDL_PIXELFORMAT_RGB888;
83
84 /* Create a new one */
85 SDL_memset(info, 0, size);
86 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
87 info->bmiHeader.biPlanes = 1;
88 info->bmiHeader.biBitCount = 32;
89 info->bmiHeader.biCompression = BI_RGB;
90 }
91
92 /* Fill in the size information */
42 *pitch = (((window->w * SDL_BYTESPERPIXEL(*format)) + 3) & ~3); 93 *pitch = (((window->w * SDL_BYTESPERPIXEL(*format)) + 3) & ~3);
43 94 info->bmiHeader.biWidth = window->w;
44 /* Create a new one */ 95 info->bmiHeader.biHeight = -window->h; /* negative for topdown bitmap */
45 info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); 96 info->bmiHeader.biSizeImage = window->h * (*pitch);
46 info.bmiHeader.biWidth = window->w;
47 info.bmiHeader.biHeight = -window->h; /* negative for topdown bitmap */
48 info.bmiHeader.biPlanes = 1;
49 info.bmiHeader.biBitCount = 32;
50 info.bmiHeader.biCompression = BI_RGB;
51 info.bmiHeader.biSizeImage = window->h * (*pitch);
52 info.bmiHeader.biXPelsPerMeter = 0;
53 info.bmiHeader.biYPelsPerMeter = 0;
54 info.bmiHeader.biClrUsed = 0;
55 info.bmiHeader.biClrImportant = 0;
56 97
57 data->mdc = CreateCompatibleDC(data->hdc); 98 data->mdc = CreateCompatibleDC(data->hdc);
58 data->hbm = CreateDIBSection(data->hdc, &info, DIB_RGB_COLORS, pixels, NULL, 0); 99 data->hbm = CreateDIBSection(data->hdc, info, DIB_RGB_COLORS, pixels, NULL, 0);
100 SDL_stack_free(info);
101
59 if (!data->hbm) { 102 if (!data->hbm) {
60 WIN_SetError("Unable to create DIB"); 103 WIN_SetError("Unable to create DIB");
61 return -1; 104 return -1;
62 } 105 }
63 SelectObject(data->mdc, data->hbm); 106 SelectObject(data->mdc, data->hbm);