Mail Archives: djgpp/2000/08/06/01:48:08
Hi,
My name is Dave, and I'm sort of new here, but I found something that
I think is an interesting glitch using C++ iostreams while writing
graphics direct to VGA in mode 13h. I was wondering if any of you can
compile this and tell me if it does the same thing on your system?
And I also was wondering whether any of you can explain why the
problem happens in the first place....
I work as a tutor at a community college. The students here learn C++
using MS Visual C++, and never use C-style input/output (unless they
teach themselves). I was trying to explain one day what it used to be
like, programming graphics on the VGA under DOS. A couple of them
showed interest in seeing what it was like, so I found DJGPP,
downloaded it, and started writing a low-level graphics "library" to
demonstrate some relevent algorithms.
I wanted to show them Bresenham (line & circle) one dot at a time,
hitting the enter key to continue. Since they only know C++, I chose
to read the keyboard using C++ iostreams instead of CONIO functions
like getch(), which these students have never seen before. I knew
this was inefficient, but I did't imagine it would cause a problem.
When wrote a program to draw 1/8 of a circle using Bresenham, the VGA
screen started to scroll away after a number of pixels had been
drawn! I tried to find the bug in my routine for several days, and
was about to give up when the thought of trying simpler CONIO routines
occurred to me. When I replaced the IOSTREAM functions with CONIO
equivalents, the program worked just fine!
Of course, this problem I've discovered is not important. I was just
curious if someone with knowledge of the DJGPP IOSTREAMs source code
would be able to explain was causes it. I am including a demo program
(below) which causes the undesired scrolling on my Pentium III machine
at home as well as on older Win 95 machines at work.
As provided, the program will compile to the IOSTREAMs version.
(Since cin doesn't respond until you hit <enter>, I used cin.getline()
and "dummy" to throw away all other chars besides <enter>.)
If you want the working version of the program, you should comment out
the IOSTREAMs code, and un-comment the CONIO code. If the same
problem happens on your machine, the screen will not begin to scroll
away until after you "continue" to a radius of 30 or 40, after the
screen has switched back and forth between graphics mode and text mode
once or twice. Under IOSTREAMS, that is. If you compile the CONIO
version, the glitch never occurs.
// CBUGDEM3.CPP Program to illustrate undesired VGA scrolling
//#include <conio.h>
#include <iostream.h>
#include <dpmi.h>
#include <go32.h>
#include <sys/farptr.h>
void BresCirc (int, int, int);
int GraphicsMode (void);
int TextMode (void);
void PutPixel (int, int, int);
int main (void)
{ char dummy[256];
for (int radius = 20; radius <= 90; radius +=10)
{ GraphicsMode();
BresCirc(160, 100, radius);
//getch();
cin.getline(dummy, 256);
TextMode();
//cputs ("Continue? ");
cout << "Continue? ";
//char ch = getch();
cin.getline(dummy, 256);
//if (ch != 'y' && ch != 'Y') break;
if (*dummy != 'y' && *dummy != 'Y') break;
}
TextMode();
return 0;
}
void BresCirc (int cx, int cy, int r)
{ int d = 3 - 2*r;
int x = 0, y = r;
while (x < y)
{ PutPixel (cx + x, cy - y, 15);
if (d < 0) d += 4*x + 6;
else d += 4*(x - y) + 10, --y;
++x;
//getch();
char dummy[256];
cin.getline(dummy, 256);
}
}
int GraphicsMode (void)
{ __dpmi_regs r;
r.x.ax = 0x13; // graphics mode = 13h
int error = __dpmi_int (0x10, &r);
return error;
}
int TextMode (void)
{ __dpmi_regs r;
r.x.ax = 3; // text mode = 3h
int error = __dpmi_int (0x10, &r);
return error;
}
void PutPixel (int x, int y, int color)
{ unsigned addr = 0xA0000 + (y << 8) + (y << 6) + x;
_farpokeb (_dos_ds, addr, color);
}
Dave W.
"... for if leisure and security were enjoyed by all alike, the great
mass of human beings ... would learn to think for themselves; and when
once they had done this, they would sooner or later realize that the
privileged minority had no function, and they would sweep it away." -
Emmanuel Goldstein
- Raw text -