Mercurial > sdl-ios-xcode
comparison src/video/dummy/SDL_nullrender.c @ 2227:b252359547ed
Exported the software renderer texture functions to make easier to create
a renderer based on a framebuffer.
Fixed an initialization bug with the dummy video display mode.
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Sat, 11 Aug 2007 23:26:03 +0000 |
parents | 7387e0514595 |
children | bb67fd5ccfdb |
comparison
equal
deleted
inserted
replaced
2226:0e70b4b8cf84 | 2227:b252359547ed |
---|---|
29 | 29 |
30 /* SDL surface based renderer implementation */ | 30 /* SDL surface based renderer implementation */ |
31 | 31 |
32 static SDL_Renderer *SDL_DUMMY_CreateRenderer(SDL_Window * window, | 32 static SDL_Renderer *SDL_DUMMY_CreateRenderer(SDL_Window * window, |
33 Uint32 flags); | 33 Uint32 flags); |
34 static int SDL_DUMMY_CreateTexture(SDL_Renderer * renderer, | |
35 SDL_Texture * texture); | |
36 static int SDL_DUMMY_QueryTexturePixels(SDL_Renderer * renderer, | |
37 SDL_Texture * texture, void **pixels, | |
38 int *pitch); | |
39 static int SDL_DUMMY_SetTexturePalette(SDL_Renderer * renderer, | |
40 SDL_Texture * texture, | |
41 const SDL_Color * colors, | |
42 int firstcolor, int ncolors); | |
43 static int SDL_DUMMY_GetTexturePalette(SDL_Renderer * renderer, | |
44 SDL_Texture * texture, | |
45 SDL_Color * colors, int firstcolor, | |
46 int ncolors); | |
47 static int SDL_DUMMY_SetTextureColorMod(SDL_Renderer * renderer, | |
48 SDL_Texture * texture); | |
49 static int SDL_DUMMY_SetTextureAlphaMod(SDL_Renderer * renderer, | |
50 SDL_Texture * texture); | |
51 static int SDL_DUMMY_SetTextureBlendMode(SDL_Renderer * renderer, | |
52 SDL_Texture * texture); | |
53 static int SDL_DUMMY_SetTextureScaleMode(SDL_Renderer * renderer, | |
54 SDL_Texture * texture); | |
55 static int SDL_DUMMY_UpdateTexture(SDL_Renderer * renderer, | |
56 SDL_Texture * texture, | |
57 const SDL_Rect * rect, const void *pixels, | |
58 int pitch); | |
59 static int SDL_DUMMY_LockTexture(SDL_Renderer * renderer, | |
60 SDL_Texture * texture, const SDL_Rect * rect, | |
61 int markDirty, void **pixels, int *pitch); | |
62 static void SDL_DUMMY_UnlockTexture(SDL_Renderer * renderer, | |
63 SDL_Texture * texture); | |
64 static void SDL_DUMMY_DirtyTexture(SDL_Renderer * renderer, | |
65 SDL_Texture * texture, int numrects, | |
66 const SDL_Rect * rects); | |
67 static void SDL_DUMMY_SelectRenderTexture(SDL_Renderer * renderer, | |
68 SDL_Texture * texture); | |
69 static int SDL_DUMMY_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, | 34 static int SDL_DUMMY_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, |
70 Uint8 b, Uint8 a, const SDL_Rect * rect); | 35 Uint8 b, Uint8 a, const SDL_Rect * rect); |
71 static int SDL_DUMMY_RenderCopy(SDL_Renderer * renderer, | 36 static int SDL_DUMMY_RenderCopy(SDL_Renderer * renderer, |
72 SDL_Texture * texture, | 37 SDL_Texture * texture, |
73 const SDL_Rect * srcrect, | 38 const SDL_Rect * srcrect, |
74 const SDL_Rect * dstrect); | 39 const SDL_Rect * dstrect); |
75 static int SDL_DUMMY_RenderReadPixels(SDL_Renderer * renderer, | |
76 const SDL_Rect * rect, void *pixels, | |
77 int pitch); | |
78 static int SDL_DUMMY_RenderWritePixels(SDL_Renderer * renderer, | |
79 const SDL_Rect * rect, | |
80 const void *pixels, int pitch); | |
81 static void SDL_DUMMY_RenderPresent(SDL_Renderer * renderer); | 40 static void SDL_DUMMY_RenderPresent(SDL_Renderer * renderer); |
82 static void SDL_DUMMY_DestroyTexture(SDL_Renderer * renderer, | |
83 SDL_Texture * texture); | |
84 static void SDL_DUMMY_DestroyRenderer(SDL_Renderer * renderer); | 41 static void SDL_DUMMY_DestroyRenderer(SDL_Renderer * renderer); |
85 | 42 |
86 | 43 |
87 SDL_RenderDriver SDL_DUMMY_RenderDriver = { | 44 SDL_RenderDriver SDL_DUMMY_RenderDriver = { |
88 SDL_DUMMY_CreateRenderer, | 45 SDL_DUMMY_CreateRenderer, |
89 { | 46 { |
90 "dummy", | 47 "dummy", |
91 (SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTCOPY | | 48 (SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTCOPY | |
92 SDL_RENDERER_PRESENTFLIP2 | SDL_RENDERER_PRESENTFLIP3 | | 49 SDL_RENDERER_PRESENTFLIP2 | SDL_RENDERER_PRESENTFLIP3 | |
93 SDL_RENDERER_PRESENTDISCARD), | 50 SDL_RENDERER_PRESENTDISCARD), |
94 (SDL_TEXTUREMODULATE_NONE | SDL_TEXTUREMODULATE_COLOR | | 51 } |
95 SDL_TEXTUREMODULATE_ALPHA), | |
96 (SDL_TEXTUREBLENDMODE_NONE | SDL_TEXTUREBLENDMODE_MASK | | |
97 SDL_TEXTUREBLENDMODE_BLEND | SDL_TEXTUREBLENDMODE_ADD | | |
98 SDL_TEXTUREBLENDMODE_MOD), | |
99 (SDL_TEXTURESCALEMODE_NONE | SDL_TEXTURESCALEMODE_FAST), | |
100 11, | |
101 { | |
102 SDL_PIXELFORMAT_INDEX8, | |
103 SDL_PIXELFORMAT_RGB555, | |
104 SDL_PIXELFORMAT_RGB565, | |
105 SDL_PIXELFORMAT_RGB888, | |
106 SDL_PIXELFORMAT_BGR888, | |
107 SDL_PIXELFORMAT_ARGB8888, | |
108 SDL_PIXELFORMAT_RGBA8888, | |
109 SDL_PIXELFORMAT_ABGR8888, | |
110 SDL_PIXELFORMAT_BGRA8888, | |
111 SDL_PIXELFORMAT_YUY2, | |
112 SDL_PIXELFORMAT_UYVY}, | |
113 0, | |
114 0} | |
115 }; | 52 }; |
116 | 53 |
117 typedef struct | 54 typedef struct |
118 { | 55 { |
119 int current_screen; | 56 int current_screen; |
149 SDL_OutOfMemory(); | 86 SDL_OutOfMemory(); |
150 return NULL; | 87 return NULL; |
151 } | 88 } |
152 SDL_zerop(data); | 89 SDL_zerop(data); |
153 | 90 |
154 renderer->CreateTexture = SDL_DUMMY_CreateTexture; | |
155 renderer->QueryTexturePixels = SDL_DUMMY_QueryTexturePixels; | |
156 renderer->SetTexturePalette = SDL_DUMMY_SetTexturePalette; | |
157 renderer->GetTexturePalette = SDL_DUMMY_GetTexturePalette; | |
158 renderer->SetTextureColorMod = SDL_DUMMY_SetTextureColorMod; | |
159 renderer->SetTextureAlphaMod = SDL_DUMMY_SetTextureAlphaMod; | |
160 renderer->SetTextureBlendMode = SDL_DUMMY_SetTextureBlendMode; | |
161 renderer->SetTextureScaleMode = SDL_DUMMY_SetTextureScaleMode; | |
162 renderer->UpdateTexture = SDL_DUMMY_UpdateTexture; | |
163 renderer->LockTexture = SDL_DUMMY_LockTexture; | |
164 renderer->UnlockTexture = SDL_DUMMY_UnlockTexture; | |
165 renderer->DirtyTexture = SDL_DUMMY_DirtyTexture; | |
166 renderer->RenderFill = SDL_DUMMY_RenderFill; | 91 renderer->RenderFill = SDL_DUMMY_RenderFill; |
167 renderer->RenderCopy = SDL_DUMMY_RenderCopy; | 92 renderer->RenderCopy = SDL_DUMMY_RenderCopy; |
168 renderer->RenderPresent = SDL_DUMMY_RenderPresent; | 93 renderer->RenderPresent = SDL_DUMMY_RenderPresent; |
169 renderer->DestroyTexture = SDL_DUMMY_DestroyTexture; | |
170 renderer->DestroyRenderer = SDL_DUMMY_DestroyRenderer; | 94 renderer->DestroyRenderer = SDL_DUMMY_DestroyRenderer; |
171 renderer->info = SDL_DUMMY_RenderDriver.info; | 95 renderer->info.name = SDL_DUMMY_RenderDriver.info.name; |
96 renderer->info.flags = 0; | |
172 renderer->window = window->id; | 97 renderer->window = window->id; |
173 renderer->driverdata = data; | 98 renderer->driverdata = data; |
174 | 99 Setup_SoftwareRenderer(renderer); |
175 renderer->info.flags = 0; | |
176 | 100 |
177 if (flags & SDL_RENDERER_PRESENTFLIP2) { | 101 if (flags & SDL_RENDERER_PRESENTFLIP2) { |
178 renderer->info.flags |= SDL_RENDERER_PRESENTFLIP2; | 102 renderer->info.flags |= SDL_RENDERER_PRESENTFLIP2; |
179 n = 2; | 103 n = 2; |
180 } else if (flags & SDL_RENDERER_PRESENTFLIP3) { | 104 } else if (flags & SDL_RENDERER_PRESENTFLIP3) { |
195 SDL_SetSurfacePalette(data->screens[i], display->palette); | 119 SDL_SetSurfacePalette(data->screens[i], display->palette); |
196 } | 120 } |
197 data->current_screen = 0; | 121 data->current_screen = 0; |
198 | 122 |
199 return renderer; | 123 return renderer; |
200 } | |
201 | |
202 static int | |
203 SDL_DUMMY_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture) | |
204 { | |
205 if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) { | |
206 texture->driverdata = SDL_SW_CreateYUVTexture(texture); | |
207 } else { | |
208 int bpp; | |
209 Uint32 Rmask, Gmask, Bmask, Amask; | |
210 | |
211 if (!SDL_PixelFormatEnumToMasks | |
212 (texture->format, &bpp, &Rmask, &Gmask, &Bmask, &Amask)) { | |
213 SDL_SetError("Unknown texture format"); | |
214 return -1; | |
215 } | |
216 | |
217 texture->driverdata = | |
218 SDL_CreateRGBSurface(0, texture->w, texture->h, bpp, Rmask, Gmask, | |
219 Bmask, Amask); | |
220 } | |
221 | |
222 if (!texture->driverdata) { | |
223 return -1; | |
224 } | |
225 return 0; | |
226 } | |
227 | |
228 static int | |
229 SDL_DUMMY_QueryTexturePixels(SDL_Renderer * renderer, SDL_Texture * texture, | |
230 void **pixels, int *pitch) | |
231 { | |
232 if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) { | |
233 return SDL_SW_QueryYUVTexturePixels((SDL_SW_YUVTexture *) texture-> | |
234 driverdata, pixels, pitch); | |
235 } else { | |
236 SDL_Surface *surface = (SDL_Surface *) texture->driverdata; | |
237 | |
238 *pixels = surface->pixels; | |
239 *pitch = surface->pitch; | |
240 return 0; | |
241 } | |
242 } | |
243 | |
244 static int | |
245 SDL_DUMMY_SetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture, | |
246 const SDL_Color * colors, int firstcolor, | |
247 int ncolors) | |
248 { | |
249 if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) { | |
250 SDL_SetError("YUV textures don't have a palette"); | |
251 return -1; | |
252 } else { | |
253 SDL_Surface *surface = (SDL_Surface *) texture->driverdata; | |
254 | |
255 return SDL_SetPaletteColors(surface->format->palette, colors, | |
256 firstcolor, ncolors); | |
257 } | |
258 } | |
259 | |
260 static int | |
261 SDL_DUMMY_GetTexturePalette(SDL_Renderer * renderer, SDL_Texture * texture, | |
262 SDL_Color * colors, int firstcolor, int ncolors) | |
263 { | |
264 if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) { | |
265 SDL_SetError("YUV textures don't have a palette"); | |
266 return -1; | |
267 } else { | |
268 SDL_Surface *surface = (SDL_Surface *) texture->driverdata; | |
269 | |
270 SDL_memcpy(colors, &surface->format->palette->colors[firstcolor], | |
271 ncolors * sizeof(*colors)); | |
272 return 0; | |
273 } | |
274 } | |
275 | |
276 static void | |
277 SDL_DUMMY_UpdateRenderCopyFunc(SDL_Renderer * renderer, SDL_Texture * texture) | |
278 { | |
279 SDL_Window *window = SDL_GetWindowFromID(renderer->window); | |
280 SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window); | |
281 SDL_Surface *surface = (SDL_Surface *) texture->driverdata; | |
282 | |
283 /* We only need a special copy function for advanced features */ | |
284 if (texture->modMode | |
285 || (texture-> | |
286 blendMode & (SDL_TEXTUREBLENDMODE_ADD | SDL_TEXTUREBLENDMODE_MOD)) | |
287 || texture->scaleMode) { | |
288 surface->userdata = | |
289 SDL_GetRenderCopyFunc(texture->format, | |
290 display->current_mode.format, | |
291 texture->modMode, texture->blendMode, | |
292 texture->scaleMode); | |
293 } else { | |
294 surface->userdata = NULL; | |
295 } | |
296 } | |
297 | |
298 static int | |
299 SDL_DUMMY_SetTextureColorMod(SDL_Renderer * renderer, SDL_Texture * texture) | |
300 { | |
301 SDL_DUMMY_UpdateRenderCopyFunc(renderer, texture); | |
302 return 0; | |
303 } | |
304 | |
305 static int | |
306 SDL_DUMMY_SetTextureAlphaMod(SDL_Renderer * renderer, SDL_Texture * texture) | |
307 { | |
308 SDL_DUMMY_UpdateRenderCopyFunc(renderer, texture); | |
309 return 0; | |
310 } | |
311 | |
312 static int | |
313 SDL_DUMMY_SetTextureBlendMode(SDL_Renderer * renderer, SDL_Texture * texture) | |
314 { | |
315 switch (texture->blendMode) { | |
316 case SDL_TEXTUREBLENDMODE_NONE: | |
317 case SDL_TEXTUREBLENDMODE_MASK: | |
318 case SDL_TEXTUREBLENDMODE_BLEND: | |
319 case SDL_TEXTUREBLENDMODE_ADD: | |
320 case SDL_TEXTUREBLENDMODE_MOD: | |
321 SDL_DUMMY_UpdateRenderCopyFunc(renderer, texture); | |
322 return 0; | |
323 default: | |
324 SDL_Unsupported(); | |
325 texture->blendMode = SDL_TEXTUREBLENDMODE_NONE; | |
326 SDL_DUMMY_UpdateRenderCopyFunc(renderer, texture); | |
327 return -1; | |
328 } | |
329 } | |
330 | |
331 static int | |
332 SDL_DUMMY_SetTextureScaleMode(SDL_Renderer * renderer, SDL_Texture * texture) | |
333 { | |
334 switch (texture->scaleMode) { | |
335 case SDL_TEXTURESCALEMODE_NONE: | |
336 case SDL_TEXTURESCALEMODE_FAST: | |
337 SDL_DUMMY_UpdateRenderCopyFunc(renderer, texture); | |
338 return 0; | |
339 case SDL_TEXTURESCALEMODE_SLOW: | |
340 case SDL_TEXTURESCALEMODE_BEST: | |
341 SDL_Unsupported(); | |
342 texture->scaleMode = SDL_TEXTURESCALEMODE_FAST; | |
343 SDL_DUMMY_UpdateRenderCopyFunc(renderer, texture); | |
344 return -1; | |
345 default: | |
346 SDL_Unsupported(); | |
347 texture->scaleMode = SDL_TEXTURESCALEMODE_NONE; | |
348 SDL_DUMMY_UpdateRenderCopyFunc(renderer, texture); | |
349 return -1; | |
350 } | |
351 } | |
352 | |
353 static int | |
354 SDL_DUMMY_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture, | |
355 const SDL_Rect * rect, const void *pixels, int pitch) | |
356 { | |
357 if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) { | |
358 return SDL_SW_UpdateYUVTexture((SDL_SW_YUVTexture *) texture-> | |
359 driverdata, rect, pixels, pitch); | |
360 } else { | |
361 SDL_Surface *surface = (SDL_Surface *) texture->driverdata; | |
362 Uint8 *src, *dst; | |
363 int row; | |
364 size_t length; | |
365 | |
366 src = (Uint8 *) pixels; | |
367 dst = | |
368 (Uint8 *) surface->pixels + rect->y * surface->pitch + | |
369 rect->x * surface->format->BytesPerPixel; | |
370 length = rect->w * surface->format->BytesPerPixel; | |
371 for (row = 0; row < rect->h; ++row) { | |
372 SDL_memcpy(dst, src, length); | |
373 src += pitch; | |
374 dst += surface->pitch; | |
375 } | |
376 return 0; | |
377 } | |
378 } | |
379 | |
380 static int | |
381 SDL_DUMMY_LockTexture(SDL_Renderer * renderer, SDL_Texture * texture, | |
382 const SDL_Rect * rect, int markDirty, void **pixels, | |
383 int *pitch) | |
384 { | |
385 if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) { | |
386 return SDL_SW_LockYUVTexture((SDL_SW_YUVTexture *) texture-> | |
387 driverdata, rect, markDirty, pixels, | |
388 pitch); | |
389 } else { | |
390 SDL_Surface *surface = (SDL_Surface *) texture->driverdata; | |
391 | |
392 *pixels = | |
393 (void *) ((Uint8 *) surface->pixels + rect->y * surface->pitch + | |
394 rect->x * surface->format->BytesPerPixel); | |
395 *pitch = surface->pitch; | |
396 return 0; | |
397 } | |
398 } | |
399 | |
400 static void | |
401 SDL_DUMMY_UnlockTexture(SDL_Renderer * renderer, SDL_Texture * texture) | |
402 { | |
403 if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) { | |
404 SDL_SW_UnlockYUVTexture((SDL_SW_YUVTexture *) texture->driverdata); | |
405 } | |
406 } | |
407 | |
408 static void | |
409 SDL_DUMMY_DirtyTexture(SDL_Renderer * renderer, SDL_Texture * texture, | |
410 int numrects, const SDL_Rect * rects) | |
411 { | |
412 } | 124 } |
413 | 125 |
414 static int | 126 static int |
415 SDL_DUMMY_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, Uint8 b, | 127 SDL_DUMMY_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, Uint8 b, |
416 Uint8 a, const SDL_Rect * rect) | 128 Uint8 a, const SDL_Rect * rect) |
520 data->current_screen = (data->current_screen + 1) % 3; | 232 data->current_screen = (data->current_screen + 1) % 3; |
521 } | 233 } |
522 } | 234 } |
523 | 235 |
524 static void | 236 static void |
525 SDL_DUMMY_DestroyTexture(SDL_Renderer * renderer, SDL_Texture * texture) | |
526 { | |
527 if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) { | |
528 SDL_SW_DestroyYUVTexture((SDL_SW_YUVTexture *) texture->driverdata); | |
529 } else { | |
530 SDL_Surface *surface = (SDL_Surface *) texture->driverdata; | |
531 | |
532 SDL_FreeSurface(surface); | |
533 } | |
534 } | |
535 | |
536 static void | |
537 SDL_DUMMY_DestroyRenderer(SDL_Renderer * renderer) | 237 SDL_DUMMY_DestroyRenderer(SDL_Renderer * renderer) |
538 { | 238 { |
539 SDL_DUMMY_RenderData *data = | 239 SDL_DUMMY_RenderData *data = |
540 (SDL_DUMMY_RenderData *) renderer->driverdata; | 240 (SDL_DUMMY_RenderData *) renderer->driverdata; |
541 int i; | 241 int i; |