Wpasujbud.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. // Wpasujbud.cpp : main project file.
  2. //#include <msclr\marshal.h>
  3. #include "SDL.h"
  4. #include "Wpasujbud.h"
  5. #include "tchar.h"
  6. #include "obrazek.h"
  7. #include "dataEditor.h"
  8. using namespace Wpasuj;
  9. int Inaccuracy=5;
  10. int zgodnosc(Bitmap^ bg, Bitmap^ st, int x, int y);
  11. int zgodnosc(SDL_Surface * bg, SDL_Surface * st, int x, int y);
  12. std::string Wpasujbud::ToString(System::String^ src)
  13. {
  14. std::string dest;
  15. using namespace System::Runtime::InteropServices;
  16. const char* chars = (const char*)(Marshal::StringToHGlobalAnsi(src)).ToPointer();
  17. dest = chars;
  18. Marshal::FreeHGlobal(IntPtr((void*)chars));
  19. return dest;
  20. }
  21. [STAThreadAttribute]
  22. int __stdcall WinMain()
  23. {
  24. // Enabling Windows XP visual effects before any controls are created
  25. Application::EnableVisualStyles();
  26. Application::SetCompatibleTextRenderingDefault(false);
  27. Application::Run(gcnew Wpasujbud());
  28. exit(0);
  29. return 0;
  30. }
  31. Wpasujbud::Wpasujbud(void)
  32. {
  33. InitializeComponent();
  34. openFileDialog1->InitialDirectory = IO::Directory::GetCurrentDirectory();
  35. IO::StreamReader ^sr = gcnew IO::StreamReader(L"config/TOWNTYPE.TXT");
  36. Int32 i=0;
  37. while(!sr->EndOfStream)
  38. {
  39. String ^ n = sr->ReadLine();
  40. n = (i.ToString()) + L". " + n;
  41. townID->Items->Add(n);
  42. i++;
  43. }
  44. townID->SelectedIndex=0;
  45. sr->Close();
  46. i++;
  47. sr = gcnew IO::StreamReader(L"config/BNAMES.TXT");
  48. i=0;
  49. while(!sr->EndOfStream)
  50. {
  51. String ^ n = sr->ReadLine();
  52. //n = (i.ToString()) + L". " + n;
  53. buildingType->Items->Add(n);
  54. i++;
  55. }
  56. buildingType->SelectedIndex=0;
  57. sr->Close();
  58. i++;
  59. bitmapsFolder->Text = openFileDialog1->InitialDirectory;
  60. }
  61. System::Void Wpasujbud::searchBitmaps_Click(System::Object^ sender, System::EventArgs^ e)
  62. {
  63. bitmapList->Items->Clear();
  64. array<String^>^ pliki = IO::Directory::GetFiles(bitmapsFolder->Text,bmpPattern->Text);
  65. for each(String ^ plik in pliki)
  66. {
  67. //if(plik->EndsWith(L".bmp") || plik->EndsWith(L".BMP"))
  68. bitmapList->Items->Add(plik->Substring(plik->LastIndexOf('\\')+1));
  69. }
  70. if(bitmapList->Items->Count>0)
  71. bitmapList->SelectedIndex=0;
  72. }
  73. System::Void Wpasujbud::startLocating_Click(System::Object^ sender, System::EventArgs^ e)
  74. {
  75. searchPicturePos();
  76. }
  77. System::Void Wpasujbud::setBackground_Click(System::Object^ sender, System::EventArgs^ e)
  78. {
  79. if(townBgPath->Text->Length==0)
  80. return;
  81. Bitmap ^ bg = gcnew Bitmap(townBgPath->Text);
  82. townBg->Image = dynamic_cast<Image^>(bg);
  83. sbg = SDL_LoadBMP(ToString(townBgPath->Text).c_str());
  84. }
  85. System::Void Wpasujbud::openFileDialog1_FileOk(System::Object^ sender, System::ComponentModel::CancelEventArgs^ e)
  86. {
  87. townBgPath->Text = (dynamic_cast<OpenFileDialog^>(sender))->FileName;
  88. }
  89. System::Void Wpasujbud::browseForBg_Click(System::Object^ sender, System::EventArgs^ e)
  90. {
  91. openFileDialog1->ShowDialog();
  92. }
  93. System::Void Wpasujbud::browseForbmpfol_Click(System::Object^ sender, System::EventArgs^ e)
  94. {
  95. folderBrowserDialog1->ShowDialog();
  96. bitmapsFolder->Text = folderBrowserDialog1->SelectedPath;
  97. }
  98. System::Void Wpasujbud::bitmapList_SelectedIndexChanged(System::Object^ sender, System::EventArgs^ e)
  99. {
  100. int nsel = bitmapList->SelectedIndex;
  101. if (nsel<0)
  102. return;
  103. String ^ bmpname = bitmapsFolder->Text + "\\" + dynamic_cast<String^>(bitmapList->SelectedItem);
  104. Bitmap ^building = gcnew Bitmap(bmpname);
  105. buildingImg->Image = dynamic_cast<Image^>(building);
  106. }
  107. System::Void Wpasujbud::townBg_Click(System::Object^ sender, System::EventArgs^ e)
  108. {
  109. //Int32 tx, ty, ux, uy;
  110. //tx = townBg->Location.X;
  111. //ty = townBg->Location.Y;
  112. //ux = Location.X;
  113. //uy = Location.Y;
  114. mx = MousePosition.X - townBg->Location.X - Location.X - 4;
  115. my = MousePosition.Y - townBg->Location.Y - Location.Y - 30;
  116. koordy->Text = mx.ToString() + L", " + my.ToString();
  117. }
  118. System::Void Wpasujbud::button1_Click(System::Object^ sender, System::EventArgs^ e) //skip
  119. {
  120. nextPicture();
  121. }
  122. Uint32 SDL_GetPixel(SDL_Surface *surface, int x, int y)
  123. {
  124. Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * surface->format->BytesPerPixel;
  125. switch(surface->format->BytesPerPixel)
  126. {
  127. case 1:
  128. {
  129. SDL_Color * color = surface->format->palette->colors+(*p);
  130. return color->r<<16 | color->g<<8 | color->b;
  131. }
  132. case 3:
  133. return p[0] | p[1] << 8 | p[2] << 16;
  134. }
  135. }
  136. int zgodnosc(SDL_Surface * bg, SDL_Surface * st, int x, int y)
  137. {
  138. int ret=0;
  139. for(int i=0;i<st->w;i+=Inaccuracy)
  140. {
  141. for(int j=0;j<st->h;j+=Inaccuracy)
  142. {
  143. int c1 = SDL_GetPixel(bg,i+x,j+y);
  144. int c2 = SDL_GetPixel(st,i,j);
  145. if((i+x)>=bg->w)
  146. break;
  147. if((j+y)>=bg->h)
  148. break;
  149. if(SDL_GetPixel(bg,i+x,j+y) == SDL_GetPixel(st,i,j))
  150. {
  151. ret++;
  152. }
  153. }
  154. }
  155. return ret*(Inaccuracy*Inaccuracy);
  156. }
  157. int zgodnosc(Bitmap^ bg, Bitmap^ st, int x, int y)
  158. {
  159. int ret=0;
  160. for(int i=0;i<st->Width;i++)
  161. {
  162. for(int j=0;j<st->Height;j++)
  163. {
  164. //Color c1 = bg->GetPixel(i+x,j+y);
  165. //Color c2 = st->GetPixel(i,j);
  166. if((i+x)>=bg->Width)
  167. break;
  168. if((j+y)>=bg->Height)
  169. break;
  170. if(bg->GetPixel(i+x,j+y) == st->GetPixel(i,j))
  171. {
  172. ret++;
  173. }
  174. }
  175. }
  176. return ret;
  177. }
  178. void Wpasujbud::searchPicturePos()
  179. {
  180. //Bitmap^ b = gcnew Bitmap((townBg->Image)->Width,(townBg->Image)->Height,Imaging::PixelFormat::Format24bppRgb);
  181. //Bitmap^ s = dynamic_cast<Bitmap^>(buildingImg->Image);
  182. //Graphics ^ g = Graphics::FromImage(b);
  183. //g->DrawImage(townBg->Image,0,0);
  184. //townBg->Image = b;
  185. String^ sss = bitmapsFolder->Text;
  186. if(!(sss->EndsWith(L"/")||sss->EndsWith(L"\\")))
  187. sss+=L"\\";
  188. sss+=dynamic_cast<String^>(bitmapList->SelectedItem);
  189. SDL_Surface *str = SDL_LoadBMP(ToString(sss).c_str());
  190. Int32 colorPxs = 0;
  191. int aq = 0x00ffff;
  192. for (int x=0;x<str->w;x++)
  193. {
  194. for(int y=0;y<str->h;y++)
  195. {
  196. if(SDL_GetPixel(str,x,y) != aq)
  197. colorPxs++;
  198. }
  199. }
  200. int zgoda=0, retx, rety;
  201. int px=mx, py=my, maxr = Convert::ToInt32(radious->Text);
  202. if (radioButton2->Checked)
  203. {
  204. px-=str->w/2;
  205. py-=str->h/2;
  206. }
  207. else if (radioButton3->Checked)
  208. {
  209. px-=str->w;
  210. py-=str->h;
  211. }
  212. px-=maxr/2;
  213. py-=maxr/2;
  214. for(int i=0;i<maxr;i++)
  215. {
  216. for (int j=0;j<maxr;j++)
  217. {
  218. int pom = zgodnosc(sbg,str,px+i,py+j);
  219. if (pom>zgoda)
  220. {
  221. zgoda = pom;
  222. retx = px+i;
  223. rety = py+j;
  224. }
  225. }
  226. }
  227. //for(int r=0;r<maxr;r++)
  228. //{
  229. // for(int i=0;i<=r;i++)
  230. // {
  231. // int pom, pom2;
  232. // pom = zgodnosc(sbg,str,px+i,py);
  233. // if (pom>zgoda)
  234. // {
  235. // zgoda = pom;
  236. // retx = px+i;
  237. // rety = py;
  238. // }
  239. // pom = zgodnosc(sbg,str,px+i,py+r);
  240. // if (pom>zgoda)
  241. // {
  242. // zgoda = pom;
  243. // retx = px+i;
  244. // rety = py+r;
  245. // }
  246. // pom = zgodnosc(sbg,str,px,py+i);
  247. // if (pom>zgoda)
  248. // {
  249. // zgoda = pom;
  250. // retx = px;
  251. // rety = py+i;
  252. // }
  253. // pom = zgodnosc(sbg,str,px+r,py+i);
  254. // if (pom>zgoda)
  255. // {
  256. // zgoda = pom;
  257. // retx = px+r;
  258. // rety = py+i;
  259. // }
  260. // float per = zgoda/colorPxs;
  261. //
  262. // }
  263. // px--;
  264. // py--;
  265. //}
  266. accordanceBox->Text = zgoda.ToString() + L"/" + colorPxs.ToString();
  267. curx = retx;
  268. cury = rety;
  269. foundedCoords->Text = retx.ToString() + L", " + rety.ToString();
  270. //townBg->Refresh();
  271. }
  272. System::Void Wpasujbud::inaccuracy_TextChanged(System::Object^ sender, System::EventArgs^ e)
  273. {
  274. int ni = Convert::ToUInt32(inaccuracy->Text);
  275. if(ni==0)
  276. {
  277. inaccuracy->Text == L"1";
  278. Inaccuracy = 1;
  279. }
  280. else
  281. {
  282. Inaccuracy = ni;
  283. }
  284. }
  285. void Wpasujbud::nextPicture()
  286. {
  287. if(bitmapList->SelectedIndex<(bitmapList->Items->Count-1))
  288. bitmapList->SelectedIndex++;
  289. }
  290. void Wpasujbud::previousPicture()
  291. {
  292. if(bitmapList->SelectedIndex>0)
  293. bitmapList->SelectedIndex--;
  294. }
  295. System::Void Wpasujbud::confirm_Click(System::Object^ sender, System::EventArgs^ e)
  296. {
  297. CBuildingData ^cbd = gcnew CBuildingData();
  298. //cbd->defname = bitmapsFolder->Text;
  299. //if(!(cbd->defname->EndsWith(L"/")||cbd->defname->EndsWith(L"\\")))
  300. // cbd->defname+=L"\\";
  301. cbd->defname+=dynamic_cast<String^>(bitmapList->SelectedItem);
  302. cbd->defname = (cbd->defname)->Substring(0,cbd->defname->Length-7)+".def";
  303. cbd->ID = buildingType->SelectedIndex;
  304. cbd->townID = townID->SelectedIndex;
  305. cbd->x = curx;
  306. cbd->y = cury;
  307. //int curind = bitmapList->SelectedIndex;
  308. bitmapList->Items->RemoveAt(bitmapList->SelectedIndex);
  309. nextPicture();
  310. nextPicture();
  311. data.push_back(cbd);
  312. }
  313. //System::String^ defname;
  314. //System::Int32 ID, x, y;
  315. //System::Int32 townID;
  316. System::Void Wpasujbud::save_Click(System::Object^ sender, System::EventArgs^ e)
  317. {
  318. String ^n = DateTime::Now.ToString()+".txt";
  319. n = n->Replace(L" ",L"___")->Replace(':','_');
  320. IO::StreamWriter sr(IO::Directory::GetCurrentDirectory() + L"\\" + n);
  321. for each (CBuildingData ^Data in data)
  322. {
  323. String ^temp = Data->ToString();
  324. sr.Write(temp);
  325. }
  326. sr.Close();
  327. }
  328. System::Void Wpasujbud::buildingImg_Click(System::Object^ sender, System::EventArgs^ e)
  329. {
  330. if (!buildingImg->Image)
  331. return;
  332. obrazek ^ob = gcnew obrazek(buildingImg->Image);
  333. ob->Show();
  334. }
  335. System::Void Wpasujbud::dataview_Click(System::Object^ sender, System::EventArgs^ e)
  336. {
  337. if (data.size()<=0)
  338. return;
  339. dataEditor ^ de = gcnew dataEditor(%data);
  340. de->Show();
  341. }