src/video/win32/SDL_gdirender.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 05 Aug 2006 17:09:42 +0000
changeset 1965 a788656ca29a
parent 1924 69217fdd2c0a
child 1975 ccef0d0c40c6
permissions -rw-r--r--
SDL constants are all uppercase.
slouken@1895
     1
/*
slouken@1895
     2
    SDL - Simple DirectMedia Layer
slouken@1895
     3
    Copyright (C) 1997-2006 Sam Lantinga
slouken@1895
     4
slouken@1895
     5
    This library is free software; you can redistribute it and/or
slouken@1895
     6
    modify it under the terms of the GNU Lesser General Public
slouken@1895
     7
    License as published by the Free Software Foundation; either
slouken@1895
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@1895
     9
slouken@1895
    10
    This library is distributed in the hope that it will be useful,
slouken@1895
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@1895
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@1895
    13
    Lesser General Public License for more details.
slouken@1895
    14
slouken@1895
    15
    You should have received a copy of the GNU Lesser General Public
slouken@1895
    16
    License along with this library; if not, write to the Free Software
slouken@1895
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@1895
    18
slouken@1895
    19
    Sam Lantinga
slouken@1895
    20
    slouken@libsdl.org
slouken@1895
    21
*/
slouken@1895
    22
#include "SDL_config.h"
slouken@1895
    23
slouken@1895
    24
#if SDL_VIDEO_RENDER_GDI
slouken@1895
    25
slouken@1895
    26
#include "SDL_win32video.h"
slouken@1897
    27
#include "../SDL_rect_c.h"
slouken@1895
    28
#include "../SDL_yuv_sw_c.h"
slouken@1895
    29
slouken@1895
    30
/* GDI renderer implementation */
slouken@1895
    31
slouken@1913
    32
static SDL_Renderer *GDI_CreateRenderer(SDL_Window * window, Uint32 flags);
slouken@1913
    33
static int GDI_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1913
    34
static int GDI_QueryTexturePixels(SDL_Renderer * renderer,
slouken@1913
    35
                                  SDL_Texture * texture, void **pixels,
slouken@1913
    36
                                  int *pitch);
slouken@1913
    37
static int GDI_SetTexturePalette(SDL_Renderer * renderer,
slouken@1913
    38
                                 SDL_Texture * texture,
slouken@1913
    39
                                 const SDL_Color * colors, int firstcolor,
slouken@1913
    40
                                 int ncolors);
slouken@1913
    41
static int GDI_GetTexturePalette(SDL_Renderer * renderer,
slouken@1913
    42
                                 SDL_Texture * texture, SDL_Color * colors,
slouken@1913
    43
                                 int firstcolor, int ncolors);
slouken@1913
    44
static int GDI_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
    45
                             const SDL_Rect * rect, const void *pixels,
slouken@1913
    46
                             int pitch);
slouken@1913
    47
static int GDI_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
    48
                           const SDL_Rect * rect, int markDirty,
slouken@1913
    49
                           void **pixels, int *pitch);
slouken@1913
    50
static void GDI_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
slouken@1913
    51
static void GDI_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
    52
                             int numrects, const SDL_Rect * rects);
slouken@1913
    53
static int GDI_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect,
slouken@1913
    54
                          Uint32 color);
slouken@1913
    55
static int GDI_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
    56
                          const SDL_Rect * srcrect, const SDL_Rect * dstrect,
slouken@1913
    57
                          int blendMode, int scaleMode);
slouken@1913
    58
static void GDI_RenderPresent(SDL_Renderer * renderer);
slouken@1913
    59
static void GDI_DestroyTexture(SDL_Renderer * renderer,
slouken@1913
    60
                               SDL_Texture * texture);
slouken@1913
    61
static void GDI_DestroyRenderer(SDL_Renderer * renderer);
slouken@1895
    62
slouken@1895
    63
slouken@1913
    64
SDL_RenderDriver GDI_RenderDriver = {
slouken@1913
    65
    GDI_CreateRenderer,
slouken@1895
    66
    {
slouken@1895
    67
     "gdi",
slouken@1965
    68
     (SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTCOPY |
slouken@1965
    69
      SDL_RENDERER_PRESENTFLIP2 | sDL_RENDERER_PRESENTFLIP3 |
slouken@1965
    70
      SDL_RENDERER_PRESENTDISCARD | SDL_RENDERER_ACCELERATED),
slouken@1965
    71
     (SDL_TEXTUREBLENDMODE_NONE | SDL_TEXTUREBLENDMODE_MASK |
slouken@1965
    72
      SDL_TEXTUREBLENDMODE_BLEND),
slouken@1965
    73
     (SDL_TEXTURESCALEMODE_NONE | SDL_TEXTURESCALEMODE_FAST),
slouken@1895
    74
     11,
slouken@1895
    75
     {
slouken@1965
    76
      SDL_PIXELFORMAT_INDEX8,
slouken@1965
    77
      SDL_PIXELFORMAT_RGB555,
slouken@1965
    78
      SDL_PIXELFORMAT_RGB565,
slouken@1965
    79
      SDL_PIXELFORMAT_RGB888,
slouken@1965
    80
      SDL_PIXELFORMAT_BGR888,
slouken@1965
    81
      SDL_PIXELFORMAT_ARGB8888,
slouken@1965
    82
      SDL_PIXELFORMAT_RGBA8888,
slouken@1965
    83
      SDL_PIXELFORMAT_ABGR8888,
slouken@1965
    84
      SDL_PIXELFORMAT_BGRA8888,
slouken@1965
    85
      SDL_PIXELFORMAT_YUY2,
slouken@1965
    86
      SDL_PIXELFORMAT_UYVY},
slouken@1895
    87
     0,
slouken@1895
    88
     0}
slouken@1895
    89
};
slouken@1895
    90
