CSemiDefHandler.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #include "stdafx.h"
  2. #include "CSemiDefHandler.h"
  3. #include <fstream>
  4. extern SDL_Surface * ekran;
  5. std::string CSemiDefHandler::nameFromType (EterrainType typ)
  6. {
  7. switch(typ)
  8. {
  9. case dirt:
  10. {
  11. return std::string("DIRTTL.DEF");
  12. break;
  13. }
  14. case sand:
  15. {
  16. return std::string("SANDTL.DEF");
  17. break;
  18. }
  19. case grass:
  20. {
  21. return std::string("GRASTL.DEF");
  22. break;
  23. }
  24. case snow:
  25. {
  26. return std::string("SNOWTL.DEF");
  27. break;
  28. }
  29. case swamp:
  30. {
  31. return std::string("SWMPTL.DEF");
  32. break;
  33. }
  34. case rough:
  35. {
  36. return std::string("ROUGTL.DEF");
  37. break;
  38. }
  39. case subterranean:
  40. {
  41. return std::string("SUBBTL.DEF");
  42. break;
  43. }
  44. case lava:
  45. {
  46. return std::string("LAVATL.DEF");
  47. break;
  48. }
  49. case water:
  50. {
  51. return std::string("WATRTL.DEF");
  52. break;
  53. }
  54. case rock:
  55. {
  56. return std::string("ROCKTL.DEF");
  57. break;
  58. }
  59. }
  60. }
  61. void CSemiDefHandler::openDef(std::string name, std::string lodName)
  62. {
  63. std::ifstream * is = new std::ifstream();
  64. is -> open((lodName+"\\"+name).c_str(),std::ios::binary);
  65. is->seekg(0,std::ios::end); // na koniec
  66. int andame = is->tellg(); // read length
  67. is->seekg(0,std::ios::beg); // wracamy na poczatek
  68. buforD = new unsigned char[andame]; // allocate memory
  69. is->read((char*)buforD, andame); // read map file to buffer
  70. defName = name;
  71. int gdzie = defName.find_last_of("\\");
  72. defName = defName.substr(gdzie+1, gdzie-defName.length());
  73. delete is;
  74. readFileList();
  75. loadImages(lodName);
  76. }
  77. void CSemiDefHandler::readFileList()
  78. {
  79. howManyImgs = buforD[788];
  80. int i = 800;
  81. for (int pom=0;pom<howManyImgs;pom++)
  82. {
  83. std::string temp;
  84. while (buforD[i]!=0)
  85. {
  86. temp+=buforD[i++];
  87. }
  88. i++;;
  89. if (temp!="")
  90. {
  91. temp = temp.substr(0,temp.length()-4) + ".BMP";
  92. namesOfImgs.push_back(temp);
  93. }
  94. else pom--;
  95. }
  96. }
  97. void CSemiDefHandler::loadImages(std::string path)
  98. {
  99. for (int i=0; i<namesOfImgs.size(); i++)
  100. {
  101. openImg((path+"\\_"+defName+"\\"+namesOfImgs[i]).c_str());
  102. }
  103. }
  104. void SDL_DisplayBitmap(const char *file, SDL_Surface *ekran, int x, int y)
  105. {
  106. SDL_Surface *image;
  107. SDL_Rect dest;
  108. image = SDL_LoadBMP(file);
  109. if ( image == NULL )
  110. {
  111. fprintf(stderr, "Nie mo¿na wczytaæ %s: %s\n", file, SDL_GetError());
  112. return;
  113. }
  114. dest.x = x;
  115. dest.y = y;
  116. dest.w = image->w;
  117. dest.h = image->h;
  118. SDL_BlitSurface(image, NULL, ekran, &dest);
  119. SDL_UpdateRects(ekran, 1, &dest);
  120. SDL_FreeSurface(image);
  121. }
  122. void CSemiDefHandler::openImg(const char *name)
  123. {
  124. SDL_Surface *image;
  125. image=IMG_Load(name);
  126. //SDL_DisplayBitmap(name,image, 0,0);
  127. if(!image)
  128. {
  129. printf("IMG_Load: %s\n", IMG_GetError());
  130. // handle error
  131. }
  132. Cimage vinya;
  133. vinya.bitmap = image;
  134. SDL_SetColorKey(vinya.bitmap,SDL_SRCCOLORKEY,SDL_MapRGB(vinya.bitmap->format,0,255,255));
  135. vinya.imName = name;
  136. ourImages.push_back(vinya);
  137. }