comparison src/video/dummy/SDL_nullrender.c @ 1985:8055185ae4ed

Added source color and alpha modulation support. Added perl script to generate optimized render copy functions.
author Sam Lantinga <slouken@libsdl.org>
date Mon, 28 Aug 2006 03:17:39 +0000
parents a788656ca29a
children 7b573c59cb1f
comparison
equal deleted inserted replaced
1984:b910bcabec26 1985:8055185ae4ed
22 #include "SDL_config.h" 22 #include "SDL_config.h"
23 23
24 #include "SDL_video.h" 24 #include "SDL_video.h"
25 #include "../SDL_sysvideo.h" 25 #include "../SDL_sysvideo.h"
26 #include "../SDL_yuv_sw_c.h" 26 #include "../SDL_yuv_sw_c.h"
27 #include "../SDL_rendercopy.h"
27 28
28 29
29 /* SDL surface based renderer implementation */ 30 /* SDL surface based renderer implementation */
30 31
31 static SDL_Renderer *SDL_DUMMY_CreateRenderer(SDL_Window * window, 32 static SDL_Renderer *SDL_DUMMY_CreateRenderer(SDL_Window * window,
41 int firstcolor, int ncolors); 42 int firstcolor, int ncolors);
42 static int SDL_DUMMY_GetTexturePalette(SDL_Renderer * renderer, 43 static int SDL_DUMMY_GetTexturePalette(SDL_Renderer * renderer,
43 SDL_Texture * texture, 44 SDL_Texture * texture,
44 SDL_Color * colors, int firstcolor, 45 SDL_Color * colors, int firstcolor,
45 int ncolors); 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);
46 static int SDL_DUMMY_UpdateTexture(SDL_Renderer * renderer, 55 static int SDL_DUMMY_UpdateTexture(SDL_Renderer * renderer,
47 SDL_Texture * texture, 56 SDL_Texture * texture,
48 const SDL_Rect * rect, const void *pixels, 57 const SDL_Rect * rect, const void *pixels,
49 int pitch); 58 int pitch);
50 static int SDL_DUMMY_LockTexture(SDL_Renderer * renderer, 59 static int SDL_DUMMY_LockTexture(SDL_Renderer * renderer,
55 static void SDL_DUMMY_DirtyTexture(SDL_Renderer * renderer, 64 static void SDL_DUMMY_DirtyTexture(SDL_Renderer * renderer,
56 SDL_Texture * texture, int numrects, 65 SDL_Texture * texture, int numrects,
57 const SDL_Rect * rects); 66 const SDL_Rect * rects);
58 static void SDL_DUMMY_SelectRenderTexture(SDL_Renderer * renderer, 67 static void SDL_DUMMY_SelectRenderTexture(SDL_Renderer * renderer,
59 SDL_Texture * texture); 68 SDL_Texture * texture);
60 static int SDL_DUMMY_RenderFill(SDL_Renderer * renderer, 69 static int SDL_DUMMY_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g,
61 const SDL_Rect * rect, Uint32 color); 70 Uint8 b, Uint8 a, const SDL_Rect * rect);
62 static int SDL_DUMMY_RenderCopy(SDL_Renderer * renderer, 71 static int SDL_DUMMY_RenderCopy(SDL_Renderer * renderer,
63 SDL_Texture * texture, 72 SDL_Texture * texture,
64 const SDL_Rect * srcrect, 73 const SDL_Rect * srcrect,
65 const SDL_Rect * dstrect, int blendMode, 74 const SDL_Rect * dstrect);
66 int scaleMode);
67 static int SDL_DUMMY_RenderReadPixels(SDL_Renderer * renderer, 75 static int SDL_DUMMY_RenderReadPixels(SDL_Renderer * renderer,
68 const SDL_Rect * rect, void *pixels, 76 const SDL_Rect * rect, void *pixels,
69 int pitch); 77 int pitch);
70 static int SDL_DUMMY_RenderWritePixels(SDL_Renderer * renderer, 78 static int SDL_DUMMY_RenderWritePixels(SDL_Renderer * renderer,
71 const SDL_Rect * rect, 79 const SDL_Rect * rect,
81 { 89 {
82 "dummy", 90 "dummy",
83 (SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTCOPY | 91 (SDL_RENDERER_SINGLEBUFFER | SDL_RENDERER_PRESENTCOPY |
84 SDL_RENDERER_PRESENTFLIP2 | SDL_RENDERER_PRESENTFLIP3 | 92 SDL_RENDERER_PRESENTFLIP2 | SDL_RENDERER_PRESENTFLIP3 |
85 SDL_RENDERER_PRESENTDISCARD), 93 SDL_RENDERER_PRESENTDISCARD),
94 (SDL_TEXTUREMODULATE_NONE | SDL_TEXTUREMODULATE_COLOR |
95 SDL_TEXTUREMODULATE_ALPHA),
86 (SDL_TEXTUREBLENDMODE_NONE | SDL_TEXTUREBLENDMODE_MASK | 96 (SDL_TEXTUREBLENDMODE_NONE | SDL_TEXTUREBLENDMODE_MASK |
87 SDL_TEXTUREBLENDMODE_BLEND), 97 SDL_TEXTUREBLENDMODE_BLEND | SDL_TEXTUREBLENDMODE_ADD |
98 SDL_TEXTUREBLENDMODE_MOD),
88 (SDL_TEXTURESCALEMODE_NONE | SDL_TEXTURESCALEMODE_FAST), 99 (SDL_TEXTURESCALEMODE_NONE | SDL_TEXTURESCALEMODE_FAST),
89 11, 100 11,
90 { 101 {
91 SDL_PIXELFORMAT_INDEX8, 102 SDL_PIXELFORMAT_INDEX8,
92 SDL_PIXELFORMAT_RGB555, 103 SDL_PIXELFORMAT_RGB555,
142 153
143 renderer->CreateTexture = SDL_DUMMY_CreateTexture; 154 renderer->CreateTexture = SDL_DUMMY_CreateTexture;
144 renderer->QueryTexturePixels = SDL_DUMMY_QueryTexturePixels; 155 renderer->QueryTexturePixels = SDL_DUMMY_QueryTexturePixels;
145 renderer->SetTexturePalette = SDL_DUMMY_SetTexturePalette; 156 renderer->SetTexturePalette = SDL_DUMMY_SetTexturePalette;
146 renderer->GetTexturePalette = SDL_DUMMY_GetTexturePalette; 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;
147 renderer->UpdateTexture = SDL_DUMMY_UpdateTexture; 162 renderer->UpdateTexture = SDL_DUMMY_UpdateTexture;
148 renderer->LockTexture = SDL_DUMMY_LockTexture; 163 renderer->LockTexture = SDL_DUMMY_LockTexture;
149 renderer->UnlockTexture = SDL_DUMMY_UnlockTexture; 164 renderer->UnlockTexture = SDL_DUMMY_UnlockTexture;
150 renderer->DirtyTexture = SDL_DUMMY_DirtyTexture; 165 renderer->DirtyTexture = SDL_DUMMY_DirtyTexture;
151 renderer->RenderFill = SDL_DUMMY_RenderFill; 166 renderer->RenderFill = SDL_DUMMY_RenderFill;
256 ncolors * sizeof(*colors)); 271 ncolors * sizeof(*colors));
257 return 0; 272 return 0;
258 } 273 }
259 } 274 }
260 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 surface->userdata =
284 SDL_GetRenderCopyFunc(texture->format, display->current_mode.format,
285 texture->modMode, texture->blendMode,
286 texture->scaleMode);
287 }
288
289 static int
290 SDL_DUMMY_SetTextureColorMod(SDL_Renderer * renderer, SDL_Texture * texture)
291 {
292 SDL_DUMMY_UpdateRenderCopyFunc(renderer, texture);
293 return 0;
294 }
295
296 static int
297 SDL_DUMMY_SetTextureAlphaMod(SDL_Renderer * renderer, SDL_Texture * texture)
298 {
299 SDL_DUMMY_UpdateRenderCopyFunc(renderer, texture);
300 return 0;
301 }
302
303 static int
304 SDL_DUMMY_SetTextureBlendMode(SDL_Renderer * renderer, SDL_Texture * texture)
305 {
306 switch (texture->blendMode) {
307 case SDL_TEXTUREBLENDMODE_NONE:
308 case SDL_TEXTUREBLENDMODE_MASK:
309 case SDL_TEXTUREBLENDMODE_BLEND:
310 case SDL_TEXTUREBLENDMODE_ADD:
311 case SDL_TEXTUREBLENDMODE_MOD:
312 SDL_DUMMY_UpdateRenderCopyFunc(renderer, texture);
313 return 0;
314 default:
315 SDL_Unsupported();
316 texture->blendMode = SDL_TEXTUREBLENDMODE_NONE;
317 SDL_DUMMY_UpdateRenderCopyFunc(renderer, texture);
318 return -1;
319 }
320 }
321
322 static int
323 SDL_DUMMY_SetTextureScaleMode(SDL_Renderer * renderer, SDL_Texture * texture)
324 {
325 switch (texture->scaleMode) {
326 case SDL_TEXTURESCALEMODE_NONE:
327 case SDL_TEXTURESCALEMODE_FAST:
328 SDL_DUMMY_UpdateRenderCopyFunc(renderer, texture);
329 return 0;
330 case SDL_TEXTURESCALEMODE_SLOW:
331 case SDL_TEXTURESCALEMODE_BEST:
332 SDL_Unsupported();
333 texture->scaleMode = SDL_TEXTURESCALEMODE_FAST;
334 SDL_DUMMY_UpdateRenderCopyFunc(renderer, texture);
335 return -1;
336 default:
337 SDL_Unsupported();
338 texture->scaleMode = SDL_TEXTURESCALEMODE_NONE;
339 SDL_DUMMY_UpdateRenderCopyFunc(renderer, texture);
340 return -1;
341 }
342 }
343
261 static int 344 static int
262 SDL_DUMMY_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture, 345 SDL_DUMMY_UpdateTexture(SDL_Renderer * renderer, SDL_Texture * texture,
263 const SDL_Rect * rect, const void *pixels, int pitch) 346 const SDL_Rect * rect, const void *pixels, int pitch)
264 { 347 {
265 if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) { 348 if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
318 int numrects, const SDL_Rect * rects) 401 int numrects, const SDL_Rect * rects)
319 { 402 {
320 } 403 }
321 404
322 static int 405 static int
323 SDL_DUMMY_RenderFill(SDL_Renderer * renderer, const SDL_Rect * rect, 406 SDL_DUMMY_RenderFill(SDL_Renderer * renderer, Uint8 r, Uint8 g, Uint8 b,
324 Uint32 color) 407 Uint8 a, const SDL_Rect * rect)
325 { 408 {
326 SDL_DUMMY_RenderData *data = 409 SDL_DUMMY_RenderData *data =
327 (SDL_DUMMY_RenderData *) renderer->driverdata; 410 (SDL_DUMMY_RenderData *) renderer->driverdata;
328 SDL_Surface *target = data->screens[data->current_screen]; 411 SDL_Surface *target = data->screens[data->current_screen];
412 Uint32 color;
329 SDL_Rect real_rect = *rect; 413 SDL_Rect real_rect = *rect;
330 Uint8 r, g, b, a; 414
331
332 a = (Uint8) ((color >> 24) & 0xFF);
333 r = (Uint8) ((color >> 16) & 0xFF);
334 g = (Uint8) ((color >> 8) & 0xFF);
335 b = (Uint8) (color & 0xFF);
336 color = SDL_MapRGBA(target->format, r, g, b, a); 415 color = SDL_MapRGBA(target->format, r, g, b, a);
337 416
338 return SDL_FillRect(target, &real_rect, color); 417 return SDL_FillRect(target, &real_rect, color);
339 } 418 }
340 419
341 static int 420 static int
342 SDL_DUMMY_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture, 421 SDL_DUMMY_RenderCopy(SDL_Renderer * renderer, SDL_Texture * texture,
343 const SDL_Rect * srcrect, const SDL_Rect * dstrect, 422 const SDL_Rect * srcrect, const SDL_Rect * dstrect)
344 int blendMode, int scaleMode)
345 { 423 {
346 SDL_DUMMY_RenderData *data = 424 SDL_DUMMY_RenderData *data =
347 (SDL_DUMMY_RenderData *) renderer->driverdata; 425 (SDL_DUMMY_RenderData *) renderer->driverdata;
348 SDL_Window *window = SDL_GetWindowFromID(renderer->window); 426 SDL_Window *window = SDL_GetWindowFromID(renderer->window);
349 SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window); 427 SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
358 dstrect->w, dstrect->h, pixels, 436 dstrect->w, dstrect->h, pixels,
359 target->pitch); 437 target->pitch);
360 } else { 438 } else {
361 SDL_Surface *surface = (SDL_Surface *) texture->driverdata; 439 SDL_Surface *surface = (SDL_Surface *) texture->driverdata;
362 SDL_Surface *target = data->screens[data->current_screen]; 440 SDL_Surface *target = data->screens[data->current_screen];
363 SDL_Rect real_srcrect = *srcrect; 441 SDL_RenderCopyFunc copyfunc = (SDL_RenderCopyFunc) surface->userdata;
364 SDL_Rect real_dstrect = *dstrect; 442
365 443 if (copyfunc) {
366 if (blendMode & 444 SDL_RenderCopyData copydata;
367 (SDL_TEXTUREBLENDMODE_MASK | SDL_TEXTUREBLENDMODE_BLEND)) { 445
368 SDL_SetAlpha(surface, SDL_SRCALPHA, 0); 446 copydata.src =
447 (Uint8 *) surface->pixels + srcrect->y * surface->pitch +
448 srcrect->x * surface->format->BytesPerPixel;
449 copydata.src_w = srcrect->w;
450 copydata.src_h = srcrect->h;
451 copydata.src_pitch = surface->pitch;
452 copydata.dst =
453 (Uint8 *) target->pixels + dstrect->y * target->pitch +
454 dstrect->x * target->format->BytesPerPixel;
455 copydata.dst_w = dstrect->w;
456 copydata.dst_h = dstrect->h;
457 copydata.dst_pitch = target->pitch;
458 copydata.flags = 0;
459 if (texture->modMode & SDL_TEXTUREMODULATE_COLOR) {
460 copydata.flags |= SDL_RENDERCOPY_MODULATE_COLOR;
461 copydata.r = texture->r;
462 copydata.g = texture->g;
463 copydata.b = texture->b;
464 }
465 if (texture->modMode & SDL_TEXTUREMODULATE_ALPHA) {
466 copydata.flags |= SDL_RENDERCOPY_MODULATE_ALPHA;
467 copydata.a = texture->a;
468 }
469 if (texture->
470 blendMode & (SDL_TEXTUREBLENDMODE_MASK |
471 SDL_TEXTUREBLENDMODE_BLEND)) {
472 copydata.flags |= SDL_RENDERCOPY_BLEND;
473 } else if (texture->blendMode & SDL_TEXTUREBLENDMODE_ADD) {
474 copydata.flags |= SDL_RENDERCOPY_ADD;
475 } else if (texture->blendMode & SDL_TEXTUREBLENDMODE_MOD) {
476 copydata.flags |= SDL_RENDERCOPY_MOD;
477 }
478 if (texture->scaleMode) {
479 copydata.flags |= SDL_RENDERCOPY_NEAREST;
480 }
481 return copyfunc(&copydata);
369 } else { 482 } else {
370 SDL_SetAlpha(surface, 0, 0); 483 SDL_Rect real_srcrect = *srcrect;
371 } 484 SDL_Rect real_dstrect = *dstrect;
372 if (scaleMode != SDL_TEXTURESCALEMODE_NONE && 485
373 (srcrect->w != dstrect->w || srcrect->h != dstrect->h)) {
374 return SDL_SoftStretch(surface, &real_srcrect, target,
375 &real_dstrect);
376 } else {
377 return SDL_LowerBlit(surface, &real_srcrect, target, 486 return SDL_LowerBlit(surface, &real_srcrect, target,
378 &real_dstrect); 487 &real_dstrect);
379 } 488 }
380 } 489 }
381 } 490 }