slouken@1895
    91
typedef struct
slouken@1895
    92
{
slouken@1895
    93
    HWND hwnd;
slouken@1895
    94
    HDC window_hdc;
slouken@1895
    95
    HDC render_hdc;
slouken@1895
    96
    HDC memory_hdc;
slouken@1895
    97
    HDC current_hdc;
slouken@1895
    98
    LPBITMAPINFO bmi;
slouken@1897
    99
    HBITMAP hbm[3];
slouken@1897
   100
    int current_hbm;
slouken@1897
   101
    SDL_DirtyRectList dirty;
slouken@1897
   102
    SDL_bool makedirty;
slouken@1913
   103
} GDI_RenderData;
slouken@1895
   104
slouken@1895
   105
typedef struct
slouken@1895
   106
{
slouken@1895
   107
    SDL_SW_YUVTexture *yuv;
slouken@1895
   108
    Uint32 format;
slouken@1895
   109
    HPALETTE hpal;
slouken@1895
   110
    HBITMAP hbm;
slouken@1895
   111
    void *pixels;
slouken@1895
   112
    int pitch;
slouken@1913
   113
} GDI_TextureData;
slouken@1895
   114
slouken@1895
   115
static void
slouken@1895
   116
UpdateYUVTextureData(SDL_Texture * texture)
slouken@1895
   117
{
slouken@1913
   118
    GDI_TextureData *data = (GDI_TextureData *) texture->driverdata;
slouken@1895
   119
    SDL_Rect rect;
slouken@1895
   120
slouken@1895
   121
    rect.x = 0;
slouken@1895
   122
    rect.y = 0;
slouken@1895
   123
    rect.w = texture->w;
slouken@1895
   124
    rect.h = texture->h;
slouken@1895
   125
    SDL_SW_CopyYUVToRGB(data->yuv, &rect, data->format, texture->w,
slouken@1895
   126
                        texture->h, data->pixels, data->pitch);
slouken@1895
   127
}
slouken@1895
   128
slouken@1895
   129
void
slouken@1895
   130
GDI_AddRenderDriver(_THIS)
slouken@1895
   131
{
slouken@1913
   132
    SDL_AddRenderDriver(0, &GDI_RenderDriver);
slouken@1895
   133
}
slouken@1895
   134
slouken@1895
   135
SDL_Renderer *
slouken@1913
   136
