CSemiDefHandler.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. return std::string();
  61. }
  62. void CSemiDefHandler::openDef(std::string name, std::string lodName, int dist)
  63. {
  64. std::ifstream * is = new std::ifstream();
  65. is -> open((lodName+"\\"+name).c_str(),std::ios::binary);
  66. is->seekg(0,std::ios::end); // na koniec
  67. int andame = is->tellg(); // read length
  68. is->seekg(0,std::ios::beg); // wracamy na poczatek
  69. buforD = new unsigned char[andame]; // allocate memory
  70. is->read((char*)buforD, andame); // read map file to buffer
  71. defName = name;
  72. int gdzie = defName.find_last_of("\\");
  73. defName = defName.substr(gdzie+1, gdzie-defName.length());
  74. delete is;
  75. readFileList(dist);
  76. loadImages(lodName);
  77. }
  78. CSemiDefHandler::~CSemiDefHandler()
  79. {
  80. for (int i=0;i<ourImages.size();i++)
  81. SDL_FreeSurface(ourImages[i].bitmap);
  82. }
  83. void CSemiDefHandler::readFileList(int dist)
  84. {
  85. howManyImgs = buforD[788];
  86. int i = 800;
  87. for (int pom=0;pom<howManyImgs;pom++)
  88. {
  89. std::string temp;
  90. while (buforD[i]!=0)
  91. {
  92. temp+=buforD[i++];
  93. }
  94. i+=dist; //by³o zwiêkszenie tylko o jedno
  95. if (temp!="")
  96. {
  97. temp = temp.substr(0,temp.length()-4) + ".BMP";
  98. namesOfImgs.push_back(temp);
  99. }
  100. else pom--;
  101. }
  102. delete buforD;
  103. }
  104. void CSemiDefHandler::loadImages(std::string path)
  105. {
  106. for (int i=0; i<namesOfImgs.size(); i++)
  107. {
  108. openImg((path+"\\_"+defName+"\\"+namesOfImgs[i]).c_str());
  109. }
  110. }
  111. void SDL_DisplayBitmap(const char *file, SDL_Surface *ekran, int x, int y)
  112. {
  113. SDL_Surface *image;
  114. SDL_Rect dest;
  115. image = SDL_LoadBMP(file);
  116. if ( image == NULL )
  117. {
  118. fprintf(stderr, "Nie mo¿na wczytaæ %s: %s\n", file, SDL_GetError());
  119. return;
  120. }
  121. dest.x = x;
  122. dest.y = y;
  123. dest.w = image->w;
  124. dest.h = image->h;
  125. SDL_BlitSurface(image, NULL, ekran, &dest);
  126. SDL_UpdateRects(ekran, 1, &dest);
  127. SDL_FreeSurface(image);
  128. }
  129. void CSemiDefHandler::openImg(const char *name)
  130. {
  131. SDL_Surface *image;
  132. image=IMG_Load(name);
  133. //SDL_DisplayBitmap(name,image, 0,0);
  134. if(!image)
  135. {
  136. printf("IMG_Load: %s\n", IMG_GetError());
  137. return;
  138. // handle error
  139. }
  140. Cimage vinya;
  141. vinya.bitmap = image;
  142. SDL_SetColorKey(vinya.bitmap,SDL_SRCCOLORKEY,SDL_MapRGB(vinya.bitmap->format,0,255,255));
  143. vinya.imName = name;
  144. ourImages.push_back(vinya);
  145. }