Howdy, Stranger!

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

Categories

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.

Reading a char from CRT screen

rianmalrianmal Posts: 13Member
Hi there, I am developing a small game in which I draw a map from a file to the screen. The player moves a "@" symbol. Now, I can program PASCAL to move the player char and check that the player char does not cross the walls.

BUT in order to do that I need code that will read the relevant character from the relevant part of the screen. My uses clause is "uses crt;".

FYI I am running Freepascal on Windows Vista Home Edition.

Does anyone have any code to read a char from CRT screen? Or can anyone suggest a way to handle collision detection?

Thanks in advance for your help.

Comments

  • PP2005PP2005 Posts: 111Member
    Instad of trying to read a character from the screen, just have a two dimensional array that stores information about the map and walls. You can then always check whether or not the player is moving into a wall by checking the player's position against this array.
  • Phat NatPhat Nat Posts: 757Member
    I agree with using the 2D array method, but if you are really bent on reading a character from the screen, you can use:

    FUNCTION GetChar(X, Y : Byte) : Char;
    Begin
    GetChar := Chr(Mem[$B800: X*2 + Y*160]);
    End;

    Quick Explanation:
    The standard color capable DOS screen is held in memory at location [$B800:0000] (Note monochrome is at {$B000:0000])
    You may read or write to the screen by accessing this area.

    Each character on the screen is made up of two bytes: One carrying the Character ASCII value and the other carrying the Color information. That is why above you can see we multiply the X value by 2 (because we don't care about the color information) and each Y is multiplyed by 160 (80 characters + 80 color informations)

    In old DOS days, the default screen was 80 characters wide x 25 high. Windows likes to use 80 x 50. So depending on the screen size, you will either access the first 4000 bytes of memory (160*25 lines) or 800 bytes (160*50 lines)

    Hope you learned something or at least got what you needed ;)

    Phat Nat
  • rianmalrianmal Posts: 13Member
    Thanks for your answers both of you.
    PhatNat, in particular, I learned from your explanation.
    I guess I will have to do the 2d array thing because FPC doesn't recognise "Mem", even with the dos unit in the uses clause.
    Will be back in touch if I have any trouble.
    Cheers,
    Rianmal.

  • rianmalrianmal Posts: 13Member
    Thanks for your answers both of you.
    PhatNat, in particular, I learned from your explanation.
    I guess I will have to do the 2d array thing because FPC doesn't recognise "Mem", even with the dos unit in the uses clause.
    Will be back in touch if I have any trouble.
    Cheers,
    Rianmal.

  • ActorActor Posts: 438Member

    Don't use an array that you have to maintain. To quote Kernighan and Plauger "Let the machine do the dirty work."

    First define a type
    [code]
    Type
    ScreenType = Array [1 .. 80, 1 .. 25] of record
    Ch : Char ;
    At : Byte
    end ; { record }
    [/code]

    Now declare two variables
    [code]
    Var
    Screen : ScreenType Absolute $B800 ; { word 'Absolute' is absolutely necessary ;) }
    ScreenChar : Char ;
    [/code]
    Now you can assign the character at screen position X,Y to the variable ScreenChar with the statement
    [code]
    ScreenChar := Screen[X,Y].Ch ;
    [/code]

    With this approach you don't have to keep track of what is where on the screen. You are actually reading the data from the screen.

    This works with a color screen in Turbo Pascal. Try it with your Pascal.

Sign In or Register to comment.