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

Latest commit

 

History

History
418 lines (354 loc) · 12.7 KB

SDL_render_sw.c

File metadata and controls

418 lines (354 loc) · 12.7 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);
Dec 23, 2009
Dec 23, 2009
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);
Jul 19, 2006
Jul 19, 2006
59
static int SW_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
Aug 28, 2006
Aug 28, 2006
60
const SDL_Rect * srcrect, const SDL_Rect * dstrect);
Nov 9, 2009
Nov 9, 2009
61
static int SW_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
Nov 16, 2009
Nov 16, 2009
62
Uint32 format, void * pixels, int pitch);
Jul 19, 2006
Jul 19, 2006
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,
Feb 3, 2011
Feb 3, 2011
72
0,
Feb 3, 2011
Feb 3, 2011
73
8,
Aug 5, 2006
Aug 5, 2006
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,
Feb 3, 2011
Feb 3, 2011
82
83
SDL_PIXELFORMAT_BGRA8888
},
84
85
86
87
88
89
0,
0}
};
typedef struct
{
Aug 6, 2006
Aug 6, 2006
90
SDL_bool updateSize;
Feb 3, 2011
Feb 3, 2011
91
SDL_Surface *surface;
Jul 19, 2006
Jul 19, 2006
92
} SW_RenderData;
Jul 15, 2006
Jul 15, 2006
94
Feb 3, 2011
Feb 3, 2011
96
SW_CreateRendererForSurface(SDL_Surface * surface)
97
98
{
SDL_Renderer *renderer;
Jul 19, 2006
Jul 19, 2006
99
SW_RenderData *data;
Feb 3, 2011
Feb 3, 2011
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;
}
Jul 22, 2006
Jul 22, 2006
112
data = (SW_RenderData *) SDL_calloc(1, sizeof(*data));
Jul 19, 2006
Jul 19, 2006
114
SW_DestroyRenderer(renderer);
115
116
117
SDL_OutOfMemory();
return NULL;
}
Feb 3, 2011
Feb 3, 2011
118
119
data->surface = surface;
Feb 2, 2011
Feb 2, 2011
120
renderer->WindowEvent = SW_WindowEvent;
Feb 2, 2011
Feb 2, 2011
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;
Dec 23, 2009
Dec 23, 2009
129
130
131
renderer->RenderDrawPoints = SW_RenderDrawPoints;
renderer->RenderDrawLines = SW_RenderDrawLines;
renderer->RenderFillRects = SW_RenderFillRects;
Jul 19, 2006
Jul 19, 2006
132
renderer->RenderCopy = SW_RenderCopy;
Nov 9, 2009
Nov 9, 2009
133
renderer->RenderReadPixels = SW_RenderReadPixels;
Jul 19, 2006
Jul 19, 2006
134
135
renderer->RenderPresent = SW_RenderPresent;
renderer->DestroyRenderer = SW_DestroyRenderer;
Feb 2, 2011
Feb 2, 2011
136
renderer->info = SW_RenderDriver.info;
137
138
renderer->driverdata = data;
Feb 3, 2011
Feb 3, 2011
139
140
return renderer;
}
Jul 15, 2006
Jul 15, 2006
141
Feb 3, 2011
Feb 3, 2011
142
143
144
145
SDL_Renderer *
SW_CreateRenderer(SDL_Window * window, Uint32 flags)
{
SDL_Surface *surface;
Jul 15, 2006
Jul 15, 2006
146
Feb 3, 2011
Feb 3, 2011
147
148
surface = SDL_GetWindowSurface(window);
if (!surface) {
Jul 15, 2006
Jul 15, 2006
149
150
return NULL;
}
Feb 3, 2011
Feb 3, 2011
151
return SW_CreateRendererForSurface(surface);
Feb 3, 2011
Feb 3, 2011
154
static SDL_Surface *
Aug 6, 2006
Aug 6, 2006
155
156
157
SW_ActivateRenderer(SDL_Renderer * renderer)
{
SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
Jan 21, 2010
Jan 21, 2010
158
SDL_Window *window = renderer->window;
Aug 6, 2006
Aug 6, 2006
159
Aug 6, 2006
Aug 6, 2006
160
if (data->updateSize) {
Feb 3, 2011
Feb 3, 2011
161
162
data->surface = SDL_GetWindowSurface(window);
data->updateSize = SDL_FALSE;
Aug 6, 2006
Aug 6, 2006
163
}
Feb 3, 2011
Feb 3, 2011
164
return data->surface;
Aug 6, 2006
Aug 6, 2006
165
166
}
Feb 2, 2011
Feb 2, 2011
167
168
static void
SW_WindowEvent(SDL_Renderer * renderer, const SDL_WindowEvent *event)
Aug 6, 2006
Aug 6, 2006
169
170
171
{
SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
Feb 2, 2011
Feb 2, 2011
172
173
if (event->event == SDL_WINDOWEVENT_RESIZED) {
data->updateSize = SDL_TRUE;
Aug 6, 2006
Aug 6, 2006
174
175
176
}
}
Jul 19, 2006
Jul 19, 2006
178
SW_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
Feb 3, 2011
Feb 3, 2011
180
181
int bpp;
Uint32 Rmask, Gmask, Bmask, Amask;
Feb 3, 2011
Feb 3, 2011
183
184
185
186
187
if (!SDL_PixelFormatEnumToMasks
(texture->format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
SDL_SetError("Unknown texture format");
return -1;
}
Feb 3, 2011
Feb 3, 2011
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);
Jan 30, 2009
Jan 30, 2009
196
Feb 3, 2011
Feb 3, 2011
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;
}
Aug 28, 2006
Aug 28, 2006
207
208
209
static int
SW_SetTextureColorMod(SDL_Renderer * renderer, SDL_Texture * texture)
{
Aug 18, 2007
Aug 18, 2007
210
211
212
SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
return SDL_SetSurfaceColorMod(surface, texture->r, texture->g,
texture->b);
Aug 28, 2006
Aug 28, 2006
213
214
215
216
217
}
static int
SW_SetTextureAlphaMod(SDL_Renderer * renderer, SDL_Texture * texture)
{
Aug 18, 2007
Aug 18, 2007
218
219
SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
return SDL_SetSurfaceAlphaMod(surface, texture->a);
Aug 28, 2006
Aug 28, 2006
220
221
222
223
224
}
static int
SW_SetTextureBlendMode(SDL_Renderer * renderer, SDL_Texture * texture)
{
Aug 18, 2007
Aug 18, 2007
225
226
SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
return SDL_SetSurfaceBlendMode(surface, texture->blendMode);
Aug 28, 2006
Aug 28, 2006
227
228
}
Jul 19, 2006
Jul 19, 2006
230
231
SW_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
const SDL_Rect * rect, const void *pixels, int pitch)
Feb 3, 2011
Feb 3, 2011
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;
Feb 3, 2011
Feb 3, 2011
248
return 0;
249
250
251
}
static int
Jul 19, 2006
Jul 19, 2006
252
SW_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture,
Feb 3, 2011
Feb 3, 2011
253
const SDL_Rect * rect, void **pixels, int *pitch)
Feb 3, 2011
Feb 3, 2011
255
SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
Feb 3, 2011
Feb 3, 2011
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
Jul 19, 2006
Jul 19, 2006
265
SW_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture)
Dec 21, 2008
Dec 21, 2008
269
static int
Dec 23, 2009
Dec 23, 2009
270
271
SW_RenderDrawPoints(SDL_Renderer * renderer, const SDL_Point * points,
int count)
Dec 21, 2008
Dec 21, 2008
272
{
Feb 3, 2011
Feb 3, 2011
273
SDL_Surface *surface = SW_ActivateRenderer(renderer);
Dec 9, 2009
Dec 9, 2009
274
Feb 3, 2011
Feb 3, 2011
275
if (!surface) {
Feb 2, 2011
Feb 2, 2011
276
277
278
return -1;
}
Dec 9, 2009
Dec 9, 2009
279
/* Draw the points! */
Feb 1, 2011
Feb 1, 2011
280
if (renderer->blendMode == SDL_BLENDMODE_NONE) {
Feb 3, 2011
Feb 3, 2011
281
Uint32 color = SDL_MapRGBA(surface->format,
Dec 9, 2009
Dec 9, 2009
282
283
284
renderer->r, renderer->g, renderer->b,
renderer->a);
Feb 3, 2011
Feb 3, 2011
285
return SDL_DrawPoints(surface, points, count, color);
Dec 21, 2008
Dec 21, 2008
286
} else {
Feb 3, 2011
Feb 3, 2011
287
288
289
290
return SDL_BlendPoints(surface, points, count,
renderer->blendMode,
renderer->r, renderer->g, renderer->b,
renderer->a);
Dec 21, 2008
Dec 21, 2008
291
292
293
}
}
Dec 20, 2008
Dec 20, 2008
294
static int
Dec 23, 2009
Dec 23, 2009
295
296
SW_RenderDrawLines(SDL_Renderer * renderer, const SDL_Point * points,
int count)
Dec 20, 2008
Dec 20, 2008
297
{
Feb 3, 2011
Feb 3, 2011
298
SDL_Surface *surface = SW_ActivateRenderer(renderer);
Dec 9, 2009
Dec 9, 2009
299
Feb 3, 2011
Feb 3, 2011
300
if (!surface) {
Feb 2, 2011
Feb 2, 2011
301
302
303
return -1;
}
Feb 3, 2011
Feb 3, 2011
304
/* Draw the lines! */
Feb 1, 2011
Feb 1, 2011
305
if (renderer->blendMode == SDL_BLENDMODE_NONE) {
Feb 3, 2011
Feb 3, 2011
306
Uint32 color = SDL_MapRGBA(surface->format,
Dec 9, 2009
Dec 9, 2009
307
308
309
renderer->r, renderer->g, renderer->b,
renderer->a);
Feb 3, 2011
Feb 3, 2011
310
return SDL_DrawLines(surface, points, count, color);
Dec 20, 2008
Dec 20, 2008
311
} else {
Feb 3, 2011
Feb 3, 2011
312
313
314
315
return SDL_BlendLines(surface, points, count,
renderer->blendMode,
renderer->r, renderer->g, renderer->b,
renderer->a);
Dec 20, 2008
Dec 20, 2008
316
317
318
}
}
Dec 23, 2009
Dec 23, 2009
319
320
321
322
static int
SW_RenderFillRects(SDL_Renderer * renderer, const SDL_Rect ** rects,
int count)
{
Feb 3, 2011
Feb 3, 2011
323
SDL_Surface *surface = SW_ActivateRenderer(renderer);
Dec 23, 2009
Dec 23, 2009
324
Feb 3, 2011
Feb 3, 2011
325
if (!surface) {
Feb 2, 2011
Feb 2, 2011
326
327
328
return -1;
}
Feb 1, 2011
Feb 1, 2011
329
if (renderer->blendMode == SDL_BLENDMODE_NONE) {
Feb 3, 2011
Feb 3, 2011
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);
Dec 23, 2009
Dec 23, 2009
339
340
341
}
}
Jul 19, 2006
Jul 19, 2006
343
SW_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
Aug 28, 2006
Aug 28, 2006
344
const SDL_Rect * srcrect, const SDL_Rect * dstrect)
Feb 3, 2011
Feb 3, 2011
346
347
348
SDL_Surface *surface = SW_ActivateRenderer(renderer);
SDL_Surface *src = (SDL_Surface *) texture->driverdata;
SDL_Rect final_rect = *dstrect;
Feb 3, 2011
Feb 3, 2011
350
if (!surface) {
Feb 2, 2011
Feb 2, 2011
351
352
return -1;
}
Feb 3, 2011
Feb 3, 2011
353
return SDL_BlitSurface(src, srcrect, surface, &final_rect);
Nov 9, 2009
Nov 9, 2009
356
357
static int
SW_RenderReadPixels(SDL_Renderer * renderer, const SDL_Rect * rect,
Nov 16, 2009
Nov 16, 2009
358
Uint32 format, void * pixels, int pitch)
Nov 9, 2009
Nov 9, 2009
359
{
Feb 3, 2011
Feb 3, 2011
360
361
362
SDL_Surface *surface = SW_ActivateRenderer(renderer);
Uint32 src_format;
void *src_pixels;
Nov 9, 2009
Nov 9, 2009
363
Feb 3, 2011
Feb 3, 2011
364
if (!surface) {
Feb 2, 2011
Feb 2, 2011
365
366
367
return -1;
}
Feb 3, 2011
Feb 3, 2011
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");
Nov 9, 2009
Nov 9, 2009
371
372
373
return -1;
}
Feb 3, 2011
Feb 3, 2011
374
375
376
377
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
378
Feb 3, 2011
Feb 3, 2011
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);
Nov 9, 2009
Nov 9, 2009
386
387
}
Jul 19, 2006
Jul 19, 2006
389
SW_RenderPresent(SDL_Renderer * renderer)
Jul 19, 2006
Jul 19, 2006
391
SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
Feb 3, 2011
Feb 3, 2011
392
SDL_Window *window = renderer->window;
Feb 3, 2011
Feb 3, 2011
394
395
if (window) {
SDL_UpdateWindowSurface(window);
Feb 2, 2011
Feb 2, 2011
396
}
397
398
399
}
static void
Jul 19, 2006
Jul 19, 2006
400
SW_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture)
Feb 3, 2011
Feb 3, 2011
402
SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
Feb 3, 2011
Feb 3, 2011
404
SDL_FreeSurface(surface);
405
406
407
}
static void
Jul 19, 2006
Jul 19, 2006
408
SW_DestroyRenderer(SDL_Renderer * renderer)
Jul 19, 2006
Jul 19, 2006
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: */