Recent

Author Topic: Convert pictures of a Turbo Pascal program ported to ptcGraph  (Read 12828 times)

Roland57

  • Full Member
  • ***
  • Posts: 187
Hello!

I am trying to port to ptcGraph a Turbo Pascal game, a Pacman clone (by George M. Tzoumas) that I found here.

My code compiles and runs, but the screen is black. I think that pictures need to be converted. I wrote a little program to convert one picture, but the result is bad. Could you help me to find what I am doing wrong?

Regards.

Roland

retronick

  • New member
  • *
  • Posts: 9
Re: Convert pictures of a Turbo Pascal program ported to ptcGraph
« Reply #1 on: May 15, 2021, 08:19:46 pm »
Hi Roland,

That looks like an interesting version of ms pac man. I would like to see you port it to freepascal/lazarus.  Turbo Pascal 16 color modes store bitmaps in a planar mode. Just like EGA\VGA hardware specs describe. Also Turbo Pascal and Turbo C remap the plane/color order to make it even more confusing.

Freepascal stores the bitmaps for putimage slightly different.  I have modified the advance unit to do an on the fly conversion when you BLoad the image. This way you don't need to convert every single image. You can if you like - just bsave the new converted image.
Code: [Select]
unit Advanced;

{ Advanced Routines Unit, Version 1.0, Copyright 1996 by George M. Tzoumas }

interface

procedure BSave(FN: String; P: Pointer; Length: Word);
procedure BLoad(FN: String; var P: Pointer);

implementation
const
  MaxWidth = 2047;
type
   BLine       = Array [0..MaxWidth]  of Byte;
   WLine       = Array [0..MaxWidth]  of Word;

 TPImgRec = Record
            width,height : word;
            Buf : array[0..10000] of byte;
          end;

 FPImgRec = Record
            width,height,reserved : longint;
            Buf : array[0..10000] of word;
          end;
var
  BorlandColorMap : array[0..15] of Word = (0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15);


Function BitOn(Position,Testbyte : Byte) : Boolean;
Var
  Bt : Byte;
Begin
  Bt :=$01;
  Bt :=Bt Shl Position;
  Biton :=(Bt And Testbyte) > 0;
End;

Function BytesPerRow(width : word) : Word;
begin
 BytesPerRow :=(width+7) div 8;
end;

Procedure mpTOsp(Var mPlane : bLine;Var splane : wLine;BPR : Word);
Var
 i,j    : Word;
 xpos : Word;
 Col  : Word;
 ImgOff2,ImgOff3,ImgOff4 : Word;
begin
 ImgOff2:=BPR;
 ImgOff3:=BPR*2;
 ImgOff4:=BPR*3;
 xpos:=0;
 FillChar(splane,SizeOf(sPlane),0);
 For i:=0 to ImgOff2-1 do
 begin
   For j:=7 downto 0 do
   begin
     Col:=0;
     if biton(j,mPlane[i]) then
     begin
       Inc(Col,1);
     end;

     if biton(j,mPlane[i+ImgOff2]) then
     begin
       Inc(Col,2);
     end;

     if biton(j,mPlane[i+ImgOff3]) then
     begin
       Inc(Col,4);
     end;

     if biton(j,mPlane[i+ImgOff4]) then
     begin
       Inc(Col,8);
     end;

//     Splane[xpos]:=Col;
     Splane[xpos]:=BorlandColorMap[Col];

     Inc(xpos);
   end;
 end;
end;


procedure BSave(FN: String; P: Pointer; Length: Word);
var
  F: File;
begin
  Assign(F, FN);
  Rewrite(F, Length);
  BlockWrite(F, P^, 1);
  Close(F);
end;



Procedure TPImageToFPImage(var P : Pointer;PSize : LongInt);
var
 FPImgPtr : ^FPImgRec;
 TPImgPtr : ^TPImgRec;
 bbuf     : bline;
 wbuf     : wline;
 pos,pos2      : word;
 width,height : word;
 BPR : word;
 NP : Pointer;
 NPSize : longint;
 i,j : word;
