-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconsole.pas
771 lines (620 loc) · 15.6 KB
/
console.pas
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
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
// ------------------------------------------------------------------------------
// DelphiQuake, Copyright (C) 2005-2011 by Jim Valavanis
// E-Mail: [email protected]
//
// Copyright (C) 1996-1997 Id Software, Inc.
//
// 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.
//
// ------------------------------------------------------------------------------
{$I dquake.inc}
{$Z4}
unit console;
interface
// console.c
uses
q_delphi,
cvar;
var
con_linewidth: integer;
con_cursorspeed: single = 4;
const
CON_TEXTSIZE = 16384;
var
con_forcedup: qboolean; // because no entities to refresh
con_totallines: integer; // total lines in console scrollback
con_backscroll: integer; // lines up from bottom to display
con_current: integer; // where next message will be printed
con_x: integer; // offset in current line for next print
con_text: PChar = nil;
con_notifytime: cvar_t =
(name: 'con_notifytime'; text: '3'); //seconds
const
NUM_CON_TIMES = 4;
var
con_times: array[0..NUM_CON_TIMES - 1] of single; // realtime time the line was generated
// for transparent notify lines
con_vislines: integer;
con_dodebuglog: qboolean;
const
MAXCMDLINE = 256;
var
con_initialized: qboolean = false;
con_notifylines: integer; // scan lines to clear for notify lines
procedure Con_ToggleConsole_f;
procedure Con_Clear_f;
procedure Con_ClearNotify;
procedure Con_MessageMode_f;
procedure Con_MessageMode2_f;
procedure Con_CheckResize;
procedure Con_Init;
procedure Con_Linefeed;
procedure Con_Print(txt: PChar);
procedure Con_DebugLog(filename: PChar; fmt: PChar; const Args: array of const);
procedure Con_Printf(fmt: PChar); overload;
procedure Con_Printf(fmt: PChar; const Args: array of const); overload;
procedure Con_Printf(fmt: string); overload;
procedure Con_Printf(fmt: string; const Args: array of const); overload;
procedure Con_DPrintf(fmt: PChar); overload;
procedure Con_DPrintf(fmt: PChar; const Args: array of const); overload;
procedure Con_DPrintf(fmt: string); overload;
procedure Con_DPrintf(fmt: string; const Args: array of const); overload;
procedure Con_SafePrintf(fmt: PChar); overload;
procedure Con_SafePrintf(fmt: PChar; const Args: array of const); overload;
procedure Con_SafePrintf(fmt: string); overload;
procedure Con_SafePrintf(fmt: string; const Args: array of const); overload;
procedure Con_DrawInput;
procedure Con_DrawNotify;
procedure Con_DrawConsole(lines: integer; drawinput: qboolean);
procedure Con_NotifyBox(text: PChar);
const
CON_LINE = #29#30#30#30#30#30#30#30#30#30#30#30#30#30#30#30#30#30#30#30#30#30#30#30#30#30#30#30#30#30#30#30#30#30#30#30#31;
implementation
uses
keys,
cl_main_h,
client,
menu,
gl_screen,
common,
gl_vidnt,
zone,
cmd,
snd_dma,
host_h,
sys_win,
gl_draw;
(*
================
Con_ToggleConsole_f
================
*)
procedure Con_ToggleConsole_f;
begin
if key_dest = key_console then
begin
if cls.state = ca_connected then
begin
key_dest := key_game;
key_lines[edit_line][1] := #0; // clear any typing
key_linepos := 1;
end
else
begin
M_Menu_Main_f;
end;
end
else
key_dest := key_console;
SCR_EndLoadingPlaque;
ZeroMemory(@con_times, SizeOf(con_times));
end;
(*
================
Con_Clear_f
================
*)
procedure Con_Clear_f;
begin
if con_text <> nil then
memset(con_text, Ord(' '), CON_TEXTSIZE);
end;
(*
================
Con_ClearNotify
================
*)
procedure Con_ClearNotify;
var
i: integer;
begin
for i := 0 to NUM_CON_TIMES - 1 do
con_times[i] := 0;
end;
(*
================
Con_MessageMode_f
================
*)
procedure Con_MessageMode_f;
begin
key_dest := key_message;
team_message := false;
end;
(*
================
Con_MessageMode2_f
================
*)
procedure Con_MessageMode2_f;
begin
key_dest := key_message;
team_message := true;
end;
(*
================
Con_CheckResize
If the line width has changed, reformat the buffer.
================
*)
procedure Con_CheckResize;
var
i, j, width, oldwidth, oldtotallines, numlines, numchars: integer;
tbuf: array[0..CON_TEXTSIZE - 1] of char;
begin
width := (vid.width div 8) - 2;
if width = con_linewidth then
exit;
if width < 1 then // video hasn't been initialized yet
begin
width := 38;
con_linewidth := width;
con_totallines := CON_TEXTSIZE div con_linewidth;
memset(con_text, Ord(' '), CON_TEXTSIZE);
end
else
begin
oldwidth := con_linewidth;
con_linewidth := width;
oldtotallines := con_totallines;
con_totallines := CON_TEXTSIZE div con_linewidth;
numlines := oldtotallines;
if con_totallines < numlines then
numlines := con_totallines;
numchars := oldwidth;
if con_linewidth < numchars then
numchars := con_linewidth;
memcpy(@tbuf, con_text, CON_TEXTSIZE);
memset(con_text, Ord(' '), CON_TEXTSIZE);
for i := 0 to numlines - 1 do
begin
for j := 0 to numchars - 1 do
begin
con_text[(con_totallines - 1 - i) * con_linewidth + j] :=
tbuf[((con_current - i + oldtotallines) mod oldtotallines) * oldwidth + j];
end;
end;
Con_ClearNotify;
end;
con_backscroll := 0;
con_current := con_totallines - 1;
end;
(*
================
Con_Init
================
*)
procedure Con_Init;
const
MAXGAMEDIRLEN = 1000;
var
temp: array[0..MAXGAMEDIRLEN] of char;
t2: PChar;
begin
t2 := '/qconsole.log';
con_dodebuglog := COM_CheckParm('-condebug') <> 0;
if con_dodebuglog then
begin
if strlen(com_gamedir) < (MAXGAMEDIRLEN - strlen(t2)) then
begin
sprintf(temp, '%s%s', [com_gamedir, t2]);
unlink(temp);
end;
end;
con_text := Hunk_AllocName(CON_TEXTSIZE, 'context');
memset(con_text, Ord(' '), CON_TEXTSIZE);
con_linewidth := -1;
Con_CheckResize;
Con_Printf('Console initialized.'#10);
//
// register our commands
//
Cvar_RegisterVariable(@con_notifytime);
Cmd_AddCommand('toggleconsole', Con_ToggleConsole_f);
Cmd_AddCommand('messagemode', Con_MessageMode_f);
Cmd_AddCommand('messagemode2', Con_MessageMode2_f);
Cmd_AddCommand('clear', Con_Clear_f);
con_initialized := true;
end;
(*
===============
Con_Linefeed
===============
*)
procedure Con_Linefeed;
begin
con_x := 0;
inc(con_current);
memset(@con_text[(con_current mod con_totallines) * con_linewidth], Ord(' '), con_linewidth);
end;
(*
================
Con_Print
Handles cursor positioning, line wrapping, etc
All console printing must go through this in order to be logged to disk
If no console is visible, the notify window will pop up.
================
*)
var
cr_Con_Print: qboolean = false;
procedure Con_Print(txt: PChar);
var
y: integer;
l: integer;
c: char;
mask: integer;
begin
con_backscroll := 0;
if txt[0] = #1 then
begin
mask := 128; // go to colored text
S_LocalSound('misc/talk.wav');
// play talk wav
inc(txt);
end
else if txt[0] = #2 then
begin
mask := 128; // go to colored text
inc(txt);
end
else
mask := 0;
while true do
begin
c := txt^;
if c = #0 then break;
// count word length
for l := 0 to con_linewidth - 1 do
if txt[l] <= ' ' then
break;
// word wrap
if (l <> con_linewidth) and (con_x + l > con_linewidth) then
con_x := 0;
inc(txt);
if cr_Con_Print then
begin
dec(con_current);
cr_Con_Print := false;
end;
if con_x = 0 then
begin
Con_Linefeed;
// mark time for transparent overlay
if con_current >= 0 then
con_times[con_current mod NUM_CON_TIMES] := realtime;
end;
case c of
#10:
begin
con_x := 0;
end;
#13:
begin
con_x := 0;
cr_Con_Print := true;
end;
else // display character and advance
begin
y := con_current mod con_totallines;
con_text[y * con_linewidth + con_x] := Chr(Ord(c) or mask);
inc(con_x);
if con_x >= con_linewidth then
con_x := 0;
end;
end;
end;
end;
(*
================
Con_DebugLog
================
*)
var
data_Con_DebugLog: array[0..1023] of char;
procedure Con_DebugLog(filename: PChar; fmt: PChar; const Args: array of const);
var
fd: file;
i: integer;
c: char;
begin
sprintf(data_Con_DebugLog, PChar(fmt + #10#0), Args); // JVAL check!
if fopen(filename, 'rb', fd) then
begin
seek(fd, filesize(fd));
for i := 0 to 1023 do
begin
c := data_Con_DebugLog[i];
if c = #0 then
break;
fwrite(@c, 1, 1, fd);
end;
fclose(fd);
end
else
Sys_Printf('Con_DebugLog: can''t write to %s'#10, [filename]);
end;
(*
================
Con_Printf
Handles cursor positioning, line wrapping, etc
================
*)
const
MAXPRINTMSG = $10000;
// FIXME: make a buffer size safe vsprintf?
var
inupdate_Con_Printf: qboolean = false;
procedure Con_Printf(fmt: PChar);
begin
Con_Printf(fmt, []);
end;
procedure Con_Printf(fmt: string);
begin
Con_Printf(PChar(fmt));
end;
procedure Con_Printf(fmt: string; const Args: array of const);
begin
Con_Printf(PChar(fmt), Args);
end;
procedure Con_Printf(fmt: PChar; const Args: array of const);
var
msg: array[0..MAXPRINTMSG - 1] of char;
begin
sprintf(msg, fmt, Args);
// also echo to debugging console
Sys_Printf('%s', [msg]); // also echo to debugging console
// log all messages to file
if con_dodebuglog then
Con_DebugLog(va('%s/qconsole.log', [com_gamedir]), '%s', [msg]);
if not con_initialized then
exit;
if cls.state = ca_dedicated then
exit; // no graphics mode
// write it to the scrollable buffer
Con_Print(msg);
// update the screen if the console is displayed
if (cls.signon <> SIGNONS) and not scr_disabled_for_loading then
begin
// protect against infinite loop if something in SCR_UpdateScreen calls
// Con_Printd
if not inupdate_Con_Printf then
begin
inupdate_Con_Printf := true;
SCR_UpdateScreen;
inupdate_Con_Printf := false;
end;
end;
end;
(*
================
Con_DPrintf
A Con_Printf that only shows up if the "developer" cvar is set
================
*)
procedure Con_DPrintf(fmt: PChar);
begin
Con_DPrintf(fmt, []);
end;
procedure Con_DPrintf(fmt: PChar; const Args: array of const);
begin
if boolval(developer.value) then // don't confuse non-developers with techie stuff...
Con_Printf(fmt, Args);
end;
procedure Con_DPrintf(fmt: string);
begin
Con_DPrintf(PChar(fmt));
end;
procedure Con_DPrintf(fmt: string; const Args: array of const); overload;
begin
Con_DPrintf(PChar(fmt), Args);
end;
(*
==================
Con_SafePrintf
Okay to call even when the screen can't be updated
==================
*)
procedure Con_SafePrintf(fmt: PChar);
begin
Con_SafePrintf(fmt, []);
end;
procedure Con_SafePrintf(fmt: PChar; const Args: array of const);
var
msg: array[0..1023] of char;
temp: qboolean;
begin
sprintf(msg, fmt, Args);
temp := scr_disabled_for_loading;
scr_disabled_for_loading := true;
Con_Printf('%s', [msg]);
scr_disabled_for_loading := temp;
end;
procedure Con_SafePrintf(fmt: string);
begin
Con_SafePrintf(PChar(fmt));
end;
procedure Con_SafePrintf(fmt: string; const Args: array of const); overload;
begin
Con_SafePrintf(PChar(fmt), Args);
end;
(*
==============================================================================
DRAWING
==============================================================================
*)
(*
================
Con_DrawInput
The input line scrolls horizontally if typing goes beyond the right edge
================
*)
procedure Con_DrawInput;
var
y: integer;
i: integer;
text: PChar;
begin
if (key_dest <> key_console) and not con_forcedup then
exit; // don't draw anything
text := key_lines[edit_line];
// add the cursor frame
text[key_linepos] := Chr(10 + intval(realtime * con_cursorspeed) and 1);
// fill out remainder with spaces
for i := key_linepos + 1 to con_linewidth - 1 do
text[i] := ' ';
// prestep if horizontally scrolling
if key_linepos >= con_linewidth then
text := text + 1 + key_linepos - con_linewidth;
// draw it
y := con_vislines - 16;
for i := 0 to con_linewidth - 1 do
Draw_Character(((i + 1) shl 3), y, text[i]);
// remove cursor
key_lines[edit_line][key_linepos] := #0;
end;
(*
================
Con_DrawNotify
Draws the last few lines of output transparently over the game top
================
*)
procedure Con_DrawNotify;
var
x, v: integer;
text: PChar;
i: integer;
time: single;
// extern char chat_buffer[]; JVAL ??
begin
v := 0;
for i := con_current - NUM_CON_TIMES + 1 to con_current do
begin
if i < 0 then
continue;
time := con_times[i mod NUM_CON_TIMES];
if time = 0 then
continue;
time := realtime - time;
if time > con_notifytime.value then
continue;
text := con_text + (i mod con_totallines) * con_linewidth;
clearnotify := 0;
scr_copytop := 1;
for x := 0 to con_linewidth - 1 do
Draw_Character(((x + 1) shl 3), v, text[x]);
inc(v, 8);
end;
if key_dest = key_message then
begin
clearnotify := 0;
scr_copytop := 1;
x := 0;
Draw_String(8, v, 'say:');
while chat_buffer[x] <> '' do
begin
Draw_Character(((x + 5) shl 3), v, chat_buffer[x]);
inc(x);
end;
Draw_Character(((x + 5) shl 3), v, 10 + intval(realtime * con_cursorspeed) and 1);
inc(v, 8);
end;
if v > con_notifylines then
con_notifylines := v;
end;
(*
================
Con_DrawConsole
Draws the console with the solid background
The typing input line at the bottom should only be drawn if typing is allowed
================
*)
procedure Con_DrawConsole(lines: integer; drawinput: qboolean);
var
i, x, y: integer;
rows: integer;
text: PChar;
j: integer;
begin
if lines <= 0 then
exit;
// draw the background
Draw_ConsoleBackground(lines);
// draw the text
con_vislines := lines;
rows := (lines - 16) div 8; // rows of text to draw
y := lines - 16 - (rows shl 3); // may start slightly negative
for i := con_current - rows + 1 to con_current do
begin
j := i - con_backscroll;
if j < 0 then
j := 0;
text := con_text + (j mod con_totallines) * con_linewidth;
for x := 0 to con_linewidth - 1 do
Draw_Character(((x + 1) shl 3), y, text[x]);
inc(y, 8);
end;
// draw the input prompt, user text, and cursor if desired
if drawinput then
Con_DrawInput;
end;
(*
==================
Con_NotifyBox
==================
*)
procedure Con_NotifyBox(text: PChar);
var
t1, t2: double;
begin
// during startup for sound / cd warnings
Con_Printf(#10#10 + CON_LINE + #10);
Con_Printf(text);
Con_Printf('Press a key.'#10);
Con_Printf(CON_LINE + #10);
key_count := -2; // wait for a key down and up
key_dest := key_console;
repeat
t1 := Sys_FloatTime;
SCR_UpdateScreen;
Sys_SendKeyEvents;
t2 := Sys_FloatTime;
realtime := realtime + t2 - t1; // make the cursor blink
until key_count >= 0;
Con_Printf(#10);
key_dest := key_game;
realtime := 0; // put the cursor back to invisible
end;
end.