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

Latest commit

 

History

History
431 lines (365 loc) · 13 KB

SDL_render_sw.c

File metadata and controls

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