Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

Author Topic: Repair the colors of a Dos program ported to ptcGraph unit  (Read 1851 times)

Roland57

• Full Member
• Posts: 115
Repair the colors of a Dos program ported to ptcGraph unit
« on: October 26, 2020, 01:43:56 pm »
Hello!

I try to update a Dos program originally based on the Graph unit. I am on Linux. Since I had problems with the VGA library missing or not correctly installed, I decided to try to use ptcGraph and I had the joy to see "my" old program compiling and running! There is just a problem with colors, and I would need your advice on the method to repair the colors of the game. Currently all is blue on the screen.

If I remember correctly, ptcGraph uses 16-bit colors. Are there existing functions to convert 32-bit colors to 16-bit?

Any help or suggestion on this problem or on any another problem that you would detect is welcome. It seems that there also some memory leaks.

I attach the source, and a screenshot of the original program that I found on the web.

• Hero Member
• Posts: 10684
Re: Repair the colors of a Dos program ported to ptcGraph unit
« Reply #1 on: October 26, 2020, 03:53:45 pm »
32 to 16 leads to extreme data loss for the color.
you can use "nearest neighbour" and the likes to smoothen any transitions.
It is better to upscale any new  colors than trying to downscale them.
« Last Edit: October 26, 2020, 03:56:33 pm by Thaddy »

circular

• Hero Member
• Posts: 3648
Re: Repair the colors of a Dos program ported to ptcGraph unit
« Reply #2 on: October 26, 2020, 06:55:40 pm »
Hello Roland,

Hope you're alright.

I cannot find the definition of ptcGraph that would contain setColor function nor the color constants. I see in the code "setcolor(yellow)" or things like that. Why would the constant not agree with the function?

Anyway to convert from 32 bit to 16 bit you could do something like:
Code: Pascal  [Select][+][-]
1. function From32To16(AColor: longint): word;
2. var red, green, blue, red5, green6, blue5: byte;
3. begin
4.   red := AColor and \$ff;
5.   green := (AColor sur 8) and \$ff;
6.   blue := (AColor sur 16) and \$ff;
7.   red5 := red shr 3;
8.   green6 := green shr 2;
9.   blue5 := blue shr 3;
10.   result := red5 + (green6 shl 5) + (blue5 shl 11);
11. end;

@Thaddy: in this case we don't care about loss of precision for the colors to distinguish blue, yellow, white etc. as you can see from the screenshot.
Conscience is the debugger of the mind

• Hero Member
• Posts: 10684
Re: Repair the colors of a Dos program ported to ptcGraph unit
« Reply #3 on: October 26, 2020, 08:29:36 pm »
@Thaddy: in this case we don't care about loss of precision for the colors to distinguish blue, yellow, white etc. as you can see from the screenshot.
ptc differs in color definition compared to, say, Dos vs different Unix terminals. Has to do with the weight encodings (like 888, 565 and family) so you can get very different colors depending on definition. That is what I am trying to point out....

And ptc has most definitely the encoding values in the header files.
« Last Edit: October 26, 2020, 08:32:59 pm by Thaddy »

Roland57

• Full Member
• Posts: 115
Re: Repair the colors of a Dos program ported to ptcGraph unit
« Reply #4 on: October 26, 2020, 09:04:21 pm »

Hope you're alright.

Yes, I am well, thank you.

I cannot find the definition of ptcGraph that would contain setColor function nor the color constants. I see in the code "setcolor(yellow)" or things like that. Why would the constant not agree with the function?

Good question. I don't know. Maybe it's because of the compilation mode that I used (-Mtp)?

Thank you for your function. I used it to build a source code with 256 predefined 16-bit colors. Now the colors of the program are OK (excepted when I compile to 32-bit, I dont know why).

Fred vS

• Hero Member
• Posts: 2034
Re: Repair the colors of a Dos program ported to ptcGraph unit
« Reply #5 on: October 26, 2020, 09:25:52 pm »
Hello Roland.

