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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #pragma once
  2. #include <bitset>
  3. #include <map>
  4. #include <string>
  5. #include <optional>
  6. #include <SDL.h>
  7. #include "common.h"
  8. #include "Resource.h"
  9. #include "Mod.h"
  10. #include "World.h"
  11. namespace Swan {
  12. class Game {
  13. public:
  14. Game(Win &win):
  15. win_(win),
  16. mouse_pos_(0, 0) {}
  17. std::optional<ModWrapper> loadMod(std::string path, World &world);
  18. void createWorld(const std::string &worldgen, const std::vector<std::string> &mods);
  19. void onKeyDown(SDL_Keysym sym) {
  20. pressed_keys_[sym.scancode] = true;
  21. did_press_keys_[sym.scancode] = true;
  22. }
  23. void onKeyUp(SDL_Keysym sym) {
  24. pressed_keys_[sym.scancode] = false;
  25. did_release_keys_[sym.scancode] = true;
  26. }
  27. void onMouseMove(Sint32 x, Sint32 y) {
  28. mouse_pos_ = { x, y };
  29. }
  30. void onMouseDown(Sint32 x, Sint32 y, Uint8 button) {
  31. mouse_pos_ = { x, y };
  32. pressed_buttons_[button] = true;
  33. did_press_buttons_[button] = true;
  34. }
  35. void onMouseUp(Sint32 x, Sint32 y, Uint8 button) {
  36. mouse_pos_ = { x, y };
  37. pressed_buttons_[button] = false;
  38. did_release_buttons_[button] = true;
  39. }
  40. void onScrollWheel(Sint32 y) {
  41. did_scroll_ = (y > 0 ? 1 : -1 );
  42. }
  43. bool isKeyPressed(SDL_Scancode code) { return pressed_keys_[code]; }
  44. bool wasKeyPressed(SDL_Scancode code) { return did_press_keys_[code]; }
  45. bool wasKeyReleased(SDL_Scancode code) { return did_release_keys_[code]; }
  46. Vec2i getMousePos() { return mouse_pos_; }
  47. bool isMousePressed(Uint8 button) { return pressed_buttons_[button]; }
  48. bool wasMousePressed(Uint8 button) { return did_press_buttons_[button]; }
  49. bool wasMouseReleased(Uint8 button) { return did_release_buttons_[button]; }
  50. int wasWheelScrolled() { return did_scroll_; }
  51. TilePos getMouseTile();
  52. SDL_Color backgroundColor();
  53. void draw();
  54. void update(float dt);
  55. void tick(float dt);
  56. std::unique_ptr<World> world_ = NULL;
  57. std::unique_ptr<ImageResource> invalid_image_ = NULL;
  58. std::unique_ptr<Tile> invalid_tile_ = NULL;
  59. std::unique_ptr<Item> invalid_item_ = NULL;
  60. Win &win_;
  61. private:
  62. std::bitset<SDL_NUM_SCANCODES> pressed_keys_;
  63. std::bitset<SDL_NUM_SCANCODES> did_press_keys_;
  64. std::bitset<SDL_NUM_SCANCODES> did_release_keys_;
  65. Vec2i mouse_pos_;
  66. std::bitset<SDL_BUTTON_X2> pressed_buttons_;
  67. std::bitset<SDL_BUTTON_X2> did_press_buttons_;
  68. std::bitset<SDL_BUTTON_X2> did_release_buttons_;
  69. int did_scroll_ = 0;
  70. };
  71. }