sys_win.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. Copyright (C) 1996-1997 Id Software, Inc.
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. See the GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. #include <sys/types.h>
  16. #include <sys/timeb.h>
  17. #include "qwsvdef.h"
  18. #include <winsock.h>
  19. #include <conio.h>
  20. cvar_t sys_nostdout = {"sys_nostdout","0"};
  21. /*
  22. ================
  23. Sys_FileTime
  24. ================
  25. */
  26. int Sys_FileTime (char *path)
  27. {
  28. FILE *f;
  29. f = fopen(path, "rb");
  30. if (f)
  31. {
  32. fclose(f);
  33. return 1;
  34. }
  35. return -1;
  36. }
  37. /*
  38. ================
  39. Sys_mkdir
  40. ================
  41. */
  42. void Sys_mkdir (char *path)
  43. {
  44. _mkdir(path);
  45. }
  46. /*
  47. ================
  48. Sys_Error
  49. ================
  50. */
  51. void Sys_Error (char *error, ...)
  52. {
  53. va_list argptr;
  54. char text[1024];
  55. va_start (argptr,error);
  56. vsprintf (text, error,argptr);
  57. va_end (argptr);
  58. // MessageBox(NULL, text, "Error", 0 /* MB_OK */ );
  59. printf ("ERROR: %s\n", text);
  60. exit (1);
  61. }
  62. /*
  63. ================
  64. Sys_DoubleTime
  65. ================
  66. */
  67. double Sys_DoubleTime (void)
  68. {
  69. double t;
  70. struct _timeb tstruct;
  71. static int starttime;
  72. _ftime( &tstruct );
  73. if (!starttime)
  74. starttime = tstruct.time;
  75. t = (tstruct.time-starttime) + tstruct.millitm*0.001;
  76. return t;
  77. }
  78. /*
  79. ================
  80. Sys_ConsoleInput
  81. ================
  82. */
  83. char *Sys_ConsoleInput (void)
  84. {
  85. static char text[256];
  86. static int len;
  87. int c;
  88. // read a line out
  89. while (_kbhit())
  90. {
  91. c = _getch();
  92. putch (c);
  93. if (c == '\r')
  94. {
  95. text[len] = 0;
  96. putch ('\n');
  97. len = 0;
  98. return text;
  99. }
  100. if (c == 8)
  101. {
  102. if (len)
  103. {
  104. putch (' ');
  105. putch (c);
  106. len--;
  107. text[len] = 0;
  108. }
  109. continue;
  110. }
  111. text[len] = c;
  112. len++;
  113. text[len] = 0;
  114. if (len == sizeof(text))
  115. len = 0;
  116. }
  117. return NULL;
  118. }
  119. /*
  120. ================
  121. Sys_Printf
  122. ================
  123. */
  124. void Sys_Printf (char *fmt, ...)
  125. {
  126. va_list argptr;
  127. if (sys_nostdout.value)
  128. return;
  129. va_start (argptr,fmt);
  130. vprintf (fmt,argptr);
  131. va_end (argptr);
  132. }
  133. /*
  134. ================
  135. Sys_Quit
  136. ================
  137. */
  138. void Sys_Quit (void)
  139. {
  140. exit (0);
  141. }
  142. /*
  143. =============
  144. Sys_Init
  145. Quake calls this so the system can register variables before host_hunklevel
  146. is marked
  147. =============
  148. */
  149. void Sys_Init (void)
  150. {
  151. Cvar_RegisterVariable (&sys_nostdout);
  152. }
  153. /*
  154. ==================
  155. main
  156. ==================
  157. */
  158. char *newargv[256];
  159. int main (int argc, char **argv)
  160. {
  161. quakeparms_t parms;
  162. double newtime, time, oldtime;
  163. static char cwd[1024];
  164. struct timeval timeout;
  165. fd_set fdset;
  166. int t;
  167. COM_InitArgv (argc, argv);
  168. parms.argc = com_argc;
  169. parms.argv = com_argv;
  170. parms.memsize = 16*1024*1024;
  171. if ((t = COM_CheckParm ("-heapsize")) != 0 &&
  172. t + 1 < com_argc)
  173. parms.memsize = Q_atoi (com_argv[t + 1]) * 1024;
  174. if ((t = COM_CheckParm ("-mem")) != 0 &&
  175. t + 1 < com_argc)
  176. parms.memsize = Q_atoi (com_argv[t + 1]) * 1024 * 1024;
  177. parms.membase = malloc (parms.memsize);
  178. if (!parms.membase)
  179. Sys_Error("Insufficient memory.\n");
  180. parms.basedir = ".";
  181. parms.cachedir = NULL;
  182. SV_Init (&parms);
  183. // run one frame immediately for first heartbeat
  184. SV_Frame (0.1);
  185. //
  186. // main loop
  187. //
  188. oldtime = Sys_DoubleTime () - 0.1;
  189. while (1)
  190. {
  191. // select on the net socket and stdin
  192. // the only reason we have a timeout at all is so that if the last
  193. // connected client times out, the message would not otherwise
  194. // be printed until the next event.
  195. FD_ZERO(&fdset);
  196. FD_SET(net_socket, &fdset);
  197. timeout.tv_sec = 0;
  198. timeout.tv_usec = 100;
  199. if (select (net_socket+1, &fdset, NULL, NULL, &timeout) == -1)
  200. continue;
  201. // find time passed since last cycle
  202. newtime = Sys_DoubleTime ();
  203. time = newtime - oldtime;
  204. oldtime = newtime;
  205. SV_Frame (time);
  206. }
  207. return true;
  208. }