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