CSemiDefHandler.cpp 3.1 KB

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