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.

TileAtlas.cc 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include "TileAtlas.h"
  2. #include <iostream>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <swan-common/constants.h>
  8. #include "gl.h"
  9. namespace Cygnet {
  10. struct AtlasState {
  11. size_t tilesPerLine;
  12. size_t width = 0;
  13. size_t height = 0;
  14. std::vector<unsigned char> data;
  15. };
  16. TileAtlas::TileAtlas(): state_(std::make_unique<AtlasState>()) {
  17. GLint size;
  18. glGetIntegerv(GL_MAX_TEXTURE_SIZE, &size);
  19. state_->tilesPerLine = std::min(size / SwanCommon::TILE_SIZE, 256);
  20. }
  21. TileAtlas::TileAtlas(TileAtlas &&) = default;
  22. TileAtlas::~TileAtlas() = default;
  23. void TileAtlas::addTile(size_t tileId, const void *data) {
  24. const unsigned char *bytes = (const unsigned char *)data;
  25. size_t x = tileId % state_->tilesPerLine;
  26. size_t y = tileId / state_->tilesPerLine;
  27. if (state_->width <= x) {
  28. state_->width = x + 1;
  29. }
  30. if (state_->height <= y) {
  31. state_->height = y + 1;
  32. }
  33. size_t requiredSize = state_->tilesPerLine * SwanCommon::TILE_SIZE *
  34. state_->height * SwanCommon::TILE_SIZE * 4;
  35. state_->data.resize(requiredSize);
  36. for (size_t ty = 0; ty < SwanCommon::TILE_SIZE; ++ty) {
  37. const unsigned char *src = bytes + ty * SwanCommon::TILE_SIZE * 4;
  38. unsigned char *dest = state_->data.data() +
  39. (y * SwanCommon::TILE_SIZE + ty) * state_->tilesPerLine * SwanCommon::TILE_SIZE * 4 +
  40. (x * SwanCommon::TILE_SIZE * 4);
  41. memcpy(dest, src, SwanCommon::TILE_SIZE * 4);
  42. }
  43. }
  44. const unsigned char *TileAtlas::getImage(size_t *w, size_t *h) {
  45. *w = state_->tilesPerLine * SwanCommon::TILE_SIZE;
  46. *h = state_->height * SwanCommon::TILE_SIZE;
  47. return state_->data.data();
  48. }
  49. }