forked from rickparrish/RMDoor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
videoutils.pas
563 lines (513 loc) · 12.4 KB
/
videoutils.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
unit VideoUtils;
{$mode objfpc}{$H+}
interface
uses
Crt {$IFDEF WINDOWS}, Windows{$ENDIF};
type
{$IFNDEF WINDOWS}
TCharInfo = Record
Ch: Char;
Attr: Byte;
end;
{$ENDIF}
TScreenBuffer = Array[1..25, 1..80] of TCharInfo;
procedure CursorHide;
procedure CursorShow;
procedure FastWrite(ALine: String; AX, AY, AAttr: Byte);
function GetAttrAt(AX, AY: Byte): Byte;
function GetCharAt(AX, AY: Byte): Char;
procedure GotoXYAbs(AXY: Word);
procedure ScreenRestore(var AScreenBuffer: TScreenBuffer);
procedure ScreenSave(var AScreenBuffer: TScreenBuffer);
function ScreenSizeX: Word;
function ScreenSizeY: Word;
procedure SetAttrAt(AAttr, AX, AY: Byte);
procedure SetCharAt(ACh: Char; AX, AY: Byte);
function WhereXYAbs: Word;
implementation
{$IFDEF GO32V2}
var
Screen: TScreenBuffer absolute $B800:0000;
{$ENDIF}
{$IFDEF WINDOWS}
var
StdOut: THandle;
{$ENDIF}
procedure CursorHide;
begin
cursoroff;
end;
procedure CursorShow;
begin
cursoron;
end;
{
Write ALine at the screen coordinates AX, AY with text attribute AAttr
}
{$IFDEF GO32V2}
procedure FastWrite(ALine: String; AX, AY, AAttr: Byte);
var
I: Integer;
begin
{ Validate parameters }
if ((AX < 1) OR (AX > 80) OR (AY < 1) OR (AY > 25)) then Exit;
{ Trim to fit within 80 columns }
if (Length(ALine) > (80 - AX + 1)) then ALine := Copy(ALine, 1, 80 - AX + 1);
for I := 1 to Length(ALine) do
begin
Screen[AY, AX + (I - 1)].Ch := ALine[I];
Screen[AY, AX + (I - 1)].Attr := AAttr;
end;
end;
{$ENDIF}
{$IFDEF UNIX}
procedure FastWrite(ALine: String; AX, AY, AAttr: Byte);
var
NeedWindow: Boolean;
SavedAttr: Integer;
SavedWindMinX: Integer;
SavedWindMinY: Integer;
SavedWindMaxX: Integer;
SavedWindMaxY: Integer;
SavedXY: Integer;
begin
{ Validate parameters }
if ((AX < 1) OR (AX > 80) OR (AY < 1) OR (AY > 25)) then Exit;
{ Trim to fit within 80 columns }
if (Length(ALine) > (80 - AX + 1)) then ALine := Copy(ALine, 1, 80 - AX + 1);
// Save
NeedWindow := ((WindMinX > 1) OR (WindMinY > 1) OR (WindMaxX < 80) OR (WindmaxY < 25));
SavedAttr := TextAttr;
SavedWindMinX := WindMinX;
SavedWindMinY := WindMinY;
SavedWindMaxX := WindMaxX;
SavedWindMaxY := WindMaxY;
SavedXY := WhereX + (WhereY SHL 8);
// Update
if (NeedWindow) then Window(1, 1, 80, 25);
GotoXY(AX, AY);
TextAttr := AAttr;
// Trim to fit within 79 columns if on line 25
if ((AY = 25) AND (Length(ALine) > (79 - AX + 1))) then ALine := Copy(ALine, 1, 79 - AX + 1);
// Output
Write(ALine);
// Restore
TextAttr := SavedAttr;
if (NeedWindow) then Window(SavedWindMinX, SavedWindMinY, SavedWindMaxX, SavedWindMaxY);
GotoXY(SavedXY AND $00FF, (SavedXY AND $FF00) SHR 8);
end;
{$ENDIF}
{$IFDEF WINDOWS}
procedure FastWrite(ALine: String; AX, AY, AAttr: Byte);
var
Buffer: Array[0..255] of TCharInfo;
BufferCoord: TCoord;
BufferSize: TCoord;
I: Integer;
WriteRegion: TSmallRect;
begin
{ Validate parameters }
if ((AX < 1) OR (AX > 80) OR (AY < 1) OR (AY > 25)) then Exit;
{ Trim to fit within 80 columns }
if (Length(ALine) > (80 - AX + 1)) then ALine := Copy(ALine, 1, 80 - AX + 1);
for I := 0 to Length(ALine) - 1 do
begin
Buffer[I].Attributes := AAttr;
Buffer[I].AsciiChar := ALine[I + 1];
end;
BufferSize.X := Length(ALine);
BufferSize.Y := 1;
BufferCoord.X := 0;
BufferCoord.Y := 0;
WriteRegion.Left := AX - 1;
WriteRegion.Top := AY - 1;
WriteRegion.Right := AX + Length(ALine) - 2;
WriteRegion.Bottom := AY - 1;
WriteConsoleOutput(StdOut, @Buffer, BufferSize, BufferCoord, WriteRegion);
end;
{$ENDIF}
{
Returns the text attribute at screen position AX, AY
}
{$IFDEF GO32V2}
function GetAttrAt(AX, AY: Byte): Byte;
begin
{ Validate parameters }
if ((AX < 1) OR (AX > 80) OR (AY < 1) OR (AY > 25)) then
begin
GetAttrAt := 7;
Exit;
end;
GetAttrAt := Screen[AY, AX].Attr;
end;
{$ENDIF}
{$IFDEF UNIX}
function GetAttrAt(AX, AY: Byte): Byte;
begin
{ Validate parameters }
if ((AX < 1) OR (AX > 80) OR (AY < 1) OR (AY > 25)) then
begin
GetAttrAt := 7;
Exit;
end;
Result := ConsoleBuf^[((AY - 1) * ScreenWidth) + (AX - 1)].attr;
end;
{$ENDIF}
{$IFDEF WINDOWS}
function GetAttrAt(AX, AY: Byte): Byte;
var
Attr: Word;
Coord: TCoord;
NumRead: Cardinal;
begin
{ Validate parameters }
if ((AX < 1) OR (AX > 80) OR (AY < 1) OR (AY > 25)) then
begin
GetAttrAt := 7;
Exit;
end;
Coord.X := AX - 1;
Coord.Y := AY - 1;
NumRead := 0;
ReadConsoleOutputAttribute(StdOut, @Attr, 1, Coord, NumRead);
Result := Attr;
end;
{$ENDIF}
{
Returns the character at screen position AX, AY
}
{$IFDEF GO32V2}
function GetCharAt(AX, AY: Byte): Char;
begin
{ Validate parameters }
if ((AX < 1) OR (AX > 80) OR (AY < 1) OR (AY > 25)) then
begin
GetCharAt := ' ';
Exit;
end;
GetCharAt := Screen[AY, AX].Ch;
end;
{$ENDIF}
{$IFDEF UNIX}
function GetCharAt(AX, AY: Byte): Char;
begin
{ Validate parameters }
if ((AX < 1) OR (AX > 80) OR (AY < 1) OR (AY > 25)) then
begin
Result := ' ';
Exit;
end;
Result := ConsoleBuf^[((AY - 1) * ScreenWidth) + (AX - 1)].ch;
end;
{$ENDIF}
{$IFDEF WINDOWS}
function GetCharAt(AX, AY: Byte): Char;
var
Ch: Char;
Coord: TCoord;
NumRead: Cardinal;
begin
{ Validate parameters }
if ((AX < 1) OR (AX > 80) OR (AY < 1) OR (AY > 25)) then
begin
Result := ' ';
Exit;
end;
Coord.X := AX - 1;
Coord.Y := AY - 1;
NumRead := 0;
ReadConsoleOutputCharacter(StdOut, @Ch, 1, Coord, NumRead);
if (NumRead = 0) then
begin
Result := #32
end else
begin
Result := Ch;
end;
end;
{$ENDIF}
procedure GotoXYAbs(AXY: Word);
begin
// REETODO This is window relative, and it should be absolute
GotoXY((AXY AND $00FF) + 1, ((AXY AND $FF00) SHR 8) + 1);
end;
{$IFDEF GO32V2}
procedure ScreenRestore(var AScreenBuffer: TScreenBuffer);
var
X, Y: Integer;
begin
// REETODO Fails with far pointer error Move(AScreenBuffer, Screen, 4000);
// REETODO Don't hardcode to 80x25
for Y := 1 to 25 do
begin
for X := 1 to 80 do
begin
FastWrite(AScreenBuffer[Y][X].Ch, X, Y, AScreenBuffer[Y][X].Attr);
end;
end;
end;
{$ENDIF}
{$IFDEF UNIX}
procedure ScreenRestore(var AScreenBuffer: TScreenBuffer);
var
X, Y: Integer;
begin
// REETODO Don't hardcode to 80x25
for Y := 1 to 25 do
begin
for X := 1 to 80 do
begin
FastWrite(AScreenBuffer[Y][X].Ch, X, Y, AScreenBuffer[Y][X].Attr);
end;
end;
end;
{$ENDIF}
{$IFDEF WINDOWS}
procedure ScreenRestore(var AScreenBuffer: TScreenBuffer);
var
BufSize : TCoord;
WritePos : TCoord;
DestRect : TSmallRect;
begin
// REETODO Don't hardcode to 80x25
BufSize.X := 80;
BufSize.Y := 25;
WritePos.X := 0;
WritePos.Y := 0;
DestRect.Left := 0;
DestRect.Top := 0;
DestRect.Right := 79;
DestRect.Bottom := 24;
WriteConsoleOutput(StdOut, @AScreenBuffer, BufSize, WritePos, DestRect);
end;
{$ENDIF}
{ REETODO Should detect screen size }
{$IFDEF GO32V2}
procedure ScreenSave(var AScreenBuffer: TScreenBuffer);
var
X, Y: Integer;
begin
// REETODO Fails with far pointer error Move(Screen, AScreenBuffer, 4000);
// REETODO Don't hardcode to 80x25
for Y := 1 to 25 do
begin
for X := 1 to 80 do
begin
AScreenBuffer[Y][X].Ch := GetCharAt(X, Y);
AScreenBuffer[Y][X].Attr := GetAttrAt(X, Y);
end;
end;
end;
{$ENDIF}
{$IFDEF UNIX}
procedure ScreenSave(var AScreenBuffer: TScreenBuffer);
var
X, Y: Integer;
begin
// REETODO Don't hardcode to 80x25
for Y := 1 to 25 do
begin
for X := 1 to 80 do
begin
AScreenBuffer[Y][X].Ch := GetCharAt(X, Y);
AScreenBuffer[Y][X].Attr := GetAttrAt(X, Y);
end;
end;
end;
{$ENDIF}
{$IFDEF WINDOWS}
procedure ScreenSave(var AScreenBuffer: TScreenBuffer);
var
BufSize : TCoord;
ReadPos : TCoord;
SourceRect : TSmallRect;
begin
// REETODO Don't hardcode to 80x25
BufSize.X := 80;
BufSize.Y := 25;
ReadPos.X := 0;
ReadPos.Y := 0;
SourceRect.Left := 0;
SourceRect.Top := 0;
SourceRect.Right := 79;
SourceRect.Bottom := 24;
ReadConsoleOutput(StdOut, @AScreenBuffer, BufSize, ReadPos, SourceRect);
end;
{$ENDIF}
{$IFDEF GO32V2}
function ScreenSizeX: Word;
begin
{ REETODO }
ScreenSizeX := 80;
end;
{$ENDIF}
{$IFDEF UNIX}
function ScreenSizeX: Word;
begin
{ REETODO }
ScreenSizeX := 80;
end;
{$ENDIF}
{$IFDEF WINDOWS}
function ScreenSizeX: Word;
var
CSBI: TConsoleScreenBufferInfo;
begin
CSBI.wAttributes := 0; // Does nothing, other than avoids "does not seem to be initialized" hint
GetConsoleScreenBufferInfo(StdOut, CSBI);
ScreenSizeX := CSBI.srWindow.Right - CSBI.srWindow.Left + 1;
end;
{$ENDIF}
{$IFDEF GO32V2}
function ScreenSizeY: Word;
begin
{ REETODO }
ScreenSizeY := 25;
end;
{$ENDIF}
{$IFDEF UNIX}
function ScreenSizeY: Word;
begin
{ REETODO }
ScreenSizeY := 25;
end;
{$ENDIF}
{$IFDEF WINDOWS}
function ScreenSizeY: Word;
var
CSBI: TConsoleScreenBufferInfo;
begin
CSBI.wAttributes := 0; // Does nothing, other than avoids "does not seem to be initialized" hint
GetConsoleScreenBufferInfo(StdOut, CSBI);
ScreenSizeY := CSBI.srWindow.Bottom - CSBI.srWindow.Top + 1;
end;
{$ENDIF}
{
Set the text attribute at screen coordinate AX, AY to AAttr
}
{$IFDEF GO32V2}
procedure SetAttrAt(AAttr, AX, AY: Byte);
begin
{ Validate parameters }
if ((AX < 1) OR (AX > 80) OR (AY < 1) OR (AY > 25)) then Exit;
Screen[AY, AX].Attr := AAttr;
end;
{$ENDIF}
{$IFDEF UNIX}
procedure SetAttrAt(AAttr, AX, AY: Byte);
var
NeedWindow: Boolean;
SavedAttr: Integer;
SavedWindMinX: Integer;
SavedWindMinY: Integer;
SavedWindMaxX: Integer;
SavedWindMaxY: Integer;
SavedXY: Integer;
begin
{ Validate parameters }
if ((AX < 1) OR (AX > 80) OR (AY < 1) OR (AY > 25)) then Exit;
// Save
NeedWindow := ((WindMinX > 1) OR (WindMinY > 1) OR (WindMaxX < 80) OR (WindmaxY < 25));
SavedAttr := TextAttr;
SavedWindMinX := WindMinX;
SavedWindMinY := WindMinY;
SavedWindMaxX := WindMaxX;
SavedWindMaxY := WindMaxY;
SavedXY := WhereX + (WhereY SHL 8);
// Update
if (NeedWindow) then Window(1, 1, 80, 25);
GotoXY(AX, AY);
TextAttr := AAttr;
// Output
Write(GetCharAt(AX, AY));
// Restore
TextAttr := SavedAttr;
if (NeedWindow) then Window(SavedWindMinX, SavedWindMinY, SavedWindMaxX, SavedWindMaxY);
GotoXY(SavedXY AND $00FF, (SavedXY AND $FF00) SHR 8);
end;
{$ENDIF}
{$IFDEF WINDOWS}
procedure SetAttrAt(AAttr, AX, AY: Byte);
var
WriteCoord: TCoord;
NumWritten: Cardinal;
begin
{ Validate parameters }
if ((AX < 1) OR (AX > 80) OR (AY < 1) OR (AY > 25)) then Exit;
WriteCoord.X := AX - 1;
WriteCoord.Y := AY - 1;
NumWritten := 0;
WriteConsoleOutputAttribute(StdOut, @AAttr, 1, WriteCoord, NumWritten);
end;
{$ENDIF}
{
Set the character at screen coordinate AX, AY to ACH
}
{$IFDEF GO32V2}
procedure SetCharAt(ACh: Char; AX, AY: Byte);
begin
{ Validate parameters }
if ((AX < 1) OR (AX > 80) OR (AY < 1) OR (AY > 25)) then Exit;
Screen[AY, AX].Ch := ACh;
end;
{$ENDIF}
{$IFDEF UNIX}
procedure SetCharAt(ACh: Char; AX, AY: Byte);
var
NeedWindow: Boolean;
SavedAttr: Integer;
SavedWindMinX: Integer;
SavedWindMinY: Integer;
SavedWindMaxX: Integer;
SavedWindMaxY: Integer;
SavedXY: Integer;
begin
{ Validate parameters }
if ((AX < 1) OR (AX > 80) OR (AY < 1) OR (AY > 25)) then Exit;
// Save
NeedWindow := ((WindMinX > 1) OR (WindMinY > 1) OR (WindMaxX < 80) OR (WindmaxY < 25));
SavedAttr := TextAttr;
SavedWindMinX := WindMinX;
SavedWindMinY := WindMinY;
SavedWindMaxX := WindMaxX;
SavedWindMaxY := WindMaxY;
SavedXY := WhereX + (WhereY SHL 8);
// Update
if (NeedWindow) then Window(1, 1, 80, 25);
GotoXY(AX, AY);
TextAttr := GetAttrAt(AX, AY);
// Output
Write(ACh);
// Restore
TextAttr := SavedAttr;
if (NeedWindow) then Window(SavedWindMinX, SavedWindMinY, SavedWindMaxX, SavedWindMaxY);
GotoXY(SavedXY AND $00FF, (SavedXY AND $FF00) SHR 8);
end;
{$ENDIF}
{$IFDEF WINDOWS}
procedure SetCharAt(ACh: Char; AX, AY: Byte);
var
WriteCoord: TCoord;
NumWritten: Cardinal;
begin
{ Validate parameters }
if ((AX < 1) OR (AX > 80) OR (AY < 1) OR (AY > 25)) then Exit;
WriteCoord.X := AX - 1;
WriteCoord.Y := AY - 1;
NumWritten := 0;
WriteConsoleOutputCharacter(StdOut, @ACh, 1, WriteCoord, NumWritten);
end;
{$ENDIF}
function WhereXYAbs: Word;
begin
// REETODO This is window relative, and it should be absolute
WhereXYAbs := (WhereX - 1) + ((WhereY - 1) SHL 8);
end;
{
Initialization stuff
}
begin
{$IFDEF WINDOWS}
StdOut := GetStdHandle(STD_OUTPUT_HANDLE);
{$ENDIF}
end.