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