NXEngine v1.0.0.5
[NXEngine.git] / graphics / palette.cpp
blob503b03f534b418407c280df84412b34b13f37437
2 #include "../nx.h"
3 #include "palette.h"
4 #include "palette.fdh"
6 #define MAX_COLORS 256
7 static SDL_Color screenpal[MAX_COLORS];
8 int ncolors = -1;
10 // clear out all palette entries
11 void palette_reset(void)
13 ncolors = 0;
16 // given a paletted surface add it's colors in to the screen colormap
17 // then return a surface with the color indexes remapped ready to
18 // be displayed on the screen. insfc is either freed, or reused to
19 // create the returned surface.
20 SDL_Surface *palette_add(SDL_Surface *sfc)
22 SDL_Palette *pal = sfc->format->palette;
23 int remap[MAX_COLORS];
24 int x, y, i;
26 if (sfc->format->BitsPerPixel > 8)
28 staterr("palette_add: input surface is > 8bpp");
29 return NULL;
32 stat("palette_add: adding %d colors to screen palette...", pal->ncolors);
33 for(i=0;i<pal->ncolors;i++)
35 remap[i] = palette_alloc(pal->colors[i].r, pal->colors[i].g, pal->colors[i].b);
36 if (remap[i] == -1)
37 return sfc;
40 SDL_SetColors(screen->GetSDLSurface(), screenpal, 0, ncolors);
41 return sfc;
43 // remap indexes in surface
44 for(y=0;y<sfc->h;y++)
46 uint8_t *pixels = (uint8_t *)sfc->pixels + (y * sfc->pitch);
48 for(x=0;x<sfc->w;x++)
50 *pixels = remap[*pixels];
51 pixels++;
55 return sfc;*/
59 // add the given color to the screen palette and return it's index.
60 int palette_alloc(uint8_t r, uint8_t g, uint8_t b)
62 int i;
64 for(i=0;i<ncolors;i++)
66 if (screenpal[i].r == r && \
67 screenpal[i].g == g && \
68 screenpal[i].b == b)
70 return i;
74 if (ncolors >= MAX_COLORS)
76 staterr("palette_alloc: out of color space!");
77 return -1;
80 screenpal[ncolors].r = r;
81 screenpal[ncolors].g = g;
82 screenpal[ncolors].b = b;
83 return ncolors++;