Bitmap2MapConverter.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. FinalSun/FinalAlert 2 Mission Editor
  3. Copyright (C) 1999-2024 Electronic Arts, Inc.
  4. Authored by Matthias Wagner
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. */
  16. // Bitmap2MapConverter.cpp: Implementierung der Klasse CBitmap2MapConverter.
  17. //
  18. //////////////////////////////////////////////////////////////////////
  19. #include "stdafx.h"
  20. #include "stdafx.h"
  21. #include "Bitmap2MapConverter.h"
  22. #ifdef _DEBUG
  23. #undef THIS_FILE
  24. static char THIS_FILE[]=__FILE__;
  25. #define new DEBUG_NEW
  26. #endif
  27. //////////////////////////////////////////////////////////////////////
  28. // Konstruktion/Destruktion
  29. //////////////////////////////////////////////////////////////////////
  30. CBitmap2MapConverter::CBitmap2MapConverter()
  31. {
  32. }
  33. CBitmap2MapConverter::~CBitmap2MapConverter()
  34. {
  35. }
  36. /*
  37. I originally intended this tool to support different theaters,
  38. but currently it does only support temperate. People can use
  39. copy/paste to convert those maps though!
  40. */
  41. BOOL CBitmap2MapConverter::Convert(HBITMAP hBitmap, CMapData & mapdata)
  42. {
  43. BITMAP bm;
  44. GetObject(hBitmap, sizeof(BITMAP), &bm);
  45. HBITMAP hUsed=hBitmap;
  46. if(bm.bmWidth+bm.bmHeight>255)
  47. {
  48. float scalex=(float)bm.bmWidth/(float)bm.bmHeight;
  49. int neededheight, neededwidth;
  50. neededheight=255.0f/(scalex+1.0f);
  51. neededwidth=255-neededheight;
  52. hUsed=CreateCompatibleBitmap(GetDC(NULL), neededwidth, neededheight);
  53. HDC hDC=CreateCompatibleDC(GetDC(NULL));
  54. SelectObject(hDC, hUsed);
  55. HDC hSrcDC=CreateCompatibleDC(GetDC(NULL));
  56. SelectObject(hSrcDC, hBitmap);
  57. StretchBlt(hDC, 0,0,neededwidth,neededheight, hSrcDC, 0,0,bm.bmWidth, bm.bmHeight, SRCCOPY);
  58. DeleteDC(hDC);
  59. DeleteDC(hSrcDC);
  60. GetObject(hUsed, sizeof(BITMAP), &bm);
  61. }
  62. HDC hDC;
  63. hDC=CreateCompatibleDC(GetDC(NULL));
  64. SelectObject(hDC, hUsed);
  65. srand(GetTickCount());
  66. int i;
  67. int e;
  68. int theater=0;
  69. mapdata.CreateMap(bm.bmWidth, bm.bmHeight, "TEMPERATE", 0);
  70. int isosize=mapdata.GetIsoSize();
  71. for(i=0;i<(*tiledata_count);i++)
  72. if((*tiledata)[i].wTileSet==waterset) break;
  73. int water_start=i+8; // to 12
  74. int sandset=atoi((*tiles).sections["General"].values["SandTile"]);
  75. int greenset=atoi((*tiles).sections["General"].values["GreenTile"]);
  76. #ifdef RA2_MODE
  77. sandset=atoi((*tiles).sections["General"].values["GreenTile"]);
  78. greenset=atoi((*tiles).sections["General"].values["RoughTile"]);
  79. #endif
  80. for(i=0;i<(*tiledata_count);i++)
  81. if((*tiledata)[i].wTileSet==sandset) break;
  82. int sand_start=i;
  83. for(i=0;i<(*tiledata_count);i++)
  84. if((*tiledata)[i].wTileSet==greenset) break;
  85. int green_start=i;
  86. for(i=0;i<bm.bmWidth;i++)
  87. {
  88. for(e=0;e<bm.bmHeight;e++)
  89. {
  90. COLORREF col=GetPixel(hDC, i, bm.bmHeight-e);
  91. int x=(i)+mapdata.GetHeight()+1;
  92. int y=(e)+mapdata.GetWidth();
  93. int xiso;
  94. int yiso;
  95. yiso=mapdata.GetIsoSize()-(y - x);
  96. xiso=mapdata.GetIsoSize()-(x + y);
  97. yiso-=mapdata.GetHeight();
  98. for(x=-1;x<2;x++)
  99. {
  100. for(y=-1;y<2;y++)
  101. {
  102. DWORD dwPos=xiso+x+(yiso+y)*isosize;
  103. if(dwPos>isosize*isosize) continue;
  104. FIELDDATA* fd=mapdata.GetFielddataAt(dwPos);
  105. int r=GetRValue(col);
  106. int g=GetGValue(col);
  107. int b=GetBValue(col);
  108. if(g>r && g>b)
  109. {
  110. if(theater!=1)
  111. {
  112. fd->wGround=0;
  113. fd->bSubTile=0;
  114. }
  115. }
  116. if(b>g && b>r)
  117. {
  118. int p=rand()*4/RAND_MAX;
  119. fd->wGround=water_start+p;
  120. fd->bSubTile=0;
  121. }
  122. if(g>b+25 && r>b+25 && g>120 && r>120)
  123. {
  124. if(theater!=1)
  125. {
  126. fd->wGround=sand_start;
  127. fd->bSubTile=0;
  128. }
  129. }
  130. if(b<20 && r<20 && g>20)
  131. {
  132. #ifdef RA2_MODE
  133. if(g<140) // dark only
  134. #endif
  135. {
  136. fd->wGround=green_start;
  137. fd->bSubTile=0;
  138. }
  139. }
  140. }
  141. }
  142. }
  143. }
  144. mapdata.CreateShore(0,0,isosize, isosize);
  145. for(i=0;i<isosize*isosize;i++)
  146. {
  147. mapdata.SmoothAllAt(i);
  148. }
  149. DeleteDC(hDC);
  150. if(hUsed!=hBitmap) DeleteObject(hUsed);
  151. return TRUE;
  152. }