Bitflag.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. //******************************************************************************************
  2. // bitflag.cpp - This file contains the bitflag class
  3. //
  4. //---------------------------------------------------------------------------//
  5. // Copyright (C) Microsoft Corporation. All rights reserved. //
  6. //===========================================================================//
  7. //----------------------------------------------------------------------------------
  8. // Include Files
  9. #ifndef BITFLAG_H
  10. #include "bitflag.h"
  11. #endif
  12. #ifndef HEAP_H
  13. #include "heap.h"
  14. #endif
  15. #ifndef FILE_H
  16. #include "file.h"
  17. #endif
  18. #ifndef TGAINFO_H
  19. #include "tgainfo.h"
  20. #endif
  21. #include <gameos.hpp>
  22. extern void AG_ellipse_draw(PANE *pane, LONG xc, LONG yc, LONG width, LONG height, LONG color);
  23. extern void AG_ellipse_fill(PANE *pane, LONG xc, LONG yc, LONG width, LONG height, LONG color);
  24. extern void AG_ellipse_fillXor(PANE *pane, LONG xc, LONG yc, LONG width, LONG height, LONG color);
  25. extern void AG_ellipse_fillOr(PANE *pane, LONG xc, LONG yc, LONG width, LONG height, LONG color);
  26. extern void memclear(void *Dest,int Length);
  27. //------------------------------------------------------------------------
  28. // Class BitFlag
  29. long BitFlag::init (unsigned long numRows, unsigned long numColumns, unsigned long initialValue)
  30. {
  31. rows = numRows;
  32. columns = numColumns;
  33. divValue = BITS_PER_BYTE;
  34. totalFlags = numRows * numColumns;
  35. totalRAM = totalFlags / divValue;
  36. colWidth = columns / divValue;
  37. flagHeap = new HeapManager;
  38. if (!flagHeap)
  39. return(NO_RAM_FOR_FLAG_HEAP);
  40. long result = flagHeap->createHeap(totalRAM);
  41. if (result != NO_ERR)
  42. return(result);
  43. result = flagHeap->commitHeap();
  44. if (result != NO_ERR)
  45. return(result);
  46. resetAll(initialValue);
  47. return(NO_ERR);
  48. }
  49. //------------------------------------------------------------------------
  50. void BitFlag::resetAll (unsigned long initialValue)
  51. {
  52. //------------------------------------------
  53. // Set memory to initial Flag Value
  54. if (initialValue == 0)
  55. {
  56. memclear(flagHeap->getHeapPtr(),totalRAM);
  57. maskValue = 1;
  58. }
  59. else
  60. {
  61. //-----------------------------------------
  62. // Not zero, must jump through hoops.
  63. maskValue = 1;
  64. memset(flagHeap->getHeapPtr(),0xff,totalRAM);
  65. }
  66. }
  67. //------------------------------------------------------------------------
  68. void BitFlag::destroy (void)
  69. {
  70. delete flagHeap;
  71. flagHeap = NULL;
  72. init();
  73. }
  74. //------------------------------------------------------------------------
  75. // This sets location to bits
  76. void BitFlag::setFlag (unsigned long r, unsigned long c)
  77. {
  78. if ((r < 0) || (r >= rows) || (c < 0) || (c > columns))
  79. return;
  80. //-------------------------------
  81. // find out where we are setting.
  82. unsigned long location = (c>>3) + (r*colWidth);
  83. unsigned char shiftValue = (c % divValue);
  84. //----------------------------------------
  85. // find the location we care about.
  86. unsigned char *bitResult = flagHeap->getHeapPtr();
  87. bitResult += location;
  88. //----------------------------------------
  89. // Shift bits to correct place.
  90. unsigned char bits = 1;
  91. bits <<= shiftValue;
  92. *bitResult |= bits;
  93. }
  94. //------------------------------------------------------------------------
  95. void BitFlag::setGroup (unsigned long r, unsigned long c, unsigned long length)
  96. {
  97. if (length)
  98. {
  99. if ((r < 0) || (r >= rows) || (c < 0) || (c > columns) || ((r*c+length) >= (rows*columns)))
  100. return;
  101. //-------------------------------
  102. // find out where we are setting.
  103. unsigned long location = (c>>3) + (r*colWidth);
  104. unsigned char shiftValue = (c % divValue);
  105. //----------------------------------------
  106. // find the location we care about.
  107. unsigned char *bitResult = flagHeap->getHeapPtr();
  108. bitResult += location;
  109. //--------------------------------------------------
  110. // We are only setting bits in the current location
  111. if ((8 - shiftValue) >= (long)length)
  112. {
  113. unsigned char startVal = 1;
  114. unsigned long repeatVal = length;
  115. for (long i=0;i<long(repeatVal-1);i++)
  116. {
  117. startVal<<=1;
  118. startVal++;
  119. }
  120. //----------------------------------------
  121. // Shift bits to correct place.
  122. unsigned char bits = startVal;
  123. bits <<= shiftValue;
  124. *bitResult |= bits;
  125. length -= repeatVal;
  126. }
  127. else
  128. {
  129. //-------------------------------------------
  130. // set the beginning odd bits.
  131. // Remember Intel Architecture (MSB -> LSB)
  132. if (shiftValue)
  133. {
  134. unsigned char startVal = 1;
  135. unsigned long repeatVal = (8-shiftValue);
  136. for (long i=0;i<long(repeatVal-1);i++)
  137. {
  138. startVal<<=1;
  139. startVal++;
  140. }
  141. //----------------------------------------
  142. // Shift bits to correct place.
  143. unsigned char bits = startVal;
  144. bits <<= shiftValue;
  145. *bitResult |= bits;
  146. length -= repeatVal;
  147. }
  148. while (length >= 8)
  149. {
  150. bitResult++;
  151. *bitResult |= 0xff;
  152. length-=8;
  153. }
  154. if (length)
  155. {
  156. bitResult++;
  157. unsigned char startVal = 1;
  158. for (long i=0;i<long(length-1);i++)
  159. {
  160. startVal<<=1;
  161. startVal++;
  162. }
  163. //----------------------------------------
  164. // Shift bits to correct place.
  165. unsigned char bits = startVal;
  166. *bitResult |= bits;
  167. }
  168. }
  169. }
  170. }
  171. //------------------------------------------------------------------------
  172. unsigned char BitFlag::getFlag (unsigned long r, unsigned long c)
  173. {
  174. if ((r < 0) || (r >= rows) || (c < 0) || (c > columns))
  175. return 0;
  176. //------------------------------------
  177. // Find out where we are getting from
  178. unsigned long location = (c>>3) + (r*colWidth);
  179. unsigned char shiftValue = (c % divValue);
  180. //-------------------------------------
  181. // Create mask to remove unwanted bits
  182. unsigned char mask = maskValue;
  183. mask <<= shiftValue;
  184. //-------------------------------------
  185. // get Location value
  186. unsigned char result = *(flagHeap->getHeapPtr() + location);
  187. //-------------------------------------
  188. // mask then shift Bits
  189. result &= mask;
  190. result >>= shiftValue;
  191. return(result);
  192. }
  193. //----------------------------------------------------------------------------------
  194. //------------------------------------------------------------------------
  195. // Class ByteFlag
  196. long ByteFlag::init (unsigned long numRows, unsigned long numColumns, unsigned long initialValue)
  197. {
  198. rows = numRows;
  199. columns = numColumns;
  200. totalFlags = numRows * numColumns;
  201. totalRAM = totalFlags;
  202. flagHeap = new HeapManager;
  203. gosASSERT(flagHeap != NULL);
  204. long result = flagHeap->createHeap(totalRAM);
  205. gosASSERT(result == NO_ERR);
  206. result = flagHeap->commitHeap();
  207. gosASSERT(result == NO_ERR);
  208. resetAll(initialValue);
  209. flagPane = new PANE;
  210. flagWindow = new WINDOW;
  211. flagPane->x0 = 0;
  212. flagPane->y0 = 0;
  213. flagPane->x1 = numColumns;
  214. flagPane->y1 = numRows;
  215. flagPane->window = flagWindow;
  216. flagWindow->buffer = flagHeap->getHeapPtr();
  217. flagWindow->x_max = numColumns-1;
  218. flagWindow->y_max = numRows-1;
  219. flagWindow->stencil = NULL;
  220. flagWindow->shadow = NULL;
  221. return(NO_ERR);
  222. }
  223. //------------------------------------------------------------------------
  224. void ByteFlag::initTGA (char *tgaFileName)
  225. {
  226. File tgaFile;
  227. #ifdef _DEBUG
  228. long result =
  229. #endif
  230. tgaFile.open(tgaFileName);
  231. gosASSERT(result == NO_ERR);
  232. MemoryPtr tgaBuffer = (MemoryPtr)malloc(tgaFile.fileSize());
  233. tgaFile.read(tgaBuffer,tgaFile.fileSize());
  234. //---------------------------------------
  235. // Parse out TGAHeader.
  236. TGAFileHeader *header = (TGAFileHeader *)tgaBuffer;
  237. long height = header->height;
  238. long width = header->width;
  239. init(width,height,0);
  240. gosASSERT(header->image_type != UNC_TRUE);
  241. gosASSERT(header->image_type != RLE_TRUE);
  242. gosASSERT(header->image_type != RLE_PAL);
  243. switch (header->image_type)
  244. {
  245. case UNC_PAL:
  246. {
  247. //------------------------------------------------
  248. // This is just a bitmap. Copy it into ourRAM.
  249. MemoryPtr image = tgaBuffer + sizeof(TGAFileHeader);
  250. if (header->color_map)
  251. image += header->cm_length * (header->cm_entry_size>>3);
  252. MemoryPtr ourRAM = memDump();
  253. memcpy(ourRAM,image,tgaFile.fileSize() - sizeof(TGAFileHeader) - (header->cm_length * (header->cm_entry_size>>3)));
  254. //------------------------------------------------------------------------
  255. // Must check image_descriptor to see if we need to un upside down image.
  256. bool left = (header->image_descriptor & 16) != 0;
  257. bool top = (header->image_descriptor & 32) != 0;
  258. if (!top && !left)
  259. {
  260. //--------------------------------
  261. // Image is Upside down.
  262. flipTopToBottom(ourRAM,header->pixel_depth,width,height);
  263. }
  264. else if (!top && left)
  265. {
  266. flipTopToBottom(ourRAM,header->pixel_depth,width,height);
  267. //flipRightToLeft(ourRAM,header->pixel_depth,width,height);
  268. }
  269. else if (top && left)
  270. {
  271. //flipRightToLeft(ourRAM,header->pixel_depth,width,height);
  272. }
  273. }
  274. break;
  275. }
  276. free(tgaBuffer);
  277. }
  278. //------------------------------------------------------------------------
  279. void ByteFlag::setCircle (unsigned long x, unsigned long y, unsigned long radius, unsigned char value)
  280. {
  281. if (radius)
  282. AG_ellipse_fillOr(flagPane,x,y,radius,radius,value);
  283. }
  284. //------------------------------------------------------------------------
  285. void ByteFlag::clearCircle (unsigned long x, unsigned long y, unsigned long radius, unsigned char value)
  286. {
  287. if (radius)
  288. AG_ellipse_fillXor(flagPane,x,y,radius,radius,value);
  289. }
  290. //------------------------------------------------------------------------
  291. void ByteFlag::resetAll (unsigned long initialValue)
  292. {
  293. //------------------------------------------
  294. // Set memory to initial Flag Value
  295. if (initialValue == 0)
  296. {
  297. memclear(flagHeap->getHeapPtr(),totalRAM);
  298. }
  299. else
  300. {
  301. memset(flagHeap->getHeapPtr(),0xff,totalRAM);
  302. }
  303. }
  304. //------------------------------------------------------------------------
  305. void ByteFlag::destroy (void)
  306. {
  307. delete flagHeap;
  308. flagHeap = NULL;
  309. delete flagPane;
  310. delete flagWindow;
  311. flagPane = NULL;
  312. flagWindow = NULL;
  313. init();
  314. }
  315. //------------------------------------------------------------------------
  316. // This sets location to bits
  317. void ByteFlag::setFlag (unsigned long r, unsigned long c)
  318. {
  319. if ((r < 0) || (r >= rows) || (c < 0) || (c > columns))
  320. return;
  321. //-------------------------------
  322. // find out where we are setting.
  323. unsigned long location = c + (r*columns);
  324. //----------------------------------------
  325. // find the location we care about.
  326. unsigned char *bitResult = flagHeap->getHeapPtr();
  327. bitResult += location;
  328. //--------------------------------------------
  329. // Set is Zero because we use this to DRAW!!!
  330. *bitResult = 0xff;
  331. }
  332. //------------------------------------------------------------------------
  333. void ByteFlag::setGroup (unsigned long r, unsigned long c, unsigned long length)
  334. {
  335. if (length)
  336. {
  337. if ((r < 0) || (r >= rows) || (c < 0) || (c > columns) || ((r*c+length) >= (rows*columns)))
  338. return;
  339. //-------------------------------
  340. // find out where we are setting.
  341. unsigned long location = c + (r*columns);
  342. //----------------------------------------
  343. // find the location we care about.
  344. unsigned char *bitResult = flagHeap->getHeapPtr();
  345. bitResult += location;
  346. //--------------------------------------------------
  347. // We are only setting bits in the current location
  348. memset(bitResult,0xff,length);
  349. }
  350. }
  351. //------------------------------------------------------------------------
  352. unsigned char ByteFlag::getFlag (unsigned long r, unsigned long c)
  353. {
  354. if ((r < 0) || (r >= rows) || (c < 0) || (c > columns))
  355. return 0;
  356. //------------------------------------
  357. // Find out where we are getting from
  358. unsigned long location = c + (r*columns);
  359. //-------------------------------------
  360. // get Location value
  361. unsigned char result = *(flagHeap->getHeapPtr() + location);
  362. //-------------------
  363. // Just return bits.
  364. return (result);
  365. }
  366. //----------------------------------------------------------------------------------