src/video/windows/SDL_windowsframebuffer.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 02 Jan 2016 10:10:34 -0800
changeset 9998 f67cf37e9cd4
parent 9946 6f852b334192
child 10737 3406a0f8b041
permissions -rw-r--r--
Updated copyright to 2016
slouken@5175
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@9998
     3
  Copyright (C) 1997-2016 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
*/
icculus@8093
    21
#include "../../SDL_internal.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);
philipp@9946
    45
    if (!info) {
philipp@9946
    46
        return SDL_OutOfMemory();
philipp@9946
    47
    }
slouken@5176
    48
slouken@5176
    49
    SDL_memset(info, 0, size);
slouken@5176
    50
    info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
slouken@5176
    51
slouken@5176
    52
    /* The second call to GetDIBits() fills in the bitfields */
slouken@5176
    53
    hbm = CreateCompatibleBitmap(data->hdc, 1, 1);
slouken@5176
    54
    GetDIBits(data->hdc, hbm, 0, 0, NULL, info, DIB_RGB_COLORS);
slouken@5176
    55
    GetDIBits(data->hdc, hbm, 0, 0, NULL, info, DIB_RGB_COLORS);
slouken@5176
    56
    DeleteObject(hbm);
slouken@5176
    57
slouken@5176
    58
    *format = SDL_PIXELFORMAT_UNKNOWN;
slouken@5176
    59
    if (info->bmiHeader.biCompression == BI_BITFIELDS) {
slouken@5181
    60
        int bpp;
slouken@5176
    61
        Uint32 *masks;
slouken@5176
    62
slouken@5181
    63
        bpp = info->bmiHeader.biPlanes * info->bmiHeader.biBitCount;
slouken@5176
    64
        masks = (Uint32*)((Uint8*)info + info->bmiHeader.biSize);
slouken@5181
    65
        *format = SDL_MasksToPixelFormatEnum(bpp, masks[0], masks[1], masks[2], 0);
slouken@5176
    66
    }
slouken@5176
    67
    if (*format == SDL_PIXELFORMAT_UNKNOWN)
slouken@5176
    68
    {
slouken@5176
    69
        /* We'll use RGB format for now */
slouken@5176
    70
        *format = SDL_PIXELFORMAT_RGB888;
slouken@5176
    71
slouken@5176
    72
        /* Create a new one */
slouken@5176
    73
        SDL_memset(info, 0, size);
slouken@5176
    74
        info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
slouken@5176
    75
        info->bmiHeader.biPlanes = 1;
slouken@5176
    76
        info->bmiHeader.biBitCount = 32;
slouken@5176
    77
        info->bmiHeader.biCompression = BI_RGB;
slouken@5176
    78
    }
slouken@5176
    79
slouken@5176
    80
    /* Fill in the size information */
slouken@5175
    81
    *pitch = (((window->w * SDL_BYTESPERPIXEL(*format)) + 3) & ~3);
slouken@5176
    82
    info->bmiHeader.biWidth = window->w;
slouken@7191
    83
    info->bmiHeader.biHeight = -window->h;  /* negative for topdown bitmap */
slouken@5176
    84
    info->bmiHeader.biSizeImage = window->h * (*pitch);
slouken@5175
    85
slouken@5175
    86
    data->mdc = CreateCompatibleDC(data->hdc);
slouken@5176
    87
    data->hbm = CreateDIBSection(data->hdc, info, DIB_RGB_COLORS, pixels, NULL, 0);
slouken@5176
    88
    SDL_stack_free(info);
slouken@5176
    89
slouken@5175
    90
    if (!data->hbm) {
icculus@7037
    91
        return WIN_SetError("Unable to create DIB");
slouken@5175
    92
    }
slouken@5175
    93
    SelectObject(data->mdc, data->hbm);
slouken@5175
    94
slouken@5175
    95
    return 0;
slouken@5175
    96
}
slouken@5175
    97
slouken@7014
    98
int WIN_UpdateWindowFramebuffer(_THIS, SDL_Window * window, const SDL_Rect * rects, int numrects)
slouken@5175
    99
{
slouken@5175
   100
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@5175
   101
slouken@5175
   102
    BitBlt(data->hdc, 0, 0, window->w, window->h, data->mdc, 0, 0, SRCCOPY);
slouken@5175
   103
    return 0;
slouken@5175
   104
}
slouken@5175
   105
slouken@5175
   106
void WIN_DestroyWindowFramebuffer(_THIS, SDL_Window * window)
slouken@5175
   107
{
slouken@5175
   108
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@5175
   109
slouken@6180
   110
    if (!data) {
slouken@6180
   111
        /* The window wasn't fully initialized */
slouken@6180
   112
        return;
slouken@6180
   113
    }
slouken@6180
   114
slouken@5175
   115
    if (data->mdc) {
slouken@5175
   116
        DeleteDC(data->mdc);
slouken@5175
   117
        data->mdc = NULL;
slouken@5175
   118
    }
slouken@5175
   119
    if (data->hbm) {
slouken@5175
   120
        DeleteObject(data->hbm);
slouken@5175
   121
        data->hbm = NULL;
slouken@5175
   122
    }
slouken@5175
   123
}
slouken@5175
   124
slouken@6044
   125
#endif /* SDL_VIDEO_DRIVER_WINDOWS */
slouken@6044
   126
slouken@5175
   127
/* vi: set ts=4 sw=4 expandtab: */