cscg22-gearboy

CSCG 2022 Challenge 'Gearboy'
git clone https://git.sinitax.com/sinitax/cscg22-gearboy
Log | Files | Refs | sfeed.txt

SDL_windowsframebuffer.c (4006B)


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