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

Latest commit

 

History

History
435 lines (367 loc) · 13.1 KB

SDL_render_sw.c

File metadata and controls

435 lines (367 loc) · 13.1 KB
 
1
2
/*
SDL - Simple DirectMedia Layer
Feb 12, 2011
Feb 12, 2011
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"
Feb 8, 2011
Feb 8, 2011
24
25
#if !SDL_RENDER_DISABLED
Feb 2, 2011
Feb 2, 2011
26
27
#include "../SDL_sysrender.h"
#include "../../video/SDL_pixels_c.h"
Feb 3, 2011
Feb 3, 2011
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 */
Jul 19, 2006
Jul 19, 2006
39
static SDL_Renderer *SW_CreateRenderer(SDL_Window * window, Uint32 flags);
Feb 2, 2011
Feb 2, 2011
40
41
static void SW_WindowEvent(SDL_Renderer * renderer,
const SDL_WindowEvent *event);
Jul 19, 2006
Jul 19, 2006
42
static int SW_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
Aug 28, 2006
Aug 28, 2006
43
44
45
46
static int SW_SetTextureColorMod(SDL_Renderer * renderer,
SDL_Texture * texture);
static int SW_SetTextureAlphaMod(SDL_Renderer * renderer,
SDL_Texture * texture);
Feb 1, 2011
Feb 1, 2011
47
48
static int SW_SetTextureBlendMode(SDL_Renderer * renderer,
SDL_Texture * texture);
Aug 28, 2006
Aug 28, 2006
49
50
51
static int SW_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, const void *pixels,
int pitch);
Jul 19, 2006
Jul 19, 2006
52
static int SW_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
Feb 3, 2011
Feb 3, 2011
53
const SDL_Rect * rect, void **pixels, int *pitch);
Jul 19, 2006
Jul 19, 2006
54
static void SW_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture);
Feb 8, 2011
Feb 8, 2011
55
static void SW_SetClipRect(SDL_Renderer * renderer, const SDL_Rect * rect);
Dec 23, 2009
Dec 23, 2009
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);
Jul 19, 2006
Jul 19, 2006
62
static int SW_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
Aug 28, 2006
Aug 28, 2006
63
const SDL_Rect * srcrect, const SDL_Rect * dstrect);
Nov 9, 2009
Nov 9, 2009
64
static int SW_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
Nov 16, 2009
Nov 16, 2009
65
Uint32 format, void * pixels, int pitch);
Jul 19, 2006
Jul 19, 2006
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,
Feb 3, 2011
Feb 3, 2011
75
0,
Feb 3, 2011
Feb 3, 2011
76
8,
Aug 5, 2006
Aug 5, 2006
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,
Feb 3, 2011
Feb 3, 2011
85
86
SDL_PIXELFORMAT_BGRA8888
},
87
88
89
90
91
92
0,
0}
};
typedef struct
{
Aug 6, 2006
Aug 6, 2006
93
SDL_bool updateSize;
Feb 3, 2011
Feb 3, 2011
94
SDL_Surface *surface;
Jul 19, 2006
Jul 19, 2006
95
} SW_RenderData;
Jul 15, 2006
Jul 15, 2006
97
Feb 3, 2011
Feb 3, 2011
99
SW_CreateRendererForSurface(SDL_Surface * surface)
100
101
{
SDL_Renderer *renderer;
Jul 19, 2006
Jul 19, 2006
102
SW_RenderData *data;
Feb 3, 2011
Feb 3, 2011
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;
}
Jul 22, 2006
Jul 22, 2006
115
data = (SW_RenderData *) SDL_calloc(1, sizeof(*data));
Jul 19, 2006
Jul 19, 2006
117
SW_DestroyRenderer(renderer);
118
119
120
SDL_OutOfMemory();
return NULL;
}
Feb 3, 2011
Feb 3, 2011
121
122
data->surface = surface;
Feb 2, 2011
Feb 2, 2011
123
renderer->WindowEvent = SW_WindowEvent;
Feb 2, 2011
Feb 2, 2011
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;
Feb 8, 2011
Feb 8, 2011
131
renderer->SetClipRect = SW_SetClipRect;
Feb 2, 2011
Feb 2, 2011
132
renderer->DestroyTexture = SW_DestroyTexture;
Dec 23, 2009
Dec 23, 2009
133
134
135
renderer->RenderDrawPoints = SW_RenderDrawPoints;
renderer->RenderDrawLines = SW_RenderDrawLines;
renderer->RenderFillRects = SW_RenderFillRects;
Jul 19, 2006
Jul 19, 2006
136
renderer->RenderCopy = SW_RenderCopy;
Nov 9, 2009
Nov 9, 2009
137
renderer->RenderReadPixels = SW_RenderReadPixels;
Jul 19, 2006
Jul 19, 2006
138
139
renderer->RenderPresent = SW_RenderPresent;
renderer->DestroyRenderer = SW_DestroyRenderer;
Feb 2, 2011
Feb 2, 2011
140
renderer->info = SW_RenderDriver.info;
141
142
renderer->driverdata = data;
Feb 3, 2011
Feb 3, 2011
143
144
return renderer;
}
Jul 15, 2006
Jul 15, 2006
145
Feb 3, 2011
Feb 3, 2011
146
147
148
149
SDL_Renderer *
SW_CreateRenderer(SDL_Window * window, Uint32 flags)
{
SDL_Surface *surface;
Jul 15, 2006
Jul 15, 2006
150
Feb 3, 2011
Feb 3, 2011
151
152
surface = SDL_GetWindowSurface(window);
if (!surface) {
Jul 15, 2006
Jul 15, 2006
153
154
return NULL;
}
Feb 3, 2011
Feb 3, 2011
155
return SW_CreateRendererForSurface(surface);
Feb 3, 2011
Feb 3, 2011
158
static SDL_Surface *
Aug 6, 2006
Aug 6, 2006
159
160
161
SW_ActivateRenderer(SDL_Renderer * renderer)
{
SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
Jan 21, 2010
Jan 21, 2010
162
SDL_Window *window = renderer->window;
Aug 6, 2006
Aug 6, 2006
163
Aug 6, 2006
Aug 6, 2006
164
if (data->updateSize) {
Feb 3, 2011
Feb 3, 2011
165
166
data->surface = SDL_GetWindowSurface(window);
data->updateSize = SDL_FALSE;
Aug 6, 2006
Aug 6, 2006
167
}
Feb 3, 2011
Feb 3, 2011
168
return data->surface;
Aug 6, 2006
Aug 6, 2006
169
170
}
Feb 2, 2011
Feb 2, 2011
171
172
static void
SW_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
Aug 6, 2006
Aug 6, 2006
173
174
175
{
SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
Feb 2, 2011
Feb 2, 2011
176
177
if (event->event == SDL_WINDOWEVENT_RESIZED) {
data->updateSize = SDL_TRUE;
Aug 6, 2006
Aug 6, 2006
178
179
180
}
}
Jul 19, 2006
Jul 19, 2006
182
SW_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
Feb 3, 2011
Feb 3, 2011
184
185
int bpp;
Uint32 Rmask, Gmask, Bmask, Amask;
Feb 3, 2011
Feb 3, 2011
187
188
189
190
191
if (!SDL_PixelFormatEnumToMasks
(texture->format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
SDL_SetError("Unknown texture format");
return -1;
}
Feb 3, 2011
Feb 3, 2011
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);
Jan 30, 2009
Jan 30, 2009
200
Feb 3, 2011
Feb 3, 2011
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;
}
Aug 28, 2006
Aug 28, 2006
211
212
213
static int
SW_SetTextureColorMod(SDL_Renderer * renderer, SDL_Texture * texture)
{
Aug 18, 2007
Aug 18, 2007
214
215
216
SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
return SDL_SetSurfaceColorMod(surface, texture->r, texture->g,
texture->b);
Aug 28, 2006
Aug 28, 2006
217
218
219
220
221
}
static int
SW_SetTextureAlphaMod(SDL_Renderer * renderer, SDL_Texture * texture)
{
Aug 18, 2007
Aug 18, 2007
222
223
SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
return SDL_SetSurfaceAlphaMod(surface, texture->a);
Aug 28, 2006
Aug 28, 2006
224
225
226
227
228
}
static int
SW_SetTextureBlendMode(SDL_Renderer * renderer, SDL_Texture * texture)
{
Aug 18, 2007
Aug 18, 2007
229
230
SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
return SDL_SetSurfaceBlendMode(surface, texture->blendMode);
Aug 28, 2006
Aug 28, 2006
231
232
}
Jul 19, 2006
Jul 19, 2006
234
235
SW_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, const void *pixels, int pitch)
Feb 3, 2011
Feb 3, 2011
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;
Feb 3, 2011
Feb 3, 2011
252
return 0;
253
254
255
}
static int
Jul 19, 2006
Jul 19, 2006
256
SW_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
Feb 3, 2011
Feb 3, 2011
257
const SDL_Rect * rect, void **pixels, int *pitch)
Feb 3, 2011
Feb 3, 2011
259
SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
Feb 3, 2011
Feb 3, 2011
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
Jul 19, 2006
Jul 19, 2006
269
SW_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
Feb 8, 2011
Feb 8, 2011
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);
}
Dec 21, 2008
Dec 21, 2008
284
static int
Dec 23, 2009
Dec 23, 2009
285
286
SW_RenderDrawPoints(SDL_Renderer * renderer, const SDL_Point * points,
int count)
Dec 21, 2008
Dec 21, 2008
287
{
Feb 3, 2011
Feb 3, 2011
288
SDL_Surface *surface = SW_ActivateRenderer(renderer);
Dec 9, 2009
Dec 9, 2009
289
Feb 3, 2011
Feb 3, 2011
290
if (!surface) {
Feb 2, 2011
Feb 2, 2011
291
292
293
return -1;
}
Dec 9, 2009
Dec 9, 2009
294
/* Draw the points! */
Feb 1, 2011
Feb 1, 2011
295
if (renderer->blendMode == SDL_BLENDMODE_NONE) {
Feb 3, 2011
Feb 3, 2011
296
Uint32 color = SDL_MapRGBA(surface->format,
Dec 9, 2009
Dec 9, 2009
297
298
299
renderer->r, renderer->g, renderer->b,
renderer->a);
Feb 3, 2011
Feb 3, 2011
300
return SDL_DrawPoints(surface, points, count, color);
Dec 21, 2008
Dec 21, 2008
301
} else {
Feb 3, 2011
Feb 3, 2011
302
303
304
305
return SDL_BlendPoints(surface, points, count,
renderer->blendMode,
renderer->r, renderer->g, renderer->b,
renderer->a);
Dec 21, 2008
Dec 21, 2008
306
307
308
}
}
Dec 20, 2008
Dec 20, 2008
309
static int
Dec 23, 2009
Dec 23, 2009
310
311
SW_RenderDrawLines(SDL_Renderer * renderer, const SDL_Point * points,
int count)
Dec 20, 2008
Dec 20, 2008
312
{
Feb 3, 2011
Feb 3, 2011
313
SDL_Surface *surface = SW_ActivateRenderer(renderer);
Dec 9, 2009
Dec 9, 2009
314
Feb 3, 2011
Feb 3, 2011
315
if (!surface) {
Feb 2, 2011
Feb 2, 2011
316
317
318
return -1;
}
Feb 3, 2011
Feb 3, 2011
319
/* Draw the lines! */
Feb 1, 2011
Feb 1, 2011
320
if (renderer->blendMode == SDL_BLENDMODE_NONE) {
Feb 3, 2011
Feb 3, 2011
321
Uint32 color = SDL_MapRGBA(surface->format,
Dec 9, 2009
Dec 9, 2009
322
323
324
renderer->r, renderer->g, renderer->b,
renderer->a);
Feb 3, 2011
Feb 3, 2011
325
return SDL_DrawLines(surface, points, count, color);
Dec 20, 2008
Dec 20, 2008
326
} else {
Feb 3, 2011
Feb 3, 2011
327
328
329
330
return SDL_BlendLines(surface, points, count,
renderer->blendMode,
renderer->r, renderer->g, renderer->b,
renderer->a);
Dec 20, 2008
Dec 20, 2008
331
332
333
}
}
Dec 23, 2009
Dec 23, 2009
334
335
336
337
static int
SW_RenderFillRects(SDL_Renderer * renderer, const SDL_Rect ** rects,
int count)
{
Feb 3, 2011
Feb 3, 2011
338
SDL_Surface *surface = SW_ActivateRenderer(renderer);
Dec 23, 2009
Dec 23, 2009
339
Feb 3, 2011
Feb 3, 2011
340
if (!surface) {
Feb 2, 2011
Feb 2, 2011
341
342
343
return -1;
}
Feb 1, 2011
Feb 1, 2011
344
if (renderer->blendMode == SDL_BLENDMODE_NONE) {
Feb 3, 2011
Feb 3, 2011
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);
Dec 23, 2009
Dec 23, 2009
354
355
356
}
}
Jul 19, 2006
Jul 19, 2006
358
SW_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
Aug 28, 2006
Aug 28, 2006
359
const SDL_Rect * srcrect, const SDL_Rect * dstrect)
Feb 3, 2011
Feb 3, 2011
361
362
363
SDL_Surface *surface = SW_ActivateRenderer(renderer);
SDL_Surface *src = (SDL_Surface *) texture->driverdata;
SDL_Rect final_rect = *dstrect;
Feb 3, 2011
Feb 3, 2011
365
if (!surface) {
Feb 2, 2011
Feb 2, 2011
366
367
return -1;
}
Feb 3, 2011
Feb 3, 2011
368
return SDL_BlitSurface(src, srcrect, surface, &final_rect);
Nov 9, 2009
Nov 9, 2009
371
372
static int
SW_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
Nov 16, 2009
Nov 16, 2009
373
Uint32 format, void * pixels, int pitch)
Nov 9, 2009
Nov 9, 2009
374
{
Feb 3, 2011
Feb 3, 2011
375
376
377
SDL_Surface *surface = SW_ActivateRenderer(renderer);
Uint32 src_format;
void *src_pixels;
Nov 9, 2009
Nov 9, 2009
378
Feb 3, 2011
Feb 3, 2011
379
if (!surface) {
Feb 2, 2011
Feb 2, 2011
380
381
382
return -1;
}
Feb 3, 2011
Feb 3, 2011
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");
Nov 9, 2009
Nov 9, 2009
386
387
388
return -1;
}
Feb 3, 2011
Feb 3, 2011
389
390
391
392
src_format = SDL_MasksToPixelFormatEnum(
surface->format->BitsPerPixel,
surface->format->Rmask, surface->format->Gmask,
surface->format->Bmask, surface->format->Amask);
Nov 9, 2009
Nov 9, 2009
393
Feb 3, 2011
Feb 3, 2011
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);
Nov 9, 2009
Nov 9, 2009
401
402
}
Jul 19, 2006
Jul 19, 2006
404
SW_RenderPresent(SDL_Renderer * renderer)
Jul 19, 2006
Jul 19, 2006
406
SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
Feb 3, 2011
Feb 3, 2011
407
SDL_Window *window = renderer->window;
Feb 3, 2011
Feb 3, 2011
409
410
if (window) {
SDL_UpdateWindowSurface(window);
Feb 2, 2011
Feb 2, 2011
411
}
412
413
414
}
static void
Jul 19, 2006
Jul 19, 2006
415
SW_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
Feb 3, 2011
Feb 3, 2011
417
SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
Feb 3, 2011
Feb 3, 2011
419
SDL_FreeSurface(surface);
420
421
422
}
static void
Jul 19, 2006
Jul 19, 2006
423
SW_DestroyRenderer(SDL_Renderer * renderer)
Jul 19, 2006
Jul 19, 2006
425
SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
426
427
428
429
430
431
432
if (data) {
SDL_free(data);
}
SDL_free(renderer);
}
Feb 8, 2011
Feb 8, 2011
433
434
#endif /* !SDL_RENDER_DISABLED */
435
/* vi: set ts=4 sw=4 expandtab: */