GDI_CreateRenderer(SDL_Window * window, Uint32 flags)
slouken@1895
   137
{
slouken@1895
   138
    SDL_WindowData *windowdata = (SDL_WindowData *) window->driverdata;
slouken@1895
   139
    SDL_Renderer *renderer;
slouken@1913
   140
    GDI_RenderData *data;
slouken@1895
   141
    int bmi_size;
slouken@1895
   142
    HBITMAP hbm;
slouken@1897
   143
    int i, n;
slouken@1895
   144
slouken@1920
   145
    renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
slouken@1895
   146
    if (!renderer) {
slouken@1895
   147
        SDL_OutOfMemory();
slouken@1895
   148
        return NULL;
slouken@1895
   149
    }
slouken@1895
   150
slouken@1920
   151
    data = (GDI_RenderData *) SDL_calloc(1, sizeof(*data));
slouken@1895
   152
    if (!data) {
slouken@1913
   153
        GDI_DestroyRenderer(renderer);
slouken@1895
   154
        SDL_OutOfMemory();
slouken@1895
   155
        return NULL;
slouken@1895
   156
    }
slouken@1895
   157
slouken@1913
   158
    renderer->CreateTexture = GDI_CreateTexture;
slouken@1913
   159
    renderer->QueryTexturePixels = GDI_QueryTexturePixels;
slouken@1913
   160
    renderer->SetTexturePalette = GDI_SetTexturePalette;
slouken@1913
   161
    renderer->GetTexturePalette = GDI_GetTexturePalette;
slouken@1913
   162
    renderer->UpdateTexture = GDI_UpdateTexture;
slouken@1913
   163
    renderer->LockTexture = GDI_LockTexture;
slouken@1913
   164
    renderer->UnlockTexture = GDI_UnlockTexture;
slouken@1913
   165
    renderer->DirtyTexture = GDI_DirtyTexture;
slouken@1913
   166
    renderer->RenderFill = GDI_RenderFill;
slouken@1913
   167
    renderer->RenderCopy = GDI_RenderCopy;
slouken@1913
   168
    renderer->RenderPresent = GDI_RenderPresent;
slouken@1913
   169
    renderer->DestroyTexture = GDI_DestroyTexture;
slouken@1913
   170
    renderer->DestroyRenderer = GDI_DestroyRenderer;
slouken@1913
   171
    renderer->info = GDI_RenderDriver.info;
slouken@1895
   172
    renderer->window = window->id;
slouken@1895
   173
    renderer->driverdata = data;
slouken@1895
   174
slouken@1965
   175
    renderer->info.flags = SDL_RENDERER_ACCELERATED;
slouken@1895
   176
slouken@1897
   177
    data->hwnd = windowdata->hwnd;
slouken@1913
   178
    data->window_hdc = windowdata->hdc;
slouken@1897
   179
    data->render_hdc = CreateCompatibleDC(data->window_hdc);
slouken@1897
   180
    data->memory_hdc = CreateCompatibleDC(data->window_hdc);
slouken@1897
   181
slouken@1897
   182
    /* Fill in the compatible bitmap info */
slouken@1897
   183
    bmi_size = sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD);
slouken@1920
   184
    data->bmi = (LPBITMAPINFO) SDL_calloc(1, bmi_size);
slouken@1897
   185
    if (!data->bmi) {
slouken@1913
   186
        GDI_DestroyRenderer(renderer);
slouken@1897
   187
        SDL_OutOfMemory();
slouken@1897
   188
        return NULL;
slouken@1897
   189
    }
slouken@1897
   190
    data->bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
slouken@1897
   191
slouken@1897
   192
    hbm = CreateCompatibleBitmap(data->window_hdc, 1, 1);
slouken@1897
   193
    GetDIBits(data->window_hdc, hbm, 0, 1, NULL, data->bmi, DIB_RGB_COLORS);
slouken@1897
   194
    GetDIBits(data->window_hdc, hbm, 0, 1, NULL, data->bmi, DIB_RGB_COLORS);
slouken@1897
   195
    DeleteObject(hbm);
slouken@1897
   196
slouken@1965
   197
    if (flags & SDL_RENDERER_SINGLEBUFFER) {
slouken@1907
   198
        renderer->info.flags |=
slouken@1965
   199
            (SDL_RENDERER_SINGLEBUFFER | sDL_RENDERER_PRESENTCOPY);
slouken@1897
   200
        n = 0;
slouken@1965
   201
    } else if (flags & SDL_RENDERER_PRESENTFLIP2) {
slouken@1965
   202
        renderer->info.flags |= SDL_RENDERER_PRESENTFLIP2;
slouken@1897
   203
        n = 2;
slouken@1965
   204
    } else if (flags & SDL_RENDERER_PRESENTFLIP3) {
slouken@1965
   205
        renderer->info.flags |= SDL_RENDERER_PRESENTFLIP3;
slouken@1897
   206
        n = 3;
slouken@1897
   207
    } else {
slouken@1965
   208
        renderer->info.flags |= SDL_RENDERER_PRESENTCOPY;
slouken@1897
   209
        n = 1;
slouken@1897
   210
    }
slouken@1897
   211
    for (i = 0; i < n; ++i) {
slouken@1897
   212
        data->hbm[i] =
slouken@1897
   213
            CreateCompatibleBitmap(data->window_hdc, window->w, window->h);
slouken@1897
   214
        if (!data->hbm[i]) {
slouken@1913
   215
            GDI_DestroyRenderer(renderer);
slouken@1897
   216
            WIN_SetError("CreateCompatibleBitmap()");
slouken@1897
   217
            return NULL;
slouken@1897
   218
        }
slouken@1897
   219
    }
slouken@1897
   220
    if (n > 0) {
slouken@1897
   221
        SelectObject(data->render_hdc, data->hbm[0]);
slouken@1897
   222
        data->current_hdc = data->render_hdc;
slouken@1897
   223
        data->makedirty = SDL_TRUE;
slouken@1897
   224
    } else {
slouken@1897
   225
        data->current_hdc = data->window_hdc;
slouken@1897
   226
        data->makedirty = SDL_FALSE;
slouken@1897
   227
    }
slouken@1897
   228
    data->current_hbm = 0;
slouken@1897
   229
slouken@1895
   230
    return renderer;
slouken@1895
   231
}
slouken@1895
   232
slouken@1895
   233
