Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
1096 lines (979 loc) · 34.1 KB

SDL_gdirender.c

File metadata and controls

1096 lines (979 loc) · 34.1 KB
 
1
2
/*
SDL - Simple DirectMedia Layer
Dec 8, 2008
Dec 8, 2008
3
Copyright (C) 1997-2009 Sam Lantinga
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Sam Lantinga
slouken@libsdl.org
*/
#include "SDL_config.h"
#if SDL_VIDEO_RENDER_GDI
#include "SDL_win32video.h"
Jul 12, 2006
Jul 12, 2006
27
#include "../SDL_rect_c.h"
28
#include "../SDL_yuv_sw_c.h"
Feb 7, 2009
Feb 7, 2009
29
#include "../SDL_alphamult.h"
Mar 23, 2009
Mar 23, 2009
31
32
33
34
#ifdef _WIN32_WCE
#define NO_GETDIBBITS 1
#endif
35
36
/* GDI renderer implementation */
Jul 17, 2006
Jul 17, 2006
37
static SDL_Renderer *GDI_CreateRenderer(SDL_Window * window, Uint32 flags);
Aug 7, 2006
Aug 7, 2006
38
static int GDI_DisplayModeChanged(SDL_Renderer * renderer);
Jul 17, 2006
Jul 17, 2006
39
40
41
42
43
44
45
46
47
48
49
static int GDI_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
static int GDI_QueryTexturePixels(SDL_Renderer * renderer,
SDL_Texture * texture, void **pixels,
int *pitch);
static int GDI_SetTexturePalette(SDL_Renderer * renderer,
SDL_Texture * texture,
const SDL_Color * colors, int firstcolor,
int ncolors);
static int GDI_GetTexturePalette(SDL_Renderer * renderer,
SDL_Texture * texture, SDL_Color * colors,
int firstcolor, int ncolors);
Aug 28, 2006
Aug 28, 2006
50
51
52
53
54
55
static int GDI_SetTextureAlphaMod(SDL_Renderer * renderer,
SDL_Texture * texture);
static int GDI_SetTextureBlendMode(SDL_Renderer * renderer,
SDL_Texture * texture);
static int GDI_SetTextureScaleMode(SDL_Renderer * renderer,
SDL_Texture * texture);
Jul 17, 2006
Jul 17, 2006
56
57
58
59
60
61
62
static int GDI_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, const void *pixels,
int pitch);
static int GDI_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, int markDirty,
void **pixels, int *pitch);
static void GDI_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
Dec 30, 2008
Dec 30, 2008
63
static int GDI_SetDrawBlendMode(SDL_Renderer * renderer);
Dec 24, 2009
Dec 24, 2009
64
65
66
67
68
69
70
71
static int GDI_RenderDrawPoints(SDL_Renderer * renderer,
const SDL_Point * points, int count);
static int GDI_RenderDrawLines(SDL_Renderer * renderer,
const SDL_Point * points, int count);
static int GDI_RenderDrawRects(SDL_Renderer * renderer,
const SDL_Rect ** rects, int count);
static int GDI_RenderFillRects(SDL_Renderer * renderer,
const SDL_Rect ** rects, int count);
Jul 17, 2006
Jul 17, 2006
72
static int GDI_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
Aug 28, 2006
Aug 28, 2006
73
const SDL_Rect * srcrect, const SDL_Rect * dstrect);
Dec 7, 2009
Dec 7, 2009
74
75
76
77
static int GDI_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
Uint32 format, void * pixels, int pitch);
static int GDI_RenderWritePixels(SDL_Renderer * renderer, const SDL_Rect * rect,
Uint32 format, const void * pixels, int pitch);
Jul 17, 2006
Jul 17, 2006
78
79
80
81
82
83
84
85
static void GDI_RenderPresent(SDL_Renderer * renderer);
static void GDI_DestroyTexture(SDL_Renderer * renderer,
SDL_Texture * texture);
static void GDI_DestroyRenderer(SDL_Renderer * renderer);
SDL_RenderDriver GDI_RenderDriver = {
GDI_CreateRenderer,
Aug 5, 2006
Aug 5, 2006
88
(SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTCOPY |
Aug 7, 2006
Aug 7, 2006
89
SDL_RENDERER_PRESENTFLIP2 | SDL_RENDERER_PRESENTFLIP3 |
Aug 5, 2006
Aug 5, 2006
90
SDL_RENDERER_PRESENTDISCARD | SDL_RENDERER_ACCELERATED),
Aug 28, 2006
Aug 28, 2006
91
(SDL_TEXTUREMODULATE_NONE | SDL_TEXTUREMODULATE_ALPHA),
Dec 30, 2008
Dec 30, 2008
92
(SDL_BLENDMODE_NONE | SDL_BLENDMODE_MASK),
Aug 5, 2006
Aug 5, 2006
93
(SDL_TEXTURESCALEMODE_NONE | SDL_TEXTURESCALEMODE_FAST),
Nov 30, 2008
Nov 30, 2008
94
14,
Aug 5, 2006
Aug 5, 2006
96
97
98
99
100
101
102
103
104
SDL_PIXELFORMAT_INDEX8,
SDL_PIXELFORMAT_RGB555,
SDL_PIXELFORMAT_RGB565,
SDL_PIXELFORMAT_RGB888,
SDL_PIXELFORMAT_BGR888,
SDL_PIXELFORMAT_ARGB8888,
SDL_PIXELFORMAT_RGBA8888,
SDL_PIXELFORMAT_ABGR8888,
SDL_PIXELFORMAT_BGRA8888,
Nov 30, 2008
Nov 30, 2008
105
106
SDL_PIXELFORMAT_YV12,
SDL_PIXELFORMAT_IYUV,
Aug 5, 2006
Aug 5, 2006
107
SDL_PIXELFORMAT_YUY2,
Nov 30, 2008
Nov 30, 2008
108
109
SDL_PIXELFORMAT_UYVY,
SDL_PIXELFORMAT_YVYU},
110
111
112
113
114
115
116
117
118
119
120
0,
0}
};
typedef struct
{
HWND hwnd;
HDC window_hdc;
HDC render_hdc;
HDC memory_hdc;
HDC current_hdc;
Mar 23, 2009
Mar 23, 2009
121
#ifndef NO_GETDIBBITS
122
LPBITMAPINFO bmi;
Mar 23, 2009
Mar 23, 2009
123
#endif
Jul 12, 2006
Jul 12, 2006
124
125
126
127
HBITMAP hbm[3];
int current_hbm;
SDL_DirtyRectList dirty;
SDL_bool makedirty;
Jul 17, 2006
Jul 17, 2006
128
} GDI_RenderData;
129
130
131
132
133
134
135
136
137
typedef struct
{
SDL_SW_YUVTexture *yuv;
Uint32 format;
HPALETTE hpal;
HBITMAP hbm;
void *pixels;
int pitch;
Feb 7, 2009
Feb 7, 2009
138
SDL_bool premultiplied;
Jul 17, 2006
Jul 17, 2006
139
} GDI_TextureData;
140
141
142
143
static void
UpdateYUVTextureData(SDL_Texture * texture)
{
Jul 17, 2006
Jul 17, 2006
144
GDI_TextureData *data = (GDI_TextureData *) texture->driverdata;
145
146
147
148
149
150
151
152
153
154
155
156
157
SDL_Rect rect;
rect.x = 0;
rect.y = 0;
rect.w = texture->w;
rect.h = texture->h;
SDL_SW_CopyYUVToRGB(data->yuv, &rect, data->format, texture->w,
texture->h, data->pixels, data->pitch);
}
void
GDI_AddRenderDriver(_THIS)
{
Dec 4, 2009
Dec 4, 2009
158
159
160
161
int i;
for (i = 0; i < _this->num_displays; ++i) {
SDL_AddRenderDriver(&_this->displays[i], &GDI_RenderDriver);
}
162
163
164
}
SDL_Renderer *
Jul 17, 2006
Jul 17, 2006
165
GDI_CreateRenderer(SDL_Window * window, Uint32 flags)
166
167
168
{
SDL_WindowData *windowdata = (SDL_WindowData *) window->driverdata;
SDL_Renderer *renderer;
Jul 17, 2006
Jul 17, 2006
169
GDI_RenderData *data;
170
171
int bmi_size;
HBITMAP hbm;
Jul 12, 2006
Jul 12, 2006
172
int i, n;
Jul 22, 2006
Jul 22, 2006
174
renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
175
176
177
178
179
if (!renderer) {
SDL_OutOfMemory();
return NULL;
}
Jul 22, 2006
Jul 22, 2006
180
data = (GDI_RenderData *) SDL_calloc(1, sizeof(*data));
Jul 17, 2006
Jul 17, 2006
182
GDI_DestroyRenderer(renderer);
183
184
185
186
SDL_OutOfMemory();
return NULL;
}
Aug 7, 2006
Aug 7, 2006
187
renderer->DisplayModeChanged = GDI_DisplayModeChanged;
Jul 17, 2006
Jul 17, 2006
188
189
190
191
renderer->CreateTexture = GDI_CreateTexture;
renderer->QueryTexturePixels = GDI_QueryTexturePixels;
renderer->SetTexturePalette = GDI_SetTexturePalette;
renderer->GetTexturePalette = GDI_GetTexturePalette;
Aug 28, 2006
Aug 28, 2006
192
193
194
renderer->SetTextureAlphaMod = GDI_SetTextureAlphaMod;
renderer->SetTextureBlendMode = GDI_SetTextureBlendMode;
renderer->SetTextureScaleMode = GDI_SetTextureScaleMode;
Jul 17, 2006
Jul 17, 2006
195
196
197
renderer->UpdateTexture = GDI_UpdateTexture;
renderer->LockTexture = GDI_LockTexture;
renderer->UnlockTexture = GDI_UnlockTexture;
Dec 30, 2008
Dec 30, 2008
198
renderer->SetDrawBlendMode = GDI_SetDrawBlendMode;
Dec 24, 2009
Dec 24, 2009
199
200
201
202
renderer->RenderDrawPoints = GDI_RenderDrawPoints;
renderer->RenderDrawLines = GDI_RenderDrawLines;
renderer->RenderDrawRects = GDI_RenderDrawRects;
renderer->RenderFillRects = GDI_RenderFillRects;
Jul 17, 2006
Jul 17, 2006
203
renderer->RenderCopy = GDI_RenderCopy;
Dec 7, 2009
Dec 7, 2009
204
205
renderer->RenderReadPixels = GDI_RenderReadPixels;
renderer->RenderWritePixels = GDI_RenderWritePixels;
Jul 17, 2006
Jul 17, 2006
206
207
208
209
renderer->RenderPresent = GDI_RenderPresent;
renderer->DestroyTexture = GDI_DestroyTexture;
renderer->DestroyRenderer = GDI_DestroyRenderer;
renderer->info = GDI_RenderDriver.info;
Jan 21, 2010
Jan 21, 2010
210
renderer->window = window;
211
212
renderer->driverdata = data;
Aug 5, 2006
Aug 5, 2006
213
renderer->info.flags = SDL_RENDERER_ACCELERATED;
Jul 12, 2006
Jul 12, 2006
215
data->hwnd = windowdata->hwnd;
Jul 17, 2006
Jul 17, 2006
216
data->window_hdc = windowdata->hdc;
Jul 12, 2006
Jul 12, 2006
217
218
219
data->render_hdc = CreateCompatibleDC(data->window_hdc);
data->memory_hdc = CreateCompatibleDC(data->window_hdc);
Mar 23, 2009
Mar 23, 2009
220
#ifndef NO_GETDIBBITS
Jul 12, 2006
Jul 12, 2006
221
222
/* Fill in the compatible bitmap info */
bmi_size = sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD);
Jul 22, 2006
Jul 22, 2006
223
data->bmi = (LPBITMAPINFO) SDL_calloc(1, bmi_size);
Jul 12, 2006
Jul 12, 2006
224
if (!data->bmi) {
Jul 17, 2006
Jul 17, 2006
225
GDI_DestroyRenderer(renderer);
Jul 12, 2006
Jul 12, 2006
226
227
228
229
230
231
232
233
234
SDL_OutOfMemory();
return NULL;
}
data->bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
hbm = CreateCompatibleBitmap(data->window_hdc, 1, 1);
GetDIBits(data->window_hdc, hbm, 0, 1, NULL, data->bmi, DIB_RGB_COLORS);
GetDIBits(data->window_hdc, hbm, 0, 1, NULL, data->bmi, DIB_RGB_COLORS);
DeleteObject(hbm);
Mar 23, 2009
Mar 23, 2009
235
#endif
Jul 12, 2006
Jul 12, 2006
236
Aug 5, 2006
Aug 5, 2006
237
if (flags & SDL_RENDERER_SINGLEBUFFER) {
Jul 15, 2006
Jul 15, 2006
238
renderer->info.flags |=
Aug 7, 2006
Aug 7, 2006
239
(SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTCOPY);
Jul 12, 2006
Jul 12, 2006
240
n = 0;
Aug 5, 2006
Aug 5, 2006
241
242
} else if (flags & SDL_RENDERER_PRESENTFLIP2) {
renderer->info.flags |= SDL_RENDERER_PRESENTFLIP2;
Jul 12, 2006
Jul 12, 2006
243
n = 2;
Aug 5, 2006
Aug 5, 2006
244
245
} else if (flags & SDL_RENDERER_PRESENTFLIP3) {
renderer->info.flags |= SDL_RENDERER_PRESENTFLIP3;
Jul 12, 2006
Jul 12, 2006
246
247
n = 3;
} else {
Aug 5, 2006
Aug 5, 2006
248
renderer->info.flags |= SDL_RENDERER_PRESENTCOPY;
Jul 12, 2006
Jul 12, 2006
249
250
251
252
253
254
n = 1;
}
for (i = 0; i < n; ++i) {
data->hbm[i] =
CreateCompatibleBitmap(data->window_hdc, window->w, window->h);
if (!data->hbm[i]) {
Jul 17, 2006
Jul 17, 2006
255
GDI_DestroyRenderer(renderer);
Jul 12, 2006
Jul 12, 2006
256
257
258
259
260
261
262
263
264
265
266
267
268
269
WIN_SetError("CreateCompatibleBitmap()");
return NULL;
}
}
if (n > 0) {
SelectObject(data->render_hdc, data->hbm[0]);
data->current_hdc = data->render_hdc;
data->makedirty = SDL_TRUE;
} else {
data->current_hdc = data->window_hdc;
data->makedirty = SDL_FALSE;
}
data->current_hbm = 0;
270
271
272
return renderer;
}
Aug 7, 2006
Aug 7, 2006
273
274
275
276
static int
GDI_DisplayModeChanged(SDL_Renderer * renderer)
{
GDI_RenderData *data = (GDI_RenderData *) renderer->driverdata;
Jan 21, 2010
Jan 21, 2010
277
SDL_Window *window = renderer->window;
Aug 7, 2006
Aug 7, 2006
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
int i, n;
if (renderer->info.flags & SDL_RENDERER_SINGLEBUFFER) {
n = 0;
} else if (renderer->info.flags & SDL_RENDERER_PRESENTFLIP2) {
n = 2;
} else if (renderer->info.flags & SDL_RENDERER_PRESENTFLIP3) {
n = 3;
} else {
n = 1;
}
for (i = 0; i < n; ++i) {
if (data->hbm[i]) {
DeleteObject(data->hbm[i]);
data->hbm[i] = NULL;
}
}
for (i = 0; i < n; ++i) {
data->hbm[i] =
CreateCompatibleBitmap(data->window_hdc, window->w, window->h);
if (!data->hbm[i]) {
WIN_SetError("CreateCompatibleBitmap()");
return -1;
}
}
if (n > 0) {
SelectObject(data->render_hdc, data->hbm[0]);
}
Nov 30, 2008
Nov 30, 2008
306
307
data->current_hbm = 0;
Aug 7, 2006
Aug 7, 2006
308
309
310
return 0;
}
Dec 7, 2009
Dec 7, 2009
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
static HBITMAP
GDI_CreateDIBSection(HDC hdc, int w, int h, int pitch, Uint32 format,
HPALETTE * hpal, void ** pixels)
{
int bmi_size;
LPBITMAPINFO bmi;
bmi_size = sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD);
bmi = (LPBITMAPINFO) SDL_calloc(1, bmi_size);
if (!bmi) {
SDL_OutOfMemory();
return NULL;
}
bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi->bmiHeader.biWidth = w;
bmi->bmiHeader.biHeight = -h; /* topdown bitmap */
bmi->bmiHeader.biPlanes = 1;
bmi->bmiHeader.biSizeImage = h * pitch;
bmi->bmiHeader.biXPelsPerMeter = 0;
bmi->bmiHeader.biYPelsPerMeter = 0;
bmi->bmiHeader.biClrUsed = 0;
bmi->bmiHeader.biClrImportant = 0;
bmi->bmiHeader.biBitCount = SDL_BYTESPERPIXEL(format) * 8;
if (SDL_ISPIXELFORMAT_INDEXED(format)) {
bmi->bmiHeader.biCompression = BI_RGB;
if (hpal) {
int i, ncolors;
LOGPALETTE *palette;
ncolors = (1 << SDL_BITSPERPIXEL(format));
palette =
(LOGPALETTE *) SDL_malloc(sizeof(*palette) +
ncolors * sizeof(PALETTEENTRY));
if (!palette) {
SDL_free(bmi);
SDL_OutOfMemory();
return NULL;
}
palette->palVersion = 0x300;
palette->palNumEntries = ncolors;
for (i = 0; i < ncolors; ++i) {
palette->palPalEntry[i].peRed = 0xFF;
palette->palPalEntry[i].peGreen = 0xFF;
palette->palPalEntry[i].peBlue = 0xFF;
palette->palPalEntry[i].peFlags = 0;
}
*hpal = CreatePalette(palette);
SDL_free(palette);
}
} else {
int bpp;
Uint32 Rmask, Gmask, Bmask, Amask;
bmi->bmiHeader.biCompression = BI_BITFIELDS;
SDL_PixelFormatEnumToMasks(format, &bpp, &Rmask, &Gmask, &Bmask,
&Amask);
((Uint32 *) bmi->bmiColors)[0] = Rmask;
((Uint32 *) bmi->bmiColors)[1] = Gmask;
((Uint32 *) bmi->bmiColors)[2] = Bmask;
if (hpal) {
*hpal = NULL;
}
}
return CreateDIBSection(hdc, bmi, DIB_RGB_COLORS, pixels, NULL, 0);
}
Jul 17, 2006
Jul 17, 2006
378
GDI_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
Jul 17, 2006
Jul 17, 2006
380
GDI_RenderData *renderdata = (GDI_RenderData *) renderer->driverdata;
Jan 21, 2010
Jan 21, 2010
381
382
SDL_Window *window = renderer->window;
SDL_VideoDisplay *display = window->display;
Jul 17, 2006
Jul 17, 2006
383
GDI_TextureData *data;
Jul 22, 2006
Jul 22, 2006
385
data = (GDI_TextureData *) SDL_calloc(1, sizeof(*data));
386
387
388
389
390
391
392
393
if (!data) {
SDL_OutOfMemory();
return -1;
}
texture->driverdata = data;
if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
Nov 25, 2008
Nov 25, 2008
394
395
data->yuv =
SDL_SW_CreateYUVTexture(texture->format, texture->w, texture->h);
396
397
398
399
400
401
402
403
404
if (!data->yuv) {
return -1;
}
data->format = display->current_mode.format;
} else {
data->format = texture->format;
}
data->pitch = (texture->w * SDL_BYTESPERPIXEL(data->format));
Aug 11, 2007
Aug 11, 2007
405
if (data->yuv || texture->access == SDL_TEXTUREACCESS_STREAMING
Aug 7, 2006
Aug 7, 2006
406
|| texture->format != display->current_mode.format) {
Dec 7, 2009
Dec 7, 2009
407
408
409
410
data->hbm = GDI_CreateDIBSection(renderdata->memory_hdc,
texture->w, texture->h,
data->pitch, data->format,
&data->hpal, &data->pixels);
Dec 7, 2009
Dec 7, 2009
412
413
data->hbm = CreateCompatibleBitmap(renderdata->window_hdc,
texture->w, texture->h);
414
415
416
417
418
419
420
421
422
}
if (!data->hbm) {
WIN_SetError("Couldn't create bitmap");
return -1;
}
return 0;
}
static int
Jul 17, 2006
Jul 17, 2006
423
424
GDI_QueryTexturePixels(SDL_Renderer * renderer, SDL_Texture * texture,
void **pixels, int *pitch)
Jul 17, 2006
Jul 17, 2006
426
GDI_TextureData *data = (GDI_TextureData *) texture->driverdata;
427
428
429
430
431
if (data->yuv) {
return SDL_SW_QueryYUVTexturePixels(data->yuv, pixels, pitch);
} else {
*pixels = data->pixels;
Jul 12, 2006
Jul 12, 2006
432
*pitch = data->pitch;
433
434
435
436
437
return 0;
}
}
static int
Jul 17, 2006
Jul 17, 2006
438
439
GDI_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Color * colors, int firstcolor, int ncolors)
Jul 17, 2006
Jul 17, 2006
441
442
GDI_RenderData *renderdata = (GDI_RenderData *) renderer->driverdata;
GDI_TextureData *data = (GDI_TextureData *) texture->driverdata;
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
if (data->yuv) {
SDL_SetError("YUV textures don't have a palette");
return -1;
} else {
PALETTEENTRY entries[256];
int i;
for (i = 0; i < ncolors; ++i) {
entries[i].peRed = colors[i].r;
entries[i].peGreen = colors[i].g;
entries[i].peBlue = colors[i].b;
entries[i].peFlags = 0;
}
if (!SetPaletteEntries(data->hpal, firstcolor, ncolors, entries)) {
WIN_SetError("SetPaletteEntries()");
return -1;
}
return 0;
}
}
static int
Jul 17, 2006
Jul 17, 2006
466
467
GDI_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture,
SDL_Color * colors, int firstcolor, int ncolors)
Jul 17, 2006
Jul 17, 2006
469
GDI_TextureData *data = (GDI_TextureData *) texture->driverdata;
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
if (data->yuv) {
SDL_SetError("YUV textures don't have a palette");
return -1;
} else {
PALETTEENTRY entries[256];
int i;
if (!GetPaletteEntries(data->hpal, firstcolor, ncolors, entries)) {
WIN_SetError("GetPaletteEntries()");
return -1;
}
for (i = 0; i < ncolors; ++i) {
colors[i].r = entries[i].peRed;
colors[i].g = entries[i].peGreen;
colors[i].b = entries[i].peBlue;
}
return 0;
}
}
Aug 28, 2006
Aug 28, 2006
491
492
493
494
495
496
497
498
499
static int
GDI_SetTextureAlphaMod(SDL_Renderer * renderer, SDL_Texture * texture)
{
return 0;
}
static int
GDI_SetTextureBlendMode(SDL_Renderer * renderer, SDL_Texture * texture)
{
Feb 7, 2009
Feb 7, 2009
500
501
GDI_TextureData *data = (GDI_TextureData *) texture->driverdata;
Aug 28, 2006
Aug 28, 2006
502
switch (texture->blendMode) {
Dec 20, 2008
Dec 20, 2008
503
case SDL_BLENDMODE_NONE:
Feb 7, 2009
Feb 7, 2009
504
505
506
507
if (data->premultiplied) {
/* Crap, we've lost the original pixel data... *sigh* */
}
return 0;
May 23, 2009
May 23, 2009
508
#ifndef _WIN32_WCE /* WinCE has no alphablend */
Dec 20, 2008
Dec 20, 2008
509
510
case SDL_BLENDMODE_MASK:
case SDL_BLENDMODE_BLEND:
Feb 7, 2009
Feb 7, 2009
511
512
513
if (!data->premultiplied && data->pixels) {
switch (texture->format) {
case SDL_PIXELFORMAT_ARGB8888:
Feb 7, 2009
Feb 7, 2009
514
515
516
SDL_PreMultiplyAlphaARGB8888(texture->w, texture->h,
(Uint32 *) data->pixels,
data->pitch);
Feb 7, 2009
Feb 7, 2009
517
518
519
data->premultiplied = SDL_TRUE;
break;
case SDL_PIXELFORMAT_RGBA8888:
Feb 7, 2009
Feb 7, 2009
520
521
522
SDL_PreMultiplyAlphaRGBA8888(texture->w, texture->h,
(Uint32 *) data->pixels,
data->pitch);
Feb 7, 2009
Feb 7, 2009
523
524
525
data->premultiplied = SDL_TRUE;
break;
case SDL_PIXELFORMAT_ABGR8888:
Feb 7, 2009
Feb 7, 2009
526
527
528
SDL_PreMultiplyAlphaABGR8888(texture->w, texture->h,
(Uint32 *) data->pixels,
data->pitch);
Feb 7, 2009
Feb 7, 2009
529
530
531
data->premultiplied = SDL_TRUE;
break;
case SDL_PIXELFORMAT_BGRA8888:
Feb 7, 2009
Feb 7, 2009
532
533
534
SDL_PreMultiplyAlphaBGRA8888(texture->w, texture->h,
(Uint32 *) data->pixels,
data->pitch);
Feb 7, 2009
Feb 7, 2009
535
536
537
538
data->premultiplied = SDL_TRUE;
break;
}
}
Aug 28, 2006
Aug 28, 2006
539
return 0;
Mar 23, 2009
Mar 23, 2009
540
#endif
Aug 28, 2006
Aug 28, 2006
541
542
default:
SDL_Unsupported();
Dec 20, 2008
Dec 20, 2008
543
texture->blendMode = SDL_BLENDMODE_NONE;
Aug 28, 2006
Aug 28, 2006
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
return -1;
}
}
static int
GDI_SetTextureScaleMode(SDL_Renderer * renderer, SDL_Texture * texture)
{
switch (texture->scaleMode) {
case SDL_TEXTURESCALEMODE_NONE:
case SDL_TEXTURESCALEMODE_FAST:
return 0;
case SDL_TEXTURESCALEMODE_SLOW:
case SDL_TEXTURESCALEMODE_BEST:
SDL_Unsupported();
texture->scaleMode = SDL_TEXTURESCALEMODE_FAST;
return -1;
default:
SDL_Unsupported();
texture->scaleMode = SDL_TEXTURESCALEMODE_NONE;
return -1;
}
return 0;
}
Jul 17, 2006
Jul 17, 2006
569
570
GDI_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, const void *pixels, int pitch)
Jul 17, 2006
Jul 17, 2006
572
GDI_TextureData *data = (GDI_TextureData *) texture->driverdata;
573
574
575
576
577
578
579
580
if (data->yuv) {
if (SDL_SW_UpdateYUVTexture(data->yuv, rect, pixels, pitch) < 0) {
return -1;
}
UpdateYUVTextureData(texture);
return 0;
} else {
Jul 17, 2006
Jul 17, 2006
581
GDI_RenderData *renderdata = (GDI_RenderData *) renderer->driverdata;
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
if (data->pixels) {
Uint8 *src, *dst;
int row;
size_t length;
src = (Uint8 *) pixels;
dst =
(Uint8 *) data->pixels + rect->y * data->pitch +
rect->x * SDL_BYTESPERPIXEL(texture->format);
length = rect->w * SDL_BYTESPERPIXEL(texture->format);
for (row = 0; row < rect->h; ++row) {
SDL_memcpy(dst, src, length);
src += pitch;
dst += data->pitch;
}
Feb 7, 2009
Feb 7, 2009
598
if (data->premultiplied) {
Feb 7, 2009
Feb 7, 2009
599
600
601
Uint32 *pixels =
(Uint32 *) data->pixels + rect->y * (data->pitch / 4) +
rect->x;
Feb 7, 2009
Feb 7, 2009
602
603
switch (texture->format) {
case SDL_PIXELFORMAT_ARGB8888:
Feb 7, 2009
Feb 7, 2009
604
605
SDL_PreMultiplyAlphaARGB8888(rect->w, rect->h, pixels,
data->pitch);
Feb 7, 2009
Feb 7, 2009
606
607
break;
case SDL_PIXELFORMAT_RGBA8888:
Feb 7, 2009
Feb 7, 2009
608
609
SDL_PreMultiplyAlphaRGBA8888(rect->w, rect->h, pixels,
data->pitch);
Feb 7, 2009
Feb 7, 2009
610
611
break;
case SDL_PIXELFORMAT_ABGR8888:
Feb 7, 2009
Feb 7, 2009
612
613
SDL_PreMultiplyAlphaABGR8888(rect->w, rect->h, pixels,
data->pitch);
Feb 7, 2009
Feb 7, 2009
614
615
break;
case SDL_PIXELFORMAT_BGRA8888:
Feb 7, 2009
Feb 7, 2009
616
617
SDL_PreMultiplyAlphaBGRA8888(rect->w, rect->h, pixels,
data->pitch);
Feb 7, 2009
Feb 7, 2009
618
619
620
break;
}
}
621
} else if (rect->w == texture->w && pitch == data->pitch) {
Mar 23, 2009
Mar 23, 2009
622
#ifndef NO_GETDIBBITS
623
624
625
626
627
628
if (!SetDIBits
(renderdata->window_hdc, data->hbm, rect->y, rect->h, pixels,
renderdata->bmi, DIB_RGB_COLORS)) {
WIN_SetError("SetDIBits()");
return -1;
}
Mar 23, 2009
Mar 23, 2009
629
630
631
632
#else
SDL_SetError("FIXME: Update Texture");
return -1;
#endif
633
634
635
636
637
638
639
640
641
642
} else {
SDL_SetError
("FIXME: Need to allocate temporary memory and do GetDIBits() followed by SetDIBits(), since we can only set blocks of scanlines at a time");
return -1;
}
return 0;
}
}
static int
Jul 17, 2006
Jul 17, 2006
643
644
645
GDI_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, int markDirty, void **pixels,
int *pitch)
Jul 17, 2006
Jul 17, 2006
647
GDI_TextureData *data = (GDI_TextureData *) texture->driverdata;
648
649
650
651
if (data->yuv) {
return SDL_SW_LockYUVTexture(data->yuv, rect, markDirty, pixels,
pitch);
Jul 12, 2006
Jul 12, 2006
652
} else if (data->pixels) {
Mar 23, 2009
Mar 23, 2009
653
#ifndef _WIN32_WCE
May 23, 2009
May 23, 2009
654
/* WinCE has no GdiFlush */
Mar 23, 2009
Mar 23, 2009
656
#endif
657
658
659
660
661
*pixels =
(void *) ((Uint8 *) data->pixels + rect->y * data->pitch +
rect->x * SDL_BYTESPERPIXEL(texture->format));
*pitch = data->pitch;
return 0;
Jul 12, 2006
Jul 12, 2006
662
663
664
} else {
SDL_SetError("No pixels available");
return -1;
665
666
667
668
}
}
static void
Jul 17, 2006
Jul 17, 2006
669
GDI_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
Jul 17, 2006
Jul 17, 2006
671
GDI_TextureData *data = (GDI_TextureData *) texture->driverdata;
672
673
674
675
676
677
678
if (data->yuv) {
SDL_SW_UnlockYUVTexture(data->yuv);
UpdateYUVTextureData(texture);
}
}
Dec 30, 2008
Dec 30, 2008
679
680
681
682
683
684
685
686
687
688
689
690
691
static int
GDI_SetDrawBlendMode(SDL_Renderer * renderer)
{
switch (renderer->blendMode) {
case SDL_BLENDMODE_NONE:
return 0;
default:
SDL_Unsupported();
renderer->blendMode = SDL_BLENDMODE_NONE;
return -1;
}
}
Dec 30, 2008
Dec 30, 2008
692
static int
Dec 24, 2009
Dec 24, 2009
693
694
GDI_RenderDrawPoints(SDL_Renderer * renderer, const SDL_Point * points,
int count)
Dec 30, 2008
Dec 30, 2008
695
696
{
GDI_RenderData *data = (GDI_RenderData *) renderer->driverdata;
Dec 9, 2009
Dec 9, 2009
697
698
int i;
COLORREF color;
Dec 30, 2008
Dec 30, 2008
699
700
if (data->makedirty) {
Dec 9, 2009
Dec 9, 2009
701
/* Get the smallest rectangle that contains everything */
Jan 21, 2010
Jan 21, 2010
702
SDL_Window *window = renderer->window;
Dec 30, 2008
Dec 30, 2008
703
704
SDL_Rect rect;
Dec 9, 2009
Dec 9, 2009
705
706
707
708
709
710
711
712
rect.x = 0;
rect.y = 0;
rect.w = window->w;
rect.h = window->h;
if (!SDL_EnclosePoints(points, count, &rect, &rect)) {
/* Nothing to draw */
return 0;
}
Dec 30, 2008
Dec 30, 2008
713
714
715
716
SDL_AddDirtyRect(&data->dirty, &rect);
}
Dec 9, 2009
Dec 9, 2009
717
718
719
720
721
color = RGB(renderer->r, renderer->g, renderer->b);
for (i = 0; i < count; ++i) {
SetPixel(data->current_hdc, points[i].x, points[i].y, color);
}
Dec 30, 2008
Dec 30, 2008
722
723
724
725
return 0;
}
static int
Dec 24, 2009
Dec 24, 2009
726
727
GDI_RenderDrawLines(SDL_Renderer * renderer, const SDL_Point * points,
int count)
Dec 30, 2008
Dec 30, 2008
728
729
{
GDI_RenderData *data = (GDI_RenderData *) renderer->driverdata;
Feb 8, 2009
Feb 8, 2009
730
HPEN pen;
Dec 30, 2008
Dec 30, 2008
731
732
733
BOOL status;
if (data->makedirty) {
Dec 9, 2009
Dec 9, 2009
734
/* Get the smallest rectangle that contains everything */
Jan 21, 2010
Jan 21, 2010
735
SDL_Window *window = renderer->window;
Dec 9, 2009
Dec 9, 2009
736
737
738
739
740
741
742
743
744
745
SDL_Rect clip, rect;
clip.x = 0;
clip.y = 0;
clip.w = window->w;
clip.h = window->h;
SDL_EnclosePoints(points, count, NULL, &rect);
if (!SDL_IntersectRect(&rect, &clip, &rect)) {
/* Nothing to draw */
return 0;
Dec 30, 2008
Dec 30, 2008
746
}
Dec 9, 2009
Dec 9, 2009
747
Dec 30, 2008
Dec 30, 2008
748
749
750
SDL_AddDirtyRect(&data->dirty, &rect);
}
Feb 8, 2009
Feb 8, 2009
751
752
753
/* Should we cache the pen? .. it looks like GDI does for us. :) */
pen = CreatePen(PS_SOLID, 1, RGB(renderer->r, renderer->g, renderer->b));
SelectObject(data->current_hdc, pen);
Dec 9, 2009
Dec 9, 2009
754
755
756
757
758
759
760
761
762
763
764
{
LPPOINT p = SDL_stack_alloc(POINT, count);
int i;
for (i = 0; i < count; ++i) {
p[i].x = points[i].x;
p[i].y = points[i].y;
}
status = Polyline(data->current_hdc, p, count);
SDL_stack_free(p);
}
Feb 8, 2009
Feb 8, 2009
765
DeleteObject(pen);
Dec 30, 2008
Dec 30, 2008
766
Dec 7, 2009
Dec 7, 2009
767
/* Need to close the endpoint of the line */
Dec 9, 2009
Dec 9, 2009
768
769
770
771
if (points[0].x != points[count-1].x || points[0].y != points[count-1].y) {
SetPixel(data->current_hdc, points[count-1].x, points[count-1].y,
RGB(renderer->r, renderer->g, renderer->b));
}
Dec 7, 2009
Dec 7, 2009
772
Dec 30, 2008
Dec 30, 2008
773
if (!status) {
Dec 9, 2009
Dec 9, 2009
774
WIN_SetError("Polyline()");
Dec 30, 2008
Dec 30, 2008
775
776
777
778
779
return -1;
}
return 0;
}
Dec 24, 2009
Dec 24, 2009
781
782
783
784
785
786
787
788
789
GDI_RenderDrawRects(SDL_Renderer * renderer, const SDL_Rect ** rects,
int count)
{
GDI_RenderData *data = (GDI_RenderData *) renderer->driverdata;
HPEN pen;
POINT vertices[5];
int i, status = 1;
if (data->makedirty) {
Jan 21, 2010
Jan 21, 2010
790
SDL_Window *window = renderer->window;
Dec 24, 2009
Dec 24, 2009
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
SDL_Rect clip, rect;
clip.x = 0;
clip.y = 0;
clip.w = window->w;
clip.h = window->h;
for (i = 0; i < count; ++i) {
if (SDL_IntersectRect(rects[i], &clip, &rect)) {
SDL_AddDirtyRect(&data->dirty, &rect);
}
}
}
/* Should we cache the pen? .. it looks like GDI does for us. :) */
pen = CreatePen(PS_SOLID, 1, RGB(renderer->r, renderer->g, renderer->b));
SelectObject(data->current_hdc, pen);
for (i = 0; i < count; ++i) {
const SDL_Rect *rect = rects[i];
vertices[0].x = rect->x;
vertices[0].y = rect->y;
vertices[1].x = rect->x+rect->w-1;
vertices[1].y = rect->y;
vertices[2].x = rect->x+rect->w-1;
vertices[2].y = rect->y+rect->h-1;
vertices[3].x = rect->x;
vertices[3].y = rect->y+rect->h-1;
vertices[4].x = rect->x;
vertices[4].y = rect->y;
status &= Polyline(data->current_hdc, vertices, 5);
}
DeleteObject(pen);
if (!status) {
WIN_SetError("Polyline()");
return -1;
}
return 0;
}
static int
GDI_RenderFillRects(SDL_Renderer * renderer, const SDL_Rect ** rects,
int count)
Jul 17, 2006
Jul 17, 2006
841
GDI_RenderData *data = (GDI_RenderData *) renderer->driverdata;
Jul 12, 2006
Jul 12, 2006
843
HBRUSH brush;
Dec 9, 2009
Dec 9, 2009
844
int i, status = 1;
Jul 12, 2006
Jul 12, 2006
846
if (data->makedirty) {
Jan 21, 2010
Jan 21, 2010
847
SDL_Window *window = renderer->window;
Dec 9, 2009
Dec 9, 2009
848
849
850
851
852
853
SDL_Rect clip, rect;
clip.x = 0;
clip.y = 0;
clip.w = window->w;
clip.h = window->h;
Jul 12, 2006
Jul 12, 2006
854
Dec 9, 2009
Dec 9, 2009
855
856
857
858
859
860
for (i = 0; i < count; ++i) {
if (SDL_IntersectRect(rects[i], &clip, &rect)) {
SDL_AddDirtyRect(&data->dirty, &rect);
}
}
}
861
862
/* Should we cache the brushes? .. it looks like GDI does for us. :) */
Dec 25, 2008
Dec 25, 2008
863
brush = CreateSolidBrush(RGB(renderer->r, renderer->g, renderer->b));
864
SelectObject(data->current_hdc, brush);
Dec 9, 2009
Dec 9, 2009
865
866
867
868
869
870
871
872
873
874
for (i = 0; i < count; ++i) {
const SDL_Rect *rect = rects[i];
rc.left = rect->x;
rc.top = rect->y;
rc.right = rect->x + rect->w;
rc.bottom = rect->y + rect->h;
status &= FillRect(data->current_hdc, &rc, brush);
}
875
876
877
878
879
880
881
882
883
884
DeleteObject(brush);
if (!status) {
WIN_SetError("FillRect()");
return -1;
}
return 0;
}
static int
Jul 17, 2006
Jul 17, 2006
885
GDI_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
Aug 28, 2006
Aug 28, 2006
886
const SDL_Rect * srcrect, const SDL_Rect * dstrect)
Jul 17, 2006
Jul 17, 2006
888
889
GDI_RenderData *data = (GDI_RenderData *) renderer->driverdata;
GDI_TextureData *texturedata = (GDI_TextureData *) texture->driverdata;
Jul 12, 2006
Jul 12, 2006
891
892
893
894
if (data->makedirty) {
SDL_AddDirtyRect(&data->dirty, dstrect);
}
895
896
897
898
899
SelectObject(data->memory_hdc, texturedata->hbm);
if (texturedata->hpal) {
SelectPalette(data->memory_hdc, texturedata->hpal, TRUE);
RealizePalette(data->memory_hdc);
}
Feb 7, 2009
Feb 7, 2009
900
if (texture->blendMode & (SDL_BLENDMODE_MASK | SDL_BLENDMODE_BLEND)) {
Mar 23, 2009
Mar 23, 2009
901
902
903
904
#ifdef _WIN32_WCE
SDL_SetError("Texture has blendmode not supported under WinCE");
return -1;
#else
Aug 28, 2006
Aug 28, 2006
905
BLENDFUNCTION blendFunc = {
906
907
AC_SRC_OVER,
0,
Aug 28, 2006
Aug 28, 2006
908
texture->a,
909
910
911
912
913
914
915
916
917
AC_SRC_ALPHA
};
if (!AlphaBlend
(data->current_hdc, dstrect->x, dstrect->y, dstrect->w,
dstrect->h, data->memory_hdc, srcrect->x, srcrect->y, srcrect->w,
srcrect->h, blendFunc)) {
WIN_SetError("AlphaBlend()");
return -1;
}
Mar 23, 2009
Mar 23, 2009
918
#endif
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
} else {
if (srcrect->w == dstrect->w && srcrect->h == dstrect->h) {
if (!BitBlt
(data->current_hdc, dstrect->x, dstrect->y, dstrect->w,
srcrect->h, data->memory_hdc, srcrect->x, srcrect->y,
SRCCOPY)) {
WIN_SetError("BitBlt()");
return -1;
}
} else {
if (!StretchBlt
(data->current_hdc, dstrect->x, dstrect->y, dstrect->w,
dstrect->h, data->memory_hdc, srcrect->x, srcrect->y,
srcrect->w, srcrect->h, SRCCOPY)) {
WIN_SetError("StretchBlt()");
return -1;
}
}
}
return 0;
}
Dec 7, 2009
Dec 7, 2009
941
942
943
944
945
static int
GDI_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
Uint32 format, void * pixels, int pitch)
{
GDI_RenderData *renderdata = (GDI_RenderData *) renderer->driverdata;
Jan 21, 2010
Jan 21, 2010
946
947
SDL_Window *window = renderer->window;
SDL_VideoDisplay *display = window->display;
Dec 7, 2009
Dec 7, 2009
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
struct {
HBITMAP hbm;
void *pixels;
int pitch;
Uint32 format;
} data;
data.format = display->current_mode.format;
data.pitch = (rect->w * SDL_BYTESPERPIXEL(data.format));
data.hbm = GDI_CreateDIBSection(renderdata->memory_hdc, rect->w, rect->h,
data.pitch, data.format, NULL,
&data.pixels);
if (!data.hbm) {
WIN_SetError("Couldn't create bitmap");
return -1;
}
SelectObject(renderdata->memory_hdc, data.hbm);
if (!BitBlt(renderdata->memory_hdc, 0, 0, rect->w, rect->h,
renderdata->current_hdc, rect->x, rect->y, SRCCOPY)) {
WIN_SetError("BitBlt()");
DeleteObject(data.hbm);
return -1;
}
SDL_ConvertPixels(rect->w, rect->h,
data.format, data.pixels, data.pitch,
format, pixels, pitch);
DeleteObject(data.hbm);
return 0;
}
static int
GDI_RenderWritePixels(SDL_Renderer * renderer, const SDL_Rect * rect,
Uint32 format, const void * pixels, int pitch)
{
GDI_RenderData *renderdata = (GDI_RenderData *) renderer->driverdata;
Jan 21, 2010
Jan 21, 2010
987
988
SDL_Window *window = renderer->window;
SDL_VideoDisplay *display = window->display;
Dec 7, 2009
Dec 7, 2009
989
990
991
992
993
994
995
996
997
998
999
1000
struct {
HBITMAP hbm;
void *pixels;
int pitch;
Uint32 format;
} data;
data.format = display->current_mode.format;
data.pitch = (rect->w * SDL_BYTESPERPIXEL(data.format));
data.hbm = GDI_CreateDIBSection(renderdata->memory_hdc, rect->w, rect->h,
data.pitch, data.format,