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.

ResourceManager.h 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #pragma once
  2. #include <unordered_map>
  3. #include <optional>
  4. #include <memory>
  5. #include <stdint.h>
  6. #include <string.h>
  7. #include <swan-common/constants.h>
  8. #include "Renderer.h"
  9. #include "TileAtlas.h"
  10. namespace Cygnet {
  11. struct ResourceTileAnimation {
  12. uint16_t id;
  13. int frames;
  14. int index;
  15. std::unique_ptr<unsigned char[]> data;
  16. };
  17. class ResourceManager;
  18. class ResourceBuilder {
  19. public:
  20. ResourceBuilder(Renderer &rnd): rnd_(rnd) {}
  21. RenderSprite addSprite(std::string name, void *data, int width, int height, int fh);
  22. RenderSprite addSprite(std::string name, void *data, int width, int height);
  23. void addTile(Renderer::TileID id, void *data, int frames = 1);
  24. void addTile(Renderer::TileID id, std::unique_ptr<unsigned char[]> data, int frames = 1);
  25. private:
  26. Renderer &rnd_;
  27. std::unordered_map<std::string, RenderSprite> sprites_;
  28. std::vector<ResourceTileAnimation> tileAnims_;
  29. TileAtlas atlas_;
  30. friend ResourceManager;
  31. };
  32. class ResourceManager {
  33. public:
  34. ResourceManager(ResourceBuilder &&builder);
  35. ~ResourceManager();
  36. RenderSprite getSprite(std::string name) { return sprites_.at(std::move(name)); }
  37. void tick();
  38. private:
  39. Renderer &rnd_;
  40. std::unordered_map<std::string, RenderSprite> sprites_;
  41. std::unordered_map<std::string, RenderTile> tiles_;
  42. std::vector<ResourceTileAnimation> tileAnims_;
  43. };
  44. inline RenderSprite ResourceBuilder::addSprite(
  45. std::string name, void *data, int width, int height, int fh) {
  46. return sprites_[std::move(name)] = rnd_.createSprite(data, width, height, fh);
  47. }
  48. inline RenderSprite ResourceBuilder::addSprite(
  49. std::string name, void *data, int width, int height) {
  50. return sprites_[std::move(name)] = rnd_.createSprite(data, width, height);
  51. }
  52. inline void ResourceBuilder::addTile(uint16_t id, void *data, int frames) {
  53. if (frames == 0) {
  54. atlas_.addTile(id, data);
  55. } else {
  56. auto ptr = std::make_unique<unsigned char[]>(
  57. SwanCommon::TILE_SIZE * SwanCommon::TILE_SIZE * 4 * frames);
  58. memcpy(ptr.get(), data, SwanCommon::TILE_SIZE * SwanCommon::TILE_SIZE * 4 * frames);
  59. addTile(id, std::move(ptr), frames);
  60. }
  61. }
  62. inline void ResourceBuilder::addTile(Renderer::TileID id, std::unique_ptr<unsigned char[]> data, int frames) {
  63. atlas_.addTile(id, data.get());
  64. if (frames > 1) {
  65. tileAnims_.push_back({
  66. .id = id,
  67. .frames = frames,
  68. .index = 0,
  69. .data = std::move(data),
  70. });
  71. }
  72. }
  73. }