TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: CHRISTOPHER BUTLER
from: JERRY COFFIN
date: 1997-06-08 17:07:00
subject: Graphics routines

On (06 Jun 97) Christopher Butler wrote to All...
 CB> Could someone tell me whats wrong with this code?
[ ... ]
 CB> GraphicsScreen::~GraphicsScreen()
 CB> {
 CB>         asm {           // Back to text mode, via ASM again
 CB>            mov ah,0x13
 CB>            int 0x10
Unless you've got a very unusual graphics card, this isn't going to get
you back to text mode...
 CB> GraphicsScreen::Box(int x1,int y1,int x2,int y2,unsigned char col)
 CB> {
 CB>     // Do basically the same as above, but with a range.
 CB>     // I've repeated the poke instruction here as its probably
 CB>     // faster.
 CB>         int x,y;
 CB>         for (x=x1;x<=x2;x++)
 CB>         {
 CB>                 for (y=y1;y<=y2;y++)
 CB>                 {
 CB>                 pokeb(0xA000,(x+(y*320)),(char)col);
 CB>                 }
 CB>         }
 CB>         return 0;
 CB> }
This still isn't going to be any kind of speed champ with most
compilers.  The problem is that you're re-creating the pointer to memory
again at each iteration of the loop.  With most compilers, the following
is likely to be faster:
void GraphicsScreen::Box(int x1, int y1, int x2, int y2, unsigned char col) {
    int x, y;
    char _far *spot = (char far *)MK_FP(0xa000, y2*320 + x2);
    int offset = 320 - (y2 - y1);
    for ( y = y2 - y1; y; y--) {
        for ( x = x2 - x1; x; x--)
            *spot-- = col;
        spot -= offset;
    }
}
This also draws from bottom to top, which is often faster than top to
bottom.  Even if it doesn't make any difference in accessing video
memory, it avoids comparisons each time through the loops.
 CB> int main(void)
 CB> {
 CB>         GraphicsScreen gs; // Initalize object, and set screen mode.
 CB>         gs.Box(100,100,200,200,0x13);   // Draw a box
 CB>         gs.PutPixel(150,150,0x2F);      // Put a pixel in the middle
 CB>         delete &gs;             // Back to text mode
You didn't call `new' to create gs - you should NOT call `delete' to
destroy it.  This was likely what was causing your GPF.
 CB>         return 0;
 CB> }
Most likely you also want a pause between putting stuff on the screen
and going back to text mode:
int main(void) {
    GraphicsScreen gs;
    gs.Box(100, 100, 200, 200, 0x13);
    gs.PutPixel(150, 150, 0x2f);
    getch();
}
For the sake of clarity, I'll post the complete, working program in the
next message.
    Later,
    Jerry.
... The Universe is a figment of its own imagination.
--- PPoint 1.90
---------------
* Origin: Point Pointedly Pointless (1:128/166.5)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.