static int
slouken@1913
   234
GDI_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   235
{
slouken@1913
   236
    GDI_RenderData *renderdata = (GDI_RenderData *) renderer->driverdata;
slouken@1895
   237
    SDL_Window *window = SDL_GetWindowFromID(renderer->window);
slouken@1895
   238
    SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
slouken@1913
   239
    GDI_TextureData *data;
slouken@1895
   240
slouken@1920
   241
    data = (GDI_TextureData *) SDL_calloc(1, sizeof(*data));
slouken@1895
   242
    if (!data) {
slouken@1895
   243
        SDL_OutOfMemory();
slouken@1895
   244
        return -1;
slouken@1895
   245
    }
slouken@1895
   246
slouken@1895
   247
    texture->driverdata = data;
slouken@1895
   248
slouken@1895
   249
    if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
slouken@1895
   250
        data->yuv = SDL_SW_CreateYUVTexture(texture);
slouken@1895
   251
        if (!data->yuv) {
slouken@1895
   252
            return -1;
slouken@1895
   253
        }
slouken@1895
   254
        data->format = display->current_mode.format;
slouken@1895
   255
    } else {
slouken@1895
   256
        data->format = texture->format;
slouken@1895
   257
    }
slouken@1895
   258
    data->pitch = (texture->w * SDL_BYTESPERPIXEL(data->format));
slouken@1895
   259
slouken@1965
   260
    if (data->yuv || texture->access == SDL_TEXTUREACCESS_LOCAL
slouken@1895
   261
        || texture->format != SDL_GetCurrentDisplayMode()->format) {
slouken@1895
   262
        int bmi_size;
slouken@1895
   263
        LPBITMAPINFO bmi;
slouken@1895
   264
slouken@1895
   265
        bmi_size = sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD);
slouken@1920
   266
        bmi = (LPBITMAPINFO) SDL_calloc(1, bmi_size);
slouken@1895
   267
        if (!bmi) {
slouken@1895
   268
            SDL_OutOfMemory();
slouken@1895
   269
            return -1;
slouken@1895
   270
        }
slouken@1895
   271
        bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
slouken@1895
   272
        bmi->bmiHeader.biWidth = texture->w;
slouken@1895
   273
        bmi->bmiHeader.biHeight = -texture->h;  /* topdown bitmap */
slouken@1895
   274
        bmi->bmiHeader.biPlanes = 1;
slouken@1895
   275
        bmi->bmiHeader.biSizeImage = texture->h * data->pitch;
slouken@1895
   276
        bmi->bmiHeader.biXPelsPerMeter = 0;
slouken@1895
   277
        bmi->bmiHeader.biYPelsPerMeter = 0;
slouken@1895
   278
        bmi->bmiHeader.biClrUsed = 0;
slouken@1895
   279
        bmi->bmiHeader.biClrImportant = 0;
slouken@1895
   280
        bmi->bmiHeader.biBitCount = SDL_BYTESPERPIXEL(data->format) * 8;
slouken@1895
   281
        if (SDL_ISPIXELFORMAT_INDEXED(data->format)) {
slouken@1895
   282
            int i, ncolors;
slouken@1895
   283
            LOGPALETTE *palette;
slouken@1895
   284
slouken@1895
   285
            bmi->bmiHeader.biCompression = BI_RGB;
slouken@1895
   286
            ncolors = (1 << SDL_BITSPERPIXEL(data->format));
slouken@1895
   287
            palette =
slouken@1895
   288
                (LOGPALETTE *) SDL_malloc(sizeof(*palette) +
slouken@1895
   289
                                          ncolors * sizeof(PALETTEENTRY));
slouken@1895
   290
            if (!palette) {
slouken@1895
   291
                SDL_free(bmi);
slouken@1895
   292
                SDL_OutOfMemory();
slouken@1895
   293
                return -1;
slouken@1895
   294
            }
slouken@1895
   295
            palette->palVersion = 0x300;
slouken@1895
   296
            palette->palNumEntries = ncolors;
slouken@1895
   297
            for (i = 0; i < ncolors; ++i) {
slouken@1895
   298
                palette->palPalEntry[i].peRed = 0xFF;
slouken@1895
   299
                palette->palPalEntry[i].peGreen = 0xFF;
slouken@1895
   300
                palette->palPalEntry[i].peBlue = 0xFF;
slouken@1895
   301
                palette->palPalEntry[i].peFlags = 0;
slouken@1895
   302
            }
slouken@1895
   303
            data->hpal = CreatePalette(palette);
slouken@1895
   304
            SDL_free(palette);
slouken@1895
   305
        } else {
slouken@1895
   306
            int bpp;
slouken@1895
   307
            Uint32 Rmask, Gmask, Bmask, Amask;
slouken@1895
   308
slouken@1895
   309
            bmi->bmiHeader.biCompression = BI_BITFIELDS;
slouken@1895
   310
            SDL_PixelFormatEnumToMasks(data->format, &bpp, &Rmask, &Gmask,
slouken@1895
   311
                                       &Bmask, &Amask);
slouken@1895
   312
            ((Uint32 *) bmi->bmiColors)[0] = Rmask;
slouken@1895
   313
            ((Uint32 *) bmi->bmiColors)[1] = Gmask;
slouken@1895
   314
            ((Uint32 *) bmi->bmiColors)[2] = Bmask;
slouken@1895
   315
            data->hpal = NULL;
slouken@1895
   316
        }