Once again, VERY impressive.

I did use fpc 64bit with that command:

Code: Pascal  [Select][+][-]
1. fpc -Mtp -FUunits -B nero5.pas  -CX -XX -Xs

I was not able to resize-close the window but many things work, sure you will find all.

Chapeau.

Fre;D

« Last Edit: October 27, 2020, 01:08:58 am by Fred vS »
I use Lazarus 2.0.6 32/64 and FPC 3.2.0 32/64 on Debian 10.2 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64 and Mac OS X Snow Leopard 32.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt, Carbon.

https://github.com/fredvs
https://gitlab.com/fredvs

Fred vS

• Hero Member
• Posts: 2034
Re: Repair the colors of a Dos program ported to ptcGraph unit
« Reply #6 on: October 26, 2020, 09:58:04 pm »
Re-hello.

Compiling with fpc 32 bit gives better result vs fpc 64, see picture.

All works, even close the app (but not resizing).
« Last Edit: October 26, 2020, 09:59:43 pm by Fred vS »
I use Lazarus 2.0.6 32/64 and FPC 3.2.0 32/64 on Debian 10.2 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64 and Mac OS X Snow Leopard 32.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt, Carbon.

https://github.com/fredvs
https://gitlab.com/fredvs

circular

• Hero Member
• Posts: 3648
Re: Repair the colors of a Dos program ported to ptcGraph unit
« Reply #7 on: October 27, 2020, 12:28:00 am »
When trying to compile, it says it cannot find the unit ptccrt and ptcgraph. Though I have those units.
Conscience is the debugger of the mind

Fred vS

• Hero Member
• Posts: 2034
Re: Repair the colors of a Dos program ported to ptcGraph unit
« Reply #8 on: October 27, 2020, 12:48:22 am »
When trying to compile, it says it cannot find the unit ptccrt and ptcgraph. Though I have those units.

Huh, I did get those errors too using fpc 32 bit on my multi-arch Debian OS, I needed to change config of fpc.cfg.
« Last Edit: October 27, 2020, 12:58:00 am by Fred vS »
I use Lazarus 2.0.6 32/64 and FPC 3.2.0 32/64 on Debian 10.2 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64 and Mac OS X Snow Leopard 32.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt, Carbon.

https://github.com/fredvs
https://gitlab.com/fredvs

Fred vS

• Hero Member
• Posts: 2034
Re: Repair the colors of a Dos program ported to ptcGraph unit
« Reply #9 on: October 27, 2020, 12:55:46 am »
Note also that I needed to install on my Debian multi-arch this package that was missing:

Code: Pascal  [Select][+][-]
1. \$ sudo apt-get install libxxf86dga1:i386

[EDIT]

All is ok too with fpc 64 bit (I dont know why I got strange result before).

Using this:

Code: Pascal  [Select][+][-]
1. /usr/lib/fpc/3.2.1/ppcx64 -Mtp -FUunits -B -CX -XX -Xs -ghl nero5.pas

(But there is many memory leaks.)

« Last Edit: October 27, 2020, 01:35:57 am by Fred vS »
I use Lazarus 2.0.6 32/64 and FPC 3.2.0 32/64 on Debian 10.2 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64 and Mac OS X Snow Leopard 32.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt, Carbon.

https://github.com/fredvs
https://gitlab.com/fredvs

Roland57

• Full Member
• Posts: 115
Re: Repair the colors of a Dos program ported to ptcGraph unit
« Reply #10 on: October 27, 2020, 08:41:29 am »
Thank you for testing, Fred.

I retouched many little things. There remains ony one small memory leak: I don't find where it comes from.

The -Mtp option is no longer needed. You can use -Mobjfpc.

I played with the different graphic modes offered by ptcGraph. I see now that it was possible to use a 8-bit color mode. Maybe it would have been a more logical choice (if we keep the original colors).

