From:
Subject: [fpc-pascal]Reading the color attributes from the screen
Hi,
Show quoted text 9 lines
The following bit of code is something I use in my go32v2 (DOS)
programs to save/restore the contents of the screen to/from memory.
[...]
the array after I've saved the screen? Or perhaps there's another
mem[] region I can grab and modify the colors their instead?
// for screens up to 132x60
const
BUF_SIZE = 7920;
Note that this value for the buffer size only covers half of the textmode screen memory, see below why.
// array to hold screen contents
var
ScreenBuffer : array [1..BUF_SIZE] of byte;
[...]
(VGA-)Textmode has the following memory layout:
Each cell of the screen is represented by two bytes, the first is the color attribute the second is the ascii character value. The cells are stored one after another in a linear fashion from left to right, line by line from top to bottom.
E.g. a procedure which sets the attribute and ascii character value of a cell could look like the following:
procedure PutChar(x, y : DWord; attr : Byte; character : Char);
begin
ScreenBuffer[(y * WIDTH + x ) * 2] := attr;
ScreenBuffer[(y * WIDTH + x ) * 2 + 1] := Ord(character);
end;
where WIDTH is the screen's physical number of cells per line. (Note that the x/y coords are interpreted as zero based).
Use the mem[] operator with the same offsets into the screen buffer area as used in the previous example to change cell character and ascii value directly on the screen.
E.g.
procedure PutChar(x, y : DWord; attr : Byte; character : Char);
begin
mem[$B8000 + (y * WIDTH + x ) * 2] := attr;
mem[$B8000 + (y * WIDTH + x ) * 2 + 1] := Ord(character);
end;
Should do the trick (everything untested).
Regards,
Thomas