summaryrefslogtreecommitdiff
path: root/fpcdocs/go32ex/textmess.pas
blob: 10711792bec8bc4d4d4129e20b0587f697063274 (plain)
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
{ This example copies around some blocks of memory in DOS memory
space.

In more detail, the program copies a string randomly to the text
mode screen. Aditionally it messes around a bit with the color
attributes of the string.
Before doing this it saves the entire screen contents to the heap
and restores it afterwards.

Some additional background:

The text screen of a VGA card has it's address space at $B800:0;
screen memory is organized in a linear fashion, e.g. the second line
comes directly after the first, where each cell occupies 2 bytes of
memory (1 byte character data, 1 byte attributes). It is 32 kb in
size.

Hence the offset of a single memory cell from its origin is:

Y*columns*2 + X*2

where X and Y mark the point and columns is the number of character
cells per line
}

uses
        crt,
        go32;

const
        { number of columns on screen }
        columns = 80;
        { number of rows on screen }
        rows = 25;
        screensize = rows*columns*2;

        { sample text string }
        text = '! Hello world !';

var
        textofs : Longint;
        { this variable holds the entire screen contents }
        save_screen : array[0..screensize-1] of byte;
        { These two hold the previous cursor coordinates }
    curx, cury : Integer;

begin
        randomize;
        { save screen contents to save_screen variable }
        dosmemget($B800, 0, save_screen, screensize);
        { save current cursor coordinates }
        curx := wherex; cury := wherey;
        { This is our demo text }
        gotoxy(1, 1); Write(text);
        { calculate the address in offscreen memory (to be sure it will
        not be overwritten by the copy process later, we don't put it
        exactly at the end of the visible screen area) }
        textofs := screensize + length(text)*2;
        { copy it to offscreen memory }
        dosmemmove($B800, 0, $B800, textofs, length(text)*2);
        { clear the screen by writing zeros on the whole visible screen}
        dosmemfillchar($B800, 0, screensize, #0);
        while (not keypressed) do begin
                { set the attribute field (byte 2 of every cell) of the
                text in offscreen memory to random values }
                dosmemfillchar($B800, textofs + random(length(text))*2 + 1,
                        1, char(random(255)));
                { copy the string from offscreen to visibly screen by calculating
                it's destination address randomly }
                dosmemmove($B800, textofs, $B800,
                        random(columns)*2+random(rows)*columns*2,
                        length(text)*2);
                { small delay, else it is too fast }
                delay(1);
        end;
        { clear the keyboard buffer }
        readkey;
        { wait for a keypress }
        readkey;
        { restore old screen contents afterwards }
        dosmemput($B800, 0, save_screen, screensize);
        gotoxy(curx, cury);
end.