If you prefer fullscreen, change this value to 0:
Code: Pascal  [Select][+][-]
1. procedure set_to_graphics_mode;
2. { ... }
3. begin
4.   opt := 2;

Roland57

• Full Member
• Posts: 115
Re: Repair the colors of a Dos program ported to ptcGraph unit
« Reply #11 on: October 27, 2020, 08:45:06 am »
I cannot find the definition of ptcGraph that would contain setColor function nor the color constants. I see in the code "setcolor(yellow)" or things like that. Why would the constant not agree with the function?

I think these constants are for the 8-bit color mode.

PascalDragon

• Hero Member
• Posts: 2611
• Compiler Developer
Re: Repair the colors of a Dos program ported to ptcGraph unit
« Reply #12 on: October 27, 2020, 09:15:52 am »
I cannot find the definition of ptcGraph that would contain setColor function nor the color constants. I see in the code "setcolor(yellow)" or things like that. Why would the constant not agree with the function?

I think these constants are for the 8-bit color mode.

The color constants are for SetPalette and GetPalette and thus for paletted color modes (like the 8-bit one).

Routines that directly take color values (like SetColor) take 32-bit RGB values on most platforms (at least those where FPC_GRAPH_SUPPORTS_TRUECOLOR is defined, on others like i8086-msdos it's 16-bit).

Roland57

• Full Member
• Posts: 115
Re: Repair the colors of a Dos program ported to ptcGraph unit
« Reply #13 on: October 27, 2020, 10:00:59 am »
The color constants are for SetPalette and GetPalette and thus for paletted color modes (like the 8-bit one).

Routines that directly take color values (like SetColor) take 32-bit RGB values on most platforms (at least those where FPC_GRAPH_SUPPORTS_TRUECOLOR is defined, on others like i8086-msdos it's 16-bit).

Thank you for your answer. Something is not clear for me. If ptcGraph expects 32-bits colors, I should get an unexpected visual result, since I use 16-bit values for the SetColor procedure, shouldn't I?

Roland57

• Full Member
• Posts: 115
Re: Repair the colors of a Dos program ported to ptcGraph unit
« Reply #14 on: October 27, 2020, 11:48:08 am »
I used to believe that ptcGraph supported only 16-bit colors. It isn't true.

Here is a little program which make a list of all modes available. I did find it on a russian forum.

Code: Pascal  [Select][+][-]
1. { http://www.freepascal.ru/article/freepascal/20120215095658/ }
2.
3. program gmodeinfo;
4.
5. uses
6. {\$IFDEF UNIX}
8. {\$ENDIF}
9.   SysUtils, ptcGraph;
10.
11. var
12.   ModeInfo: PModeInfo; // Сюда будет заносится информация о видеорежимах
13.   Rez: string;
14.
15. begin
17.
18.   if ModeInfo = nil then
19.     WriteLn('Не удалось получить информацию у видеоадаптера...')
20.   else
21.   begin
22.     //WriteLn('№ драйвера ', '№ режима ', 'Разрешение ', '  Цветов');
23.     WriteLn('Driver num ', 'Mode num ', 'Resolution ', '  Colors', '          Mode name');
24.     WriteLn('----------------------------------------------------------');
25.     while ModeInfo^.Next <> nil do
26.     begin
27.       Write(ModeInfo^.DriverNumber: 10);
28.       Write(ModeInfo^.ModeNumber: 9);
29.       Rez := IntToStr(ModeInfo^.MaxX + 1) + 'x' + IntToStr(ModeInfo^.MaxY + 1);
30.       Write(Rez: 11);
31.       Write(ModeInfo^.MaxColor: 9);
32.       WriteLn(' ' + ModeInfo^.ModeName: 19);
33.       ModeInfo := ModeInfo^.Next;
34.     end;
35.   end;
36. end.
37.

And here is the result for me:
Code: Text  [Select][+][-]
1. Driver num Mode num Resolution   Colors          Mode name
2. ----------------------------------------------------------
3.          1        0    320x200        4   320 x 200 CGA C0
4.          1        1    320x200        4   320 x 200 CGA C1
5.          1        2    320x200        4   320 x 200 CGA C2
6.          1        3    320x200        4   320 x 200 CGA C3
7.          1        4    640x200        2      640 x 200 CGA
8.          2        0    320x200        4   320 x 200 CGA C0
9.          2        1    320x200        4   320 x 200 CGA C1
10.          2        2    320x200        4   320 x 200 CGA C2
11.          2        3    320x200        4   320 x 200 CGA C3
12.          2        4    640x200        2      640 x 200 CGA
13.          2        5    640x480        2     640 x 480 MCGA
14.          7        0    720x348        2 720 x 348 HERCULES
15.          3        0    640x200       16      640 x 200 EGA
16.          3        1    640x350       16      640 x 350 EGA
17.          9        0    640x200       16      640 x 200 EGA
18.          9        1    640x350       16      640 x 350 EGA
19.          9        2    640x480       16      640 x 480 VGA
20.          6        0    320x200      256      320 x 200 VGA
21.          6        1    320x200      256    320 x 200 ModeX
22.         10      256    640x400      256     640 x 400 VESA
23.         10      257    640x480      256     640 x 480 VESA
24.         10      269    320x200    32768     320 x 200 VESA
25.         10      272    640x480    32768     640 x 480 VESA
26.         10      270    320x200    65536     320 x 200 VESA
27.         10      273    640x480    65536     640 x 480 VESA
28.         10      271    320x200 16777216     320 x 200 VESA
29.         10      274    640x480 16777216     640 x 480 VESA
30.         10      258    800x600       16     800 x 600 VESA
31.         10      259    800x600      256     800 x 600 VESA
32.         10      275    800x600    32768     800 x 600 VESA
33.         10      276    800x600    65536     800 x 600 VESA
34.         10      277    800x600 16777216     800 x 600 VESA
35.         10      260   1024x768       16    1024 x 768 VESA
36.         10      261   1024x768      256    1024 x 768 VESA
37.         10      278   1024x768    32768    1024 x 768 VESA
38.         10      279   1024x768    65536    1024 x 768 VESA
39.         10      280   1024x768 16777216    1024 x 768 VESA
40.         10      512    720x480       16     720 x 480 VESA
41.         10      513    720x480      256     720 x 480 VESA
42.         10      514    720x480    32768     720 x 480 VESA
43.         10      515    720x480    65536     720 x 480 VESA
44.         10      516    720x480 16777216     720 x 480 VESA
45.         10      517    848x480       16     848 x 480 VESA
46.         10      518    848x480      256     848 x 480 VESA
47.         10      519    848x480    32768     848 x 480 VESA
48.         10      520    848x480    65536     848 x 480 VESA
49.         10      521    848x480 16777216     848 x 480 VESA
50.         10      522   1152x768       16    1152 x 768 VESA
51.         10      523   1152x768      256    1152 x 768 VESA
52.         10      524   1152x768    32768    1152 x 768 VESA
53.         10      525   1152x768    65536    1152 x 768 VESA
54.         10      526   1152x768 16777216    1152 x 768 VESA
55.         10      527   1280x720       16    1280 x 720 VESA
56.         10      528   1280x720      256    1280 x 720 VESA
57.         10      529   1280x720    32768    1280 x 720 VESA
58.         10      530   1280x720    65536    1280 x 720 VESA
59.         10      531   1280x720 16777216    1280 x 720 VESA
60.         10      532   1366x768       16    1366 x 768 VESA
61.         10      533   1366x768      256    1366 x 768 VESA
62.         10      534   1366x768    32768    1366 x 768 VESA
63.         10      535   1366x768    65536    1366 x 768 VESA
64.
« Last Edit: October 27, 2020, 12:13:18 pm by Roland57 »