93 行
1.7 KiB

  1. #include <cstring>
  2. #include <cstdio>
  3. #include <stdlib.h>
  4. #include "tiler.h"
  5. #include "tileset.h"
  6. /*
  7. * Tiler implementation class
  8. */
  9. static class TilerData
  10. {
  11. friend class Tiler;
  12. public:
  13. TilerData() :
  14. tilesets(0),
  15. ntilesets(0)
  16. {
  17. /* Nothing to do */
  18. }
  19. ~TilerData()
  20. {
  21. free(tilesets);
  22. }
  23. private:
  24. TileSet **tilesets;
  25. int ntilesets;
  26. }
  27. tilerdata;
  28. static TilerData * const data = &tilerdata;
  29. /*
  30. * Public Tiler class
  31. */
  32. int Tiler::Register(char const *path)
  33. {
  34. int id, empty = -1;
  35. /* If the tileset is already registered, remember its ID. Look for an
  36. * empty slot at the same time. */
  37. for (id = 0; id < data->ntilesets; id++)
  38. {
  39. TileSet *t = data->tilesets[id];
  40. if (!t)
  41. empty = id;
  42. else if (!strcasecmp(path, t->GetName()))
  43. break;
  44. }
  45. /* If this is a new tileset, create a new one. */
  46. if (id == data->ntilesets)
  47. {
  48. if (empty == -1)
  49. {
  50. empty = data->ntilesets++;
  51. data->tilesets = (TileSet **)realloc(data->tilesets,
  52. data->ntilesets * sizeof(TileSet *));
  53. }
  54. data->tilesets[empty] = new TileSet(path);
  55. id = empty;
  56. }
  57. data->tilesets[id]->Ref();
  58. return id + 1; /* ID 0 is for the empty tileset */
  59. }
  60. void Tiler::Deregister(int id)
  61. {
  62. --id; /* ID 0 is for the empty tileset */
  63. if (data->tilesets[id]->Unref() == 0)
  64. {
  65. delete data->tilesets[id];
  66. data->tilesets[id] = NULL;
  67. }
  68. }
  69. void Tiler::Render(uint32_t code, int x, int y)
  70. {
  71. int id = (code >> 16) - 1; /* ID 0 is for the empty tileset */
  72. data->tilesets[id]->BlitTile(code & 0xffff, x, y);
  73. }