[fpc-pascal]Direct writes to console buffer

Michalis Kamburelis michalis at camelot.homedns.org
Sun Aug 3 17:28:09 CEST 2003

Rich Pasco wrote:
> Michalis Kamburelis wrote:
>>Unit Video is exactly what you are looking for - it lets you write/read 
>>characters and their attributes (text color + background + blink 
>>attribute) directly to/from a buffer in memory and then use simple 
>>UpdateScreen procedure to "flush" contents of this memory buffer to the 
>>console. This way you can write console programs using the same approach 
>>as under DOS (with these ptr(SegB800,0) / ptr(SegB000,0)) BUT it works 
>>under many platforms - in particular, it works under Win32.
>>For more information, look at the FPC documentation for this unit.
> This unit looks most like what I want to do, except that the document
> says that it should not be used together with the CRT unit, and that
> doing so will result in very strange behaviour, possibly program
> crashes.  Since the rest of my code uses the CRT unit (probably even
> more than I use the direct video stuff), I am stuck.
>      - Rich

That's true, unit Video can't be used together with Crt unit because 
they both use the system console and using them both at the same time 
could make one of them not "synchronized" with the current console state 
(and maybe there are also others, possibly some platform-specific issues 
that I'm not aware of).

One solution is to implement the Crt functionality using the Video unit 
- in most cases it should be pretty simple, e.g. you can implement 
GotoXY by calling Video.SetCursorPos, you can implement WhereX / WhereY 
to return the values of Video.CursorX / CursorY etc. Functions dealing 
with keyboard (ReadKey and KeyPressed) may be implemented using the 
Keyboard unit.

Functions dealing with sound (Sound, NoSound) are not implemented 
properly in FPC Win32 Crt, and I can't help you with these - I don't 
know how to implement such functions properly using Win32 API, there 
seems to be no easy solution for this.

The only thing that remains and that is a litlle more difficult is to 
force Write[ln] functions to be also implemented by writing to screen 
using the Video unit. You can do this by changing some internal fields 
within Output variable (typecasting it to TTextRec) at the 
initialization of your program/unit, you should see examples of this in 
the implementation of Crt unit. Then you can implement Write[ln] by 
writing string to the VideoBuf, starting at current cursor position, and 
using some default color attributes (if you still want to be compatible 
with Crt, yoy should declare some global variable named TextAttr for 
this purpose).

Similarly, if you want to handle Read[ln] calls by yourself (to 
implement them using the mixture of Video/Keyboard units) you can do 
similiar trick with the Input variable.

And remember to add calls to the Video.UpdateScreen procedure to flush 
VideoBuf to console at appropriate times, since this is something 
entirely new that was not needed with Crt unit.

If you still want to use direct memory access to write to the console 
AND use Crt-like calls at the same time then this looks to be the best 

Hope this helps,
Michalis Kamburelis
michalis at camelot.homedns.org

More information about the fpc-pascal mailing list