-std::vector<video::SColor> *CNodeDefManager::getPalette(
- const ContentFeatures &f, const IGameDef *gamedef)
-{
-#ifndef SERVER
- // This works because colors always use the most significant bits
- // of param2. If you add a new colored type which uses param2
- // in a more advanced way, you should change this code, too.
- u32 palette_pixels = 0;
- switch (f.param_type_2) {
- case CPT2_COLOR:
- palette_pixels = 256;
- break;
- case CPT2_COLORED_FACEDIR:
- palette_pixels = 8;
- break;
- case CPT2_COLORED_WALLMOUNTED:
- palette_pixels = 32;
- break;
- default:
- return NULL;
- }
- // This many param2 values will have the same color
- u32 step = 256 / palette_pixels;
- const std::string &name = f.palette_name;
- if (name == "")
- return NULL;
- Client *client = (Client *) gamedef;
- ITextureSource *tsrc = client->tsrc();
-
- UNORDERED_MAP<std::string, std::vector<video::SColor> >::iterator it =
- m_palettes.find(name);
- if (it == m_palettes.end()) {
- // Create palette
- if (!tsrc->isKnownSourceImage(name)) {
- warningstream << "CNodeDefManager::getPalette(): palette \"" << name
- << "\" could not be loaded." << std::endl;
- return NULL;
- }
- video::IImage *img = tsrc->generateImage(name);
- std::vector<video::SColor> new_palette;
- u32 w = img->getDimension().Width;
- u32 h = img->getDimension().Height;
- // Real area of the image
- u32 area = h * w;
- if (area != palette_pixels)
- warningstream << "CNodeDefManager::getPalette(): the "
- << "specified palette image \"" << name << "\" does not "
- << "contain exactly " << palette_pixels
- << " pixels." << std::endl;
- if (area > palette_pixels)
- area = palette_pixels;
- // For each pixel in the image
- for (u32 i = 0; i < area; i++) {
- video::SColor c = img->getPixel(i % w, i / w);
- // Fill in palette with 'step' colors
- for (u32 j = 0; j < step; j++)
- new_palette.push_back(c);
- }
- img->drop();
- // Fill in remaining elements
- while (new_palette.size() < 256)
- new_palette.push_back(video::SColor(0xFFFFFFFF));
- m_palettes[name] = new_palette;
- it = m_palettes.find(name);
- }
- if (it != m_palettes.end())
- return &((*it).second);
-
-#endif
- return NULL;
-}
-