Pascal

Moderators: None (Apply to moderate this forum)
Number of threads: 4106
Number of posts: 14016

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Graphics discussion Posted by obiWan on 11 Feb 2009 at 2:18 AM
Hello,
I am just learning pascal and it's also my first programming language. Can someone help me along with doing graphics? I'm using freepascal on linux. What do I need to do to, for example, to draw a rectangle on the screen? And can I do something like that in a normal terminal like KDE's Konsole?
Report
Re: Graphics discussion Posted by Phat Nat on 11 Feb 2009 at 7:52 PM
You can try the code below. The assembly should all be the same, not sure about the memory addressing in Linux though? It may crash sommething, but if not I can give you a whole graphics unit.

USES Crt, Dos;

{ Draws a rectangle with Top-Left corner at X,Y and a size of Width*Height in COLOR on WHERE screen }
PROCEDURE Rectangle(X, Y, Width, Height : Word; Color : Byte; Where : Word);
Begin
     For Y := Y to (Y + Height) Do
       For X := X to (X + Width) Do
         Mem[Where: X + Y*320] := Color;
End;

CONST
     Text = $B800;
     VGA  = $A000;   { Memory location of the VGA screen (...at least in dos) }

Begin
     ASM
        Mov Ax, $0013   { 00h = Set Graphics Mode, 13h = 320x200 256 Colors }
        Int $10         { Int 10h  = Call Graphics Interrupt }
     END;

     { Draw a 30x30 rectangle, top-left location @ 10,10 in White (15) on the VGA screen }
     Rectangle(10, 10, 30, 30, 15, VGA);
     Readkey;           { Wait for a key press }

     ASM
        Mov Ax, $0003   { 00h = Set Graphics Mode, 03h = 80x25 Text Mode }
        Int $10         { Int 10h  = Call Graphics Interrupt }
     END;
End;


WHERE will always be VGA until you start working with Virtual Screens.
As you will notice, once you start drawing alot of stuff repeatedly the screen will flicker. This is handled by VIRTUAL SCREENS. Basically a chunk of memory the same size as your screen (320x200 = 64000 bytes) where you draw everything to, then you just 'flip' the whole virtual screen onto the real screen and voila! no flicker.

Notice that the VGA resolution allows a virtual screen to fit in the 64k memory limitation ;)
If this works, search this board for GRAPHICS.PAS and you should find my old unit.

I know Turbo Pascal also had BGI graphics, but not sure what FPC has.

Phat Nat
Report
Re: Graphics discussion Posted by Atex on 11 Feb 2009 at 11:59 PM
I haven't coded on linux myself, but since linux runs under protected mode and is highly secure most likely doesn't like direct memory addressing. One thing in protected mode the whole virtual memory is available in one block, therefore segm:offs addressing will not work, FPC supports "mem" and "memw" only on the 'go32v2' platform to map the dos memory. I'm new to FPC as well, so far I played only with the built in ASM, didn't try graphics yet, but I think is well worth the upgrade from TP (beside that is FREE ) If you look on the 'contributed units' page (frepascal.org) there are several units (with sources) dedicated to linux graphic programming.
Report
Re: Graphics discussion Posted by obiWan on 12 Feb 2009 at 5:24 AM
Thanks PhatNat, for the reply- I can sort of follow the program a bit, but I really don't know anything about ASM, mem, memory addressing etc. But in any case it's a good starting point. How does graphics programming work though?- I think I need a general introduction. It seems like you need a different kind of way of controlling and therefore access to the graphics hardware then text output. I really don't even know about buffers. I guess, there is a similarity to using mouse input?
I found some information on the freepascal site about using openGL- but maybe I should finish my first book- I still have a bit to do, including pointers and files, etc. before I start thinking about graphics.
Report
Re: Graphics discussion Posted by obiWan on 12 Feb 2009 at 5:38 AM
Hello Atex,
yes, are you referring to units such as 'video' <http://www.freepascal.org/docs-html/rtl/video/index.html> and 'graph' <http://www.freepascal.org/docs-html/rtl/graph/index.html>. Units are another item on the must learn list- but from a beginner's point of view they are something like a procedure only that it's an external program- mostly offering some special functionality to the program?
It seems like there is a fundamental difference between using openGL or a console- for one if you use openGl, maybe you need a window?
<http://wiki.freepascal.org/OpenGL_Tutorial>
Report
Re: Graphics discussion Posted by Atex on 12 Feb 2009 at 10:38 AM
What I meant was: http://www.freepascal.org/contrib/db.php3?category=Graphics
Units are packages of functions, procedures and definitions, they can be precompiled ( like the SYSTEM unit ) or be just in source form. They expand the possibilities by adding extra functionalities to the language. For example: if you write a procedure which swaps two longints:
procedure swap(var a,b:longint);
begin
 a:=a xor b;
 b:=a xor b;
 a:=a xor b;
end;
Something nonexistent in the out-of-the-box version, and you intend to use it quite often, then instead typing in for every program you make, it could be turned into an unit:
unit swap_int;
interface 
 procdure swap(var a,b:longint);
implementation
 procedure swap;
  begin
   a:=a xor b;
   b:=a xor b;
   a:=a xor b;
 end;
end.
Compile it ( destination = Disk ), then from this point you can use it just don't forget to mention your units name in the uses clause:
program <program name>;
uses swap_int;
{...}
var x,y:longint;
begin
 {...}
 swap(x,y);
 {...}
end.
With TP for example you quite limited with the units included by default ( BP has more, FPC is well equipped by comparison ), but there is a plethora of units/source code on the web so when comes to expand the language the sky's the limit. One more thing using units, this is not an issue with FPC becasue it uses a different memory model, but coding with TP and DOS real-mode when your program outgrows the code segment you would have to break it into units to make it work.

I could only guess how to code graphics under linux, but with windows (working under GUI) is usually done by calling windows API's or interface with the video driver directly ( that's what games do ).
Report
Re: Graphics discussion Posted by Phat Nat on 12 Feb 2009 at 7:34 PM
I believe that OpenGL (much like DirectX) is windows based only, and will not work from a command prompt (although I'm sure with Linux it probably does). The code I gave communicates directly with the hardware to do graphics. Windows will not allow you to communicate directly, but use video calls instead.
If you want to do windows based graphics, it's much more expandable and can handle higher graphics resolutions. OpenGL would be a good study. I always try to hold on to the past too long. I don't think DOS is making a comeback any time soon ;)
Report
This post has been deleted. Posted by Phat Nat on 12 Feb 2009 at 7:35 PM
This post has been deleted.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.