slouken@1895
   317
        data->hbm =
slouken@1895
   318
            CreateDIBSection(renderdata->memory_hdc, bmi, DIB_RGB_COLORS,
slouken@1895
   319
                             &data->pixels, NULL, 0);
slouken@1895
   320
    } else {
slouken@1895
   321
        data->hbm =
slouken@1895
   322
            CreateCompatibleBitmap(renderdata->window_hdc, texture->w,
slouken@1895
   323
                                   texture->h);
slouken@1895
   324
        data->pixels = NULL;
slouken@1895
   325
    }
slouken@1895
   326
    if (!data->hbm) {
slouken@1895
   327
        WIN_SetError("Couldn't create bitmap");
slouken@1895
   328
        return -1;
slouken@1895
   329
    }
slouken@1895
   330
    return 0;
slouken@1895
   331
}
slouken@1895
   332
slouken@1895
   333
static int
slouken@1913
   334
GDI_QueryTexturePixels(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
   335
                       void **pixels, int *pitch)
slouken@1895
   336
{
slouken@1913
   337
    GDI_TextureData *data = (GDI_TextureData *) texture->driverdata;
slouken@1895
   338
slouken@1895
   339
    if (data->yuv) {
slouken@1895
   340
        return SDL_SW_QueryYUVTexturePixels(data->yuv, pixels, pitch);
slouken@1895
   341
    } else {
slouken@1895
   342
        *pixels = data->pixels;
slouken@1897
   343
        *pitch = data->pitch;
slouken@1895
   344
        return 0;
slouken@1895
   345
    }
slouken@1895
   346
}
slouken@1895
   347
slouken@1895
   348
static int
slouken@1913
   349
GDI_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
   350
                      const SDL_Color * colors, int firstcolor, int ncolors)
slouken@1895
   351
{
slouken@1913
   352
    GDI_RenderData *renderdata = (GDI_RenderData *) renderer->driverdata;
slouken@1913
   353
    GDI_TextureData *data = (GDI_TextureData *) texture->driverdata;
slouken@1895
   354
slouken@1895
   355
    if (data->yuv) {
slouken@1895
   356
        SDL_SetError("YUV textures don't have a palette");
slouken@1895
   357
        return -1;
slouken@1895
   358
    } else {
slouken@1895
   359
        PALETTEENTRY entries[256];
slouken@1895
   360
        int i;
slouken@1895
   361
slouken@1895
   362
        for (i = 0; i < ncolors; ++i) {
slouken@1895
   363
            entries[i].peRed = colors[i].r;
slouken@1895
   364
            entries[i].peGreen = colors[i].g;
slouken@1895
   365
            entries[i].peBlue = colors[i].b;
slouken@1895
   366
            entries[i].peFlags = 0;
slouken@1895
   367
        }
slouken@1895
   368
        if (!SetPaletteEntries(data->hpal, firstcolor, ncolors, entries)) {
slouken@1895
   369
            WIN_SetError("SetPaletteEntries()");
slouken@1895
   370
            return -1;
slouken@1895
   371
        }
slouken@1895
   372
        return 0;
slouken@1895
   373
    }
slouken@1895
   374
}
slouken@1895
   375
slouken@1895
   376
static int
slouken@1913
   377
GDI_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
   378
                      SDL_Color * colors, int firstcolor, int ncolors)
slouken@1895
   379
{
slouken@1913
   380
    GDI_TextureData *data = (GDI_TextureData *) texture->driverdata;
slouken@1895
   381
slouken@1895
   382
    if (data->yuv) {
slouken@1895
   383
        SDL_SetError("YUV textures don't have a palette");
slouken@1895
   384
        return -1;
slouken@1895
   385
    } else {
slouken@1895
   386
        PALETTEENTRY entries[256];
slouken@1895
   387
        int i;
slouken@1895
   388
slouken@1895
   389
        if (!GetPaletteEntries(data->hpal, firstcolor, ncolors, entries)) {
slouken@1895
   390
            WIN_SetError("GetPaletteEntries()");
slouken@1895
   391
            return -1;
slouken@1895
   392
        }
slouken@1895
   393
        for (i = 0; i < ncolors; ++i) {
slouken@1895
   394
            colors[i].r = entries[i].peRed;
slouken@1895
   395
            colors[i].g = entries[i].peGreen;
slouken@1895
   396
            colors[i].b = entries[i].peBlue;
slouken@1895
   397
        }
slouken@1895
   398
        return 0;
slouken@1895
   399
    }
slouken@1895
   400
}
slouken@1895
   401
