Board index » cppbuilder » read foregrnd / backgrnd colors

read foregrnd / backgrnd colors


2003-08-27 05:51:55 AM
cppbuilder49
OK. So now I have it so the function returns the proper data,
split up in a character array. Yes it works this way, but as you
can see it is very clumsy, inelegant code. (And I get some not-
so-good warning messages). There must be a better way!
For my program below it returns 7420, which is exactly right, 7
for white background, 4 for red foreground, and 20 (decimal 32)
for whitespace character. When I run the program with different
foreground and background colors, again it is absolutely
correct.
What I need is to be able to tear the number apart, so I
have ?,??,?and ?0.?The value returned is a hexadecimal
number, however, and that causes problems for me. If it was
decimal, for example, I could easily convert it to a character
array and work with the subscripts.
#include<stdio.h>
#include<stdlib.h>
#include<dos.h>
#include<conio.h>
#include<string.h>
char *readattr(); //<- it says, no protype, but this is it, right?
int readch();//<- it says, no protype, but this is it, right?
void window(int left, int top, int right, int bottom);
void textbackground(int newcolor);
void clrscr(void);
void gotoxy(int x, int y);
int readch()
{
struct REGPACK rp = {0};
rp.r_ax = 0x0800;
intr(0x10, &rp);
return rp.r_ax & 0x00ff ;
}
char *readattr()
{
FILE *fp;
struct REGPACK rp = {0};
char attr[100];
rp.r_ax = 0x0800;
intr(0x10, &rp);
fp = fopen("convert.txt", "wt");
if (fp == NULL)
{
printf("saveattr 42 error open convert.txt for write.\n");
getch();
return 1;
}
fprintf( fp, "%x", rp.r_ax );
fclose(fp);
fp = fopen("convert.txt", "rt");
if (fp == NULL)
{
printf("saveattr 57 error open convert.txt for read.\n"); getch();
return 1;
}
fscanf( fp, "%s", attr );
fclose(fp);
return attr; //rp.r_ax; //0x00ff ;
}
int main()
{
int FG, BG, i, xcol, xrow, col=30, row=10;
int SAVECOL, SAVEROW;
char results[10], ch[5];
FG= 4;
BG= 7;
textcolor(FG);
textbackground(BG);
gotoxy( col, row-1 );
cprintf( " \n" );
gotoxy( col, row );
cprintf( " adfyghsfghdvgedhjghjrh \n" );
gotoxy( col, row+1 );
cprintf( " adfyghsfghdvgedhjghjrh \r\n" );
gotoxy( col, row+2 );
cprintf( " adfyghsfghdvgedhjghjrh \n" );
gotoxy( col, row+3 );
cprintf( " \n" );
xcol=5;
xrow=1;
SAVECOL = col;
SAVEROW = row;
while(1)
{ //94
gotoxy( col, row );
strcpy( results, readattr() ); // col, row ) );
i = readch();
sprintf( ch, "%d", i );
gotoxy( xcol, xrow );
cprintf( " Background = '%c' \n", results[0] );
gotoxy( xcol, xrow+1 );
cprintf( " Foreground = '%c' \n", results[1] );
gotoxy( xcol, xrow+2 );
cprintf( " character = '%s'\n", ch );
textcolor(3);
gotoxy( col, row );
cprintf( "%c\r\n", i );
getch();
textcolor(FG);
gotoxy( col, row );
cprintf( "%c\r\n", i );
col++;
if( i == '\0' )
{
col = SAVECOL;
row++;
}
} //94
return;
}
 
 

Re:read foregrnd / backgrnd colors

I've watched this thread for some time and I do not understand why would be
trying to delve under the language to work with the underlying firmware and
hardware when he has not yet gotten a full handle on the language. I also
do not understand why he would go to so much trouble to be be able to work
with an obsolete operating system.
If you must use the video interrupts yourself instead of the functions
prototyped in CONIO.H which easily do all that for you then it probably
would look something like this C program:
------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
int screen_width;
int screen_height;
int current_page;
unsigned char far *rows_minus_one_ptr = MK_FP(0x40, 0x84);
#define BLINKING 0x80
void GetScreenSize(void)
{
union REGS r;
r.h.ah = 0xF;
int86(0x10, &r, &r);
screen_width = r.h.ah;
current_page = r.h.bh;
screen_height = *rows_minus_one_ptr + 1;
}
#pragma startup GetScreenSize
void WhereXY(int *x, int *y)
{
union REGS r;
r.h.ah = 3;
r.h.bh = current_page;
int86(0x10, &r, &r);
*y = r.h.dh + 1;
*x = r.h.dl + 1;
}
void GoToXY(int x, int y)
{
if ((x>0) && (x <= screen_width) &&
(y>0) && (y <= screen_height))
{
union REGS r;
r.h.ah = 2;
r.h.bh = 0;
r.h.dh = y - 1;
r.h.dl = x - 1;
int86(0x10, &r, &r);
}
}
int main()
{
char *msg = NULL;
int orig_x;
int orig_y;
int result = EXIT_SUCCESS;
FILE *fout = fopen("ScnData.TXT", "wt");
/* save original cursor position */
WhereXY(&orig_x, &orig_y);
if (!fout) /* if fopen failed */
{
msg = "Error: unable to open output file\n";
result = EXIT_FAILURE;
}
else /* fopen succeeded */
{
int x;
int y;
fputs("Screen characters and attributes\n"
"[x, y] char foreground color background color\n", fout);
for (y = 1; y <= screen_height; ++y)
{
for (x = 1; x <= screen_width; ++x)
{
union REGS r;
int fg;
int bg;
char *other;
GoToXY(x, y);
r.h.ah = 8; /* read char & attrib at cursor */
r.h.bh = current_page;
int86(0x10, &r, &r);
fg = r.h.ah & 0xF; /* fg color is lower 4 bits */
bg = (r.h.ah>>4) & 7; /* bg is next 3 bits */
if (r.h.ah & BLINKING)
other = "Blinking";
else
other = " ";
fprintf(fout, "[%3d, %3d] \'%c\' %02X %02X %s\n",
x, y, r.h.al, fg, bg, other);
}
}
fclose(fout);
}
GoToXY(orig_x, orig_y);
if (msg)
printf(msg);
return result;
}
------------------------------------------
. Ed
Quote
Rob C. wrote in message
news:3f4bd67b$ XXXX@XXXXX.COM ...