A 2D tile-based sandbox game.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

drawutil.cc 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #include "drawutil.h"
  2. #include <algorithm>
  3. #include <cmath>
  4. #include "gfxutil.h"
  5. namespace Swan {
  6. namespace Draw {
  7. static float linearLine(float from, float to, float frac) {
  8. return std::clamp(to * frac + from * (1 - frac), 0.0f, 255.0f);
  9. }
  10. static Cygnet::Color linearColor(Cygnet::Color from, Cygnet::Color to, float frac) {
  11. return {
  12. .r = linearLine(from.r, to.r, frac),
  13. .g = linearLine(from.g, to.g, frac),
  14. .b = linearLine(from.b, to.b, frac),
  15. .a = linearLine(from.a, to.a, frac),
  16. };
  17. }
  18. Cygnet::Color linearGradient(
  19. float val,
  20. std::initializer_list<std::pair<float, Cygnet::Color>> colors) {
  21. const std::pair<float, Cygnet::Color> *arr = colors.begin();
  22. size_t size = colors.size();
  23. if (val < arr[0].first)
  24. return arr[0].second;
  25. for (size_t i = 1; i < size; ++i) {
  26. if (arr[i].first < val)
  27. continue;
  28. auto [fromv, fromc] = arr[i - 1];
  29. auto [tov, toc] = arr[i];
  30. float frac = (val - fromv) / (tov - fromv);
  31. return linearColor(fromc, toc, frac);
  32. }
  33. return arr[size - 1].second;
  34. }
  35. /*
  36. void parallaxBackground(
  37. Win &win, SDL_Texture *tex,
  38. std::optional<SDL_Rect> srcrect, std::optional<SDL_Rect> destrect,
  39. float x, float y, float factor) {
  40. SDL_Renderer *rnd = win.renderer_;
  41. // We only need to set a clip rect if we have a destrect
  42. std::optional<RenderClipRect> clip;
  43. if (!srcrect) {
  44. Uint32 fmt;
  45. int access, w, h;
  46. SDL_QueryTexture(tex, &fmt, &access, &w, &h);
  47. srcrect = SDL_Rect{ 0, 0, w, h };
  48. }
  49. if (destrect) {
  50. clip.emplace(rnd, &*destrect);
  51. } else {
  52. int w, h;
  53. SDL_RenderGetLogicalSize(rnd, &w, &h);
  54. destrect = SDL_Rect{ 0, 0, w, h };
  55. }
  56. x = (x * win.zoom_) * -factor;
  57. y = (y * win.zoom_) * -factor;
  58. SDL_Rect rect{
  59. 0, 0,
  60. (int)((float)srcrect->w * win.zoom_),
  61. (int)((float)srcrect->h * win.zoom_),
  62. };
  63. rect.x = (int)std::floor((int)x % rect.w);
  64. if (rect.x > 0) rect.x -= rect.w;
  65. rect.y = (int)std::floor((int)y % rect.h);
  66. if (rect.y > 0) rect.y -= rect.h;
  67. int numx = destrect->w / rect.w + 2;
  68. int numy = destrect->h / rect.h + 2;
  69. for (int x = 0; x < numx; ++x) {
  70. for (int y = 0; y < numy; ++y) {
  71. SDL_Rect r{ rect.x + x * rect.w, rect.y + y * rect.h, rect.w, rect.h };
  72. SDL_RenderCopy(rnd, tex, &*srcrect, &r);
  73. }
  74. }
  75. }
  76. TODO */
  77. }
  78. }