Recent

Author Topic: Examples from fpc.org, first time compile, graph mode  (Read 5090 times)

yugorin

  • Newbie
  • Posts: 3
Examples from fpc.org, first time compile, graph mode
« on: October 19, 2021, 04:13:42 pm »
Hello everyone

It is an honour to join this community. I have come to ask for help in understanding what is going on ;)



I have a funny problem. I have never been and will never be a professional programmer, but every time I try to learn a programming language I encounter an error in the introduction and it discourages me. And it's been like that for 30 years :P This time I didn't give up, and I'm "struggling" with Pascal. I found a source, which should write all graphic modes of the card in the computer to a file. I will describe in order what I noticed.

1. the source copied from www does not compile

https://www.freepascal.org/docs-html/rtl/graph/modes.html

Example
Program GetModeRange_Example;

Errors:
Hint: (11030) Start of reading config file D:\lazarus\fpc\3.2.0\bin\i386-win32\fpc.cfg
Hint: (11031) End of reading config file D:\lazarus\fpc\3.2.0\bin\i386-win32\fpc.cfg
Free Pascal Compiler version 3.2.0 [2021/02/21] for i386
Copyright (c) 1993-2020 by Florian Klaempfl and others
(1002) Target OS: Win32 for i386
(3104) Compiling C:\Users\ABRONO~1\AppData\Local\Temp\project1.lpr
C:\Users\ABRONO~1\AppData\Local\Temp\project1.lpr(40,29) Error: (3069) Call by var for arg no. 3 has to match exactly: Got "LongInt" expected "SmallInt"
D:\lazarus\fpc\3.2.0\units\i386-win32\graph\graph.ppu:modes.inc(371,15) Hint: (5039) Found declaration: GetModeRange(SmallInt;var SmallInt;var SmallInt);
C:\Users\ABRONO~1\AppData\Local\Temp\project1.lpr(43,36) Warning: (5037) Variable "low" does not seem to be initialized
C:\Users\ABRONO~1\AppData\Local\Temp\project1.lpr(43,60) Warning: (5037) Variable "high" does not seem to be initialized
C:\Users\ABRONO~1\AppData\Local\Temp\project1.lpr(62,9) Error: (3208) Illegal assignment to for-loop variable "c"
D:\lazarus\fpc\3.2.0\units\i386-win32\graph\graph.ppu:graph.inc(2130,13) Hint: (5039) Found declaration: InitGraph(var SmallInt;var SmallInt;const ShortString);
project1.lpr(85) Fatal: (10026) There were 2 errors compiling module, stopping
Fatal: (1018) Compilation aborted
Error: D:\lazarus\fpc\3.2.0\bin\i386-win32\ppc386.exe returned an error exitcode

I change this:

 
Code: Pascal  [Select][+][-]
  1. var low, high : smallint;    

 Next error:

 project1.lpr(63,9) Error: Illegal assignment to for-loop variable "c"

 in this for loop

 
Code: Pascal  [Select][+][-]
  1.         for c := low to high do
  2.         begin
  3.         append(t);
  4.         writeln(t,'  testing mode nr ',c);
  5.         close(t);
  6.         initgraph(gd,c,'');
  7.         res := graphresult;    

workaround:


temp vars for this loop
       
Code: Pascal  [Select][+][-]
  1. tmp := c;
  2.         tmp2 := gd;
  3.  
  4.        initgraph(tmp2,tmp,''); !!!!!!!!!!!!!{in this line}!!!!!!!!!!!!!!!!!!!!!!!
  5.         res := graphresult;
  6.        append(t);
  7.         { An error occurred when entering the mode? }
  8.         if res <> grok then
  9.           writeln(t,grapherrormsg(res))
  10.         else
  11.           begin
  12.           write(t,'maxx: ',getmaxx,', maxy: ',getmaxy);
  13.           Writeln(t,', maxcolor: ',getmaxcolor);
  14.           closegraph;
  15.           end;
  16.         writeln(t);
  17.           WriteRes(gd);
  18.         close(t);


and in this part of source I need to REM line closegraph, because I received a message "Project GetModeRange raised exception class 'External: SIGSEGV'. At address 7774F583"

     
Code: Pascal  [Select][+][-]
  1. begin
  2.           write(t,'maxx: ',getmaxx,', maxy: ',getmaxy);
  3.           Writeln(t,', maxcolor: ',getmaxcolor);
  4.          // closegraph; {!!!!!!!!!!!!! this line!!!!!!!!!!!!!!!!}
  5.           end;            


After all this, the program started and entered the available graphic modes into the file :)

