CSemiDefHandler.cpp 3.1 KB

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