Recent

Author Topic: How to change a character's text and background color in video unit?  (Read 445 times)

temp765485

  • Newbie
  • Posts: 2
So I recently discovered and have been using the video standard unit because of its system independency. So far I have learnt how to display a single character using the VideoBuf array:
Code: Pascal  [Select][+][-]
  1. uses video;
  2. begin
  3.  InitVideo;
  4.  VideoBuf^[0]:=Ord('D');
  5.  UpdateScreen(False);
  6.  readln;
  7.  DoneVideo;
  8. end.
The question is, how to display the character but with different text and background colors? I have seen the unit reference, but I think I need a more closely explanation of it. Any help will be very appreciated.
« Last Edit: February 07, 2021, 12:59:08 am by temp765485 »

Kays

  • Sr. Member
  • ****
  • Posts: 299
  • Whasup!?
    • KaiBurghardt.de
Re: How to change a character’s text and background color in video unit?
« Reply #1 on: February 07, 2021, 01:25:53 am »
[…] I have seen the unit reference, […]
You know, reading it helps. It’s everything there.
Code: Pascal  [Select][+][-]
  1. videoBuf^[0] := ord('D') + (yellow + blue shl 4) shl 8;
The background will be blue, the letter (i.e. “foreground” color) yellow.

Please take note of getCapabilities, i. e. you wanna first check colors are actually supported.

If you’re related to the anonymous user throwaway34865, I’ll tell you right away you won’t be able to display non-ASCII characters using the video unit.
Yours Sincerely
Kai Burghardt

temp765485

  • Newbie
  • Posts: 2
Re: How to change a character's text and background color in video unit?
« Reply #2 on: February 07, 2021, 01:57:00 am »
Ohh I get it now. Thank you for the help. But can you at least tell me why do we need to shift 8 bits to the left at the end? I will look more into it later. And yes, the other account is me lol, so thanks again :)
« Last Edit: February 07, 2021, 01:59:22 am by temp765485 »

lucamar

  • Hero Member
  • *****
  • Posts: 3906
Re: How to change a character's text and background color in video unit?
« Reply #3 on: February 07, 2021, 05:02:26 am »
[...] can you at least tell me why do we need to shift 8 bits to the left at the end? I will look more into it later.

Looking at it before might help you better :)

The video buffer (of type TVideoBuf) is a TVideoCell (a Word) array, with each "cell" organized like the text screen, i.e. two bytes corresponding to the attribute (the color) and the character code, so to build it you need to get the attribute in the high byte and the char in the lower. The attribute byte itself is organized as two nibbles: the background and the foreground "colors", with the background including the "blink" bit ($80). In short, each word is:
Code: [Select]
x bbb ffff - cccccccc
|  |    |       |
|  |    |       \-- character code
|  |    \-- foreground color/attribute
|  \-- background color
\-- blink bit
so you build the "color" byte by shifting the back color left 4 bits and adding the fore color, then shift that byte left 8 bits to put it in the high byte and add the character code.

Easy peasy ;)
« Last Edit: February 07, 2021, 05:04:29 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Kays

  • Sr. Member
  • ****
  • Posts: 299
  • Whasup!?
    • KaiBurghardt.de
Re: How to change a character's text and background color in video unit?
« Reply #4 on: February 07, 2021, 02:34:10 pm »
[…] shift 8 bits to the left at the end […]
I’ve quickly drawn up a unit taking care of all those shl and placed it under https://wiki.freepascal.org/video_unit
[To be precise, examining the generated code you’ll see there will be no shl anymore.]
« Last Edit: February 07, 2021, 03:11:33 pm by Kays »
Yours Sincerely
Kai Burghardt

 

TinyPortal © 2005-2018