JSHELL.H 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. //
  2. // Copyright 2020 Electronic Arts Inc.
  3. //
  4. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is free
  5. // software: you can redistribute it and/or modify it under the terms of
  6. // the GNU General Public License as published by the Free Software Foundation,
  7. // either version 3 of the License, or (at your option) any later version.
  8. // TiberianDawn.DLL and RedAlert.dll and corresponding source code is distributed
  9. // in the hope that it will be useful, but with permitted additional restrictions
  10. // under Section 7 of the GPL. See the GNU General Public License in LICENSE.TXT
  11. // distributed with this program. You should have received a copy of the
  12. // GNU General Public License along with permitted additional restrictions
  13. // with this program. If not, see https://github.com/electronicarts/CnC_Remastered_Collection
  14. /* $Header: /CounterStrike/JSHELL.H 1 3/03/97 10:24a Joe_bostic $ */
  15. /***********************************************************************************************
  16. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  17. ***********************************************************************************************
  18. * *
  19. * Project Name : Command & Conquer *
  20. * *
  21. * File Name : JSHELL.H *
  22. * *
  23. * Programmer : Joe L. Bostic *
  24. * *
  25. * Start Date : 03/13/95 *
  26. * *
  27. * Last Update : March 13, 1995 [JLB] *
  28. * *
  29. *---------------------------------------------------------------------------------------------*
  30. * Functions: *
  31. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  32. #ifndef JSHELL_H
  33. #define JSHELL_H
  34. #include <assert.h>
  35. #ifdef WIN32
  36. //#define getch Get_Key_Num
  37. //#define kbhit Check_Key_Num
  38. #include "key.h"
  39. #else
  40. #include <conio.h>
  41. #endif
  42. /*
  43. ** Interface class to the keyboard. This insulates the game from library vagaries. Most
  44. ** notable being the return values are declared as "int" in the library whereas C&C
  45. ** expects it to be of KeyNumType.
  46. */
  47. #ifdef WIN32
  48. //#define KeyNumType int
  49. //#define KeyASCIIType int
  50. //lint -esym(1725,KeyboardClass::MouseQX,KeyboardClass::MouseQY)
  51. struct KeyboardClass : public WWKeyboardClass
  52. #else
  53. struct KeyboardClass
  54. #endif
  55. {
  56. /*
  57. ** This flag is used to indicate whether the WW library has taken over
  58. ** the keyboard or not. If not, then the normal console input
  59. ** takes precedence.
  60. */
  61. unsigned IsLibrary;
  62. #ifndef WIN32
  63. int &MouseQX;
  64. int &MouseQY;
  65. KeyboardClass() :
  66. IsLibrary(true),
  67. MouseQX(::MouseQX),
  68. MouseQY(::MouseQY)
  69. {}
  70. KeyNumType Get(void) {return (IsLibrary ? (KeyNumType)Get_Key_Num() : (KeyNumType)getch());};
  71. KeyNumType Check(void) {return (IsLibrary ? (KeyNumType)Check_Key_Num() : (KeyNumType)kbhit());};
  72. KeyASCIIType To_ASCII(KeyNumType key) {return((KeyASCIIType)KN_To_KA(key));};
  73. void Clear(void) {if (IsLibrary) Clear_KeyBuffer();};
  74. int Down(KeyNumType key) {return(Key_Down(key));};
  75. #else
  76. KeyboardClass() : IsLibrary(true) {}
  77. KeyNumType Get(void) {return ((KeyNumType)WWKeyboardClass::Get());};
  78. KeyNumType Check(void) {return ((KeyNumType)WWKeyboardClass::Check());};
  79. KeyASCIIType To_ASCII(KeyNumType key) {return((KeyASCIIType)WWKeyboardClass::To_ASCII(key));};
  80. void Clear(void) {WWKeyboardClass::Clear();};
  81. int Down(KeyNumType key) {return(WWKeyboardClass::Down(key));};
  82. #endif
  83. int Mouse_X(void) {return(Get_Mouse_X());};
  84. int Mouse_Y(void) {return(Get_Mouse_Y());};
  85. };
  86. /*
  87. ** These templates allow enumeration types to have simple bitwise
  88. ** arithmatic performed. The operators must be instatiated for the
  89. ** enumerated types desired.
  90. */
  91. template<class T> inline T operator ++(T & a)
  92. {
  93. a = (T)((int)a + (int)1);
  94. return(a);
  95. }
  96. template<class T> inline T operator ++(T & a, int)
  97. {
  98. T aa = a;
  99. a = (T)((int)a + (int)1);
  100. return(aa);
  101. }
  102. template<class T> inline T operator --(T & a)
  103. {
  104. a = (T)((int)a - (int)1);
  105. return(a);
  106. }
  107. template<class T> inline T operator --(T & a, int)
  108. {
  109. T aa = a;
  110. a = (T)((int)a - (int)1);
  111. return(aa);
  112. }
  113. template<class T> inline T operator |(T t1, T t2)
  114. {
  115. return((T)((int)t1 | (int)t2));
  116. }
  117. template<class T> inline T operator &(T t1, T t2)
  118. {
  119. return((T)((int)t1 & (int)t2));
  120. }
  121. template<class T> inline T operator ~(T t1)
  122. {
  123. return((T)(~(int)t1));
  124. }
  125. #ifndef WIN32
  126. template<class T> inline T min(T value1, T value2)
  127. {
  128. if (value1 < value2) {
  129. return(value1);
  130. }
  131. return(value2);
  132. }
  133. int min(int, int);
  134. long min(long, long);
  135. template<class T> inline T max(T value1, T value2)
  136. {
  137. if (value1 > value2) {
  138. return(value1);
  139. }
  140. return(value2);
  141. }
  142. int max(int, int);
  143. long max(long, long);
  144. #endif
  145. template<class T> inline void swap(T &value1, T &value2)
  146. {
  147. T temp = value1;
  148. value1 = value2;
  149. value2 = temp;
  150. }
  151. int swap(int, int);
  152. long swap(long, long);
  153. template<class T> inline
  154. T Bound(T original, T minval, T maxval)
  155. {
  156. if (original < minval) return(minval);
  157. if (original > maxval) return(maxval);
  158. return(original);
  159. };
  160. int Bound(signed int, signed int, signed int);
  161. unsigned Bound(unsigned, unsigned, unsigned);
  162. long Bound(long, long, long);
  163. template<class T>
  164. T _rotl(T X, int n)
  165. {
  166. return((T)(( ( ( X ) << n ) | ( ( X ) >> ( (sizeof(T)*8) - n ) ) )));
  167. }
  168. /*
  169. ** This macro serves as a general way to determine the number of elements
  170. ** within an array.
  171. */
  172. #define ARRAY_LENGTH(x) int(sizeof(x)/sizeof(x[0]))
  173. #define ARRAY_SIZE(x) int(sizeof(x)/sizeof(x[0]))
  174. /*
  175. ** The shape flags are likely to be "or"ed together and other such bitwise
  176. ** manipulations. These instatiated operator templates allow this.
  177. */
  178. inline ShapeFlags_Type operator |(ShapeFlags_Type, ShapeFlags_Type);
  179. inline ShapeFlags_Type operator &(ShapeFlags_Type, ShapeFlags_Type);
  180. inline ShapeFlags_Type operator ~(ShapeFlags_Type);
  181. void __cdecl Set_Bit(void * array, int bit, int value);
  182. int __cdecl Get_Bit(void const * array, int bit);
  183. int __cdecl First_True_Bit(void const * array);
  184. int __cdecl First_False_Bit(void const * array);
  185. int __cdecl Bound(int original, int min, int max);
  186. #if (0)
  187. void Set_Bit(void * array, int bit, int value);
  188. #pragma aux Set_Bit parm [esi] [ecx] [eax] \
  189. modify [esi ebx] = \
  190. "mov ebx,ecx" \
  191. "shr ebx,5" \
  192. "and ecx,01Fh" \
  193. "btr [esi+ebx*4],ecx" \
  194. "or eax,eax" \
  195. "jz ok" \
  196. "bts [esi+ebx*4],ecx" \
  197. "ok:"
  198. int Get_Bit(void const * array, int bit);
  199. #pragma aux Get_Bit parm [esi] [eax] \
  200. modify [esi ebx] \
  201. value [eax] = \
  202. "mov ebx,eax" \
  203. "shr ebx,5" \
  204. "and eax,01Fh" \
  205. "bt [esi+ebx*4],eax" \
  206. "setc al"
  207. int First_True_Bit(void const * array);
  208. #pragma aux First_True_Bit parm [esi] \
  209. modify [esi ebx] \
  210. value [eax] = \
  211. "mov eax,-32" \
  212. "again:" \
  213. "add eax,32" \
  214. "mov ebx,[esi]" \
  215. "add esi,4" \
  216. "bsf ebx,ebx" \
  217. "jz again" \
  218. "add eax,ebx"
  219. int First_False_Bit(void const * array);
  220. #pragma aux First_False_Bit parm [esi] \
  221. modify [esi ebx] \
  222. value [eax] = \
  223. "mov eax,-32" \
  224. "again:" \
  225. "add eax,32" \
  226. "mov ebx,[esi]" \
  227. "not ebx" \
  228. "add esi,4" \
  229. "bsf ebx,ebx" \
  230. "jz again" \
  231. "add eax,ebx"
  232. #ifdef OBSOLETE
  233. extern int Bound(int original, int min, int max);
  234. #pragma aux Bound parm [eax] [ebx] [ecx] \
  235. modify [eax] \
  236. value [eax] = \
  237. "cmp ebx,ecx" \
  238. "jl okorder" \
  239. "xchg ebx,ecx" \
  240. "okorder: cmp eax,ebx" \
  241. "jg okmin" \
  242. "mov eax,ebx" \
  243. "okmin: cmp eax,ecx" \
  244. "jl okmax" \
  245. "mov eax,ecx" \
  246. "okmax:"
  247. extern unsigned Bound(unsigned original, unsigned min, unsigned max);
  248. #pragma aux Bound parm [eax] [ebx] [ecx] \
  249. modify [eax] \
  250. value [eax] = \
  251. "cmp ebx,ecx" \
  252. "jb okorder" \
  253. "xchg ebx,ecx" \
  254. "okorder: cmp eax,ebx" \
  255. "ja okmin" \
  256. "mov eax,ebx" \
  257. "okmin: cmp eax,ecx" \
  258. "jb okmax" \
  259. "mov eax,ecx" \
  260. "okmax:"
  261. #endif
  262. unsigned Fixed_To_Cardinal(unsigned base, unsigned fixed);
  263. #pragma aux Fixed_To_Cardinal parm [eax] [edx] \
  264. modify [edx] \
  265. value [eax] = \
  266. "mul edx" \
  267. "add eax,080h" \
  268. "test eax,0FF000000h" \
  269. "jz ok" \
  270. "mov eax,000FFFFFFh" \
  271. "ok:" \
  272. "shr eax,8"
  273. unsigned Cardinal_To_Fixed(unsigned base, unsigned cardinal);
  274. #pragma aux Cardinal_To_Fixed parm [ebx] [eax] \
  275. modify [edx] \
  276. value [eax] = \
  277. "or ebx,ebx" \
  278. "jz fini" \
  279. "shl eax,8" \
  280. "xor edx,edx" \
  281. "div ebx" \
  282. "fini:"
  283. #ifndef OUTPORTB
  284. #define OUTPORTB
  285. extern void outportb(int port, unsigned char data);
  286. #pragma aux outportb parm [edx] [al] = \
  287. "out dx,al"
  288. extern void outport(int port, unsigned short data);
  289. #pragma aux outport parm [edx] [ax] = \
  290. "out dx,al" \
  291. "inc dx" \
  292. "mov al,ah" \
  293. "out dx,al"
  294. #endif
  295. #endif
  296. /*
  297. ** Timer objects that fetch the appropriate timer value according to
  298. ** the type of timer they are.
  299. */
  300. extern long Frame;
  301. class FrameTimerClass
  302. {
  303. public:
  304. long operator () (void) const {return(Frame);};
  305. operator long (void) const {return(Frame);};
  306. };
  307. #ifndef WIN32
  308. extern bool TimerSystemOn;
  309. extern "C" {
  310. long Get_System_Tick_Count(void);
  311. long Get_User_Tick_Count(void);
  312. }
  313. //bool Init_Timer_System(unsigned int freq, int partial=false);
  314. bool Remove_Timer_System(void);
  315. #else
  316. extern WinTimerClass * WindowsTimer;
  317. #endif
  318. #ifndef SYSTEM_TIMER_CLASS
  319. #define SYSTEM_TIMER_CLASS
  320. class SystemTimerClass
  321. {
  322. public:
  323. #ifdef WIN32
  324. long operator () (void) const {if (!WindowsTimer) return(0);return(WindowsTimer->Get_System_Tick_Count());};
  325. operator long (void) const {if (!WindowsTimer) return(0);return(WindowsTimer->Get_System_Tick_Count());};
  326. #else
  327. long operator () (void) const {return(Get_System_Tick_Count());};
  328. operator long (void) const {return(Get_System_Tick_Count());};
  329. #endif
  330. };
  331. #endif
  332. class UserTimerClass
  333. {
  334. public:
  335. #ifdef WIN32
  336. long operator () (void) const {if (!WindowsTimer) return(0);return(WindowsTimer->Get_User_Tick_Count());};
  337. operator long (void) const {if (!WindowsTimer) return(0);return(WindowsTimer->Get_User_Tick_Count());};
  338. #else
  339. long operator () (void) const {return(Get_User_Tick_Count());};
  340. operator long (void) const {return(Get_User_Tick_Count());};
  341. #endif
  342. };
  343. template<class T>
  344. void Bubble_Sort(T * array, int count)
  345. {
  346. if (array != NULL && count > 1) {
  347. bool swapflag;
  348. do {
  349. swapflag = false;
  350. for (int index = 0; index < count-1; index++) {
  351. if (array[index] > array[index+1]) {
  352. T temp = array[index];
  353. array[index] = array[index+1];
  354. array[index+1] = temp;
  355. swapflag = true;
  356. }
  357. }
  358. } while (swapflag);
  359. }
  360. }
  361. template<class T>
  362. void PBubble_Sort(T * array, int count)
  363. {
  364. if (array != NULL && count > 1) {
  365. bool swapflag;
  366. do {
  367. swapflag = false;
  368. for (int index = 0; index < count-1; index++) {
  369. if (*array[index] > *array[index+1]) {
  370. T temp = array[index];
  371. array[index] = array[index+1];
  372. array[index+1] = temp;
  373. swapflag = true;
  374. }
  375. }
  376. } while (swapflag);
  377. }
  378. }
  379. template<class T>
  380. void PNBubble_Sort(T * array, int count)
  381. {
  382. if (array != NULL && count > 1) {
  383. bool swapflag;
  384. do {
  385. swapflag = false;
  386. for (int index = 0; index < count-1; index++) {
  387. if (stricmp(array[index]->Name(), array[index+1]->Name()) > 0) {
  388. T temp = array[index];
  389. array[index] = array[index+1];
  390. array[index+1] = temp;
  391. swapflag = true;
  392. }
  393. }
  394. } while (swapflag);
  395. }
  396. }
  397. template<class T>
  398. class SmartPtr
  399. {
  400. public:
  401. SmartPtr(NoInitClass const &) {}
  402. SmartPtr(T * realptr = 0) : Pointer(realptr) {}
  403. SmartPtr(SmartPtr const & rvalue) : Pointer(rvalue.Pointer) {}
  404. ~SmartPtr(void) {Pointer = 0;}
  405. operator T * (void) const {return(Pointer);}
  406. operator long (void) const {return((long)Pointer);}
  407. SmartPtr<T> operator ++ (int) {assert(Pointer != 0);SmartPtr<T> temp = *this;++Pointer;return(temp);}
  408. SmartPtr<T> & operator ++ (void) {assert(Pointer != 0);++Pointer;return(*this);}
  409. SmartPtr<T> operator -- (int) {assert(Pointer != 0);SmartPtr<T> temp = *this;--Pointer;return(temp);}
  410. SmartPtr<T> & operator -- (void) {assert(Pointer != 0);--Pointer;return(*this);}
  411. SmartPtr & operator = (SmartPtr const & rvalue) {Pointer = rvalue.Pointer;return(*this);}
  412. T * operator -> (void) const {assert(Pointer != 0);return(Pointer);}
  413. T & operator * (void) const {assert(Pointer != 0);return(*Pointer);}
  414. private:
  415. T * Pointer;
  416. };
  417. #endif