Advertisement
Guest User

Untitled

a guest
Oct 1st, 2014
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. /**
  3. * Preamble:
  4. * this is the most horrible function i've ever written, and it makes me sad.
  5. * this is, however, a necessary evil, in order to save massive amounts of time in the draw function.
  6. * when used with the default TFTD ruleset, this function loops 4,194,304 times
  7. * (4 palettes, 4 tints, 4 levels of opacity, 256 colors, 256 comparisons per)
  8. * each additional tint in the rulesets will result in over a million iterations more.
  9. * @param pal the palette to base the lookup table on.
  10. */
  11. void XcomResourcePack::createTransparencyLUT(Palette *pal)
  12. {
  13. SDL_Color desiredColor;
  14. std::vector<Uint8> lookUpTable;
  15. // start with the color sets
  16. for (std::vector<std::vector<SDL_Color> >::const_iterator tint = _ruleset->getTints()->begin(); tint != _ruleset->getTints()->end(); ++tint)
  17. {
  18. // then the opacity levels
  19. for (std::vector<SDL_Color>::const_iterator opacity = tint->begin(); opacity != tint->end(); ++opacity)
  20. {
  21. // then the palette itself
  22. for (int currentColor = 0; currentColor != 256; ++currentColor)
  23. {
  24. // add the RGB values from the ruleset to those of the colors contained in the palette
  25. // in order to determine the desired color
  26. // yes all this casting and clamping is required, we're dealing with Uint8s here, and there's
  27. // a lot of potential for values to wrap around, which would be very bad indeed.
  28. desiredColor.r = std::min(255, (int)(pal->getColors(currentColor)->r) + (int)(opacity->r));
  29. desiredColor.g = std::min(255, (int)(pal->getColors(currentColor)->g) + (int)(opacity->g));
  30. desiredColor.b = std::min(255, (int)(pal->getColors(currentColor)->b) + (int)(opacity->b));
  31.  
  32. Uint8 closest = 0;
  33. int lowestDifference = INT_MAX;
  34. // now compare each color in the palette to find the closest match to our desired one
  35. for (int comparator = 0; comparator != 256; ++comparator)
  36. {
  37. int currentDifference = Sqr(desiredColor.r - pal->getColors(comparator)->r) +
  38. Sqr(desiredColor.g-pal->getColors(comparator)->g) +
  39. Sqr(desiredColor.b-pal->getColors(comparator)->b);
  40. // this is a closer match than our current pick
  41. if (currentDifference < lowestDifference)
  42. {
  43. closest = comparator;
  44. lowestDifference = currentDifference;
  45. }
  46. }
  47. lookUpTable.push_back(closest);
  48. }
  49. }
  50. }
  51. _transparencyLUTs.push_back(lookUpTable);
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement