src/video/windows/SDL_windowsframebuffer.c
author Sam Lantinga
Tue, 08 Feb 2011 16:50:51 -0800
changeset 5229 c015d3e63631
parent 5181 d2652aafafbb
child 5262 b530ef003506
permissions -rw-r--r--
Fixed setting the texture unit, still doesn't work.
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2010 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 #include "SDL_windowsvideo.h"
    25 
    26 #ifndef _WIN32_WCE
    27 #define HAVE_GETDIBITS
    28 #endif
    29 
    30 int WIN_CreateWindowFramebuffer(_THIS, SDL_Window * window, Uint32 * format, void ** pixels, int *pitch)
    31 {
    32     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
    33     size_t size;
    34     LPBITMAPINFO info;
    35 #ifdef HAVE_GETDIBITS
    36     HBITMAP hbm;
    37 #endif
    38 
    39     /* Free the old framebuffer surface */
    40     if (data->mdc) {
    41         DeleteDC(data->mdc);
    42     }
    43     if (data->hbm) {
    44         DeleteObject(data->hbm);
    45     }
    46 
    47     /* Find out the format of the screen */
    48     size = sizeof(BITMAPINFOHEADER) + 256 * sizeof (RGBQUAD);
    49     info = (LPBITMAPINFO)SDL_stack_alloc(Uint8, size);
    50 
    51 #ifdef HAVE_GETDIBITS
    52     SDL_memset(info, 0, size);
    53     info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    54 
    55     /* The second call to GetDIBits() fills in the bitfields */
    56     hbm = CreateCompatibleBitmap(data->hdc, 1, 1);
    57     GetDIBits(data->hdc, hbm, 0, 0, NULL, info, DIB_RGB_COLORS);
    58     GetDIBits(data->hdc, hbm, 0, 0, NULL, info, DIB_RGB_COLORS);
    59     DeleteObject(hbm);
    60 
    61     *format = SDL_PIXELFORMAT_UNKNOWN;
    62     if (info->bmiHeader.biCompression == BI_BITFIELDS) {
    63         int bpp;
    64         Uint32 *masks;
    65 
    66         bpp = info->bmiHeader.biPlanes * info->bmiHeader.biBitCount;
    67         masks = (Uint32*)((Uint8*)info + info->bmiHeader.biSize);
    68         *format = SDL_MasksToPixelFormatEnum(bpp, masks[0], masks[1], masks[2], 0);
    69     }
    70     if (*format == SDL_PIXELFORMAT_UNKNOWN)
    71 #endif
    72     {
    73         /* We'll use RGB format for now */
    74         *format = SDL_PIXELFORMAT_RGB888;
    75 
    76         /* Create a new one */
    77         SDL_memset(info, 0, size);
    78         info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    79         info->bmiHeader.biPlanes = 1;
    80         info->bmiHeader.biBitCount = 32;
    81         info->bmiHeader.biCompression = BI_RGB;
    82     }
    83 
    84     /* Fill in the size information */
    85     *pitch = (((window->w * SDL_BYTESPERPIXEL(*format)) + 3) & ~3);
    86     info->bmiHeader.biWidth = window->w;
    87     info->bmiHeader.biHeight = -window->h;	/* negative for topdown bitmap */
    88     info->bmiHeader.biSizeImage = window->h * (*pitch);
    89 
    90     data->mdc = CreateCompatibleDC(data->hdc);
    91     data->hbm = CreateDIBSection(data->hdc, info, DIB_RGB_COLORS, pixels, NULL, 0);
    92     SDL_stack_free(info);
    93 
    94     if (!data->hbm) {
    95         WIN_SetError("Unable to create DIB");
    96         return -1;
    97     }
    98     SelectObject(data->mdc, data->hbm);
    99 
   100     return 0;
   101 }
   102 
   103 int WIN_UpdateWindowFramebuffer(_THIS, SDL_Window * window, int numrects, SDL_Rect * rects)
   104 {
   105     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
   106 
   107     BitBlt(data->hdc, 0, 0, window->w, window->h, data->mdc, 0, 0, SRCCOPY);
   108     return 0;
   109 }
   110 
   111 void WIN_DestroyWindowFramebuffer(_THIS, SDL_Window * window)
   112 {
   113     SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
   114 
   115     if (data->mdc) {
   116         DeleteDC(data->mdc);
   117         data->mdc = NULL;
   118     }
   119     if (data->hbm) {
   120         DeleteObject(data->hbm);
   121         data->hbm = NULL;
   122     }
   123 }
   124 
   125 /* vi: set ts=4 sw=4 expandtab: */