slouken@1895
   402
static int
slouken@1913
   403
GDI_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
   404
                  const SDL_Rect * rect, const void *pixels, int pitch)
slouken@1895
   405
{
slouken@1913
   406
    GDI_TextureData *data = (GDI_TextureData *) texture->driverdata;
slouken@1895
   407
slouken@1895
   408
    if (data->yuv) {
slouken@1895
   409
        if (SDL_SW_UpdateYUVTexture(data->yuv, rect, pixels, pitch) < 0) {
slouken@1895
   410
            return -1;
slouken@1895
   411
        }
slouken@1895
   412
        UpdateYUVTextureData(texture);
slouken@1895
   413
        return 0;
slouken@1895
   414
    } else {
slouken@1913
   415
        GDI_RenderData *renderdata = (GDI_RenderData *) renderer->driverdata;
slouken@1895
   416
slouken@1895
   417
        if (data->pixels) {
slouken@1895
   418
            Uint8 *src, *dst;
slouken@1895
   419
            int row;
slouken@1895
   420
            size_t length;
slouken@1895
   421
slouken@1895
   422
            src = (Uint8 *) pixels;
slouken@1895
   423
            dst =
slouken@1895
   424
                (Uint8 *) data->pixels + rect->y * data->pitch +
slouken@1895
   425
                rect->x * SDL_BYTESPERPIXEL(texture->format);
slouken@1895
   426
            length = rect->w * SDL_BYTESPERPIXEL(texture->format);
slouken@1895
   427
            for (row = 0; row < rect->h; ++row) {
slouken@1895
   428
                SDL_memcpy(dst, src, length);
slouken@1895
   429
                src += pitch;
slouken@1895
   430
                dst += data->pitch;
slouken@1895
   431
            }
slouken@1895
   432
        } else if (rect->w == texture->w && pitch == data->pitch) {
slouken@1895
   433
            if (!SetDIBits
slouken@1895
   434
                (renderdata->window_hdc, data->hbm, rect->y, rect->h, pixels,
slouken@1895
   435
                 renderdata->bmi, DIB_RGB_COLORS)) {
slouken@1895
   436
                WIN_SetError("SetDIBits()");
slouken@1895
   437
                return -1;
slouken@1895
   438
            }
slouken@1895
   439
        } else {
slouken@1895
   440
            SDL_SetError
slouken@1895
   441
                ("FIXME: Need to allocate temporary memory and do GetDIBits() followed by SetDIBits(), since we can only set blocks of scanlines at a time");
slouken@1895
   442
            return -1;
slouken@1895
   443
        }
slouken@1895
   444
        return 0;
slouken@1895
   445
    }
slouken@1895
   446
}
slouken@1895
   447
slouken@1895
   448
static int
slouken@1913
   449
GDI_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
   450
                const SDL_Rect * rect, int markDirty, void **pixels,
slouken@1913
   451
                int *pitch)
slouken@1895
   452
{
slouken@1913
   453
    GDI_TextureData *data = (GDI_TextureData *) texture->driverdata;
slouken@1895
   454
slouken@1895
   455
    if (data->yuv) {
slouken@1895
   456
        return SDL_SW_LockYUVTexture(data->yuv, rect, markDirty, pixels,
slouken@1895
   457
                                     pitch);
slouken@1898
   458
    } else if (data->pixels) {
slouken@1895
   459
        GdiFlush();
slouken@1895
   460
        *pixels =
slouken@1895
   461
            (void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
slouken@1895
   462
                      rect->x * SDL_BYTESPERPIXEL(texture->format));
slouken@1895
   463
        *pitch = data->pitch;
slouken@1895
   464
        return 0;
slouken@1898
   465
    } else {
slouken@1898
   466
        SDL_SetError("No pixels available");
slouken@1898
   467
        return -1;
slouken@1895
   468
    }
slouken@1895
   469
}
slouken@1895
   470
slouken@1895
   471
static void
slouken@1913
   472
GDI_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   473
{
slouken@1913
   474
    GDI_TextureData *data = (GDI_TextureData *) texture->driverdata;
slouken@1895
   475
slouken@1895
   476
    if (data->yuv) {
slouken@1895
   477
        SDL_SW_UnlockYUVTexture(data->yuv);
slouken@1895
   478
        UpdateYUVTextureData(texture);
slouken@1895
   479
    }
slouken@1895
   480
}
slouken@1895
   481
slouken@1895
   482
static void
slouken@1913
   483
GDI_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture, int numrects,
slouken@1913
   484
                 const SDL_Rect * rects)
