Add key bindings for pause, message refresh.
[chocolate-doom.git] / src / i_system.c
blob5f90fd7dc2512a63dfa16dca6b28d1f528c38627
1 // Emacs style mode select -*- C++ -*-
2 //-----------------------------------------------------------------------------
3 //
4 // Copyright(C) 1993-1996 Id Software, Inc.
5 // Copyright(C) 2005 Simon Howard
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License
9 // as published by the Free Software Foundation; either version 2
10 // of the License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 // 02111-1307, USA.
22 // DESCRIPTION:
24 //-----------------------------------------------------------------------------
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
32 #include <stdarg.h>
34 #ifdef _WIN32
35 #define WIN32_LEAN_AND_MEAN
36 #include <windows.h>
37 #else
38 #include <unistd.h>
39 #endif
41 #include "deh_main.h"
42 #include "doomdef.h"
43 #include "doomstat.h"
44 #include "m_argv.h"
45 #include "m_config.h"
46 #include "m_misc.h"
47 #include "i_joystick.h"
48 #include "i_timer.h"
49 #include "i_video.h"
50 #include "s_sound.h"
52 #include "d_net.h"
53 #include "g_game.h"
55 #include "i_system.h"
56 #include "txt_main.h"
59 #include "w_wad.h"
60 #include "z_zone.h"
62 int mb_used = 16;
63 int show_endoom = 1;
65 // Tactile feedback function, probably used for the Logitech Cyberman
67 void I_Tactile(int on, int off, int total)
71 int I_GetHeapSize (void)
73 int p;
75 //!
76 // @arg <mb>
78 // Specify the heap size, in MiB (default 16).
81 p = M_CheckParm("-mb");
83 if (p > 0)
85 mb_used = atoi(myargv[p+1]);
88 return mb_used*1024*1024;
91 byte *I_ZoneBase (int *size)
93 byte *zonemem;
95 *size = I_GetHeapSize();
97 zonemem = malloc(*size);
99 if (zonemem == NULL)
101 I_Error("Failed to allocate %i bytes for zone memory", *size);
104 printf("zone memory: %p, %x allocated for zone\n",
105 zonemem, *size);
107 return zonemem;
111 // I_ConsoleStdout
113 // Returns true if stdout is a real console, false if it is a file
116 boolean I_ConsoleStdout(void)
118 #ifdef _WIN32
119 // SDL "helpfully" always redirects stdout to a file.
120 return 0;
121 #else
122 return isatty(fileno(stdout));
123 #endif
127 // I_Init
129 void I_Init (void)
131 I_CheckIsScreensaver();
132 I_InitTimer();
133 I_InitJoystick();
136 #define ENDOOM_W 80
137 #define ENDOOM_H 25
140 // Displays the text mode ending screen after the game quits
143 void I_Endoom(void)
145 unsigned char *endoom_data;
146 unsigned char *screendata;
147 int y;
148 int indent;
150 endoom_data = W_CacheLumpName(DEH_String("ENDOOM"), PU_STATIC);
152 // Set up text mode screen
154 TXT_Init();
156 // Make sure the new window has the right title and icon
158 I_SetWindowCaption();
159 I_SetWindowIcon();
161 // Write the data to the screen memory
163 screendata = TXT_GetScreenData();
165 indent = (ENDOOM_W - TXT_SCREEN_W) / 2;
167 for (y=0; y<TXT_SCREEN_H; ++y)
169 memcpy(screendata + (y * TXT_SCREEN_W * 2),
170 endoom_data + (y * ENDOOM_W + indent) * 2,
171 TXT_SCREEN_W * 2);
174 // Wait for a keypress
176 while (true)
178 TXT_UpdateScreen();
180 if (TXT_GetChar() >= 0)
182 break;
185 TXT_Sleep(0);
188 // Shut down text mode screen
190 TXT_Shutdown();
194 // I_Quit
197 void I_Quit (void)
199 D_QuitNetGame ();
200 G_CheckDemoStatus();
201 S_Shutdown();
203 if (!screensaver_mode)
205 M_SaveDefaults ();
208 I_ShutdownGraphics();
210 if (show_endoom && !testcontrols && !screensaver_mode)
212 I_Endoom();
215 exit(0);
218 void I_WaitVBL(int count)
220 I_Sleep((count * 1000) / 70);
224 // I_Error
226 extern boolean demorecording;
228 static boolean already_quitting = false;
230 void I_Error (char *error, ...)
232 va_list argptr;
234 if (already_quitting)
236 fprintf(stderr, "Warning: recursive call to I_Error detected.\n");
237 exit(-1);
239 else
241 already_quitting = true;
244 // Message first.
245 va_start(argptr, error);
246 fprintf(stderr, "\nError: ");
247 vfprintf(stderr, error, argptr);
248 fprintf(stderr, "\n");
249 va_end(argptr);
250 fflush(stderr);
252 // Shutdown. Here might be other errors.
254 if (demorecording)
256 G_CheckDemoStatus();
259 D_QuitNetGame ();
260 I_ShutdownGraphics();
261 S_Shutdown();
263 #ifdef _WIN32
264 // On Windows, pop up a dialog box with the error message.
266 char msgbuf[512];
267 wchar_t wmsgbuf[512];
269 va_start(argptr, error);
270 memset(msgbuf, 0, sizeof(msgbuf));
271 vsnprintf(msgbuf, sizeof(msgbuf) - 1, error, argptr);
272 va_end(argptr);
274 MultiByteToWideChar(CP_ACP, 0,
275 msgbuf, strlen(msgbuf) + 1,
276 wmsgbuf, sizeof(wmsgbuf));
278 MessageBoxW(NULL, wmsgbuf, L"Error", MB_OK);
280 #endif
282 // abort();
284 exit(-1);