⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 palette.c

📁 十七种模拟器源代码 非常有用的作课程设计不可缺少的
💻 C
📖 第 1 页 / 共 4 页
字号:
/******************************************************************************//*                                                                            *//*                          RAINE COLOUR MAPPING                              *//*                                                                            *//******************************************************************************/#include <limits.h>#include "raine.h"#include "gameinc.h"#include "palette.h"#include "blitasm.h"/*Closest Colour finder (when there are no free pens)*/static void closest_colour_init(void);static UINT8 closest_colour(int r, int g, int b);/*Data*/enum map_type{   PMAP_NONE = 0,   PMAP_MAPPED,   PMAP_DIRECT,};static UINT8 map_mode = PMAP_NONE;UINT8 *RAM_PAL;		// points to source colour ramstatic UINT8 *colour_ram_src[MAX_COLBANKS];static UINT8 *bankmap;static UINT8 *cloc;static UINT32 res_pens;		// number of pens reserved in the palette (0-255)static UINT32 talc;		// next free pen in the palette (0-255)static UINT8 *cloc_2;		// cloc data from last framestatic UINT8 *pmap;		// list of which pens are allocated (0=free 1=used)static UINT8 *pmap_2;		// pmap data from last framestatic UINT16 pal_banks;		// number of colour banks the hardware supportsstatic UINT32 map_size;static UINT32 bank_cols;#ifdef TRIPLE_BUFFERstatic UINT8 *cloc_2a;		// extra bufferstatic UINT8 *pmap_2a;		// extra buffer#else#define cloc_2a	cloc_2#define pmap_2a	pmap_2#endifstatic PALETTE pal_screen;static UINT8 white_pen;void InitPaletteMap(UINT8 *src, int banks, int bankcols, int mapsize){   int ta;   int bmsize;   map_mode = PMAP_MAPPED;   RAM_PAL = src;   pal_banks = banks;   bank_cols = bankcols;   map_size  = mapsize;      bmsize = pal_banks * bankcols * (internal_bpp(display_cfg.bpp) / 8);   if (!(bankmap = AllocateMem(bmsize ))) return;   memset(bankmap,0,bmsize); // For MAP_PALETTE_MULTI...      if (!(cloc    = AllocateMem(map_size))) return;   if (!(cloc_2  = AllocateMem(map_size))) return;#ifdef TRIPLE_BUFFER   if (!(cloc_2a = AllocateMem(map_size))) return;#endif   if (!(pmap    = AllocateMem(256))) return;   if (!(pmap_2  = AllocateMem(256))) return;   memset(pmap,0,256);   memset(pmap_2,0,256);   #ifdef TRIPLE_BUFFER   if (!(pmap_2a = AllocateMem(256))) return;#endif   memset(pmap_2a,0,256);   for(ta=0; ta<pal_banks; ta++){      coltab[ta] = bankmap + (ta * bankcols * (internal_bpp(display_cfg.bpp) / 8));      colour_ram_src[ta] = src + (ta * bankcols * 2);   }   res_pens = 1;	// 1 reserved (pen 0 == black)   white_pen = 255;   closest_colour_init();   reset_palette_map();}void ResetPalette() {   if (cloc) FreeMem(cloc);   if (cloc_2) FreeMem(cloc_2);#ifdef TRIPLE_BUFFER   if (cloc_2a) FreeMem(cloc_2a);#endif   if (pmap)   FreeMem(pmap);   if (pmap_2) FreeMem(pmap_2);#ifdef TRIPLE_BUFFER   if (pmap_2a) FreeMem(pmap_2a);#endif   InitPaletteMap(RAM_PAL,pal_banks,bank_cols,map_size);}		 void set_colour_mapper(COLOUR_MAPPER *colour_mapper){   current_colour_mapper = colour_mapper;   switch(display_cfg.bpp){      case 8:         current_cmap_func = current_colour_mapper->mapper_8bpp;      break;      case 15:         current_cmap_func = current_colour_mapper->mapper_15bpp;      break;      case 16:         current_cmap_func = current_colour_mapper->mapper_16bpp;      break;      case 24:         current_cmap_func = current_colour_mapper->mapper_24bpp;      break;      case 32:         current_cmap_func = current_colour_mapper->mapper_32bpp;      break;      default:         current_cmap_func = current_colour_mapper->mapper_8bpp;      break;   }}void set_white_pen(int pen){   white_pen = pen;}// Note: get_white_pen does not return a usable white pen value// unless you are in 8 bit color mode.  Otherwise you should use// makecol( 255,255,255 ); to get a true white value.UINT8 get_white_pen(void){   pal[white_pen].r = 63;   pal[white_pen].g = 63;   pal[white_pen].b = 63;   return white_pen;}int Reserve_Pens(int num){   int ret;   ret = res_pens;   res_pens += num;   return ret;}void Set_Pens_15bit_xRGB(int talc, int start, int cols){   UINT16 yy;   UINT16 *ta;   ta = (UINT16 *) (RAM_PAL+(start<<1));   do{      yy = (*ta++) & 0x7FFF;      pal[talc].r = (yy&0x7C00)>>9;      pal[talc].g = (yy&0x03E0)>>4;      pal[talc].b = (yy&0x001F)<<1;      talc++;   }while(--cols);}void Set_Pens_12bit_RGBx(int talc, int start, int cols){   UINT16 yy;   UINT16 *ta;   ta = (UINT16 *) (RAM_PAL+(start<<1));   do{      yy = (*ta++) >> 4;      pal[talc].r = (yy&0x0F00)>>6;      pal[talc].g = (yy&0x00F0)>>2;      pal[talc].b = (yy&0x000F)<<2;      talc++;   }while(--cols);}void ClearPaletteMap(void){   UINT8 *ta;   switch(map_mode){   case PMAP_MAPPED:#ifdef TRIPLE_BUFFER   ta      = cloc_2;   cloc_2  = cloc_2a;		// cloc_2  = <last-last frame map>   cloc_2a = cloc;		// cloc_2a = <last frame map>   cloc    = ta;		// cloc    = <new frame map>   ta      = pmap_2;   pmap_2  = pmap_2a;		// pmap_2  = <last-last frame map>   pmap_2a = pmap;		// pmap_2a = <last frame map>   pmap    = ta;		// pmap    = <new frame map>#else   ta     = cloc_2;   cloc_2 = cloc;		// cloc_2 = <last frame map>   cloc   = ta;			// cloc   = <new frame map>   ta     = pmap_2;   pmap_2 = pmap;		// pmap_2 = <last frame map>   pmap   = ta;			// pmap   = <new frame map>#endif   memset(bank_status, 0, pal_banks * 2);   memset(cloc, 0xFF, map_size);   memset(pmap, 0x00, 256);   // pen 0 is always black   cloc[0] = 0;   pal[0].r = 0;   pal[0].g = 0;   pal[0].b = 0;   // Find first free pen   talc = res_pens;#ifdef TRIPLE_BUFFER   while((pmap_2[talc])||(pmap_2a[talc])){ talc++; }#else   talc --;   while(pmap_2[++talc]);#endif   break;   case PMAP_DIRECT:   break;   default:   break;   }}void reset_palette_map(void){   switch(map_mode){   case PMAP_MAPPED:      ClearPaletteMap();		// Make sure no colours are cached      ClearPaletteMap();#ifdef TRIPLE_BUFFER      ClearPaletteMap();#endif   break;   case PMAP_DIRECT:   break;   default:   break;   }   memset(pal       , 0x00, sizeof(PALETTE));   memset(pal_screen, 0xFF, sizeof(PALETTE));}void destroy_palette_map(void){   map_mode = PMAP_NONE;}void silly_wgp_colour_ram(UINT8 *src){   RAM_PAL = src;}#define GET_PEN_FOR_COLOUR_8(red, green, blue, result)                       \      if(cloc_2a[yy] == 255){                                                \         if(talc < 255){                                                     \            result = cloc_2a[yy] = cloc[yy] = talc;                          \            pal[talc].r = red;                                               \            pal[talc].g = green;                                             \            pal[talc].b = blue;                                              \            talc++;                                                          \            while((pmap_2[talc])||(pmap_2a[talc])){ talc++; }                \         }                                                                   \         else{				                                     \            result = closest_colour(red, green, blue);                       \         }                                                                   \      }                                                                      \      else{                                                                  \         result = cloc[yy] = cloc_2a[yy];                                    \      }                                                                      \      pmap[result] = 1;			                                     \/******************************************************************************//* 12-bit xBGR (12-bit mapping)                                               *//******************************************************************************/void Map_12bit_xRGB(int bank, int cols){   UINT16 yy;   UINT16 *ta;   UINT8 *ct,res;   bank_status[bank] = cols;   ta = (UINT16 *) (RAM_PAL+(bank<<5));   ct = coltab[bank];   do{      yy = (*ta++) & 0x0FFF;      GET_PEN_FOR_COLOUR_8(         (yy&0x0F00)>>6,         (yy&0x00F0)>>2,         (yy&0x000F)<<2,         res      );      *ct++ = res;   }while(--cols);}void map_12bit_xxxx_rrrr_gggg_bbbb_8(UINT32 bank, UINT32 cols){   UINT16 yy;   UINT16 *ta;   UINT8 *ct,res;   bank_status[bank] = cols;   ta = (UINT16 *) (RAM_PAL+(bank<<5));   ct = coltab[bank];   do{      yy = (*ta++) & 0x0FFF;      GET_PEN_FOR_COLOUR_8(         (yy&0x0F00)>>6,         (yy&0x00F0)>>2,         (yy&0x000F)<<2,         res      );      *ct++ = res;   }while(--cols);}void map_12bit_xxxx_rrrr_gggg_bbbb_15(UINT32 bank, UINT32 cols){   UINT16 yy;   UINT16 *ta;   UINT16 *ct,res;   bank_status[bank] = cols;   ta = (UINT16 *) (RAM_PAL+(bank<<5));   ct = (UINT16 *) coltab[bank];   do{      yy = (*ta++) & 0x0FFF;      GET_PEN_FOR_COLOUR_15(         (yy&0x0F00)>>6,         (yy&0x00F0)>>2,         (yy&0x000F)<<2,         res      );      *ct++ = res;   }while(--cols);}void map_12bit_xxxx_rrrr_gggg_bbbb_16(UINT32 bank, UINT32 cols){   UINT16 yy;   UINT16 *ta;   UINT16 *ct,res;   bank_status[bank] = cols;   ta = (UINT16 *) (RAM_PAL+(bank<<5));   ct = (UINT16 *) coltab[bank];   do{      yy = (*ta++) & 0x0FFF;      GET_PEN_FOR_COLOUR_16(         (yy&0x0F00)>>6,         (yy&0x00F0)>>2,         (yy&0x000F)<<2,         res      );      *ct++ = res;   }while(--cols);}void map_12bit_xxxx_rrrr_gggg_bbbb_24(UINT32 bank, UINT32 cols){   UINT16 yy;   UINT16 *ta;   UINT32 *ct,res;   bank_status[bank] = cols;   ta = (UINT16 *) (RAM_PAL+(bank<<5));   ct = (UINT32 *) coltab[bank];   do{      yy = (*ta++) & 0x0FFF;      GET_PEN_FOR_COLOUR_24(         (yy&0x0F00)>>6,         (yy&0x00F0)>>2,         (yy&0x000F)<<2,         res      );      *ct++ = res;   }while(--cols);}void map_12bit_xxxx_rrrr_gggg_bbbb_32(UINT32 bank, UINT32 cols){   UINT16 yy;   UINT16 *ta;   UINT32 *ct,res;   bank_status[bank] = cols;   ta = (UINT16 *) (RAM_PAL+(bank<<5));   ct = (UINT32 *) coltab[bank];   do{      yy = (*ta++) & 0x0FFF;      GET_PEN_FOR_COLOUR_32(         (yy&0x0F00)>>6,         (yy&0x00F0)>>2,         (yy&0x000F)<<2,         res      );      *ct++ = res;   }while(--cols);}struct COLOUR_MAPPER col_map_xxxx_rrrr_gggg_bbbb ={   "12bit xxxx rrrr gggg bbbb",   map_12bit_xxxx_rrrr_gggg_bbbb_8,   map_12bit_xxxx_rrrr_gggg_bbbb_15,   map_12bit_xxxx_rrrr_gggg_bbbb_16,   map_12bit_xxxx_rrrr_gggg_bbbb_24,   map_12bit_xxxx_rrrr_gggg_bbbb_32,};void map_12bit_nnnn_rrrr_gggg_bbbb_8(UINT32 bank, UINT32 cols){   UINT16 yy, bright;   UINT16 *ta;   UINT8 *ct,res,red,green,blue;   bank_status[bank] = cols;   ta = (UINT16 *) (RAM_PAL+(bank<<5)+30);   ct = coltab[bank];   do{      yy = (*ta--);      bright = (yy >> 12);      //if (bright) bright += 2;      red = (((yy&0x0F00)>>8) * bright) >> 2;      green = (((yy&0x00F0)>>4) * bright) >> 2;      blue = (((yy&0x000F)<<0) * bright) >> 2;             GET_PEN_FOR_COLOUR_8(			   red,green,blue,         res      );      *ct++ = res;   }while(--cols);}void map_12bit_nnnn_rrrr_gggg_bbbb_15(UINT32 bank, UINT32 cols){   UINT16 yy, bright;   UINT16 *ta;   UINT16 *ct,res;   UINT8 red,green,blue;   bank_status[bank] = cols;   ta = (UINT16 *) (RAM_PAL+(bank<<5)+30);   ct = (UINT16 *) coltab[bank];   do{      yy = (*ta--);      bright = (yy >> 12);      //if (bright) bright += 2;      red = (((yy&0x0F00)>>8) * bright); // 5 bits, not 6 !!!      green = (((yy&0x00F0)>>4) * bright);      blue = (((yy&0x000F)<<0) * bright);            res = makecol(red,green,blue);      *ct++ = res;   }while(--cols);}void map_12bit_nnnn_rrrr_gggg_bbbb_16(UINT32 bank, UINT32 cols){   UINT16 yy, bright;   UINT16 *ta;   UINT16 *ct,res;   UINT8 red,green,blue;      bank_status[bank] = cols;   ta = (UINT16 *) (RAM_PAL+(bank<<5)+30);   ct = (UINT16 *) coltab[bank];   do{      yy = (*ta--);      bright = (yy >> 12);      red = (((yy&0x0F00)>>8) * bright); // 5 bits, not 6 !!!      green = (((yy&0x00F0)>>4) * bright);      blue = (((yy&0x000F)<<0) * bright);            res = makecol(red,green,blue);            *ct++ = res;   }while(--cols);}void map_12bit_nnnn_rrrr_gggg_bbbb_24(UINT32 bank, UINT32 cols){   UINT16 yy, bright;   UINT16 *ta;   UINT32 *ct,res;   UINT8 red,green,blue;   bank_status[bank] = cols;   ta = (UINT16 *) (RAM_PAL+(bank<<5)+30);   ct = (UINT32 *) coltab[bank];   do{      yy = (*ta--);      bright = (yy >> 12);      //if (bright) bright += 2;      red = (((yy&0x0F00)>>8) * bright); // 5 bits, not 6 !!!      green = (((yy&0x00F0)>>4) * bright);      blue = (((yy&0x000F)<<0) * bright);            res = makecol(red,green,blue);      *ct++ = res;   }while(--cols);}void map_12bit_nnnn_rrrr_gggg_bbbb_32(UINT32 bank, UINT32 cols){   UINT16 yy, bright;   UINT16 *ta;   UINT32 *ct,res;   UINT8 red,green,blue;   bank_status[bank] = cols;   ta = (UINT16 *) (RAM_PAL+(bank<<5)+30);   ct = (UINT32 *) coltab[bank];   do{      yy = (*ta--);      bright = (yy >> 12);      //if (bright) bright += 2;      red = (((yy&0x0F00)>>8) * bright); // 5 bits, not 6 !!!      green = (((yy&0x00F0)>>4) * bright);      blue = (((yy&0x000F)<<0) * bright);            res = makecol(red,green,blue);      *ct++ = res;   }while(--cols);}struct COLOUR_MAPPER col_map_nnnn_rrrr_gggg_bbbb ={   "12bit nnnn rrrr gggg bbbb",   map_12bit_nnnn_rrrr_gggg_bbbb_8,   map_12bit_nnnn_rrrr_gggg_bbbb_15,   map_12bit_nnnn_rrrr_gggg_bbbb_16,   map_12bit_nnnn_rrrr_gggg_bbbb_24,   map_12bit_nnnn_rrrr_gggg_bbbb_32,};void Map_12bit_xxxxRRRRGGGGBBBB_Rev(int bank, int cols){   UINT16 yy;   UINT16 *ta;   UINT8 *ct,res;   bank_status[bank] = cols;   ta = (UINT16 *) (RAM_PAL+(bank<<5)+30);   ct = coltab[bank];   do{      yy = (*ta--) & 0x0FFF;      GET_PEN_FOR_COLOUR_8(         (yy&0x0F00)>>6,         (yy&0x00F0)>>2,         (yy&0x000F)<<2,         res      );      *ct++ = res;   }while(--cols);}/******************************************************************************//* 12-bit xBGR (12-bit mapping)                                               *//******************************************************************************/// Map_12bit_xBGR#define BUILD_MAPPER3(NAME, TYPE, PEN_FUNC) \void NAME(UINT32 bank, UINT32 cols)	\{					\   UINT16 yy;				\   UINT16 *ta;				\   TYPE *ct,res;			\					\   bank_status[bank] = cols;		\   ta = (UINT16 *) (RAM_PAL+(bank<<5));	\   ct = (TYPE *)coltab[bank];		\   do{					\      yy = (*ta++) & 0x0FFF;		\					\      PEN_FUNC(				\         (yy&0x000F)<<2,		\         (yy&0x00F0)>>2,		\         (yy&0x0F00)>>6,		\         res				\      );				\

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -