Asa arata un program intr-adevar complex:
#include <iostream>
#include <dos.h>
#include <conio.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <fstream.h>
#define PRESS 1
int x,y,x1,y1,x2,y2,x3,y3;
int color=0,prev_color=0;
unsigned char* buffer;
int get_pixel(int x,int y)
{
REGS r;
r.h.ah=0x0d;
r.x.cx=x;
r.x.dx=y;
r.h.bh=0;
int86(0x10, &r, &r);
return r.h.al;
}
void setmode(int mode)
{
REGS r;
r.h.ah=0x00;
r.h.al=mode;
int86(0x10, &r, &r);
}
void plot_pixel(int x,int y, int color)
{
REGS r;
r.h.ah=0x0c;
r.h.al=color;
r.x.cx=x;
r.x.dx=y;
r.h.bh=0;
int86(0x10, &r, &r);
}
int round(float f)
{
return (f+0.5);
}
char getch(int *attr){
REGS r;
r.h.ah = 0x08;
r.h.bh = 0;
int86(0x10,&r,&r);
*attr = r.h.bl;
return r.h.al;
}
void putch(char ch,int *attr)
{
REGS r;
r.h.ah = 0x09;
r.h.bh = 0;
r.h.bl=*attr;
r.h.al=ch;
r.x.cx=1;
int86(0x10,&r,&r);
}
void moveto(int col,int row) {
REGS r;
r.h.ah = 0x02;
r.h.bh = 0;
r.h.dh = row;
r.h.dl = col;
int86(0x10,&r,&r);
}
void line_dda(int x1, int y1, int x2, int y2, int color)
{
if (x1==x2 && y1==y2)
{plot_pixel(x1,y1,color); return;}
int i,loop;
float x_inc, y_inc,x,y,dx,dy;
dx=x2-x1;
dy=y2-y1;
if( abs(dy)<=abs(dx) ) loop=abs(dx);
else loop=abs(dy);
x_inc=(float)dx/loop;
y_inc=(float)dy/loop;
x=x1; y=y1;
plot_pixel( round(x), round(y), color );
for(i=1; i<=loop; i++)
{
x+=x_inc;
y+=y_inc;
plot_pixel( round(x), round(y), color );
}
}
void polar_circle(int xc,int yc,int r, int color)
{
float x,y;
int i;
for (i=0; i<=360; i++)
{
x = xc + r*cos(3.1415*i/180);
y = yc + r*sin(3.1415*i/180);
plot_pixel(x,y,color);
}
}
void polar_ellipse(int xc,int yc,int rx,int ry, int color)
{
float x,y;
int i;
for (i=0; i<=360; i++)
{
x = xc + rx*cos(3.1415*i/180);
y = yc + ry*sin(3.1415*i/180);
plot_pixel(x,y,color);
}
}
void rectangle(int x1, int y1, int x2, int y2, int color)
{
line_dda(x1,y1,x2,y1,color);
line_dda(x1,y1,x1,y2,color);
line_dda(x1,y2,x2,y2,color);
line_dda(x2,y1,x2,y2,color);
}
void fill_rectangle(int x1, int y1, int x2, int y2, int color)
{
for(int i=x1; i<=x2; i++)
line_dda(i,y1,i,y2,color);
}
void flood_fill(int x, int y, int f_color, int o_color)
{
int current=get_pixel(x,y);
if(current==o_color && f_color!=o_color)
{
plot_pixel(x,y,f_color);
flood_fill(x+1,y,f_color,o_color);
flood_fill(x-1,y,f_color,o_color);
flood_fill(x,y+1,f_color,o_color);
flood_fill(x,y-1,f_color,o_color);
}
}
int chk_mouse(void)
{
REGS r;
r.x.ax=0x0000;
int86(0x33, &r, &r);
return r.x.ax;
}
void show_mouse(void)
{
REGS r;
r.x.ax=0x0001;
int86(0x33, &r, &r);
}
void hide_mouse(void)
{
REGS r;
r.x.ax=0x0002;
int86(0x33, &r, &r);
}
int btn_pressed(int btn_no)
{
REGS r;
r.x.ax=0x0005;
r.x.bx=btn_no;
int86(0x33, &r, &r);
return r.x.bx;
}
int btn_released(int btn_no)
{
REGS r;
r.x.ax=0x0006;
r.x.bx=btn_no;
int86(0x33, &r, &r);
return r.x.bx;
}
void get_mouse_pos(int *x, int *y)
{
REGS r;
r.x.ax=0x0003;
int86(0x33, &r, &r);
*x=r.x.cx;
*y=r.x.dx;
}
//******************************GUI CLASS**********************************
class GUI
{
public:
void button(int x1, int y1, int x2, int y2, int press=0)
{
if(press)
{
line_dda(x1,y1,x2,y1,0);
line_dda(x1,y1,x1,y2,0);
line_dda(x1,y2,x2,y2,15);
line_dda(x2,y1,x2,y2,15);
}
else
{
line_dda(x1,y1,x2,y1,15);
line_dda(x1,y1,x1,y2,15);
line_dda(x1,y2,x2,y2,0);
line_dda(x2,y1,x2,y2,0);
}
}
void window(int x1, int y1, int x2, int y2)
{
button(x1,y1,x2,y2);
fill_rectangle(x1+1, y1+1, x2-2, y1+15, 1);
button(x2-14,y1+2,x2-2,y1+14);
fill_rectangle(x2-13, y1+3, x2-3, y1+13, 7); //CLOSE
line_dda(x2-13,y1+4,x2-4,y1+12,BLACK);
line_dda(x2-12,y1+4,x2-3,y1+12,BLACK);//
line_dda(x2-4,y1+4,x2-13,y1+12,BLACK);
line_dda(x2-3,y1+4,x2-12,y1+12,BLACK);//
button(x2-28,y1+2,x2-16,y1+14);
fill_rectangle(x2-27, y1+3, x2-17, y1+13, 7); //MAXIMIZE
rectangle(x2-27,y1+4,x2-18,y1+12,BLACK);
line_dda(x2-26,y1+5,x2-19,y1+5,BLACK);
button(x2-42,y1+2,x2-30,y1+14);
fill_rectangle(x2-41, y1+3, x2-31, y1+13, 7); //MINIMIZE
line_dda(x2-39,y1+10,x2-34,y1+10,BLACK);
line_dda(x2-39,y1+11,x2-34,y1+11,BLACK);
}
void panel(int x1,int y1,int x2,int y2)
{
line_dda(x1,y1,x2,y1,0);
line_dda(x1,y1,x1,y2,0);
line_dda(x1,y2,x2,y2,15);
line_dda(x2,y1,x2,y2,15);
}
};
//***************************GUI CLASS END******************************
////////////////////////////PAINT BRUSH//////////////////////////////////
//********************************LOAD_ICON*******************************
void load_icon(char *fn,int x, int y)
{
fstream fs;
int index=0;
fs.open(fn,ios::in|ios::binary);
fs.read(buffer,121);
for(int j=0; j<11; j++)
{
for(int i=0; i<11; i++){
plot_pixel(x+i,y+j,buffer[index]);
index++;
}
}
}
//****************************COLOR_SELECT*********************************
void color_select()
{
hide_mouse();
color=get_pixel(x,y);
fill_rectangle(16,441,29,454,color);
show_mouse();
}
//******************************LINE*************************************
void line()
{
GUI gui;
hide_mouse();
gui.button(5,60,25,80,PRESS);
show_mouse();
while(!kbhit())
{
if (btn_pressed(0))
{ get_mouse_pos(&x,&y);
if(x>=5 && x<=50 && y>=60 && y<=210 )
break;
if(x>=60 && x<=235 && y>=430 && y<=472 ) // Color Selection
color_select();
if( x>=62 && x<=450 && y>=42 && y<=350 )
{
btn_released(0);
while(btn_released(0)!=1)
{
get_mouse_pos(&x3,&y3);
if( x3<62 || x3>450 || y3<42 || y3>350 )
break;
line_dda(x,y,x3,y3,color);
line_dda(x,y,x3,y3,WHITE);
}
hide_mouse(); line_dda(x,y,x3,y3,color);show_mouse();
}
}
}
hide_mouse(); gui.button(5,60,25,80); show_mouse();
}
//*******************************PENCIL*********************************
void pencil()
{
GUI gui;
hide_mouse();
gui.button(5,85,25,105,PRESS);
show_mouse();
while(!kbhit())
{
if (btn_pressed(0))
{ get_mouse_pos(&x,&y);
if(x>=5 && x<=50 && y>=60 && y<=210 )
break;
if(x>=60 && x<=235 && y>=430 && y<=472 ) // Color Selection
color_select();
if( x>=62 && x<=450 && y>=42 && y<=350 )
{
btn_released(0);
while(btn_released(0)!=1)
{
get_mouse_pos(&x1,&y1);
delay(5);
get_mouse_pos(&x3,&y3);
if( x3<62 || x3>450 || y3<42 || y3>350 )
break;
hide_mouse();
line_dda(x1,y1,x3,y3,color);
line_dda(x3,y3,x1,y1,color);
show_mouse();
}
}
}
}
hide_mouse(); gui.button(5,85,25,105); show_mouse();
}
//*******************************ERASER*********************************
void eraser()
{
GUI gui;
hide_mouse();
gui.button(30,85,50,105,PRESS);
show_mouse();
while(!kbhit())
{
if (btn_pressed(0))
{ get_mouse_pos(&x,&y);
if(x>=5 && x<=50 && y>=60 && y<=210 )
break;
if(x>=60 && x<=235 && y>=430 && y<=472 ) // Color Selection
color_select();
if( x>=62 && x<=450 && y>=42 && y<=350 )
{
btn_released(0);
while(btn_released(0)!=1)
{
get_mouse_pos(&x1,&y1);
if( x1<62 || x1>450 || y1<42 || y1>350 )
break;
delay(5);
hide_mouse();
fill_rectangle(x1,y1,x1+15,y1+15,WHITE);
show_mouse();
}
}
}
}
hide_mouse(); gui.button(30,85,50,105); show_mouse();
}
//***********************************FILL********************************
void fill()
{
GUI gui;
hide_mouse();
gui.button(5,110,25,130,PRESS);
show_mouse();
while(!kbhit())
{
if (btn_pressed(0))
{ get_mouse_pos(&x,&y);
if(x>=5 && x<=50 && y>=60 && y<=210 )
break;
if(x>=60 && x<=235 && y>=430 && y<=472 ) // Color Selection
color_select();
if( x>=62 && x<=450 && y>=42 && y<=350 )
{
hide_mouse();
prev_color=get_pixel(x,y);
flood_fill(x,y,color,prev_color);
show_mouse();
}
}
}
hide_mouse(); gui.button(5,110,25,130); show_mouse();
}
//******************************AIR_BRUSH*******************************
void air_brush()
{
GUI gui;
hide_mouse();
gui.button(30,110,50,130,PRESS);
show_mouse();
while(!kbhit())
{
if (btn_pressed(0))
{ get_mouse_pos(&x,&y);
if(x>=5 && x<=50 && y>=60 && y<=210 )
break;
if(x>=60 && x<=235 && y>=430 && y<=472 ) // Color Selection
color_select();
if( x>=62 && x<=450 && y>=42 && y<=350 )
{
btn_released(0);
while(btn_released(0)!=1)
{
get_mouse_pos(&x1,&y1);
if( x1<62 || x1>450 || y1<42 || y1>350 )
break;
hide_mouse();
for(int i=0; i<=5;i++)
{plot_pixel(x1,y1,color); // plot_pixel(x1+1,y1+1,color);
plot_pixel(x1,y1+1,color);plot_pixel(x1,y1-1,color);
plot_pixel(x1-1,y1,color);plot_pixel(x1+1,y1,color);
plot_pixel(x1+2,y1+2,color);plot_pixel(x1-2,y1-2,color);
plot_pixel(x1-2,y1+2,color);plot_pixel(x1+2,y1-2,color);}
show_mouse();
}
}
}
}
hide_mouse(); gui.button(30,110,50,130); show_mouse();
}
//******************************RECTANGLE1*******************************
void rectangle1()
{
GUI gui;
hide_mouse();
gui.button(30,60,50,80,PRESS);
show_mouse();
while(!kbhit())
{
if (btn_pressed(0))
{ get_mouse_pos(&x,&y);
if(x>=5 && x<=50 && y>=60 && y<=210 )
break;
if(x>=60 && x<=235 && y>=430 && y<=472 ) // Color Selection
color_select();
if( x>=62 && x<=450 && y>=42 && y<=350 )
{
btn_released(0);
while(btn_released(0)!=1)
{
get_mouse_pos(&x3,&y3);
if( x3<62 || x3>450 || y3<42 || y3>350 )
break;
rectangle(x,y,x3,y3,color);
rectangle(x,y,x3,y3,WHITE);
}
hide_mouse(); rectangle(x,y,x3,y3,color);show_mouse();
}
}
}
hide_mouse(); gui.button(30,60,50,80); show_mouse();
}
//******************************CIRCLE**********************************
void circle()
{
GUI gui;
hide_mouse();
gui.button(5,135,25,155,PRESS);
show_mouse();
while(!kbhit())
{
if (btn_pressed(0))
{ get_mouse_pos(&x,&y);
if(x>=5 && x<=50 && y>=60 && y<=210 )
break;
if(x>=60 && x<=235 && y>=430 && y<=472 ) // Color Selection
color_select();
if( x>=62 && x<=450 && y>=42 && y<=350 )
{
btn_released(0);
while(btn_released(0)!=1)
{
get_mouse_pos(&x3,&y3);
if( x3<62 || x3>450 || y3<42 || y3>350 )
break;
polar_circle(x,y,(x3-x)/2,color);
polar_circle(x,y,(x3-x)/2,WHITE);
}
hide_mouse(); polar_circle(x,y,(x3-x)/2,color);show_mouse();
}
}
}
hide_mouse(); gui.button(5,135,25,155); show_mouse();
}
//******************************ELLIPSE*********************************
void ellipse()
{
GUI gui;
hide_mouse();
gui.button(30,135,50,155,PRESS);
show_mouse();
while(!kbhit())
{
if (btn_pressed(0))
{ get_mouse_pos(&x,&y);
if(x>=5 && x<=50 && y>=60 && y<=210 )
break;
if(x>=60 && x<=235 && y>=430 && y<=472 ) // Color Selection
color_select();
if( x>=62 && x<=450 && y>=42 && y<=350 )
{
btn_released(0);
while(btn_released(0)!=1)
{
get_mouse_pos(&x3,&y3);
if( x3<62 || x3>450 || y3<42 || y3>350 )
break;
polar_ellipse(x,y,(x3-x)/2,(y3-y)/2,color);
polar_ellipse(x,y,(x3-x)/2,(y3-y)/2,WHITE);
}
hide_mouse(); polar_ellipse(x,y,(x3-x)/2,(y3-y)/2,color);show_mouse();
}
}
}
hide_mouse(); gui.button(30,135,50,155); show_mouse();
}
//******************************POLYGON*************************************
void polygon()
{
GUI gui;
hide_mouse();
gui.button(5,160,25,180,PRESS);
show_mouse();
while(!kbhit())
{
if (btn_pressed(0))
{ get_mouse_pos(&x,&y);
if(x>=5 && x<=50 && y>=60 && y<=210 )
break;
if(x>=60 && x<=235 && y>=430 && y<=472 ) // Color Selection
color_select();
if( x>=62 && x<=450 && y>=42 && y<=350 )
{
btn_released(0);
while(btn_released(0)!=1)
{
get_mouse_pos(&x3,&y3);
if( x3<62 || x3>450 || y3<42 || y3>350 )
break;
line_dda(x,y,x3,y3,color);
line_dda(x,y,x3,y3,WHITE);
}
hide_mouse();
if(x2==0)
line_dda(x,y,x3,y3,color);
else
line_dda(x2,y2,x3,y3,color);
get_mouse_pos(&x2,&y2);
show_mouse();
}
}
}
hide_mouse(); gui.button(5,160,25,180); show_mouse();
}
//******************************BRUSH**********************************
void brush()
{
GUI gui;
hide_mouse();
gui.button(30,160,50,180,PRESS);
show_mouse();
while(!kbhit())
{
if (btn_pressed(0))
{ get_mouse_pos(&x,&y);
if(x>=5 && x<=50 && y>=60 && y<=210 )
break;
if(x>=60 && x<=235 && y>=430 && y<=472 ) // Color Selection
color_select();
if( x>=62 && x<=450 && y>=42 && y<=350 )
{
btn_released(0);
while(btn_released(0)!=1)
{
get_mouse_pos(&x1,&y1);
delay(5);
get_mouse_pos(&x3,&y3);
if( x3<62 || x3>450 || y3<42 || y3>350 )
break;
hide_mouse();
line_dda(x1,y1,x3,y3,color);
line_dda(x1+1,y1+1,x3+1,y3+1,color);
line_dda(x1+2,y1+2,x3+2,y3+2,color);
line_dda(x1+3,y1+3,x3+3,y3+3,color);
line_dda(x1+4,y1+4,x3+4,y3+4,color);
show_mouse();
}
}
}
}
hide_mouse(); gui.button(30,160,50,180); show_mouse();
}
//******************************INTERFACE*******************************
void interface()
{
GUI gui;
fill_rectangle(0,0,639,40,LIGHTGRAY);
fill_rectangle(0,40,60,479,LIGHTGRAY);
fill_rectangle(620,40,639,479,LIGHTGRAY);
fill_rectangle(60,400,620,479,LIGHTGRAY);
gui.window(0,0,639,479);
fill_rectangle(60,350,450,400,DARKGRAY);
line_dda(60,40,450,40,DARKGRAY);
line_dda(60,40,60,350,DARKGRAY);
fill_rectangle(450,40,620,400,DARKGRAY);
fill_rectangle(62,42,450,350,WHITE);
plot_pixel(451,351,BLUE);
plot_pixel(452,351,BLUE);
plot_pixel(451,352,BLUE);
plot_pixel(452,352,BLUE);
gui.panel(10,240,40,300);
int x=5,y=60;
for (int i=0; i<=5; i++) //Function Buttons
{ gui.button(x,y,x+20,y+20);
gui.button(x+25,y,x+45,y+20);
y+=25;
}
load_icon("line.icn",10,65);
load_icon("rectangl.icn",35,65);
load_icon("pencil.icn",10,90);
load_icon("eraser.icn",35,90);
load_icon("fill.icn",10,115);
load_icon("airbrush.icn",35,115);
load_icon("polygon.icn",10,165);
load_icon("brush.icn",35,165);
load_icon("clr.icn",10,190);
polar_circle(16,145,6,BLACK);
flood_fill(16,145,DARKGRAY,LIGHTGRAY);
polar_ellipse(40,145,7,4,BLACK);
flood_fill(40,145,DARKGRAY,LIGHTGRAY);
gui.panel(5,435,50,470);
gui.panel(25,450,40,465);
gui.panel(15,440,30,455);
fill_rectangle(16,441,29,454,color);
int color=0;
x=60;y=430;
for (int j=0; j<2; j++)
{ for (i=0; i<8; i++)
{
fill_rectangle(x,y,x+19,y+19,color);
gui.button(x-1,y-1,x+20,y+20,PRESS);
color++; x+=22;
}
x=60;y+=22;
}
}
//********************************* MAIN **********************************
void main(void)
{
if( !chk_mouse() ) // if( chk_mouse()==0 )
{
cout << "Driver Not found..." << endl;
return;
}
GUI gui;
int width=11; int height=11;
buffer=(unsigned char*)malloc(width*height);
setmode(18);
interface();
show_mouse();
while(!kbhit())
{
if( btn_pressed(0) ) //Left Button Pressed
{
get_mouse_pos(&x,&y);
if(x>=5 && x<=25 && y>=60 && y<=80 ) // Line
{line();}
if(x>=30 && x<=50 && y>=60 && y<=80 ) // Rectangle
{ rectangle1();}
if(x>=60 && x<=235 && y>=430 && y<=472 ) // Color Selection
{ color_select(); }
if(x>=5 && x<=25 && y>=85 && y<=105 ) // Pencil
{ pencil(); }
if(x>=30 && x<=50 && y>=85 && y<=105 ) // Eraser
{ eraser(); }
if(x>=5 && x<=25 && y>=110 && y<=130 ) // Fill
{ fill(); }
if(x>=30 && x<=50 && y>=110 && y<=130 ) // Air Brush
{ air_brush(); }
if(x>=5 && x<=25 && y>=135 && y<=155 ) // Circle
{ circle(); }
if(x>=30 && x<=50 && y>=135 && y<=155 ) // Ellipse
{ ellipse(); }
if(x>=5 && x<=25 && y>=160 && y<=180 ) // Polygon
{ polygon(); }
if(x>=30 && x<=50 && y>=160 && y<=180 ) // BRUSH
{ brush(); }
if(x>=5 && x<=25 && y>=185 && y<=205 ) // CLR
{ gui.button(5,185,25,205,PRESS);
fill_rectangle(62,42,450,350,WHITE);
gui.button(5,185,25,205);
}
if(x>=624 && x<=637 && y>=2 && y<=13 ) // X Button
{ gui.button(624,2,637,13,PRESS);exit(1); }
}
if( btn_released(0) ) //Left Button Released
{
get_mouse_pos(&x1,&y1);
}
} //while(!kbhit())
} //main()
Acesta este programul Paint din Windows.
Tot acest text/cod pe care l-ati vazut se numeste codul sursa al programului.
Tot acest text/cod pe care l-ati vazut se numeste codul sursa al programului.