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