src/blitter/32bpp_simple.cpp
changeset 7465 6f991aac5551
parent 7455 d6f39d44664b
child 8048 7547093f21e6
equal deleted inserted replaced
7464:1ea676d8eb7d 7465:6f991aac5551
     4 #include "../debug.h"
     4 #include "../debug.h"
     5 #include "../table/sprites.h"
     5 #include "../table/sprites.h"
     6 #include "32bpp_simple.hpp"
     6 #include "32bpp_simple.hpp"
     7 
     7 
     8 static FBlitter_32bppSimple iFBlitter_32bppSimple;
     8 static FBlitter_32bppSimple iFBlitter_32bppSimple;
     9 
       
    10 /**
       
    11  * Compose a color based on RGB values.
       
    12  */
       
    13 static inline uint ComposeColor(uint r, uint g, uint b)
       
    14 {
       
    15 	return (r & 0xFF) << 16 | (g & 0xFF) << 8 | (b & 0xFF) << 0;
       
    16 }
       
    17 
       
    18 /**
       
    19  * Compose a color based on RGBA values and the current pixel value.
       
    20  */
       
    21 static inline uint ComposeColorRGBA(uint r, uint g, uint b, uint a, uint current)
       
    22 {
       
    23 	uint cr, cg, cb;
       
    24 	cr = GB(current, 16, 8);
       
    25 	cg = GB(current, 8,  8);
       
    26 	cb = GB(current, 0,  8);
       
    27 
       
    28 	return ComposeColor((r * a + cr * (255 - a)) / 255,
       
    29 											(g * a + cg * (255 - a)) / 255,
       
    30 											(b * a + cb * (255 - a)) / 255);
       
    31 }
       
    32 
       
    33 /**
       
    34  * Compose a color based on Pixel value, alpha value, and the current pixel value.
       
    35  */
       
    36 static inline uint ComposeColorPA(uint color, uint a, uint current)
       
    37 {
       
    38 	uint r, g, b, cr, cg, cb;
       
    39 	r  = GB(color,   16, 8);
       
    40 	g  = GB(color,   8,  8);
       
    41 	b  = GB(color,   0,  8);
       
    42 	cr = GB(current, 16, 8);
       
    43 	cg = GB(current, 8,  8);
       
    44 	cb = GB(current, 0,  8);
       
    45 
       
    46 	return ComposeColor((r * a + cr * (255 - a)) / 255,
       
    47 											(g * a + cg * (255 - a)) / 255,
       
    48 											(b * a + cb * (255 - a)) / 255);
       
    49 }
       
    50 
       
    51 /**
       
    52  * Make a pixel looks like it is transparent.
       
    53  * @param color the color already on the screen.
       
    54  * @param amount the amount of transparency, times 100.
       
    55  * @return the new color for the screen.
       
    56  */
       
    57 static inline uint MakeTransparent(uint color, uint amount)
       
    58 {
       
    59 	uint r, g, b;
       
    60 	r = GB(color, 16, 8);
       
    61 	g = GB(color, 8,  8);
       
    62 	b = GB(color, 0,  8);
       
    63 
       
    64 	return ComposeColor(r * amount / 100, g * amount / 100, b * amount / 100);
       
    65 }
       
    66 
       
    67 /**
       
    68  * Make a color grey-based.
       
    69  * @param color the color to make grey.
       
    70  * @return the new color, now grey.
       
    71  */
       
    72 static inline uint MakeGrey(uint color)
       
    73 {
       
    74 	uint r, g, b;
       
    75 	r = GB(color, 16, 8);
       
    76 	g = GB(color, 8,  8);
       
    77 	b = GB(color, 0,  8);
       
    78 
       
    79 	/* To avoid doubles and stuff, multiple it with a total of 65536 (16bits), then
       
    80 	 *  divide by it to normalize the value to a byte again. See heightmap.cpp for
       
    81 	 *  information about the formula. */
       
    82 	color = ((r * 19595) + (g * 38470) + (b * 7471)) / 65536;
       
    83 
       
    84 	return ComposeColor(color, color, color);
       
    85 }
       
    86 
     9 
    87 void Blitter_32bppSimple::Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom)
    10 void Blitter_32bppSimple::Draw(Blitter::BlitterParams *bp, BlitterMode mode, ZoomLevel zoom)
    88 {
    11 {
    89 	const SpriteLoader::CommonPixel *src, *src_line;
    12 	const SpriteLoader::CommonPixel *src, *src_line;
    90 	uint32 *dst, *dst_line;
    13 	uint32 *dst, *dst_line;
   103 		for (int x = 0; x < bp->width; x++) {
    26 		for (int x = 0; x < bp->width; x++) {
   104 			switch (mode) {
    27 			switch (mode) {
   105 				case BM_COLOUR_REMAP:
    28 				case BM_COLOUR_REMAP:
   106 					/* In case the m-channel is zero, do not remap this pixel in any way */
    29 					/* In case the m-channel is zero, do not remap this pixel in any way */
   107 					if (src->m == 0) {
    30 					if (src->m == 0) {
   108 						if (src->a != 0) *dst = ComposeColorRGBA(src->r, src->g, src->b, src->a, *dst);
    31 						if (src->a != 0) *dst = ComposeColourRGBA(src->r, src->g, src->b, src->a, *dst);
   109 					} else {
    32 					} else {
   110 						if (bp->remap[src->m] != 0) *dst = ComposeColorPA(this->LookupColourInPalette(bp->remap[src->m]), src->a, *dst);
    33 						if (bp->remap[src->m] != 0) *dst = ComposeColourPA(this->LookupColourInPalette(bp->remap[src->m]), src->a, *dst);
   111 					}
    34 					}
   112 					break;
    35 					break;
   113 
    36 
   114 				case BM_TRANSPARENT:
    37 				case BM_TRANSPARENT:
   115 					/* TODO -- We make an assumption here that the remap in fact is transparency, not some color.
    38 					/* TODO -- We make an assumption here that the remap in fact is transparency, not some color.
   119 					/* Make the current color a bit more black, so it looks like this image is transparent */
    42 					/* Make the current color a bit more black, so it looks like this image is transparent */
   120 					if (src->a != 0) *dst = MakeTransparent(*dst, 75);
    43 					if (src->a != 0) *dst = MakeTransparent(*dst, 75);
   121 					break;
    44 					break;
   122 
    45 
   123 				default:
    46 				default:
   124 					if (src->a != 0) *dst = ComposeColorRGBA(src->r, src->g, src->b, src->a, *dst);
    47 					if (src->a != 0) *dst = ComposeColourRGBA(src->r, src->g, src->b, src->a, *dst);
   125 					break;
    48 					break;
   126 			}
    49 			}
   127 			dst++;
    50 			dst++;
   128 			src += ScaleByZoom(1, zoom);
    51 			src += ScaleByZoom(1, zoom);
   129 		}
    52 		}