What am I doing wrong that the examples from the FreePascal website don't want to work for me and I have to change the sources?
« Last Edit: October 22, 2021, 07:47:35 am by yugorin »

PascalDragon

  • Hero Member
  • *****
  • Posts: 3525
  • Compiler Developer
Re: Examples from fpc.org, first time compile, graph mode
« Reply #1 on: October 21, 2021, 09:45:41 pm »
What am I doing wrong that the examples from the FreePascal website don't want to work for me and I have to change the sources?

You're not doing anything wrong. It seems that nobody tested that example with Free Pascal and only with Turbo Pascal. Turbo Pascal doesn't seem to have a problem when the index variable of the for is changed, but FPC does.

So what you did as a workaround is indeed correct.

I don't know why the CloseGraph leads to an error however. I can't reproduce it on my system (x86_64-win64). Would you please try the example as-is (with the exception of your workaround)?

yugorin

  • Newbie
  • Posts: 3
Re: Examples from fpc.org, first time compile, graph mode
« Reply #2 on: October 22, 2021, 07:51:26 am »
Hello PascalDragon

This is what I did. I managed to compile the source code after my changes and got the resulting exe file back with modes.txt with graphics modes. Thanks also for explaining the differences between TurboPascal and FreePascal. I was just convinced that the source codes on the site were runnable and written for FreePascal.

All the best
Yugorin

PascalDragon

  • Hero Member
  • *****
  • Posts: 3525
  • Compiler Developer
Re: Examples from fpc.org, first time compile, graph mode
« Reply #3 on: October 22, 2021, 03:46:47 pm »
I was just convinced that the source codes on the site were runnable and written for FreePascal.

That is definitely how it should be. It simply slipped through probably...

BobDog

  • Full Member
  • ***
  • Posts: 170
Re: Examples from fpc.org, first time compile, graph mode
« Reply #4 on: October 23, 2021, 05:57:45 pm »

The graph unit seems a bit restrictive, I cannot retrieve the mouse position and I have to use readkey from wincrt.
I have put together a little line intersection demo.
(I use Geany ide for this, not Lazarus)
Code: Pascal  [Select][+][-]
  1. program line_intersections;
  2.  
  3. uses
  4. math,graph,wincrt;
  5.  
  6.  
  7. Type Point=object
  8. x,y:single;
  9. End;
  10.  
  11. Type _Line=object
  12.     s,f:point;
  13.     procedure Draw();
  14. End;
  15.  
  16. type aol=array of _Line;
  17.  
  18. procedure _Line.draw();
  19. begin
  20. line(trunc(s.x),trunc(s.y),trunc(f.x),trunc(f.y));
  21. end;
  22.  
  23. Function isleft(L:_Line;p:point):integer;
  24. begin
  25.     exit (-sign((L.s.x-L.f.x)*(p.y-L.f.y)-(p.x-L.f.x)*(L.s.y-L.f.y)));
  26. End;
  27.  
  28. Function intersects(L1:_Line;L2:_Line):boolean;
  29. begin
  30.     If (isleft(L2,L1.s) = isleft(L2,L1.f)) Then exit (false);
  31.     If (isleft(L1,L2.s) = isleft(L1,L2.f)) Then exit (false);
  32.     exit(true);
  33. End;
  34.  
  35.  
  36. function intersections(l1:_Line;l2:_Line;var _out:point):boolean;
  37. var
  38. p1,p2,p3,p4:point;
  39. x12,x34,y12,y34:single;
  40.  c:single;
  41.  a,b,x,y:single;
  42.  begin
  43.  p1:=l1.s;p2:=l1.f;p3:=l2.s;p4:=l2.f;
  44.  x12:=p1.x-p2.x;x34:=p3.x-p4.x;y12:=p1.y-p2.y;y34:=p3.y-p4.y;
  45.  c:=x12 * y34 - y12 * x34;
  46. if (abs(c) < 0.01) then exit(false);
  47.   a := p1.x * p2.y - p1.y * p2.x;
  48.   b := p3.x * p4.y - p3.y * p4.x;
  49.   x := (a * x34 - b * x12) / c;
  50.   y := (a * y34 - b * y12) / c;
  51.   _out.x:=x;
  52.   _out.y:=y;
  53.   exit(true)
  54. end;
  55.  
  56. procedure setup(var k:aol;var s:point);
  57. var
  58. i:integer;
  59. begin
  60. setlength(k,3);
  61. for i:=0 to 2 do
  62. begin
  63. k[i].s.x:=random(800);
  64. k[i].s.y:=random(600);
  65. k[i].f.x:=random(800);
  66. k[i].f.y:=random(600);
  67. s.x:=random(800);s.y:=random(600);
  68. end;
  69. end;
  70.  
  71. var
  72.  k:aol;
  73.  gd, gm: SmallInt;
  74.  i:integer;
  75.  ch:char;
  76.  m:_Line;
  77.  xpos,ypos:string;
  78.  start,p:point;
  79.  
  80. begin
  81. setup(k,start);
  82. m.s.x:=random(800);
  83. m.s.y:=random(600);
  84. {==========  set up graph =========}
  85.       gd := D8bit;
  86.       gm :=  m800x600;
  87.       InitGraph(gd, gm, '');
  88.       if GraphResult <> grok then  halt;
  89.       setbkcolor(white);
  90.       settextstyle(BoldFont,HorizDir,1);
  91.      
  92.       repeat
  93.       cleardevice;
  94.       setcolor(red);
  95.       outtextxy(10,0,'Line intersection demo');
  96.       outtextxy(10,20,'Use the arrow keys');
  97.       outtextxy(10,40,'Space key to refresh -- esc key to end.');
  98.       setcolor(blue);
  99.      
  100.       for i:=0 to 2 do k[i].draw;
  101.       m.f.x:=start.x;m.f.y:=start.y;
  102.       setcolor(red);
  103.       m.draw;
  104.       fillellipse(trunc(m.s.x),trunc(m.s.y),5,5) ;
  105.       for i :=0 to 2 do
  106.       begin
  107.     if ((intersections(m,k[i],p)) and (intersects(k[i],m))) then
  108.     begin
  109.      str(trunc(p.x),xpos);
  110.      str(trunc(p.y),ypos);
  111.       fillellipse(trunc(p.x),trunc(p.y),2,2) ;
  112.       setcolor(green);
  113.      outtextxy(trunc(p.x),trunc(p.y),xpos+','+ypos);
  114.     end;
  115.     end;
  116.    
  117.       ch:=readkey;
  118.       if ch='P' then m.s.y:=m.s.y+10;
  119.       if ch='H' then m.s.y:=m.s.y-10;
  120.       if ch='M' then m.s.x:=m.s.x+10;
  121.       if ch='K' then m.s.x:=m.s.x-10;
  122.       if ch=' ' then setup(k,start);
  123.       until (ord(ch)=27);
  124.      
  125.      closegraph;
  126. end.
  127.  

