www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/02/18/00:51:33

From: ellman AT xs4all DOT nl ()
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Allegro lines vs. pixels
Date: 18 Feb 1997 04:11:07 GMT
Organization: XS4ALL Internet B.V.
Lines: 100
Message-ID: <5eba4r$epn$1@news1.xs4all.nl>
References: <330792EC DOT 7443 AT byu DOT edu>
NNTP-Posting-Host: xs1.xs4all.nl
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

In article <330792EC DOT 7443 AT byu DOT edu>, Christian Bird  <cbird AT byu DOT edu> wrote:
>I've got a question for some of those allegro guru's out there.  I'm
>writing a program right now that uses lines of length 2 to 4 pixels
>long.  I'm using 640x480x256 grphics mode and I'm wondering if it's
>faster to do a vline or 2 inline _putpixel's.  I would think that doing
>the putpixels would be faster, but I'm not sure.  At some point they one
>becomes faster than the other. Does anyone know which is faster for
>certain length lines? Any help would be greatly appreciated.  Thanks a
>lot!

The best way to find out which is faster is to write both versions and
compare how fast they run. You can either write a program that draws thousands
of lines and time how long it takes to run each version, or you can use
gprof to find out which version takes up the most time (for information about
gprof, see the DJGPP FAQ).

If you want lines of length 2 or 4 pixels, an even faster way to do them is
to write two or four pixels at once using words or longwords.
If you are writing to an area of memory, you can declare a variable of type
'short *' and get it to point to the start of your memory block. To write
two pixels to the screen, use:

screen[pos]=value;

screen is the short pointer you've set to the memory.
pos is (x+y*screenwidth)/2
value=(pixel1<<8)+(pixel2)

Of course, you should try to avoid multiplication and division where speed is
critical.

The limitation is that the start of your line must have even X co-ordinates.

For groups of 4 pixels, declare your pointer 'long *'. To write 4 pixels: use

screen[pos]=value;

screen is the long pointer you've set to the memory.
pos is (x+y*screenwidth)/4
value=(pixel1<<24)+(pixel2+<<16)+(pixel3<<8)+(pixel4)

The limitation is that the start of your line must have X co-ordinates
divisible by 4.


When writing directly to screen memory, conventional C pointers no
longer work due to the nature of DPMI. Instead, you must use the _far
functions (defined in <sys/farptr.h>. You must set the selector to _dos_ds
(which is defined in <go32.h>)
You can then use the _farnspoke function to write a value to screen memory.

When you are about to start writing to the screen, use this line of code:

_farsetsel(_dos_ds);

As 640x480x256 is an SVGA mode, writing to the screen tends to get a bit
tricky as each line doesn't nescescarily start at the address after the
previous line. Fortunately, allegro provides a function
bmp_read_line(BITMAP *any_bitmap, int y) that finds the start address of
a line of SVGA video memory. To make things simple, I will show the
linear-memory version (eg. mode 13h), and show what you need to change to SVGA.

You can write to a single pixel with:
_farnspokeb(0xA0000L+(x+y*screenwidth),value1);

To write to two pixels, use:
_farnspokew(0xA0000L+(x+y*screenwidth),(pixel1<<8)+(pixel2));
The start of your two pixel line needn't nescescarily have even co-ordinates,
but the function works quicker if it does because with even co-ordinates,
the CPU can write the pixel in a single bus access.

To write 4 pixels: use
_farnspokel(0xA0000L+(x+y*screenwidth),(pixel1<<24)+(pixel2<<16)+(pixel3<<8)+(pixel4));
This is also faster if aligned correctly (ie. x is divisible by 4).

to get it to work on SVGA, use:
lineadr=bmp_read_line(screen, y);
Then, use one of the _far functions, but replace '0xA0000L+(x+y*screenwidth)'
with x+lineadr

An even faster way to do this is to enable near pointers and write to
screen memory without using selectors. Unfortunately, I havent sussed it out
yet, but you can view Brennan Underwood's tutorial on DJGPP VGA access at
http://www.rt66.com/~brennan/djgpp/how_to_access_vga.html

In fact, Brennan has a whole page devoded to DJGPP and games at:
http://www.rt66.com/~brennan/djgpp/


Keep hacking...


The fluffy AE.

--
Andrei Ellman -- URL: http://www.xs4all.nl/~ellman/ae-a -- ae1 AT york DOT ac DOT uk
"All I wanna do is have some fun     :-)      ||  ae-a AT minster DOT york DOT ac DOT uk
 I've got the feeling I'm not the only one"   ||  mailto:ellman AT xs4all DOT nl
     -- Sheryl Crow      :-)    ||       It's what you make of it.

- Raw text -


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