JSHELL.H 12 KB

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