Forum > Beginners

Examples from fpc.org, first time compile, graph mode

(1/3) > >>

yugorin:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var low, high : smallint;    
 Next error:

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

 in this for loop

 
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---        for c := low to high do        begin        append(t);        writeln(t,'  testing mode nr ',c);        close(t);        initgraph(gd,c,'');        res := graphresult;    
workaround:


temp vars for this loop
       
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---tmp := c;        tmp2 := gd;         initgraph(tmp2,tmp,''); !!!!!!!!!!!!!{in this line}!!!!!!!!!!!!!!!!!!!!!!!        res := graphresult;       append(t);        { An error occurred when entering the mode? }        if res <> grok then          writeln(t,grapherrormsg(res))        else          begin          write(t,'maxx: ',getmaxx,', maxy: ',getmaxy);          Writeln(t,', maxcolor: ',getmaxcolor);          closegraph;          end;        writeln(t);          WriteRes(gd);         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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---begin          write(t,'maxx: ',getmaxx,', maxy: ',getmaxy);          Writeln(t,', maxcolor: ',getmaxcolor);         // closegraph; {!!!!!!!!!!!!! this line!!!!!!!!!!!!!!!!}          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?

PascalDragon:

--- Quote from: yugorin on October 19, 2021, 04:13:42 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?

--- End quote ---

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:
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:

--- Quote from: yugorin on October 22, 2021, 07:51:26 am ---I was just convinced that the source codes on the site were runnable and written for FreePascal.

--- End quote ---

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

BobDog:

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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program line_intersections; usesmath,graph,wincrt;  Type Point=objectx,y:single;End; Type _Line=object    s,f:point;    procedure Draw();End; type aol=array of _Line; procedure _Line.draw();beginline(trunc(s.x),trunc(s.y),trunc(f.x),trunc(f.y));end; Function isleft(L:_Line;p:point):integer;begin    exit (-sign((L.s.x-L.f.x)*(p.y-L.f.y)-(p.x-L.f.x)*(L.s.y-L.f.y)));End; Function intersects(L1:_Line;L2:_Line):boolean;begin    If (isleft(L2,L1.s) = isleft(L2,L1.f)) Then exit (false);     If (isleft(L1,L2.s) = isleft(L1,L2.f)) Then exit (false);    exit(true);End;  function intersections(l1:_Line;l2:_Line;var _out:point):boolean;varp1,p2,p3,p4:point;x12,x34,y12,y34:single; c:single; a,b,x,y:single; begin p1:=l1.s;p2:=l1.f;p3:=l2.s;p4:=l2.f; x12:=p1.x-p2.x;x34:=p3.x-p4.x;y12:=p1.y-p2.y;y34:=p3.y-p4.y; c:=x12 * y34 - y12 * x34;if (abs(c) < 0.01) then exit(false);  a := p1.x * p2.y - p1.y * p2.x;  b := p3.x * p4.y - p3.y * p4.x;  x := (a * x34 - b * x12) / c;  y := (a * y34 - b * y12) / c;  _out.x:=x;  _out.y:=y;  exit(true)end; procedure setup(var k:aol;var s:point);vari:integer;beginsetlength(k,3);for i:=0 to 2 dobegink[i].s.x:=random(800);k[i].s.y:=random(600);k[i].f.x:=random(800);k[i].f.y:=random(600);s.x:=random(800);s.y:=random(600);end;end; var k:aol; gd, gm: SmallInt; i:integer; ch:char; m:_Line; xpos,ypos:string; start,p:point; beginsetup(k,start);m.s.x:=random(800);m.s.y:=random(600);{==========  set up graph =========}      gd := D8bit;      gm :=  m800x600;      InitGraph(gd, gm, '');      if GraphResult <> grok then  halt;      setbkcolor(white);      settextstyle(BoldFont,HorizDir,1);            repeat      cleardevice;      setcolor(red);      outtextxy(10,0,'Line intersection demo');      outtextxy(10,20,'Use the arrow keys');      outtextxy(10,40,'Space key to refresh -- esc key to end.');      setcolor(blue);           for i:=0 to 2 do k[i].draw;      m.f.x:=start.x;m.f.y:=start.y;      setcolor(red);      m.draw;      fillellipse(trunc(m.s.x),trunc(m.s.y),5,5) ;      for i :=0 to 2 do      begin    if ((intersections(m,k[i],p)) and (intersects(k[i],m))) then     begin     str(trunc(p.x),xpos);     str(trunc(p.y),ypos);      fillellipse(trunc(p.x),trunc(p.y),2,2) ;      setcolor(green);     outtextxy(trunc(p.x),trunc(p.y),xpos+','+ypos);    end;    end;          ch:=readkey;      if ch='P' then m.s.y:=m.s.y+10;      if ch='H' then m.s.y:=m.s.y-10;       if ch='M' then m.s.x:=m.s.x+10;      if ch='K' then m.s.x:=m.s.x-10;      if ch=' ' then setup(k,start);      until (ord(ch)=27);           closegraph; end. 

Navigation

[0] Message Index

[#] Next page

Go to full version