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.
 
 
 

108 rivejä
2.5 KiB

  1. #include <cstdio>
  2. #include <malloc.h>
  3. #include "map.h"
  4. #include "layer.h"
  5. Map::Map(char const *path) :
  6. layers(0),
  7. nlayers(0)
  8. {
  9. char tmp[BUFSIZ];
  10. int firstgid = 0, width = 0, height = 0, level = 0, data = 0;
  11. FILE *fp = fopen(path, "r");
  12. if (!fp)
  13. return;
  14. while (!feof(fp))
  15. {
  16. char str[1024];
  17. int i, j, k;
  18. char a, b;
  19. fgets(tmp, BUFSIZ, fp);
  20. if (data)
  21. {
  22. if (--data == 0)
  23. {
  24. layers[nlayers] = new Layer(width, height, level, tmp);
  25. nlayers++;
  26. }
  27. }
  28. else if (sscanf(tmp, " <tileset firstgid=\"%i\"", &i) == 1)
  29. {
  30. firstgid = i;
  31. fprintf(stderr, "found tileset, firstgid %i\n", firstgid);
  32. }
  33. else if (sscanf(tmp, " <image source=\"%[^\"]\"", str) == 1)
  34. {
  35. fprintf(stderr, "image %s\n", str);
  36. }
  37. else if (sscanf(tmp, " <layer name=\"%c%i%c%*[^\"]\" width=\"%i\" height=\"%i\"",
  38. &a, &i, &b, &j, &k) == 5)
  39. {
  40. fprintf(stderr, "%s layer, level %i, sublevel %c, %ix%i\n",
  41. a == 'H' ? "horizontal" : "vertical", i, b, j, k);
  42. layers = (Layer **)realloc(layers, sizeof(Layer **) * (nlayers + 1));
  43. width = j;
  44. height = k;
  45. data = 2;
  46. }
  47. else
  48. {
  49. fprintf(stderr, ".");
  50. }
  51. }
  52. fclose(fp);
  53. /*
  54. char tmp[1024];
  55. sprintf(tmp, "grep '\\(^ [^< ]\\|layer name\\)' %s | while read i; do echo \"$i\"; read i; echo -n \"$i\" | perl -MMIME::Base64 -ne 'print decode_base64($_)' | gunzip; done", path);
  56. FILE *fp = popen(tmp, "r");
  57. while (fp && !feof(fp))
  58. {
  59. int width, height;
  60. fscanf(fp, "<layer name=\"%[^\"]\" ", name);
  61. if (feof(fp))
  62. break;
  63. fscanf(fp, "width=\"%u\" ", &width);
  64. fscanf(fp, "height=\"%u\" ", &height);
  65. fgets(tmp, 1024, fp); // Ignore rest of line
  66. layers = (Layer **)realloc(layers, sizeof(Layer **) * (nlayers + 1));
  67. layers[nlayers] = new Layer(name, width, height, fp);
  68. nlayers++;
  69. }
  70. pclose(fp);
  71. */
  72. }
  73. Map::~Map()
  74. {
  75. for (int i = 0; i < nlayers; i++)
  76. delete layers[i];
  77. free(layers);
  78. }
  79. void Map::Draw(Tiler *tiler)
  80. {
  81. for (int i = 0; i < nlayers; i++)
  82. {
  83. int z = layers[i]->GetZ();
  84. for (int y = 0; y < 32; y++)
  85. for (int x = 0; x < 32; x++)
  86. tiler->AddTile(layers[i]->GetTile(x, y), x * 32, y * 32, z);
  87. }
  88. }