delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/01/18/15:45:17

From: "John M. Aldrich" <fighteer AT cs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: *help reading char in text mode from (X,Y)
Date: Sun, 18 Jan 1998 15:39:42 -0500
Organization: Two pounds of chaos and a pinch of salt.
Lines: 95
Message-ID: <34C2688E.513D@cs.com>
References: <34c256ed DOT 471444 AT news DOT ziplink DOT net>
NNTP-Posting-Host: ppp227.cs.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

This is a multi-part message in MIME format.

--------------9D710015069
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Jeff W./DMT wrote:
> 
> char GetChar(char x, char y)
> {
>  //returns the character at (x,y)
>  unsigned char *videoptr = (unsigned char *)0xB800;
> 
>  __djgpp_nearptr_enable();
>  c = videoptr[160*(y-1) + 2*(x-1) + __djgpp_conventional_base];
>  return c;
> 
> }
> 
> I tried testing it out by doing:
> clrscr();
> printf("a");
> c = GetChar(0,0);
> printf("%c",c);
> 
> and also c = GetChar(1,1) just in case the coordinates started at 1,1
> and not 0,0.  Can anyone help me out??

Well, calling your function with coordinates {0, 0} obviously won't work
because it would then try to read {-1, -1}.  Calling it with {1, 1}
should work, except that because standard output in DJGPP is
line-buffered, the 'a' doesn't get displayed until you print a newline,
so there's nothing at coordinates {0, 0} for your function to read.  

You should never mix stdio and conio functions without understanding how
they work together.  See chapter 9.4 of the Frequently Asked Questions
list (v2/faq210b.zip or online at http://www.delorie.com/djgpp/v2faq/)
to learn more.  In this case, simply substituting cprintf() for printf()
would have worked.

Except for this line:

>  unsigned char *videoptr = (unsigned char *)0xB800;

Try reading from 0xb8000, not 0xb800.

Another bug:  you don't declare 'c' in GetChar().

The attached program works correctly.

-- 
---------------------------------------------------------------------
|      John M. Aldrich       | "Autocracy is based on the assumption|
|       aka Fighteer I       | that one man is wiser than a million |
|   mailto:fighteer AT cs DOT com   | men.  Let's play that over again,    |
| http://www.cs.com/fighteer | too.  Who decides?"   - Lazarus Long |
---------------------------------------------------------------------

--------------9D710015069
Content-Type: text/plain; charset=us-ascii; name="sgetchar.c"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline; filename="sgetchar.c"

#include <stdio.h>
#include <conio.h>
#include <dpmi.h>
#include <sys/nearptr.h>

char GetChar(char x, char y)
{
    char c;

    //returns the character at (x,y)
    unsigned char *videoptr = (unsigned char *)0xB8000;

    __djgpp_nearptr_enable();
    c = videoptr[ ( x + y * 80 ) * 2 + __djgpp_conventional_base];
    return c;

}

int main( void )
{
    char c;

    clrscr();
    cprintf("a");
    c = GetChar(0,0);
    printf("%c",c);

    return 0;
}

--------------9D710015069--

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019