Bart

  • Hero Member
  • *****
  • Posts: 4516
    • Bart en Mariska's Webstek
Re: Examples from fpc.org, first time compile, graph mode
« Reply #5 on: October 23, 2021, 06:46:30 pm »
Turbo Pascal doesn't seem to have a problem when the index variable of the for is changed, but FPC does.
Adding {$mode TP} does however allow this.
This will also make integer=smallint, so that error goes away too.

I also get a RTE, but on a different line:
C:\Users\Bart\LazarusProjecten\ConsoleProjecten>test
Runtime error 216 at $770AF583
  $770AF583
  $770900EA
  $7708FF39
  $00412506
  $00401A31  WRITERES,  line 26 of test.pas
  $00401DE8  main,  line 75 of test.pas
  $004097E7

Line 25+26 are:
Code: Pascal  [Select][+][-]
  1.   outTextXY((getMaxX - TW) div 2,
  2.             (getMaxY - TH) div 2,text);

Even if I change all parameters of outTextXY to fixed safe values (e.g. outTextXY(1,1,'dummy')) or replace outTextXY with outText it will crash.

Bart

Bart

  • Hero Member
  • *****
  • Posts: 4516
    • Bart en Mariska's Webstek
Re: Examples from fpc.org, first time compile, graph mode
« Reply #6 on: October 23, 2021, 07:12:55 pm »
It crashes when testing
4 bit: low modenr = 1, high modenr = 7
  testing mode nr 1
gd = 13
and it tries to do
outTextXY(179,95, 'Current resolution is 640x200x4 bit');

When I assign outTextXY to a dummy procedure the RTE goes away.

Bart

Bart

  • Hero Member
  • *****
  • Posts: 4516
    • Bart en Mariska's Webstek
Re: Examples from fpc.org, first time compile, graph mode
« Reply #7 on: October 23, 2021, 07:19:27 pm »
Since I built fpc trunk with debug info I got a potentially better output:
Code: [Select]
Runtime error 216 at $770AF583
  $770AF583
  $770900EA
  $7708FF39
  $0041071A  OUTTEXTXYWIN32GUI,  line 364 of ./graph/src/win32/graph.pp
  $00401BC4  WRITERES,  line 40 of test.pas
  $0040244E  main,  line 104 of test.pas
  $00409B77  EXE_ENTRY,  line 171 of system.pp