slouken@1895
   485
{
slouken@1895
   486
}
slouken@1895
   487
slouken@1895
   488
static int
slouken@1913
   489
GDI_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect, Uint32 color)
slouken@1895
   490
{
slouken@1913
   491
    GDI_RenderData *data = (GDI_RenderData *) renderer->driverdata;
slouken@1895
   492
    Uint8 r, g, b;
slouken@1895
   493
    RECT rc;
slouken@1899
   494
    HBRUSH brush;
slouken@1895
   495
    int status;
slouken@1895
   496
slouken@1897
   497
    if (data->makedirty) {
slouken@1897
   498
        SDL_AddDirtyRect(&data->dirty, rect);
slouken@1897
   499
    }
slouken@1897
   500
slouken@1895
   501
    r = (Uint8) ((color >> 16) & 0xFF);
slouken@1895
   502
    g = (Uint8) ((color >> 8) & 0xFF);
slouken@1895
   503
    b = (Uint8) (color & 0xFF);
slouken@1895
   504
slouken@1895
   505
    rc.left = rect->x;
slouken@1895
   506
    rc.top = rect->y;
slouken@1895
   507
    rc.right = rect->x + rect->w + 1;
slouken@1895
   508
    rc.bottom = rect->y + rect->h + 1;
slouken@1895
   509
slouken@1895
   510
    /* Should we cache the brushes? .. it looks like GDI does for us. :) */
slouken@1895
   511
    brush = CreateSolidBrush(RGB(r, g, b));
slouken@1895
   512
    SelectObject(data->current_hdc, brush);
slouken@1895
   513
    status = FillRect(data->current_hdc, &rc, brush);
slouken@1895
   514
    DeleteObject(brush);
slouken@1895
   515
slouken@1895
   516
    if (!status) {
slouken@1895
   517
        WIN_SetError("FillRect()");
slouken@1895
   518
        return -1;
slouken@1895
   519
    }
slouken@1895
   520
    return 0;
slouken@1895
   521
}
slouken@1895
   522
slouken@1895
   523
static int
slouken@1913
   524
GDI_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
slouken@1913
   525
               const SDL_Rect * srcrect, const SDL_Rect * dstrect,
slouken@1913
   526
               int blendMode, int scaleMode)
slouken@1895
   527
{
slouken@1913
   528
    GDI_RenderData *data = (GDI_RenderData *) renderer->driverdata;
slouken@1913
   529
    GDI_TextureData *texturedata = (GDI_TextureData *) texture->driverdata;
slouken@1895
   530
slouken@1897
   531
    if (data->makedirty) {
slouken@1897
   532
        SDL_AddDirtyRect(&data->dirty, dstrect);
slouken@1897
   533
    }
slouken@1897
   534
slouken@1895
   535
    SelectObject(data->memory_hdc, texturedata->hbm);
slouken@1895
   536
    if (texturedata->hpal) {
slouken@1895
   537
        SelectPalette(data->memory_hdc, texturedata->hpal, TRUE);
slouken@1895
   538
        RealizePalette(data->memory_hdc);
slouken@1895
   539
    }
slouken@1965
   540
    if (blendMode & (SDL_TEXTUREBLENDMODE_MASK | SDL_TEXTUREBLENDMODE_BLEND)) {
slouken@1895
   541
        static BLENDFUNCTION blendFunc = {
slouken@1895
   542
            AC_SRC_OVER,
slouken@1895
   543
            0,
slouken@1895
   544
            255,
slouken@1895
   545
            AC_SRC_ALPHA
slouken@1895
   546
        };
slouken@1895
   547
        /* FIXME: GDI uses premultiplied alpha! */
slouken@1895
   548
        if (!AlphaBlend
slouken@1895
   549
            (data->current_hdc, dstrect->x, dstrect->y, dstrect->w,
slouken@1895
   550
             dstrect->h, data->memory_hdc, srcrect->x, srcrect->y, srcrect->w,
slouken@1895
   551
             srcrect->h, blendFunc)) {
slouken@1895
   552
            WIN_SetError("AlphaBlend()");
slouken@1895
   553
            return -1;
slouken@1895
   554
        }
slouken@1895
   555
    } else {
slouken@1895
   556
        if (srcrect->w == dstrect->w && srcrect->h == dstrect->h) {
slouken@1895
   557
            if (!BitBlt
slouken@1895
   558
                (data->current_hdc, dstrect->x, dstrect->y, dstrect->w,
slouken@1895
   559
                 srcrect->h, data->memory_hdc, srcrect->x, srcrect->y,
slouken@1895
   560
                 SRCCOPY)) {
slouken@1895
   561
                WIN_SetError("BitBlt()");
slouken@1895
   562
                return -1;
slouken@1895
   563
            }
slouken@1895
   564
        } else {
slouken@1895
   565
            if (!StretchBlt
slouken@1895
   566
                (data->current_hdc, dstrect->x, dstrect->y, dstrect->w,
slouken@1895
   567
                 dstrect->h, data->memory_hdc, srcrect->x, srcrect->y,
slouken@1895
   568
                 srcrect->w, srcrect->h, SRCCOPY)) {
slouken@1895
   569
                WIN_SetError("StretchBlt()");
slouken@1895
   570
                return -1;
slouken@1895
   571
            }
slouken@1895
   572
        }
slouken@1895
   573
    }
slouken@1895
   574
    return 0;
slouken@1895
   575
}
slouken@1895
   576
