delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/01/18/20:01:36

From: "A. Sinan Unur" <sinan DOT unur AT cornell DOT edu>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: *help reading char in text mode from (X,Y)
Date: Sun, 18 Jan 1998 19:40:40 -0500
Organization: Cornell University (http://www.cornell.edu/)
Lines: 58
Sender: asu1 AT cornell DOT edu (Verified)
Message-ID: <34C2A107.D914181A@cornell.edu>
References: <34c256ed DOT 471444 AT news DOT ziplink DOT net>
NNTP-Posting-Host: cu-dialup-0078.cit.cornell.edu
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Jeff W./DMT wrote:
> 
> I know how to do it in turbo pascal:
> 
> FUNCTION GetChar(X,Y:BYTE) : CHAR
> CONST
>         ColorSeg = $B800;
> 
> BEGIN
>         GetChar := Chr(Mem[ColorSeg:160*(Y-1) + 2*(X-1)]);
> END;
> 
> but I can't figure out how to do the equivalent in DJGPP.  Here's what
> I tried, but to no avail:
> 
> 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;
> }

IMHO, you are better of using a coordinate system with (0,0) as the
top-left corner of the screen. In any case, I think

unsigned char get_char(unsigned x, unsigned y)
{ /* assumes (0,0) is the top-left corner of the screen */
  return _farpeekb(_dos_ds, 0xb8000+160*y+2*x);
}

is clearer, and more portable, and with any optimization, fast enough.
of course, if you want to write routine to copy a block of the screen,
you would be better of writing it in terms of movedata rather than
individual get_char()s in a loop.
 
> 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??

of course, you could use the available conio functions such as
ScreenGetChar, ScreenRetrieve etc. type info libc func conio to get the
info.
-- 
----------------------------------------------------------------------
A. Sinan Unur
Department of Policy Analysis and Management, College of Human Ecology,
Cornell University, Ithaca, NY 14853, USA

mailto:sinan DOT unur AT cornell DOT edu
http://www.people.cornell.edu/pages/asu1/

- Raw text -


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