Line 364 of ./graph/src/win32/graph.pp reads:
EnterCriticalSection(graphdrawing);

Bart

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 9713
  • FPC developer.
Re: Examples from fpc.org, first time compile, graph mode
« Reply #8 on: October 23, 2021, 11:03:42 pm »
The graph unit seems a bit restrictive, I cannot retrieve the mouse position and I have to use readkey from wincrt.

The fpctris unit does iirc.

PascalDragon

  • Hero Member
  • *****
  • Posts: 3525
  • Compiler Developer
Re: Examples from fpc.org, first time compile, graph mode
« Reply #9 on: October 25, 2021, 09:13:52 am »
Turbo Pascal doesn't seem to have a problem when the index variable of the for is changed, but FPC does.
Adding {$mode TP} does however allow this.
This will also make integer=smallint, so that error goes away too.

I know that using mode TP solves the problem, but that's not the point: the example should work as is. Also it has no mode declaration thus the mode is FPC and there Integer = SmallInt as well.

Bart

  • Hero Member
  • *****
  • Posts: 4516
    • Bart en Mariska's Webstek
Re: Examples from fpc.org, first time compile, graph mode
« Reply #10 on: October 25, 2021, 10:06:26 am »
I know that using mode TP solves the problem, but that's not the point: the example should work as is. Also it has no mode declaration thus the mode is FPC and there Integer = SmallInt as well.

That observation was merely meant as a suggesntion how to fix the compilation error of the example, nothing else.
If a certain mode is expected for some example code to compile, it should be specified in the example, either via a compiler directive or in a comment telling the user to use the appropriate commandline parameter for the compiler.

Bart

yugorin

  • Newbie
  • Posts: 3
Re: Examples from fpc.org, first time compile, graph mode
« Reply #11 on: October 26, 2021, 05:23:23 pm »
Thank you so much for taking such a deep approach to my problem. Heated discussion as always makes sense. Please look at it from the perspective of someone like me. I wanted to play around, I wanted to learn a programming language (Pascal, C++, python - doesn't matter). I go to a site dedicated to it, take an example, compile and get an error. It's frustrating when such an example doesn't work. When you don't have the right knowledge (and a beginner doesn't have it) it's hard to deal with this topic. You can ask a friend, you can ask on the forum, but on the other hand, respecting the time of people on the forum, you could do it by asking about really more serious matters. Again, I want to say that I really appreciate this discussion. Perhaps it will cause that someone will review the sources, check whether they compile. :)

Thank you
Yugorin

BobDog

  • Full Member
  • ***
  • Posts: 170
Re: Examples from fpc.org, first time compile, graph mode
« Reply #12 on: October 26, 2021, 07:49:08 pm »

Freepascal lacks simple graphics as far as I can see.
Just to get little jobs done like plotting functions, drawing pixels, lines, circles e.t.c.
Also getting mouse and keyboard inputs in a graphics loop.
I don't use Lazarus so including graph and wincrt is the only way I think, and it is really slow.
In windows you can use the console.
https://forum.lazarus.freepascal.org/index.php/topic,26288.msg417652.html#new
I submitted dll's a while back
https://forum.lazarus.freepascal.org/index.php/topic,44021.msg309066.html#msg309066
But if the graph unit was brought up to date it might suffice for simple stuff.




winni

  • Hero Member
  • *****
  • Posts: 2807
Re: Examples from fpc.org, first time compile, graph mode
« Reply #13 on: October 26, 2021, 08:41:26 pm »
Hi!

For the simple graphics there is since Delphi times the property Canvas.

A Canvas you can find on the TForm, the Timage, the TPaintBox, the TPanel and many more.

Simple example:

https://wiki.lazarus.freepascal.org/Drawing_with_canvas

Here is the class definition of TCanvas:

https://lazarus-ccr.sourceforge.io/docs/lcl/graphics/tcanvas.html


Winni

BobDog

  • Full Member
  • ***
  • Posts: 170
Re: Examples from fpc.org, first time compile, graph mode
« Reply #14 on: October 26, 2021, 11:44:03 pm »

Hello winni
Thank you.
Your links show only bits and pieces, how do I get a graphics screen? If I manage that then the class definitions would be easy enough to follow.
What files do I include in the code to get started?
Please remember that I am using the Geany ide, must I use the Lazarus ide to get a form or graphics screen?
For the OS, opengl is OK for graphics, 3D or 2D.
Here I supply a 64 bit .dll for a basic openGL graphics screen and a few procedures.
tested win 10 64 bits
fpc 3.2.2.





 

TinyPortal © 2005-2018