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