begin
 TPImgPtr:=P;
 width:=TPImgPtr^.width+1;
 height:=TPImgPtr^.height+1;

 BPR:=BytesPerRow(width);
 NPSize:=12+ (BPR*4*2)*height;

 GetMem(NP, NPSize);
 FPImgPtr:=NP;
 FPImgPtr^.width:=width;
 FPImgPtr^.height:=height;
 FPImgPtr^.reserved:=0;

 pos:=0;
 pos2:=0;

 For i:=1 to height do
 begin
   Move(TPImgPtr^.Buf[pos],bbuf,BPR*4);
   mpTosp(bbuf,wbuf,BPR);
   Move(wbuf,FPImgPtr^.Buf[pos2],width*2);
   inc(pos,BPR*4);
   inc(pos2,width);
 end;

 FreeMem(P,PSize);
 P:=NP;
 end;

procedure BLoad(FN: String; var P: Pointer);
var
  F: File;
  FSize : LongInt;
begin
  Assign(F, FN);
  Reset(F, 1);
  FSize:= FileSize(F);
  GetMem(P, FSize );
  BlockRead(F, P^, FSize);
  Close(F);
  TPImageToFPImage(P,FSize);
end;

end.
« Last Edit: May 17, 2021, 10:06:49 am by marcov »

lucamar

  • Hero Member
  • *****
  • Posts: 4009
Re: Convert pictures of a Turbo Pascal program ported to ptcGraph
« Reply #2 on: May 15, 2021, 08:45:42 pm »
For gods sake, please use code tags or learn to share larger pieces of code! ;)
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.

Roland57

  • Full Member
  • ***
  • Posts: 187
Re: Convert pictures of a Turbo Pascal program ported to ptcGraph
« Reply #3 on: May 15, 2021, 09:16:18 pm »
@retronick

Thank you for your message and for your adaptation of the Advanced unit.

I had to modify this line to compile the code. I don't know if my modification has sense.

Code: Pascal  [Select][+][-]
  1.      if biton(j,mPlane[i]) then // <---

After that the program can be compiled but here, it crashes.

Quote
Loading ...An unhandled exception occurred at $0000000000422608:
EAccessViolation: Access violation
$0000000000422608
$0000000000474991  BLOAD,  line 155 of advanced.pas

Did you manage, on your side, to run the program?

ASAP I will try to use your code for the little test program, which is made for displaying a single (strawberry) image.

Regards.

Roland 
« Last Edit: May 15, 2021, 09:18:20 pm by Roland57 »

retronick

  • New member
  • *
  • Posts: 9
Re: Convert pictures of a Turbo Pascal program ported to ptcGraph
« Reply #4 on: May 15, 2021, 09:38:21 pm »
I think it got mangled - I have attached a zip version.

I was also calculating the memory size wrong.

I was able to compile the original TP code to the point where it was displaying the pacman and ghost properly but the maze was messed up.

Try it with just a small test program to see if this code works better.
« Last Edit: May 16, 2021, 02:02:32 am by retronick »

Roland57

  • Full Member
  • ***
  • Posts: 187
Re: Convert pictures of a Turbo Pascal program ported to ptcGraph
« Reply #5 on: May 16, 2021, 03:43:44 pm »
I was able to compile the original TP code to the point where it was displaying the pacman and ghost properly but the maze was messed up.

Same thing for me with your latest code, integrated in the ptcGraph project. The pictures are correctly loaded. Thank you very much for your help.

Now I will try to find why the maze is messed up. I will come back to this discussion as soon as I find something.

retronick

  • New member
  • *
  • Posts: 9
Re: Convert pictures of a Turbo Pascal program ported to ptcGraph
« Reply #6 on: May 17, 2021, 12:12:28 am »
that's great I am glad I could help. If you ever finish this let me know i would like to see the final version.

Roland57

  • Full Member
  • ***
  • Posts: 187
