Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Graphics discussion

obiWanobiWan Posts: 12Member
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?

Comments

  • Phat NatPhat Nat Posts: 757Member
    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.

    [code]
    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;
    [/code]

    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
  • AtexAtex Posts: 268Member
    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.
  • obiWanobiWan Posts: 12Member
    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.
  • obiWanobiWan Posts: 12Member
    Hello Atex,
    yes, are you referring to units such as 'video' and 'graph' . 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?

  • AtexAtex Posts: 268Member
    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:[code][color=Blue]procedure swap(var a,b:longint);
    begin
    a:=a xor b;
    b:=a xor b;
    a:=a xor b;
    end;
    [/color][/code]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:[code][color=Blue]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.
    [/color][/code]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:[code][color=Blue]program ;
    uses swap_int;
    {...}
    var x,y:longint;
    begin
    {...}
    swap(x,y);
    {...}
    end.
    [/color][/code]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 ).
  • Phat NatPhat Nat Posts: 757Member
    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 ;)
  • Phat NatPhat Nat Posts: 757Member
    This post has been deleted.
Sign In or Register to comment.