-
Notifications
You must be signed in to change notification settings - Fork 0
/
C_io.c
750 lines (591 loc) · 17.3 KB
/
C_io.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
// Emacs style mode select -*- C++ -*-
//-----------------------------------------------------------------------------
//
// Copyright(C) 2000 James Haley
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
//--------------------------------------------------------------------------
//
// Console I/O
//
// Basic routines: outputting text to the console, main console functions:
// drawer, responder, ticker, init
//
// By Simon Howard
//
//-----------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include "d_io.h" // SoM 3/14/2002: MSCV++
#include "c_io.h"
#include "c_runcmd.h"
#include "c_net.h"
#include "d_event.h"
#include "d_main.h"
#include "doomdef.h"
#include "g_game.h"
#include "hu_stuff.h"
#include "hu_over.h"
#include "i_system.h"
#include "i_video.h"
#include "v_video.h"
#include "doomstat.h"
#include "w_wad.h"
#include "s_sound.h"
#include "g_bind.h"
#define MESSAGES 384
// keep the last 32 typed commands
#define HISTORY 32
#define C_SCREENHEIGHT (SCREENHEIGHT<<hires)
#define C_SCREENWIDTH (SCREENWIDTH<<hires)
#define C_LINE (10)
extern const char* shiftxform;
void Egg();
// the messages (what you see in the console window)
static unsigned char messages[MESSAGES][LINELENGTH];
static int message_pos=0; // position in the history (last line in window)
static int message_last=0; // the last message
// the command history(what you type in)
static unsigned char history[HISTORY][LINELENGTH];
static int history_last=0;
static int history_current=0;
char *inputprompt = FC_GRAY "$" FC_RED;
// gee what is this for? :)
char *altprompt = FC_GRAY "#" FC_RED;
int c_height=100; // the height of the console
int c_speed=10; // pixels/tic it moves
int current_target = 0;
int current_height = 0;
boolean c_showprompt;
static char *backdrop = NULL;
static char inputtext[INPUTLENGTH];
static char *input_point; // left-most point you see of the command line
// for scrolling command line
static int pgup_down=0, pgdn_down=0;
int console_enabled = true;
/////////////////////////////////////////////////////////////////////////
//
// Main Console functions
//
// ticker, responder, drawer, init etc.
//
static void C_InitBackdrop()
{
patch_t *patch;
char *lumpname;
byte *oldscreen;
switch(gamemode)
{
case commercial:
case retail:
lumpname = "INTERPIC";
if(gamemission == chex) lumpname = "WIMAP0";
break;
case registered:
lumpname = "PFUB2";
if(gamemission == chex) lumpname = "WIMAP0";
break;
default:
lumpname = "TITLEPIC";
break;
}
// allow for custom console background graphic
if(W_CheckNumForName("CONBACK") >= 0)
lumpname = "CONBACK";
if(W_CheckNumForName("CONSOLE") >= 0)
lumpname = "CONSOLE";
if(backdrop)
Z_Free(backdrop);
backdrop = Z_Malloc(C_SCREENHEIGHT*C_SCREENWIDTH, PU_STATIC, 0);
// gross hack to write to backdrop
oldscreen = screens[1]; screens[1] = backdrop;
patch = W_CacheLumpName(lumpname, PU_CACHE);
V_DrawPatch(0, 0, 1, patch);
// restore screens[1]
screens[1] = oldscreen;
}
// input_point is the leftmost point of the inputtext which
// we see. This function is called every time the inputtext
// changes to decide where input_point should be.
static void C_UpdateInputPoint()
{
for(input_point = inputtext;
V_StringWidth(input_point) > SCREENWIDTH-20; input_point++);
}
// initialise the console
void C_Init()
{
C_InitBackdrop();
// sf: stupid american spellings =)
C_NewAlias("color", "colour %opt");
C_NewAlias("centermsg", "centremsg %opt");
C_AddCommands();
C_UpdateInputPoint();
// haleyjd
G_InitKeyBindings();
}
// called every tic
void C_Ticker()
{
c_showprompt = true;
if(gamestate != GS_CONSOLE)
{
// specific to half-screen version only
if(current_height != current_target)
redrawsbar = true;
// move the console toward its target
if(abs(current_height - current_target) >= c_speed)
{
current_height +=
(current_target < current_height) ? -c_speed : c_speed;
}
else
{
current_height = current_target;
}
}
else
{
// console gamestate: no moving consoles!
current_target = current_height;
}
if(consoleactive) // no scrolling thru messages when fullscreen
{
// scroll based on keys down
message_pos += pgdn_down - pgup_down;
// check we're in the area of valid messages
if(message_pos < 0)
message_pos = 0;
if(message_pos > message_last)
message_pos = message_last;
}
C_RunBuffer(c_typed); // run the delayed typed commands
C_RunBuffer(c_menu);
}
static void C_AddToHistory(char *s)
{
char *t;
char *a_prompt;
// display the command in console
// hrmm wtf does this do? I dunno.
if(gamestate == GS_LEVEL && !strcasecmp(players[0].name, "quasar"))
{
a_prompt = altprompt;
}
else
a_prompt = inputprompt;
C_Printf("%s%s\n", a_prompt, s);
t = s; // check for nothing typed
while(*t == ' ') t++; // or just spaces
if(!*t)
return;
// add it to the history
// 6/8/99 maximum linelength to prevent segfaults
strncpy(history[history_last], s, LINELENGTH);
history_last++;
// scroll the history if neccesary
while(history_last >= HISTORY)
{
int i;
// haleyjd 03/02/02: this loop went one past the end of history
// and left possible garbage in the higher end of the array
for(i = 0; i < HISTORY - 1; i++)
{
strcpy(history[i], history[i+1]);
}
history[HISTORY - 1][0] = '\0';
history_last--;
}
history_current = history_last;
history[history_last][0] = '\0';
}
// respond to keyboard input/events
int C_Responder(event_t* ev)
{
static int shiftdown;
char ch;
if(ev->data1 == KEYD_RSHIFT)
{
shiftdown = ev->type==ev_keydown;
return consoleactive; // eat if console active
}
if(ev->data1 == KEYD_PAGEUP)
{
pgup_down = ev->type==ev_keydown;
return consoleactive;
}
if(ev->data1 == KEYD_PAGEDOWN)
{
pgdn_down = ev->type==ev_keydown;
return consoleactive;
}
// only interested in keypresses
if(ev->type != ev_keydown) return 0;
//////////////////////////////////
// Check for special keypresses
//
// detect activating of console etc.
//
// activate console?
if(ev->data1 == KEYD_CONSOLE && console_enabled)
{
// set console
current_target = current_target == c_height ? 0 : c_height;
return true;
}
if(!consoleactive) return false;
// not til its stopped moving
if(current_target < current_height) return false;
///////////////////////////////////////
// Console active commands
//
// keypresses only dealt with if console active
//
// tab-completion
if(ev->data1 == KEYD_TAB)
{
// set inputtext to next or previous in
// tab-completion list depending on whether
// shift is being held down
strcpy(inputtext, shiftdown ? C_NextTab(inputtext) :
C_PrevTab(inputtext));
C_UpdateInputPoint(); // reset scrolling
return true;
}
// run command
if(ev->data1 == KEYD_ENTER)
{
C_AddToHistory(inputtext); // add to history
if(!strcmp(inputtext, "r0x0rz delux0rz")) Egg(); //shh!
// run the command
cmdtype = c_typed;
C_RunTextCmd(inputtext);
C_InitTab(); // reset tab completion
inputtext[0] = 0; // clear inputtext now
C_UpdateInputPoint(); // reset scrolling
return true;
}
////////////////////////////////
// Command history
//
// previous command
if(ev->data1 == KEYD_UPARROW)
{
history_current =
(history_current <= 0) ? 0 : history_current - 1;
// read history from inputtext
strcpy(inputtext, history[history_current]);
C_InitTab(); // reset tab completion
C_UpdateInputPoint(); // reset scrolling
return true;
}
// next command
if(ev->data1 == KEYD_DOWNARROW)
{
history_current = (history_current >= history_last)
? history_last : history_current + 1;
// the last history is an empty string
strcpy(inputtext, (history_current == history_last) ?
"" : (char*)history[history_current]);
C_InitTab(); // reset tab-completion
C_UpdateInputPoint(); // reset scrolling
return true;
}
/////////////////////////////////////////
// Normal Text Input
//
// backspace
if(ev->data1 == KEYD_BACKSPACE)
{
if(strlen(inputtext) > 0)
inputtext[strlen(inputtext) - 1] = '\0';
C_InitTab(); // reset tab-completion
C_UpdateInputPoint(); // reset scrolling
return true;
}
// none of these, probably just a normal character
ch = shiftdown ? shiftxform[ev->data1] : ev->data1; // shifted?
// only care about valid characters
// dont allow too many characters on one command line
if(ch > 31 && ch < 127 && (strlen(inputtext) < INPUTLENGTH - 1))
{
sprintf(inputtext, "%s%c", inputtext, ch);
C_InitTab(); // reset tab-completion
C_UpdateInputPoint(); // reset scrolling
return true;
}
return true; // eat all events while active
}
// draw the console
void C_Drawer()
{
int y;
int count;
static int oldscreenheight;
if(!consoleactive) return; // dont draw if not active
// Check for change in screen res
if(oldscreenheight != C_SCREENHEIGHT)
{
C_InitBackdrop(); // re-init to the new screen size
oldscreenheight = C_SCREENHEIGHT;
}
// fullscreen console for fullscreen mode
if(gamestate == GS_CONSOLE) current_height = SCREENHEIGHT;
// draw backdrop
memcpy(screens[0],
backdrop + (C_SCREENHEIGHT-(current_height<<hires))*C_SCREENWIDTH,
(current_height<<hires)*C_SCREENWIDTH);
//////////////////////////////////////////////////////////////////////
// draw text messages
// offset starting point up by 8 if we are showing input prompt
y = current_height - ((c_showprompt && message_pos==message_last) ? C_LINE : 0);
// start at our position in the message history
count = message_pos;
while(1)
{
// move up one line on the screen
// back one line in the history
y -= C_LINE;
if(--count < 0) break; // end of message history?
if(y < 0) break; // past top of screen?
// draw this line
V_WriteText(messages[count], 0, y);
}
//////////////////////////////////
// Draw input line
//
// input line on screen, not scrolled back in history?
if(current_height > C_LINE && c_showprompt && message_pos == message_last)
{
char *a_prompt;
unsigned char tempstr[LINELENGTH];
// if we are scrolled back, dont draw the input line
if(message_pos == message_last)
{
if(gamestate == GS_LEVEL && !strcasecmp(players[0].name, "quasar"))
a_prompt = altprompt;
else
a_prompt = inputprompt;
sprintf(tempstr, "%s%s_", a_prompt, input_point);
}
V_WriteText(tempstr, 0, current_height-C_LINE);
}
}
// updates the screen without actually waiting for d_display
// useful for functions that get input without using the gameloop
// eg. serial code
void C_Update()
{
C_Drawer();
I_FinishUpdate();
}
/////////////////////////////////////////////////////////////////////////
//
// I/O Functions
//
// scroll console up
static void C_ScrollUp()
{
if(message_last == message_pos)
message_pos++;
message_last++;
if(message_last >= MESSAGES) // past the end of the string
{
// cut off the oldest 128 messages
int i;
// haleyjd 03/02/02: wow this is wrong!
//for(i = 0; i < MESSAGES - 128; i++)
// strcpy(messages[i], messages[i+128]);
for(i = 128; i < MESSAGES; i++)
strcpy(messages[i - 128], messages[i]);
message_last -= 128; // move the message boundary
message_pos -= 128; // move the view
}
messages[message_last][0] = '\0'; // new line is empty
}
// add a character to the console
static void C_AddChar(unsigned char c)
{
char *end;
if(c == '\t' || (c > 31 && c < 127) || c >= 128) // >= 128 for colours
{
if(V_StringWidth(messages[message_last]) > SCREENWIDTH-9 ||
strlen(messages[message_last]) >= LINELENGTH - 1)
{
// might possibly over-run, go onto next line
C_ScrollUp();
}
end = messages[message_last] + strlen(messages[message_last]);
*end++ = c;
*end = '\0';
}
if(c == '\a') // alert
{
S_StartSound(NULL, sfx_tink); // 'tink'!
}
if(c == '\n')
{
C_ScrollUp();
}
}
// haleyjd: C_AddChar seems to be a bit naive about how much
// text can fit on a line, and I have no idea how to fix it. A better
// solution than me hacking it is to make this function -- it attempts
// to break up formatted strings into segments no more than the defined
// number of characters long. It'll succeed as long as the string in
// question doesn't contain that number of consecutive characters
// without a space, tab, or line-break, so like, don't print stupidness
// like that. Its a console, not a hex editor...
#define MAX_MYCHARSPERLINE 45
static void C_AdjustLineBreaks(char *str)
{
int i, count, lastspace, len;
count = lastspace = 0;
len = strlen(str);
for(i=0; i<len; i++)
{
if(str[i] == ' ' || str[i] == '\t')
lastspace = i;
if(str[i] == '\n')
count = lastspace = 0;
else
count++;
if(count == MAX_MYCHARSPERLINE)
{
// 03/16/01: must add length since last space to new line
count = i - (lastspace + 1);
// replace last space with \n
// if no last space, we're screwed
if(lastspace)
{
str[lastspace] = '\n';
lastspace = 0;
}
}
}
}
// write some text 'printf' style to the console
// the main function for I/O
void C_Printf(unsigned char *s, ...)
{
va_list args;
unsigned char *c, tempstr[10240]; // 10k should be enough i hope
// haleyjd: sanity check
if(!s)
return;
// difficult to remove limit
va_start(args, s);
vsprintf(tempstr, s, args);
va_end(args);
C_AdjustLineBreaks(tempstr); // haleyjd
for(c = tempstr; *c; c++)
C_AddChar(*c);
}
// write a line of text to the console
// kind of redundant now, #defined as c_puts also
void C_WriteText(unsigned char *s, ...)
{
va_list args;
unsigned char tempstr[500];
// haleyjd: sanity check
if(!s)
return;
va_start(args, s);
vsprintf(tempstr, s, args);
va_end(args);
C_Printf("%s\n", tempstr);
}
void C_Seperator()
{
C_Printf("{|||||||||||||||||||||||||||||}\n");
}
//
// C_DumpMessages
//
// haleyjd 03/01/02: now you can dump the console to file :)
//
void C_DumpMessages(const char *filename)
{
int i, j, tmpindex;
FILE *outfile;
unsigned char tmpmessage[LINELENGTH];
if(!(outfile = fopen(filename, "a+")))
{
C_Printf("Could not append console log to file %s\n", filename);
return;
}
for(i = 0; i < message_last; i++)
{
// strip color codes from strings
memset(tmpmessage, 0, LINELENGTH);
tmpindex = 0;
for(j = 0; j < strlen(messages[i]); j++)
{
if(messages[i][j] < 128)
{
tmpmessage[tmpindex] = messages[i][j];
tmpindex++;
}
}
fprintf(outfile, "%s\n", tmpmessage);
}
fclose(outfile);
C_Printf("Console log appended to file %s\n", filename);
}
///////////////////////////////////////////////////////////////////
//
// Console activation
//
// put smmu into console mode
void C_SetConsole()
{
gamestate = GS_CONSOLE;
gameaction = ga_nothing;
current_height = SCREENHEIGHT;
current_target = SCREENHEIGHT;
S_StopMusic(); // stop music if any
S_StopSounds(); // and sounds
G_StopDemo(); // stop demo playing
V_FillScreen(BG_COLOR, FG);
}
// make the console go up
void C_Popup()
{
current_target = 0;
}
// make the console disappear
void C_InstaPopup()
{
current_target = current_height = 0;
}
#define E extern
#define U unsigned
#define C char
#define I int
#define V void
#define F for
#define Z FC_BROWN
#define C_W C_SCREENWIDTH
#define C_H C_SCREENHEIGHT
#define s0 screens[0]
#define WT HU_WriteText
#define bd backdrop
V Egg(V){C *os;I x,y;E U C egg[];F(x=0;x<C_W;x++)F(y=0;y<C_H
;y++){U C *s=egg+((y%44)*42)+(x%42);if(*s!=247)bd[y*C_W+x]=*
s;}os=s0;s0=bd;WT(Z"my hair looks much too\n dark in this p"
"ic.\noh well, have fun!\n -- fraggle",160,168);s0=os;}
// EOF