slouken@1895
   577
static void
slouken@1913
   578
GDI_RenderPresent(SDL_Renderer * renderer)
slouken@1895
   579
{
slouken@1913
   580
    GDI_RenderData *data = (GDI_RenderData *) renderer->driverdata;
slouken@1897
   581
    SDL_DirtyRect *dirty;
slouken@1897
   582
slouken@1897
   583
    /* Send the data to the display */
slouken@1965
   584
    if (!(renderer->info.flags & SDL_RENDERER_SINGLEBUFFER)) {
slouken@1897
   585
        for (dirty = data->dirty.list; dirty; dirty = dirty->next) {
slouken@1897
   586
            const SDL_Rect *rect = &dirty->rect;
slouken@1897
   587
            BitBlt(data->window_hdc, rect->x, rect->y, rect->w, rect->h,
slouken@1897
   588
                   data->render_hdc, rect->x, rect->y, SRCCOPY);
slouken@1897
   589
        }
slouken@1897
   590
        SDL_ClearDirtyRects(&data->dirty);
slouken@1897
   591
    }
slouken@1897
   592
slouken@1897
   593
    /* Update the flipping chain, if any */
slouken@1965
   594
    if (renderer->info.flags & SDL_RENDERER_PRESENTFLIP2) {
slouken@1897
   595
        data->current_hbm = (data->current_hbm + 1) % 2;
slouken@1897
   596
        SelectObject(data->render_hdc, data->hbm[data->current_hbm]);
slouken@1965
   597
    } else if (renderer->info.flags & SDL_RENDERER_PRESENTFLIP3) {
slouken@1897
   598
        data->current_hbm = (data->current_hbm + 1) % 3;
slouken@1897
   599
        SelectObject(data->render_hdc, data->hbm[data->current_hbm]);
slouken@1897
   600
    }
slouken@1895
   601
}
slouken@1895
   602
slouken@1895
   603
static void
slouken@1913
   604
GDI_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
slouken@1895
   605
{
slouken@1913
   606
    GDI_TextureData *data = (GDI_TextureData *) texture->driverdata;
slouken@1895
   607
slouken@1895
   608
    if (!data) {
slouken@1895
   609
        return;
slouken@1895
   610
    }
slouken@1895
   611
    if (data->yuv) {
slouken@1895
   612
        SDL_SW_DestroyYUVTexture(data->yuv);
slouken@1895
   613
    }
slouken@1895
   614
    if (data->hpal) {
slouken@1895
   615
        DeleteObject(data->hpal);
slouken@1895
   616
    }
slouken@1895
   617
    if (data->hbm) {
slouken@1895
   618
        DeleteObject(data->hbm);
slouken@1895
   619
    }
slouken@1895
   620
    SDL_free(data);
slouken@1895
   621
    texture->driverdata = NULL;
slouken@1895
   622
}
slouken@1895
   623
slouken@1895
   624
void
slouken@1913
   625
GDI_DestroyRenderer(SDL_Renderer * renderer)
slouken@1895
   626
{
slouken@1913
   627
    GDI_RenderData *data = (GDI_RenderData *) renderer->driverdata;
slouken@1897
   628
    int i;
slouken@1895
   629
slouken@1895
   630
    if (data) {
slouken@1895
   631
        DeleteDC(data->render_hdc);
slouken@1895
   632
        DeleteDC(data->memory_hdc);
slouken@1895
   633
        if (data->bmi) {
slouken@1895
   634
            SDL_free(data->bmi);
slouken@1895
   635
        }
slouken@1897
   636
        for (i = 0; i < SDL_arraysize(data->hbm); ++i) {
slouken@1897
   637
            if (data->hbm[i]) {
slouken@1897
   638
                DeleteObject(data->hbm[i]);
slouken@1897
   639
            }
slouken@1897
   640
        }
slouken@1897
   641
        SDL_FreeDirtyRects(&data->dirty);
slouken@1895
   642
        SDL_free(data);
slouken@1895
   643
    }
slouken@1895
   644
    SDL_free(renderer);
slouken@1895
   645
}
slouken@1895
   646
slouken@1895
   647
#endif /* SDL_VIDEO_RENDER_GDI */
slouken@1895
   648
slouken@1895
   649
/* vi: set ts=4 sw=4 expandtab: */