This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
/
SDL_nullrender.c
256 lines (224 loc) · 8.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/*
SDL - Simple DirectMedia Layer
Copyright (C) 1997-2006 Sam Lantinga
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"
#include "SDL_video.h"
#include "../SDL_sysvideo.h"
26
#include "../SDL_yuv_sw_c.h"
27
#include "../SDL_renderer_sw.h"
28
#include "../SDL_rendercopy.h"
29
30
31
32
/* SDL surface based renderer implementation */
33
34
static SDL_Renderer *SDL_DUMMY_CreateRenderer(SDL_Window * window,
Uint32 flags);
35
36
static int SDL_DUMMY_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g,
Uint8 b, Uint8 a, const SDL_Rect * rect);
37
38
39
static int SDL_DUMMY_RenderCopy(SDL_Renderer * renderer,
SDL_Texture * texture,
const SDL_Rect * srcrect,
40
const SDL_Rect * dstrect);
41
42
43
44
45
46
47
static void SDL_DUMMY_RenderPresent(SDL_Renderer * renderer);
static void SDL_DUMMY_DestroyRenderer(SDL_Renderer * renderer);
SDL_RenderDriver SDL_DUMMY_RenderDriver = {
SDL_DUMMY_CreateRenderer,
{
48
"dummy",
49
50
51
(SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTCOPY |
SDL_RENDERER_PRESENTFLIP2 | SDL_RENDERER_PRESENTFLIP3 |
SDL_RENDERER_PRESENTDISCARD),
52
}
53
54
55
56
};
typedef struct
{
57
58
int current_screen;
SDL_Surface *screens[3];
59
60
61
62
63
64
65
66
67
} SDL_DUMMY_RenderData;
SDL_Renderer *
SDL_DUMMY_CreateRenderer(SDL_Window * window, Uint32 flags)
{
SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
SDL_DisplayMode *displayMode = &display->current_mode;
SDL_Renderer *renderer;
SDL_DUMMY_RenderData *data;
68
int i, n;
69
70
71
72
73
74
75
76
77
int bpp;
Uint32 Rmask, Gmask, Bmask, Amask;
if (!SDL_PixelFormatEnumToMasks
(displayMode->format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
SDL_SetError("Unknown display format");
return NULL;
}
78
renderer = (SDL_Renderer *) SDL_calloc(1, sizeof(*renderer));
79
80
81
82
83
84
85
86
87
88
89
90
91
if (!renderer) {
SDL_OutOfMemory();
return NULL;
}
data = (SDL_DUMMY_RenderData *) SDL_malloc(sizeof(*data));
if (!data) {
SDL_DUMMY_DestroyRenderer(renderer);
SDL_OutOfMemory();
return NULL;
}
SDL_zerop(data);
92
93
renderer->RenderFill = SDL_DUMMY_RenderFill;
renderer->RenderCopy = SDL_DUMMY_RenderCopy;
94
95
renderer->RenderPresent = SDL_DUMMY_RenderPresent;
renderer->DestroyRenderer = SDL_DUMMY_DestroyRenderer;
96
97
renderer->info.name = SDL_DUMMY_RenderDriver.info.name;
renderer->info.flags = 0;
98
99
renderer->window = window->id;
renderer->driverdata = data;
100
Setup_SoftwareRenderer(renderer);
101
102
103
if (flags & SDL_RENDERER_PRESENTFLIP2) {
renderer->info.flags |= SDL_RENDERER_PRESENTFLIP2;
104
n = 2;
105
106
} else if (flags & SDL_RENDERER_PRESENTFLIP3) {
renderer->info.flags |= SDL_RENDERER_PRESENTFLIP3;
107
108
n = 3;
} else {
109
renderer->info.flags |= SDL_RENDERER_PRESENTCOPY;
110
n = 1;
111
}
112
113
114
115
116
117
118
119
120
121
122
for (i = 0; i < n; ++i) {
data->screens[i] =
SDL_CreateRGBSurface(0, window->w, window->h, bpp, Rmask, Gmask,
Bmask, Amask);
if (!data->screens[i]) {
SDL_DUMMY_DestroyRenderer(renderer);
return NULL;
}
SDL_SetSurfacePalette(data->screens[i], display->palette);
}
data->current_screen = 0;
123
124
125
126
return renderer;
}
127
static int
128
129
SDL_DUMMY_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, Uint8 b,
Uint8 a, const SDL_Rect * rect)
130
{
131
132
SDL_DUMMY_RenderData *data =
(SDL_DUMMY_RenderData *) renderer->driverdata;
133
SDL_Surface *target = data->screens[data->current_screen];
134
Uint32 color;
135
136
137
138
139
140
141
142
143
SDL_Rect real_rect = *rect;
color = SDL_MapRGBA(target->format, r, g, b, a);
return SDL_FillRect(target, &real_rect, color);
}
static int
SDL_DUMMY_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
144
const SDL_Rect * srcrect, const SDL_Rect * dstrect)
145
{
146
147
SDL_DUMMY_RenderData *data =
(SDL_DUMMY_RenderData *) renderer->driverdata;
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
SDL_Window *window = SDL_GetWindowFromID(renderer->window);
SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
SDL_Surface *target = data->screens[data->current_screen];
void *pixels =
(Uint8 *) target->pixels + dstrect->y * target->pitch +
dstrect->x * target->format->BytesPerPixel;
return SDL_SW_CopyYUVToRGB((SDL_SW_YUVTexture *) texture->driverdata,
srcrect, display->current_mode.format,
dstrect->w, dstrect->h, pixels,
target->pitch);
} else {
SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
SDL_Surface *target = data->screens[data->current_screen];
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
SDL_RenderCopyFunc copyfunc = (SDL_RenderCopyFunc) surface->userdata;
if (copyfunc) {
SDL_RenderCopyData copydata;
copydata.src =
(Uint8 *) surface->pixels + srcrect->y * surface->pitch +
srcrect->x * surface->format->BytesPerPixel;
copydata.src_w = srcrect->w;
copydata.src_h = srcrect->h;
copydata.src_pitch = surface->pitch;
copydata.dst =
(Uint8 *) target->pixels + dstrect->y * target->pitch +
dstrect->x * target->format->BytesPerPixel;
copydata.dst_w = dstrect->w;
copydata.dst_h = dstrect->h;
copydata.dst_pitch = target->pitch;
copydata.flags = 0;
if (texture->modMode & SDL_TEXTUREMODULATE_COLOR) {
copydata.flags |= SDL_RENDERCOPY_MODULATE_COLOR;
copydata.r = texture->r;
copydata.g = texture->g;
copydata.b = texture->b;
}
if (texture->modMode & SDL_TEXTUREMODULATE_ALPHA) {
copydata.flags |= SDL_RENDERCOPY_MODULATE_ALPHA;
copydata.a = texture->a;
}
191
192
193
if (texture->blendMode & SDL_TEXTUREBLENDMODE_MASK) {
copydata.flags |= SDL_RENDERCOPY_MASK;
} else if (texture->blendMode & SDL_TEXTUREBLENDMODE_BLEND) {
194
195
196
197
198
199
200
201
202
copydata.flags |= SDL_RENDERCOPY_BLEND;
} else if (texture->blendMode & SDL_TEXTUREBLENDMODE_ADD) {
copydata.flags |= SDL_RENDERCOPY_ADD;
} else if (texture->blendMode & SDL_TEXTUREBLENDMODE_MOD) {
copydata.flags |= SDL_RENDERCOPY_MOD;
}
if (texture->scaleMode) {
copydata.flags |= SDL_RENDERCOPY_NEAREST;
}
203
204
copyfunc(©data);
return 0;
205
} else {
206
207
208
SDL_Rect real_srcrect = *srcrect;
SDL_Rect real_dstrect = *dstrect;
209
210
return SDL_LowerBlit(surface, &real_srcrect, target,
&real_dstrect);
211
212
213
214
215
}
}
}
static void
216
217
218
SDL_DUMMY_RenderPresent(SDL_Renderer * renderer)
{
static int frame_number;
219
220
SDL_DUMMY_RenderData *data =
(SDL_DUMMY_RenderData *) renderer->driverdata;
221
222
/* Send the data to the display */
223
224
225
226
if (SDL_getenv("SDL_VIDEO_DUMMY_SAVE_FRAMES")) {
char file[128];
SDL_snprintf(file, sizeof(file), "SDL_window%d-%8.8d.bmp",
renderer->window, ++frame_number);
227
228
229
230
SDL_SaveBMP(data->screens[data->current_screen], file);
}
/* Update the flipping chain, if any */
231
if (renderer->info.flags & SDL_RENDERER_PRESENTFLIP2) {
232
data->current_screen = (data->current_screen + 1) % 2;
233
} else if (renderer->info.flags & SDL_RENDERER_PRESENTFLIP3) {
234
235
236
237
238
data->current_screen = (data->current_screen + 1) % 3;
}
}
static void
239
240
SDL_DUMMY_DestroyRenderer(SDL_Renderer * renderer)
{
241
242
SDL_DUMMY_RenderData *data =
(SDL_DUMMY_RenderData *) renderer->driverdata;
243
int i;
244
245
if (data) {
246
247
248
249
for (i = 0; i < SDL_arraysize(data->screens); ++i) {
if (data->screens[i]) {
SDL_FreeSurface(data->screens[i]);
}
250
251
252
253
254
255
256
}
SDL_free(data);
}
SDL_free(renderer);
}
/* vi: set ts=4 sw=4 expandtab: */