Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Use the exact format of the window if possible, for speed.
Browse files Browse the repository at this point in the history
  • Loading branch information
slouken committed Feb 4, 2011
1 parent da111d4 commit 5961e50
Showing 1 changed file with 60 additions and 17 deletions.
77 changes: 60 additions & 17 deletions src/video/windows/SDL_windowsframebuffer.c
Expand Up @@ -23,11 +23,16 @@

#include "SDL_windowsvideo.h"

#define HAVE_GETDIBITS

int WIN_CreateWindowFramebuffer(_THIS, SDL_Window * window, Uint32 * format, void ** pixels, int *pitch)
{
SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
BITMAPINFO info;
size_t size;
LPBITMAPINFO info;
#ifdef HAVE_GETDIBITS
HBITMAP hbm;
#endif

/* Free the old framebuffer surface */
if (data->mdc) {
Expand All @@ -37,25 +42,63 @@ int WIN_CreateWindowFramebuffer(_THIS, SDL_Window * window, Uint32 * format, voi
DeleteObject(data->hbm);
}

/* We'll use RGB format for now */
*format = SDL_PIXELFORMAT_RGB888;
*pitch = (((window->w * SDL_BYTESPERPIXEL(*format)) + 3) & ~3);
/* Find out the format of the screen */
size = sizeof(BITMAPINFOHEADER) + 256 * sizeof (RGBQUAD);
info = (LPBITMAPINFO)SDL_stack_alloc(Uint8, size);

#ifdef HAVE_GETDIBITS
SDL_memset(info, 0, size);
info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);

/* The second call to GetDIBits() fills in the bitfields */
hbm = CreateCompatibleBitmap(data->hdc, 1, 1);
GetDIBits(data->hdc, hbm, 0, 0, NULL, info, DIB_RGB_COLORS);
GetDIBits(data->hdc, hbm, 0, 0, NULL, info, DIB_RGB_COLORS);
DeleteObject(hbm);

*format = SDL_PIXELFORMAT_UNKNOWN;
if (info->bmiHeader.biCompression == BI_BITFIELDS) {
Uint32 *masks;

masks = (Uint32*)((Uint8*)info + info->bmiHeader.biSize);
if (masks[0] == 0x00FF0000 && masks[2] == 0x000000FF) {
*format = SDL_PIXELFORMAT_RGB888;
} else if (masks[0] == 0x000000FF && masks[2] == 0x00FF0000) {
*format = SDL_PIXELFORMAT_BGR888;
} else if (masks[0] == 0xF800 && masks[2] == 0x001F) {
*format = SDL_PIXELFORMAT_RGB565;
} else if (masks[0] == 0x001F && masks[2] == 0xF800) {
*format = SDL_PIXELFORMAT_BGR565;
} else if (masks[0] == 0x7C00 && masks[2] == 0x001F) {
*format = SDL_PIXELFORMAT_RGB555;
} else if (masks[0] == 0x001F && masks[2] == 0x7C00) {
*format = SDL_PIXELFORMAT_BGR555;
}
}
if (*format == SDL_PIXELFORMAT_UNKNOWN)
#endif
{
/* We'll use RGB format for now */
*format = SDL_PIXELFORMAT_RGB888;

/* Create a new one */
SDL_memset(info, 0, size);
info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
info->bmiHeader.biPlanes = 1;
info->bmiHeader.biBitCount = 32;
info->bmiHeader.biCompression = BI_RGB;
}

/* Create a new one */
info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
info.bmiHeader.biWidth = window->w;
info.bmiHeader.biHeight = -window->h; /* negative for topdown bitmap */
info.bmiHeader.biPlanes = 1;
info.bmiHeader.biBitCount = 32;
info.bmiHeader.biCompression = BI_RGB;
info.bmiHeader.biSizeImage = window->h * (*pitch);
info.bmiHeader.biXPelsPerMeter = 0;
info.bmiHeader.biYPelsPerMeter = 0;
info.bmiHeader.biClrUsed = 0;
info.bmiHeader.biClrImportant = 0;
/* Fill in the size information */
*pitch = (((window->w * SDL_BYTESPERPIXEL(*format)) + 3) & ~3);
info->bmiHeader.biWidth = window->w;
info->bmiHeader.biHeight = -window->h; /* negative for topdown bitmap */
info->bmiHeader.biSizeImage = window->h * (*pitch);

data->mdc = CreateCompatibleDC(data->hdc);
data->hbm = CreateDIBSection(data->hdc, &info, DIB_RGB_COLORS, pixels, NULL, 0);
data->hbm = CreateDIBSection(data->hdc, info, DIB_RGB_COLORS, pixels, NULL, 0);
SDL_stack_free(info);

if (!data->hbm) {
WIN_SetError("Unable to create DIB");
return -1;
Expand Down

0 comments on commit 5961e50

Please sign in to comment.