Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

322 lines
5.2 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include <windows.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <stdlib.h>
  10. #include <stdarg.h>
  11. #include "goldsrc_standin.h"
  12. extern int errno;
  13. static unsigned short g_InitialColor = 0xFFFF;
  14. static unsigned short g_LastColor = 0xFFFF;
  15. static unsigned short g_BadColor = 0xFFFF;
  16. static WORD g_BackgroundFlags = 0xFFFF;
  17. static bool g_bGotInitialColors = false;
  18. static void GetInitialColors( )
  19. {
  20. if ( g_bGotInitialColors )
  21. return;
  22. g_bGotInitialColors = true;
  23. // Get the old background attributes.
  24. CONSOLE_SCREEN_BUFFER_INFO oldInfo;
  25. GetConsoleScreenBufferInfo( GetStdHandle( STD_OUTPUT_HANDLE ), &oldInfo );
  26. g_InitialColor = g_LastColor = oldInfo.wAttributes & (FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_BLUE|FOREGROUND_INTENSITY);
  27. g_BackgroundFlags = oldInfo.wAttributes & (BACKGROUND_RED|BACKGROUND_GREEN|BACKGROUND_BLUE|BACKGROUND_INTENSITY);
  28. g_BadColor = 0;
  29. if (g_BackgroundFlags & BACKGROUND_RED)
  30. g_BadColor |= FOREGROUND_RED;
  31. if (g_BackgroundFlags & BACKGROUND_GREEN)
  32. g_BadColor |= FOREGROUND_GREEN;
  33. if (g_BackgroundFlags & BACKGROUND_BLUE)
  34. g_BadColor |= FOREGROUND_BLUE;
  35. if (g_BackgroundFlags & BACKGROUND_INTENSITY)
  36. g_BadColor |= FOREGROUND_INTENSITY;
  37. }
  38. static WORD SetConsoleTextColor( int red, int green, int blue, int intensity )
  39. {
  40. GetInitialColors();
  41. WORD ret = g_LastColor;
  42. g_LastColor = 0;
  43. if( red ) g_LastColor |= FOREGROUND_RED;
  44. if( green ) g_LastColor |= FOREGROUND_GREEN;
  45. if( blue ) g_LastColor |= FOREGROUND_BLUE;
  46. if( intensity ) g_LastColor |= FOREGROUND_INTENSITY;
  47. // Just use the initial color if there's a match...
  48. if (g_LastColor == g_BadColor)
  49. g_LastColor = g_InitialColor;
  50. SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), g_LastColor | g_BackgroundFlags );
  51. return ret;
  52. }
  53. static void RestoreConsoleTextColor( WORD color )
  54. {
  55. SetConsoleTextAttribute( GetStdHandle( STD_OUTPUT_HANDLE ), color | g_BackgroundFlags );
  56. g_LastColor = color;
  57. }
  58. void Msg( const char *pMsg, ... )
  59. {
  60. va_list marker;
  61. va_start( marker, pMsg );
  62. vprintf( pMsg, marker );
  63. va_end( marker );
  64. }
  65. void Warning( const char *pMsg, ... )
  66. {
  67. WORD old = SetConsoleTextColor( 1, 1, 0, 1 );
  68. va_list marker;
  69. va_start( marker, pMsg );
  70. vprintf( pMsg, marker );
  71. va_end( marker );
  72. RestoreConsoleTextColor( old );
  73. }
  74. void Error (const char *error, ...)
  75. {
  76. WORD old = SetConsoleTextColor( 1, 0, 0, 1 );
  77. va_list argptr;
  78. printf ("\n************ ERROR ************\n");
  79. va_start (argptr,error);
  80. vprintf (error,argptr);
  81. va_end (argptr);
  82. printf ("\n");
  83. extern void PrintExitStuff();
  84. PrintExitStuff();
  85. RestoreConsoleTextColor( old );
  86. TerminateProcess( GetCurrentProcess(), 100 );
  87. }
  88. /*
  89. ================
  90. filelength
  91. ================
  92. */
  93. int filelength (FILE *f)
  94. {
  95. int pos;
  96. int end;
  97. pos = ftell (f);
  98. fseek (f, 0, SEEK_END);
  99. end = ftell (f);
  100. fseek (f, pos, SEEK_SET);
  101. return end;
  102. }
  103. FILE *SafeOpenWrite (char *filename)
  104. {
  105. FILE *f;
  106. f = fopen(filename, "wb");
  107. if (!f)
  108. Error ("Error opening %s: %s",filename,strerror(errno));
  109. return f;
  110. }
  111. FILE *SafeOpenRead (char *filename)
  112. {
  113. FILE *f;
  114. f = fopen(filename, "rb");
  115. if (!f)
  116. Error ("Error opening %s: %s",filename,strerror(errno));
  117. return f;
  118. }
  119. void SafeRead (FILE *f, void *buffer, int count)
  120. {
  121. if ( fread (buffer, 1, count, f) != (size_t)count)
  122. Error ("File read failure");
  123. }
  124. void SafeWrite (FILE *f, void *buffer, int count)
  125. {
  126. if (fwrite (buffer, 1, count, f) != (size_t)count)
  127. Error ("File read failure");
  128. }
  129. /*
  130. ==============
  131. LoadFile
  132. ==============
  133. */
  134. int LoadFile (char *filename, void **bufferptr)
  135. {
  136. FILE *f;
  137. int length;
  138. void *buffer;
  139. f = SafeOpenRead (filename);
  140. length = filelength (f);
  141. buffer = malloc (length+1);
  142. ((char *)buffer)[length] = 0;
  143. SafeRead (f, buffer, length);
  144. fclose (f);
  145. *bufferptr = buffer;
  146. return length;
  147. }
  148. void SaveFile (char *filename, void *buffer, int count)
  149. {
  150. FILE *f;
  151. f = SafeOpenWrite (filename);
  152. SafeWrite (f, buffer, count);
  153. fclose (f);
  154. }
  155. #ifdef __BIG_ENDIAN__
  156. short LittleShort (short l)
  157. {
  158. byte b1,b2;
  159. b1 = l&255;
  160. b2 = (l>>8)&255;
  161. return (b1<<8) + b2;
  162. }
  163. short BigShort (short l)
  164. {
  165. return l;
  166. }
  167. int LittleLong (int l)
  168. {
  169. byte b1,b2,b3,b4;
  170. b1 = l&255;
  171. b2 = (l>>8)&255;
  172. b3 = (l>>16)&255;
  173. b4 = (l>>24)&255;
  174. return ((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4;
  175. }
  176. int BigLong (int l)
  177. {
  178. return l;
  179. }
  180. float LittleFloat (float l)
  181. {
  182. union {byte b[4]; float f;} in, out;
  183. in.f = l;
  184. out.b[0] = in.b[3];
  185. out.b[1] = in.b[2];
  186. out.b[2] = in.b[1];
  187. out.b[3] = in.b[0];
  188. return out.f;
  189. }
  190. float BigFloat (float l)
  191. {
  192. return l;
  193. }
  194. #else
  195. short BigShort (short l)
  196. {
  197. byte b1,b2;
  198. b1 = l&255;
  199. b2 = (l>>8)&255;
  200. return (b1<<8) + b2;
  201. }
  202. short LittleShort (short l)
  203. {
  204. return l;
  205. }
  206. int BigLong (int l)
  207. {
  208. byte b1,b2,b3,b4;
  209. b1 = l&255;
  210. b2 = (l>>8)&255;
  211. b3 = (l>>16)&255;
  212. b4 = (l>>24)&255;
  213. return ((int)b1<<24) + ((int)b2<<16) + ((int)b3<<8) + b4;
  214. }
  215. int LittleLong (int l)
  216. {
  217. return l;
  218. }
  219. float BigFloat (float l)
  220. {
  221. union {byte b[4]; float f;} in, out;
  222. in.f = l;
  223. out.b[0] = in.b[3];
  224. out.b[1] = in.b[2];
  225. out.b[2] = in.b[1];
  226. out.b[3] = in.b[0];
  227. return out.f;
  228. }
  229. float LittleFloat (float l)
  230. {
  231. return l;
  232. }
  233. #endif