Using 256 colors in C++ programs

This program as header class will able you to use graphics output quality of 600*800 solution with 256 color This mode is satisfatory to identify any image. This mode generally not provided by Turbo C++ Compiler Some function are also provided for the

It must be used only with MsDOS. Because some convention I have made regarding to memory model of program

Use it on WIN98 not on WinXP (if you want then on XP use only 640*480 resolution check function InitGraph() for more details……)

The program have three classes

  1. VDU
  2. Color
  3. Box with one LineStyle class use these classes as VDU for display any char at any pos with any color
  1. putChar(int row,int column ,char ch, ClassColor colorOfChar=DefaultColor)
  2. putString (int r,int c,char *ch,ClassColor clr=DefaultColor)
  3. putLong(int r,int c,long ch,ClassColor clr=DefaultColor)
  4. putDouble (int r,int c,double ch,ClassColor clr=DefaultColor)
  5. changeColor (int r, int c,ClassColor clr) –> It changes color of any position

use Color class for

  1. SetColor(unsigned char fg,unsigned char bg ,unsigned char bl);
  2. SetColor(ClassColor cc1);
  3. MakeColor(unsigned char fg,unsigned char bg ,unsigned char bl);

#include<dos.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define round(a) ((int)(a+0.5))
void drawline(int xa, int ya, int xb, int yb);
const unsigned int MINX=0,MINY=0,MAXX=799,MAXY=599;
const unsigned int MIDX=(MAXX+1)/2,MIDY=(MAXY+1)/2;
unsigned int CentreX=MIDX,CentreY=MIDY;
char huge Timg[MAXX][MAXY];
unsigned char huge ColorLUT[768];
union REGS in, out,reg ;
union SREGS inreg ;
// —-pellate setting
typedef unsigned int Word;
typedef unsigned char Byte;
typedef struct

{
Byte Red, Grn, Blu;
}RGB;

class sColor

{};
typedef RGB PaletteRegister[255];
PaletteRegister Color,colorP;
void ClearPalette(PaletteRegister Color)

{
Word i;
for(i=0; i<=255; i++)

{
Color[i].Red=0;
Color[i].Grn=0;
Color[i].Blu=0;
}
}

void SetPalette(PaletteRegister Hue)

{
reg.x.ax=0×1012;
segread(&inreg);
inreg.es=inreg.ds;
reg.x.bx=0;reg.x.cx=256;
reg.x.dx=(int)&Hue[0];
int86x(0×10,®,®,&inreg);
}

void InitPalette(PaletteRegister Color)

{
Word i=0;
for(int r=0;r<6;r++)
for(int g=0;g<6;g++)
for(int b=0;b<6;b++)

{
Color[i].Red=r*51;
Color[i].Grn=g*51;
Color[i].Blu=b*51;
i++;
}
}

//————————————–
//     ————-
class CGBase

