starfield.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #ifndef STARFIELD_H
  2. #define STARFIELD_H
  3. #include "door.h"
  4. #include <random>
  5. #include <set>
  6. struct star_pos {
  7. int x;
  8. int y;
  9. int symbol;
  10. int color;
  11. /**
  12. * @brief Provide less than operator.
  13. *
  14. * This will allow the star_pos to be stored sorted (top->bottom,
  15. * left->right) in a set.
  16. *
  17. * @param rhs
  18. * @return true
  19. * @return false
  20. */
  21. bool operator<(const star_pos rhs) const {
  22. if (rhs.y > y)
  23. return true;
  24. if (rhs.y == y) {
  25. if (rhs.x > x)
  26. return true;
  27. return false;
  28. }
  29. return false;
  30. }
  31. };
  32. class Starfield {
  33. door::Door &door;
  34. std::mt19937 &rng;
  35. std::set<star_pos> sky;
  36. std::uniform_int_distribution<int> uni_x;
  37. std::uniform_int_distribution<int> uni_y;
  38. star_pos make_pos(void);
  39. door::ANSIColor white; //(door::COLOR::WHITE);
  40. door::ANSIColor dark; //(door::COLOR::BLACK, door::ATTR::BRIGHT);
  41. const char *stars[2];
  42. public:
  43. Starfield(door::Door &Door, std::mt19937 &Rng);
  44. void regenerate(void);
  45. void display(void);
  46. };
  47. struct moving_star {
  48. int x;
  49. int y;
  50. int symbol;
  51. int color;
  52. bool visible;
  53. double xpos;
  54. double ypos;
  55. double movex;
  56. double movey;
  57. };
  58. typedef std::function<bool(int x, int y)> checkVisibleFunction;
  59. class AnimatedStarfield {
  60. door::Door &door;
  61. std::mt19937 &rng;
  62. std::vector<moving_star> sky;
  63. std::uniform_int_distribution<int> uni_x;
  64. std::uniform_int_distribution<int> uni_y;
  65. moving_star make_pos(bool centered = false);
  66. int mx;
  67. int my;
  68. double cx;
  69. double cy;
  70. double max_d;
  71. door::ANSIColor white;
  72. door::ANSIColor dark;
  73. const char *stars[2];
  74. double distance(double x, double y);
  75. checkVisibleFunction visible;
  76. public:
  77. AnimatedStarfield(door::Door &door, std::mt19937 &Rng);
  78. void regenerate(void);
  79. void display(void);
  80. void animate(void);
  81. void setVisible(checkVisibleFunction cvf);
  82. };
  83. #endif