Re: Convert pictures of a Turbo Pascal program ported to ptcGraph
« Reply #7 on: May 17, 2021, 01:25:30 pm »
Fixed loading of maze data. The game works now.  :)

It crashes in the middle of the game when compiled with -dDEBUG. There must be errors somewhere.

Help welcome.
« Last Edit: May 17, 2021, 01:32:00 pm by Roland57 »

retronick

  • New member
  • *
  • Posts: 9
Re: Convert pictures of a Turbo Pascal program ported to ptcGraph
« Reply #8 on: May 17, 2021, 03:58:17 pm »
Wow! I didn't think you would be so quick. Try compiling with ${mode TP} and PACKRECORDS 1

Sometimes this easily make some of my old code just work.

dseligo

  • Sr. Member
  • ****
  • Posts: 276
Re: Convert pictures of a Turbo Pascal program ported to ptcGraph
« Reply #9 on: May 17, 2021, 06:26:21 pm »
Fixed loading of maze data. The game works now.  :)

Very nice :)

Roland57

  • Full Member
  • ***
  • Posts: 187
Re: Convert pictures of a Turbo Pascal program ported to ptcGraph
« Reply #10 on: May 18, 2021, 09:00:29 am »
Very nice :)
Indeed.  :)

I really do not regret my efforts. It's a very nice little game. The source code is surprisingly short. Congratulations to the author.

Wow! I didn't think you would be so quick.
It was easy. The solution was to use the original BLoad procedure for loading the maze data.

Thank you for the hints about compilation mode. It seems that the game crashes when the range checking is on. With patience I should be able to find the errors.

retronick

  • New member
  • *
  • Posts: 9
Re: Convert pictures of a Turbo Pascal program ported to ptcGraph
« Reply #11 on: May 18, 2021, 05:07:31 pm »
The issue probably also exists in the original code except no errors are thrown. probably just accessing the contents of one of the arrays limit. you can try increasing the array sizes and see if the error goes away.


Roland57

  • Full Member
  • ***
  • Posts: 187
Re: Convert pictures of a Turbo Pascal program ported to ptcGraph
« Reply #12 on: May 19, 2021, 07:24:49 am »
I found the error. It's here:

Code: Pascal  [Select][+][-]
  1. // mspacman.pas line 604
  2.   Inc(cwf); LogLn(Format({$I %LINE%} + ' cwf = %d', [cwf]));

The error happens when cwf = 255. I still have to figure how to fix it.

To compile and run the game, you can do this:
make
make run


If you wish to run the game without error:
make release
make run
« Last Edit: May 19, 2021, 08:28:10 am by Roland57 »

retronick

  • New member
  • *
  • Posts: 9
Re: Convert pictures of a Turbo Pascal program ported to ptcGraph
« Reply #13 on: May 19, 2021, 06:34:33 pm »
that variable is declared as a byte.

in turbo pascal if

var
  c  : byte;
...
c:=255;
inc(255);
writeln(c);    <-- will display 0

the variable will be set to 0

in freepascal it will not - it will generate an error

so if you want to make your code compatible with turbo pascal you should check if it is 255 and set to 0 - if not inc(cwf)

or turn off range checking before you inc
{$R-}
inc(cwf);
{$R+}

I think your release compiler options turn off range checking so that's why it works.

make sense?








« Last Edit: May 19, 2021, 07:26:56 pm by retronick »

Roland57

  • Full Member
  • ***
  • Posts: 187
Re: Convert pictures of a Turbo Pascal program ported to ptcGraph
« Reply #14 on: May 20, 2021, 10:50:23 am »
@retronick

Yes, all what you say makes sense. Thank you for your help.

So I made this:

Code: Pascal  [Select][+][-]
  1.   if cwf < High(byte) then
  2.     Inc(cwf)
  3.   else
  4.     cwf := 0;

but it seems that there still are other errors. But it isn't really an issue, since (as you said) we can simply turn the range checking off.

If I have time, I will add some sound effects.



 

TinyPortal © 2005-2018