JN> hi, im trying to write a C++ function that will display
JN> a screen of a certain ascii character, however when
JN> running the program it appears very slow :( here is the
JN> code ...
Video is relatively slow. Sometimes it's better to build an array, and then
do a mass-copy onto the video. (In fact, it's almost ALWAYS faster!)
JN> void screen(int fc, int bc, char sym) {
JN>
JN> textcolor(fc); textbackground(bc);
JN> int i = 0;
JN> int j = 0;
JN> for (i= 1; i <= 25; i++) {
JN> for (j = 1; j <= 80; j++) {
JN> cprintf("%c", sym);
JN> }
JN> }
JN> }
Not very "C++"-ish.
I would create a string for the line - 160 bytes long - that had the sym in
the first of each pair of bytes, and the colour ( (bg<<4)|fg ) in the second
of each pair:
char line[160];
const char col = (bc<<4)|fc;
for (int i = 0; i < sizeof(line); i+=2)
{
line[i] = sym;
line[i+1] = col;
}
Then I would put each line up on the screen via a direct write.
Unfortunately, I can't recall how to do this in DOS. OS/2 it is... ;-)
Another way is to build the ENTIRE screen in a buffer, and do one single
mass-write. This would be faster yet.
Of course, if you can't do that, you can use cprintf - but you'd build a
string of just sym - not the colour. And, since it is a string, now, rather
than a buffer, you need a terminating NULL.
char line[81];
memset(line, sizeof(line)-1, sym);
line[sizeof(line)] = '\0';
for (int i = 0; i < num_lines; ++i)
cprintf("%s",line);
puts may be faster, but may not hang on to the colour change. :-)
There are probably a number of functions in snippets that would help, too.
PS: All code is untested, but should be about the right idea. :-)
--- Maximus/2 3.01
---------------
* Origin: Tanktalus' Tower BBS (1:250/102)
|