{
int gd,gm,gr;
union REGS i, o,reg;
struct SREGS inreg;
int fc,bc;
void setsvga ( int m )

{
i.x.ax = 0×4f02 ;
i.x.bx = m ;
int86 ( 16, &i, &o ) ;
}
void set_vesa_seg ( int bank_number )

{
union REGS i, o ;
i.x.ax = 0×4F05 ;
i.x.bx = 0 ;
i.x.dx = bank_number ;
int86 ( 16, &i, &o ) ;
}
void putpixel ( int x, int y, unsigned char color )

{
unsigned short curr_vesa_seg = 0xffff ;
unsigned short vesa_seg,vesa_offset ;
unsigned long seg_size = 0xffff + 1L ;
unsigned long offset ;
offset = ( ( unsigned long ) y * ( unsigned long ) (MAXX+1) + ( unsigned long ) x ) ;
vesa_seg = offset / seg_size ;
vesa_offset = offset % seg_size ;
if ( vesa_seg != curr_vesa_seg )

{
set_vesa_seg ( vesa_seg ) ;
curr_vesa_seg = vesa_seg ;
}
pokeb ( 0xA000, ( unsigned ) vesa_offset, color) ;
}
void line(int xa, int ya, int xb, int yb,int c)

{
int dx=xb-xa,dy=yb-ya,steps,k;
float xi,yi,x=xa,y=ya;
if(abs(dx) > abs(dy))
steps=abs(dx);
else
steps=abs(dy);
xi=dx/(float)steps;
yi=dy/(float)steps;
putpixel(round(x),round(y),c);
for(k=0;k<steps;k++)

{
x+=xi;
y+=yi;
putpixel(round(x),round(y),fc);
}
}
void InitGraph()

{
setsvga ( 0×103) ;// *** fro Win XP use 0×101 for 640*480*256
InitPalette(colorP);
SetPalette(colorP);
// 103 for 800*600*256
}
void FinishGraph()

{
setsvga (0×03) ;
}
void Reset()

{
FinishGraph();
InitGraph();
Boundary(1);
Axis(2);
}
public:
CGBase()

{
InitGraph();
ClearPort();
ClearPort();
Axis(GREEN);
fc=1;
bc=0;
}
void putImage(int left,int top,int right,int bot)

{
unsigned short curr_vesa_seg = 0xffff ;
unsigned short vesa_seg,vesa_offset ;
unsigned long seg_size = 0xffff + 1L ;
unsigned long offset ;
int t;
if(left>right)

{
t=left;
left=right;
right=t;
}
if(top>bot)

{
t=bot;
bot=top;
top=t;
}
char color;
for(int x=left ; x<=right;x++)
for(int y=top ; y<=bot;y++)

{
color=Timg[x][y];
offset = ( ( unsigned long ) y * ( unsigned long ) (MAXX+1) + ( unsigned long ) x ) ;
vesa_seg = offset / seg_size ;
vesa_offset = offset % seg_size ;
if ( vesa_seg != curr_vesa_seg )

{
set_vesa_seg ( vesa_seg ) ;
curr_vesa_seg = vesa_seg ;
}
pokeb ( 0xA000, ( unsigned ) vesa_offset, color ) ;
}
}
~CGBase()

{
FinishGraph();
}
void ClearPort()

{
for( int i=0;i<MAXX;i++)
for(int j=0;j<MAXY;j++)
Timg[i][j]=0;
putImage(MINX,MINY,MAXX,MAXY);
}
void PutPixel(int x, int y , int c ,int rx=CentreX,int ry=CentreY)

{
if( (rx+x)<MAXX &&(rx+x)>MINX && (ry-y)<MAXY &&(ry-y)>MINY )
putpixel(rx+x,ry-y,c);
}
void Axis(int c)

{
LineAbs(CentreX,MINY,CentreX,MAXY,c);
LineAbs(MINX,CentreY,MAXX,CentreY,c);
}
void Boundary(int c)

{
RectangleAbs(MINX,MINY,MAXX,MAXY,c);
}
void LineAbs(int x1,int y1,int x2,int y2,int c=WHITE)

{
SetColor(c);
line(x1,y1,x2,y2,c);
}
void RectangleAbs(int x1,int y1,int x2,int y2,int c=WHITE)

{
SetColor(c);
Line(x1,y1,x1,y2,c);
Line(x1,y2,x2,y2,c);
Line(x2,y2,x2,y1,c);
Line(x2,y1,x1,y1,c);
}
// line according to coordinates
void Line(int x1,int y1,int x2,int y2,int c)

{
SetColor(c);
line(x1+CentreX,CentreY-y1,x2+CentreX,CentreY-y2,c);
}
void Circle(int xCenter, int yCenter, int radius,unsigned char color=WHITE)

{
float x=0;
float y=radius;
float p=1-radius;//p=(x+1)^2+(y-1/2)^2-r^2
while(x <= y)

{
PutPixel(xCenter+x, yCenter+y, color);
PutPixel(xCenter-x, yCenter+y, color);
PutPixel(xCenter+x, yCenter-y, color);
PutPixel(xCenter-x, yCenter-y, color);
PutPixel(xCenter+y, yCenter+x, color);
PutPixel(xCenter-y, yCenter+x, color);
PutPixel(xCenter+y, yCenter-x, color);
PutPixel(xCenter-y, yCenter-x, color);
x+=1;
if(p < 0)
p = p + 2 * (x + 1) – 1; //x=x+1,y=y
else

{
y–;
p = p + 2 * (x – y) + 1; //x=x+1,y=y-1
}
}
}
void Rectangle(int x1,int y1,int x2,int y2,int c )

{
SetColor(c);
RectangleAbs(x1+CentreX,CentreY-y1,x2+CentreX,CentreY-y2,c);
}
void SetColor(unsigned int c)

{
fc=c;
}
void SetBkColor(unsigned int c)

{
bc=c;
}
};

// a brief example for How to intiate th
//     e code
void main()

{
int i=0,j=0,k=0,c=0;
getch();
CGBase g;
for(i=0;i<=275;i++)

{
g.Circle(0,0,i,i);
}
getch();
}

Leave a comment

You have to be logged in, to read this comment.