Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Preamble:
- * this is the most horrible function i've ever written, and it makes me sad.
- * this is, however, a necessary evil, in order to save massive amounts of time in the draw function.
- * when used with the default TFTD ruleset, this function loops 4,194,304 times
- * (4 palettes, 4 tints, 4 levels of opacity, 256 colors, 256 comparisons per)
- * each additional tint in the rulesets will result in over a million iterations more.
- * @param pal the palette to base the lookup table on.
- */
- void XcomResourcePack::createTransparencyLUT(Palette *pal)
- {
- SDL_Color desiredColor;
- std::vector<Uint8> lookUpTable;
- // start with the color sets
- for (std::vector<std::vector<SDL_Color> >::const_iterator tint = _ruleset->getTints()->begin(); tint != _ruleset->getTints()->end(); ++tint)
- {
- // then the opacity levels
- for (std::vector<SDL_Color>::const_iterator opacity = tint->begin(); opacity != tint->end(); ++opacity)
- {
- // then the palette itself
- for (int currentColor = 0; currentColor != 256; ++currentColor)
- {
- // add the RGB values from the ruleset to those of the colors contained in the palette
- // in order to determine the desired color
- // yes all this casting and clamping is required, we're dealing with Uint8s here, and there's
- // a lot of potential for values to wrap around, which would be very bad indeed.
- desiredColor.r = std::min(255, (int)(pal->getColors(currentColor)->r) + (int)(opacity->r));
- desiredColor.g = std::min(255, (int)(pal->getColors(currentColor)->g) + (int)(opacity->g));
- desiredColor.b = std::min(255, (int)(pal->getColors(currentColor)->b) + (int)(opacity->b));
- Uint8 closest = 0;
- int lowestDifference = INT_MAX;
- // now compare each color in the palette to find the closest match to our desired one
- for (int comparator = 0; comparator != 256; ++comparator)
- {
- int currentDifference = Sqr(desiredColor.r - pal->getColors(comparator)->r) +
- Sqr(desiredColor.g-pal->getColors(comparator)->g) +
- Sqr(desiredColor.b-pal->getColors(comparator)->b);
- // this is a closer match than our current pick
- if (currentDifference < lowestDifference)
- {
- closest = comparator;
- lowestDifference = currentDifference;
- }
- }
- lookUpTable.push_back(closest);
- }
- }
- }
- _transparencyLUTs.push_back(lookUpTable);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement