GridUtils.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #ifndef CRYINCLUDE_EDITOR_GRIDUTILS_H
  9. #define CRYINCLUDE_EDITOR_GRIDUTILS_H
  10. #pragma once
  11. namespace GridUtils
  12. {
  13. template <typename F>
  14. inline void IterateGrid(F& f, const float minPixelsPerTick, float zoomX, float originX, float fps, int left, int right)
  15. {
  16. float pixelsPerSecond = zoomX;
  17. float pixelsPerFrame = pixelsPerSecond / fps;
  18. float framesPerTick = ceil(minPixelsPerTick / pixelsPerFrame);
  19. float scale = 1;
  20. bool foundScale = false;
  21. int numIters = 0;
  22. for (float OOM = 1; !foundScale; OOM *= 10)
  23. {
  24. float scales[] = {1, 2, 5};
  25. for (int scaleIndex = 0; !foundScale && scaleIndex < sizeof(scales) / sizeof(scales[0]); ++scaleIndex)
  26. {
  27. scale = scales[scaleIndex] * OOM;
  28. if (framesPerTick <= scale + 0.1f)
  29. {
  30. framesPerTick = scale;
  31. foundScale = true;
  32. if (numIters++ > 1000)
  33. {
  34. break;
  35. }
  36. }
  37. }
  38. if (numIters++ > 1000)
  39. {
  40. break;
  41. }
  42. }
  43. float pixelsPerTick = pixelsPerFrame * framesPerTick;
  44. float timeAtLeft = -left / zoomX + originX;
  45. float frameAtLeft = ceil(timeAtLeft * fps / framesPerTick) * framesPerTick;
  46. float firstTick = floor((frameAtLeft / fps - originX) * zoomX + 0.5f) + left;
  47. int frame = int(frameAtLeft);
  48. int lockupPreventionCounter = 10000;
  49. for (float tickX = firstTick; tickX < right && lockupPreventionCounter >= 0; tickX += pixelsPerTick, --lockupPreventionCounter)
  50. {
  51. f(frame, aznumeric_cast<int>(tickX));
  52. frame += int(framesPerTick);
  53. }
  54. }
  55. }
  56. #endif // CRYINCLUDE_EDITOR_GRIDUTILS_H