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.

common.h 1.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #pragma once
  2. #include <SFML/System/Vector2.hpp>
  3. #include <SFML/Graphics.hpp>
  4. namespace Swan {
  5. static constexpr float UNIT_SIZE = 12;
  6. static constexpr int TICK_RATE = 20;
  7. static constexpr int CHUNK_HEIGHT = 32;
  8. static constexpr int CHUNK_WIDTH = 32;
  9. class Vec2: public sf::Vector2<float> {
  10. public:
  11. using sf::Vector2<float>::Vector2;
  12. Vec2 operator+(Vec2 &vec) {
  13. return Vec2(x + vec.x, y + vec.y);
  14. }
  15. Vec2 &operator+=(Vec2 &vec) {
  16. this->x += vec.x;
  17. this->y += vec.y;
  18. return *this;
  19. }
  20. Vec2 operator-(Vec2 &vec) {
  21. return Vec2(x - vec.x, y - vec.y);
  22. }
  23. Vec2 &operator-=(Vec2 &vec) {
  24. this->x -= vec.x;
  25. this->y -= vec.y;
  26. return *this;
  27. }
  28. Vec2 operator*(Vec2 &vec) {
  29. return Vec2(x * vec.x, y * vec.y);
  30. }
  31. Vec2 &operator*=(Vec2 &vec) {
  32. this->x *= vec.x;
  33. this->y *= vec.y;
  34. return *this;
  35. }
  36. Vec2 operator/(Vec2 &vec) {
  37. return Vec2(x / vec.x, y / vec.y);
  38. }
  39. Vec2 &operator/=(Vec2 &vec) {
  40. this->x /= vec.x;
  41. this->y /= vec.y;
  42. return *this;
  43. }
  44. };
  45. struct Win {
  46. public:
  47. sf::RenderWindow &window_;
  48. sf::Transform &transform_;
  49. Vec2 curr_pos_ = { 0, 0 };
  50. void setPos(Vec2 &pos) {
  51. transform_.translate(-curr_pos_.x, -curr_pos_.y);
  52. transform_.translate(pos.x, pos.y);
  53. curr_pos_ = pos;
  54. }
  55. void draw(const sf::Drawable &drawable) {
  56. window_.draw(drawable, transform_);
  57. }
  58. };
  59. }