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