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.

Game.h 2.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #pragma once
  2. #include <bitset>
  3. #include <map>
  4. #include <string>
  5. #include <optional>
  6. #include <SDL.h>
  7. #include <cygnet/Renderer.h>
  8. #include "common.h"
  9. #include "Mod.h"
  10. #include "World.h"
  11. namespace Swan {
  12. class Game {
  13. public:
  14. void createWorld(const std::string &worldgen, const std::vector<std::string> &modPaths);
  15. void onKeyDown(SDL_Keysym sym) {
  16. pressedKeys_[sym.scancode] = true;
  17. didPressKeys_[sym.scancode] = true;
  18. }
  19. void onKeyUp(SDL_Keysym sym) {
  20. pressedKeys_[sym.scancode] = false;
  21. didReleaseKeys_[sym.scancode] = true;
  22. }
  23. void onMouseMove(Sint32 x, Sint32 y) {
  24. mousePos_ = (Vec2{(float)x, (float)y} / (Vec2)cam_.size) * renderer_.winScale();
  25. }
  26. void onMouseDown(Sint32 x, Sint32 y, Uint8 button) {
  27. onMouseMove(x, y);
  28. pressedButtons_[button] = true;
  29. didPressButtons_[button] = true;
  30. }
  31. void onMouseUp(Sint32 x, Sint32 y, Uint8 button) {
  32. onMouseMove(x, y);
  33. pressedButtons_[button] = false;
  34. didReleaseButtons_[button] = true;
  35. }
  36. void onScrollWheel(Sint32 y) {
  37. didScroll_ = (y > 0 ? 1 : -1 );
  38. }
  39. bool isKeyPressed(SDL_Scancode code) { return pressedKeys_[code]; }
  40. bool wasKeyPressed(SDL_Scancode code) { return didPressKeys_[code]; }
  41. bool wasKeyReleased(SDL_Scancode code) { return didReleaseKeys_[code]; }
  42. Vec2 getMousePos() { return mousePos_; }
  43. bool isMousePressed(Uint8 button) { return pressedButtons_[button]; }
  44. bool wasMousePressed(Uint8 button) { return didPressButtons_[button]; }
  45. bool wasMouseReleased(Uint8 button) { return didReleaseButtons_[button]; }
  46. int wasWheelScrolled() { return didScroll_; }
  47. TilePos getMouseTile();
  48. SDL_Color backgroundColor();
  49. void draw();
  50. void update(float dt);
  51. void tick(float dt);
  52. std::unique_ptr<World> world_ = NULL;
  53. Cygnet::Renderer renderer_;
  54. Cygnet::RenderCamera cam_{.zoom = 0.25};
  55. private:
  56. std::bitset<SDL_NUM_SCANCODES> pressedKeys_;
  57. std::bitset<SDL_NUM_SCANCODES> didPressKeys_;
  58. std::bitset<SDL_NUM_SCANCODES> didReleaseKeys_;
  59. Vec2 mousePos_;
  60. std::bitset<SDL_BUTTON_X2> pressedButtons_;
  61. std::bitset<SDL_BUTTON_X2> didPressButtons_;
  62. std::bitset<SDL_BUTTON_X2> didReleaseButtons_;
  63. int didScroll_ = 0;
  64. };
  65. }