src/video/windows/SDL_windowsframebuffer.c
author Alex Szpakowski <slime73@gmail.com>
Thu, 15 Jan 2015 01:15:24 -0400
branchiOS-improvements
changeset 9533 e5693e855338
parent 8149 681eb46b8ac4
child 9619 b94b6d0bff0f
permissions -rw-r--r--
Merged default into iOS-improvements
slouken@5175
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@8149
     3
  Copyright (C) 1997-2014 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);
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@7191
    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) {
icculus@7037
    88
        return WIN_SetError("Unable to create DIB");
slouken@5175
    89
    }
slouken@5175
    90
    SelectObject(data->mdc, data->hbm);
slouken@5175
    91
slouken@5175
    92
    return 0;
slouken@5175
    93
}
slouken@5175
    94
slouken@7014
    95
int WIN_UpdateWindowFramebuffer(_THIS, SDL_Window * window, const SDL_Rect * rects, int numrects)
slouken@5175
    96
{
slouken@5175
    97
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@5175
    98
slouken@5175
    99
    BitBlt(data->hdc, 0, 0, window->w, window->h, data->mdc, 0, 0, SRCCOPY);
slouken@5175
   100
    return 0;
slouken@5175
   101
}
slouken@5175
   102
slouken@5175
   103
void WIN_DestroyWindowFramebuffer(_THIS, SDL_Window * window)
slouken@5175
   104
{
slouken@5175
   105
    SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
slouken@5175
   106
slouken@6180
   107
    if (!data) {
slouken@6180
   108
        /* The window wasn't fully initialized */
slouken@6180
   109
        return;
slouken@6180
   110
    }
slouken@6180
   111
slouken@5175
   112
    if (data->mdc) {
slouken@5175
   113
        DeleteDC(data->mdc);
slouken@5175
   114
        data->mdc = NULL;
slouken@5175
   115
    }
slouken@5175
   116
    if (data->hbm) {
slouken@5175
   117
        DeleteObject(data->hbm);
slouken@5175
   118
        data->hbm = NULL;
slouken@5175
   119
    }
slouken@5175
   120
}
slouken@5175
   121
slouken@6044
   122
#endif /* SDL_VIDEO_DRIVER_WINDOWS */
slouken@6044
   123
slouken@5175
   124
/* vi: set ts=4 sw=4 expandtab: */