This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDL_render_sw.c
439 lines (370 loc) · 13.2 KB
1
2
/*
SDL - Simple DirectMedia Layer
3
Copyright (C) 1997-2011 Sam Lantinga
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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"
24
25
#if !SDL_RENDER_DISABLED
26
#include "../SDL_sysrender.h"
27
28
29
30
31
32
33
34
#include "SDL_draw.h"
#include "SDL_blendfillrect.h"
#include "SDL_blendline.h"
#include "SDL_blendpoint.h"
#include "SDL_drawline.h"
#include "SDL_drawpoint.h"
35
36
37
/* SDL surface based renderer implementation */
38
static SDL_Renderer *SW_CreateRenderer(SDL_Window * window, Uint32 flags);
39
40
static void SW_WindowEvent(SDL_Renderer * renderer,
const SDL_WindowEvent *event);
41
static int SW_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
42
43
44
45
static int SW_SetTextureColorMod(SDL_Renderer * renderer,
SDL_Texture * texture);
static int SW_SetTextureAlphaMod(SDL_Renderer * renderer,
SDL_Texture * texture);
46
47
static int SW_SetTextureBlendMode(SDL_Renderer * renderer,
SDL_Texture * texture);
48
49
50
static int SW_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, const void *pixels,
int pitch);
51
static int SW_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
52
const SDL_Rect * rect, void **pixels, int *pitch);
53
static void SW_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
54
static void SW_SetClipRect(SDL_Renderer * renderer, const SDL_Rect * rect);
55
56
57
58
59
60
static int SW_RenderDrawPoints(SDL_Renderer * renderer,
const SDL_Point * points, int count);
static int SW_RenderDrawLines(SDL_Renderer * renderer,
const SDL_Point * points, int count);
static int SW_RenderFillRects(SDL_Renderer * renderer,
const SDL_Rect ** rects, int count);
61
static int SW_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
62
const SDL_Rect * srcrect, const SDL_Rect * dstrect);
63
static int SW_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
64
Uint32 format, void * pixels, int pitch);
65
66
67
68
69
70
71
static void SW_RenderPresent(SDL_Renderer * renderer);
static void SW_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture);
static void SW_DestroyRenderer(SDL_Renderer * renderer);
SDL_RenderDriver SW_RenderDriver = {
SW_CreateRenderer,
72
73
{
"software",
74
0,
75
8,
76
{
77
78
79
80
81
82
83
SDL_PIXELFORMAT_RGB555,
SDL_PIXELFORMAT_RGB565,
SDL_PIXELFORMAT_RGB888,
SDL_PIXELFORMAT_BGR888,
SDL_PIXELFORMAT_ARGB8888,
SDL_PIXELFORMAT_RGBA8888,
SDL_PIXELFORMAT_ABGR8888,
84
85
SDL_PIXELFORMAT_BGRA8888
},
86
87
88
89
90
91
0,
0}
};
typedef struct
{
92
SDL_bool updateSize;
93
SDL_Surface *surface;
94
} SW_RenderData;
95
96
97
SDL_Renderer *
98
SW_CreateRendererForSurface(SDL_Surface * surface)
99
100
{
SDL_Renderer *renderer;
101
SW_RenderData *data;
102
103
104
if (!surface) {
SDL_SetError("Can't create renderer for NULL surface");
105
106
107
108
109
110
111
112
113
return NULL;
}
renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
if (!renderer) {
SDL_OutOfMemory();
return NULL;
}
114
data = (SW_RenderData *) SDL_calloc(1, sizeof(*data));
115
if (!data) {
116
SW_DestroyRenderer(renderer);
117
118
119
SDL_OutOfMemory();
return NULL;
}
120
121
data->surface = surface;
122
renderer->WindowEvent = SW_WindowEvent;
123
124
125
126
127
128
129
renderer->CreateTexture = SW_CreateTexture;
renderer->SetTextureColorMod = SW_SetTextureColorMod;
renderer->SetTextureAlphaMod = SW_SetTextureAlphaMod;
renderer->SetTextureBlendMode = SW_SetTextureBlendMode;
renderer->UpdateTexture = SW_UpdateTexture;
renderer->LockTexture = SW_LockTexture;
renderer->UnlockTexture = SW_UnlockTexture;
130
renderer->SetClipRect = SW_SetClipRect;
131
renderer->DestroyTexture = SW_DestroyTexture;
132
133
134
renderer->RenderDrawPoints = SW_RenderDrawPoints;
renderer->RenderDrawLines = SW_RenderDrawLines;
renderer->RenderFillRects = SW_RenderFillRects;
135
renderer->RenderCopy = SW_RenderCopy;
136
renderer->RenderReadPixels = SW_RenderReadPixels;
137
138
renderer->RenderPresent = SW_RenderPresent;
renderer->DestroyRenderer = SW_DestroyRenderer;
139
renderer->info = SW_RenderDriver.info;
140
141
renderer->driverdata = data;
142
143
return renderer;
}
144
145
146
147
148
SDL_Renderer *
SW_CreateRenderer(SDL_Window * window, Uint32 flags)
{
SDL_Surface *surface;
149
150
151
surface = SDL_GetWindowSurface(window);
if (!surface) {
152
153
return NULL;
}
154
return SW_CreateRendererForSurface(surface);
155
156
}
157
static SDL_Surface *
158
159
160
SW_ActivateRenderer(SDL_Renderer * renderer)
{
SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
161
SDL_Window *window = renderer->window;
162
163
if (data->updateSize) {
164
165
data->surface = SDL_GetWindowSurface(window);
data->updateSize = SDL_FALSE;
166
}
167
return data->surface;
168
169
}
170
171
static void
SW_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
172
173
174
{
SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
175
if (event->event == SDL_WINDOWEVENT_SIZE_CHANGED) {
176
data->updateSize = SDL_TRUE;
177
178
179
}
}
180
static int
181
SW_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
182
{
183
184
int bpp;
Uint32 Rmask, Gmask, Bmask, Amask;
185
186
187
188
189
190
if (!SDL_PixelFormatEnumToMasks
(texture->format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
SDL_SetError("Unknown texture format");
return -1;
}
191
192
193
194
195
196
197
198
texture->driverdata =
SDL_CreateRGBSurface(0, texture->w, texture->h, bpp, Rmask, Gmask,
Bmask, Amask);
SDL_SetSurfaceColorMod(texture->driverdata, texture->r, texture->g,
texture->b);
SDL_SetSurfaceAlphaMod(texture->driverdata, texture->a);
SDL_SetSurfaceBlendMode(texture->driverdata, texture->blendMode);
199
200
201
if (texture->access == SDL_TEXTUREACCESS_STATIC) {
SDL_SetSurfaceRLE(texture->driverdata, 1);
202
203
204
205
206
207
208
209
}
if (!texture->driverdata) {
return -1;
}
return 0;
}
210
211
212
static int
SW_SetTextureColorMod(SDL_Renderer * renderer, SDL_Texture * texture)
{
213
214
215
SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
return SDL_SetSurfaceColorMod(surface, texture->r, texture->g,
texture->b);
216
217
218
219
220
}
static int
SW_SetTextureAlphaMod(SDL_Renderer * renderer, SDL_Texture * texture)
{
221
222
SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
return SDL_SetSurfaceAlphaMod(surface, texture->a);
223
224
225
226
227
}
static int
SW_SetTextureBlendMode(SDL_Renderer * renderer, SDL_Texture * texture)
{
228
229
SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
return SDL_SetSurfaceBlendMode(surface, texture->blendMode);
230
231
}
232
static int
233
234
SW_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, const void *pixels, int pitch)
235
{
236
237
238
239
240
241
242
243
244
245
246
247
248
249
SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
Uint8 *src, *dst;
int row;
size_t length;
src = (Uint8 *) pixels;
dst = (Uint8 *) surface->pixels +
rect->y * surface->pitch +
rect->x * surface->format->BytesPerPixel;
length = rect->w * surface->format->BytesPerPixel;
for (row = 0; row < rect->h; ++row) {
SDL_memcpy(dst, src, length);
src += pitch;
dst += surface->pitch;
250
}
251
return 0;
252
253
254
}
static int
255
SW_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
256
const SDL_Rect * rect, void **pixels, int *pitch)
257
{
258
SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
259
260
261
262
263
264
*pixels =
(void *) ((Uint8 *) surface->pixels + rect->y * surface->pitch +
rect->x * surface->format->BytesPerPixel);
*pitch = surface->pitch;
return 0;
265
266
267
}
static void
268
SW_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
269
270
271
{
}
272
273
274
275
276
277
278
279
280
281
282
static void
SW_SetClipRect(SDL_Renderer * renderer, const SDL_Rect * rect)
{
SDL_Surface *surface = SW_ActivateRenderer(renderer);
if (!surface) {
return;
}
SDL_SetClipRect(surface, rect);
}
283
static int
284
285
SW_RenderDrawPoints(SDL_Renderer * renderer, const SDL_Point * points,
int count)
286
{
287
SDL_Surface *surface = SW_ActivateRenderer(renderer);
288
289
if (!surface) {
290
291
292
return -1;
}
293
/* Draw the points! */
294
if (renderer->blendMode == SDL_BLENDMODE_NONE) {
295
Uint32 color = SDL_MapRGBA(surface->format,
296
297
298
renderer->r, renderer->g, renderer->b,
renderer->a);
299
return SDL_DrawPoints(surface, points, count, color);
300
} else {
301
302
303
304
return SDL_BlendPoints(surface, points, count,
renderer->blendMode,
renderer->r, renderer->g, renderer->b,
renderer->a);
305
306
307
}
}
308
static int
309
310
SW_RenderDrawLines(SDL_Renderer * renderer, const SDL_Point * points,
int count)
311
{
312
SDL_Surface *surface = SW_ActivateRenderer(renderer);
313
314
if (!surface) {
315
316
317
return -1;
}
318
/* Draw the lines! */
319
if (renderer->blendMode == SDL_BLENDMODE_NONE) {
320
Uint32 color = SDL_MapRGBA(surface->format,
321
322
323
renderer->r, renderer->g, renderer->b,
renderer->a);
324
return SDL_DrawLines(surface, points, count, color);
325
} else {
326
327
328
329
return SDL_BlendLines(surface, points, count,
renderer->blendMode,
renderer->r, renderer->g, renderer->b,
renderer->a);
330
331
332
}
}
333
334
335
336
static int
SW_RenderFillRects(SDL_Renderer * renderer, const SDL_Rect ** rects,
int count)
{
337
SDL_Surface *surface = SW_ActivateRenderer(renderer);
338
339
if (!surface) {
340
341
342
return -1;
}
343
if (renderer->blendMode == SDL_BLENDMODE_NONE) {
344
345
346
347
348
349
350
351
352
Uint32 color = SDL_MapRGBA(surface->format,
renderer->r, renderer->g, renderer->b,
renderer->a);
return SDL_FillRects(surface, rects, count, color);
} else {
return SDL_BlendFillRects(surface, rects, count,
renderer->blendMode,
renderer->r, renderer->g, renderer->b,
renderer->a);
353
354
355
}
}
356
static int
357
SW_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
358
const SDL_Rect * srcrect, const SDL_Rect * dstrect)
359
{
360
361
362
SDL_Surface *surface = SW_ActivateRenderer(renderer);
SDL_Surface *src = (SDL_Surface *) texture->driverdata;
SDL_Rect final_rect = *dstrect;
363
364
if (!surface) {
365
366
return -1;
}
367
368
369
370
371
372
if ( srcrect->w == final_rect.w && srcrect->h == final_rect.h ) {
return SDL_BlitSurface(src, srcrect, surface, &final_rect);
} else {
return SDL_BlitScaled(src, srcrect, surface, &final_rect);
}
373
374
}
375
376
static int
SW_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
377
Uint32 format, void * pixels, int pitch)
378
{
379
380
381
SDL_Surface *surface = SW_ActivateRenderer(renderer);
Uint32 src_format;
void *src_pixels;
382
383
if (!surface) {
384
385
386
return -1;
}
387
388
389
if (rect->x < 0 || rect->x+rect->w > surface->w ||
rect->y < 0 || rect->y+rect->h > surface->h) {
SDL_SetError("Tried to read outside of surface bounds");
390
391
392
return -1;
}
393
394
395
396
src_format = SDL_MasksToPixelFormatEnum(
surface->format->BitsPerPixel,
surface->format->Rmask, surface->format->Gmask,
surface->format->Bmask, surface->format->Amask);
397
398
399
400
401
402
403
404
src_pixels = (void*)((Uint8 *) surface->pixels +
rect->y * surface->pitch +
rect->x * surface->format->BytesPerPixel);
return SDL_ConvertPixels(rect->w, rect->h,
src_format, src_pixels, surface->pitch,
format, pixels, pitch);
405
406
}
407
static void
408
SW_RenderPresent(SDL_Renderer * renderer)
409
{
410
SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
411
SDL_Window *window = renderer->window;
412
413
414
if (window) {
SDL_UpdateWindowSurface(window);
415
}
416
417
418
}
static void
419
SW_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
420
{
421
SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
422
423
SDL_FreeSurface(surface);
424
425
426
}
static void
427
SW_DestroyRenderer(SDL_Renderer * renderer)
428
{
429
SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
430
431
432
433
434
435
436
if (data) {
SDL_free(data);
}
SDL_free(renderer);
}
437
438
#endif /* !SDL_RENDER_DISABLED */
439
/* vi: set ts=4 sw=4 expandtab: */