tr-demoscene
the scene => coding => Konuyu başlatan: skate - 29.12.2004 05:15:21
-
Yeni versionları burdan yayınliicam...
/**
* Icerikler
*/
#include <conio.h>
#include <math.h>
#include <dos.h>
/**
* Tanimlar
*/
#define CHAR03 0x03
#define INT_VIDEO 0x10
#define VGA13 0x13
/**
* Yapilar
*/
class POINT2D
{
public:
POINT2D();
POINT2D(int xVal, int yVal);
~POINT2D();
int x;
int y;
};
void POINT2D::POINT2D()
{
// Bos
}
void POINT2D::POINT2D(int xVal, int yVal)
{
x = xVal;
y = yVal;
}
void POINT2D::~POINT2D()
{
// Bos
}
/**
* Global Degiskenler
*/
char far *VGAMAP = (char far *) MK_FP(0xA000, 0);
/**
* Prototip Fonksiyonlar
*/
void setScreen(int screenMode);
void plot(unsigned int x, unsigned char y, unsigned char color);
void plot(POINT2D *p2dPoint, unsigned char color);
void line(int x1, int y1, int x2, int y2, unsigned char color);
void line(POINT2D *p2dPoint1, POINT2D *p2dPoint2, unsigned char color);
/**
* Ana Fonksiyon
*/
void main()
{
// Degisken tanimlari
int x, y, i;
POINT2D myPoint1;
POINT2D myPoint2(40, 40);
myPoint1.x = 30;
myPoint1.y = 30;
// 320x200 256 renk ekran moduna gec
setScreen(VGA13);
// Ekrana birkac ornek nokta bas
plot(30, 40, 14); // Birinci kullanim bicimi
plot(&myPoint1, 10); // Ikinci kullanim bicimi
plot(&myPoint2, 13);
plot(&POINT2D(40, 30), 12); // Ucuncu kullanim bicimi
// Ekrana birkac ornek cizgi cek
// Cizgilerin baslangic ve bitislerini plot ile isaretle
// Cizgi 1
myPoint1.x = 80; myPoint1.y = 150;
myPoint2.x = 213; myPoint2.y = 48;
line(&myPoint1, &myPoint2, 12);
plot(&myPoint1, 15);
plot(&myPoint2, 15);
// Cizgi 2
myPoint1.x = 80; myPoint1.y = 48;
myPoint2.x = 213; myPoint2.y = 150;
line(&myPoint1, &myPoint2, 13);
plot(&myPoint1, 15);
plot(&myPoint2, 15);
// Cizgilerden bir daire cizer
for(i = 0; i < 512; i++)
{
x = 250 + 55 * cos((float)i / 256 * M_PI);
y = 100 + 55 * sin((float)i / 256 * M_PI);
line(250, 100, x, y, 32 + (x & 31));
}
// Bir tusa basilana kadar bekle
getch();
// 80x25 karakter ekran moduna gec
setScreen(CHAR03);
}
/**
* Name : setScreen
* Desc : 0x10 interruptini kullanarak ekran modunu degistirir
* Input : (int)screenMode
*/
void setScreen(int screenMode)
{
union REGS regs;
regs.h.ah = 0;
regs.h.al = screenMode;
int86(INT_VIDEO, ®s, ®s);
}
/**
* Name : plot
* Desc : Parametreden gelen (x,y) koordinatlarina, yine parametreden
* gelen rengi basar.
* Input : (unsigned int)x
* (unsigned char)y
* (unsigned char)color
*/
void plot(unsigned int x, unsigned char y, unsigned char color)
{
/********************************
* Formulun Cikarilisi *
*********************************
* (y * 320) + x => *
* (y * 256) + (y * 64) + x => *
* (y * 2^8) + (y * 2^6) + x => *
* (y << 8) + (y << 6) + x *
********************************/
VGAMAP[(y << 8) + (y << 6) + x] = color;
}
/**
* Name : plot
* Desc : Overloaded plot fonksiyonu. (x,y) koordinatlarini POINT2D
* yapisindaki tek bir degiskenden alir.
* Input : (POINT2D *)p2dPoint
* (unsigned char)color
*/
void plot(POINT2D *p2dPoint, unsigned char color)
{
VGAMAP[(p2dPoint->y << 8) + (p2dPoint->y << 6) + p2dPoint->x] = color;
}
/**
* Name : line
* Desc : Parametreden gelen (x1,y1)-(x2,y2) koordinatlari arasina
* cizgi ceker. <math.h>'a ihtiyac duymaz.
* Bresenham algoritmasi kullanir.
* Input : (int)x1
* (int)y1
* (int)x2
* (int)y2
* (unsigned char)color
*/
void line(int x1, int y1, int x2, int y2, unsigned char color)
{
int dx, dy, x, y, a = 0;
dx = (x1 - x2 > 0) ? x1 - x2 : x2 - x1; // Yatay duzlemdeki genislik
dy = (y1 - y2 > 0) ? y1 - y2 : y2 - y1; // Dikey duzlemdeki uzunluk
if(dx > dy) // Yatay duzlemde genis ise
{
x = x1;
y = y1;
while(1)
{
plot(x, y, color);
a += dy;
if(a >= dx)
{
a -= dx;
y += (y1 > y2) ? -1 : 1;
}
if(x == x2)
break;
x += (x1 > x2) ? -1 : 1;
}
}
else // Dikey duzlemde uzun ise
{
x = x1;
y = y1;
while(1)
{
plot(x, y, color);
a += dx;
if(a >= dy)
{
a -= dy;
x += (x1 > x2) ? -1 : 1;
}
if(y == y2)
break;
y += (y1 > y2) ? -1 : 1;
}
}
}
/**
* Name : line
* Desc : Overloaded line fonksiyonu. (x1,y1) ve (x2,y2) yerine
* POINT2D yapisindaki iki degisken kullanilir.
* Input : (POINT2D *)p2dPoint1
* (POINT2D *)p2dPoint2
* (unsigned char)color
*/
void line(POINT2D *p2dPoint1, POINT2D *p2dPoint2, unsigned char color)
{
line(p2dPoint1->x, p2dPoint1->y, p2dPoint2->x, p2dPoint2->y, color);
}
-
Grafik fonksiyonlarının isimlerini değiştirdim ve yeni fonksiyonlar ekledim. Ayrıca bir de yeni class eklendi. Devamı gelecek :)
/**
* Icerikler
*/
#include <conio.h>
#include <math.h>
#include <dos.h>
/**
* Tanimlar
*/
#define CHAR03 0x03
#define INT_VIDEO 0x10
#define VGA13 0x13
/**
* Siniflar
*/
// POINT2D Sinifi
class POINT2D
{
public:
POINT2D();
POINT2D(POINT2D *p2dPoint);
POINT2D(int xVal, int yVal);
~POINT2D();
int x;
int y;
};
void POINT2D::POINT2D()
{
// Bos
}
void POINT2D::POINT2D(POINT2D *p2dPoint)
{
x = p2dPoint->x;
y = p2dPoint->y;
}
void POINT2D::POINT2D(int xVal, int yVal)
{
x = xVal;
y = yVal;
}
void POINT2D::~POINT2D()
{
// Bos
}
// RECT Sinifi
class RECT
{
public:
RECT();
RECT(RECT *rctRect);
RECT(POINT2D *p2dPoint1, POINT2D *p2dPoint2);
RECT(int x1Val, int y1Val, int x2Val, int y2Val);
~RECT();
int x1;
int y1;
int x2;
int y2;
};
void RECT::RECT()
{
// Bos
}
void RECT::RECT(RECT *rctRect)
{
x1 = rctRect->x1;
y1 = rctRect->y1;
x2 = rctRect->x2;
y2 = rctRect->y2;
}
void RECT::RECT(POINT2D *p2dPoint1, POINT2D *p2dPoint2)
{
x1 = p2dPoint1->x;
y1 = p2dPoint1->y;
x2 = p2dPoint2->x;
y2 = p2dPoint2->y;
}
void RECT::RECT(int x1Val, int y1Val, int x2Val, int y2Val)
{
x1 = x1Val;
y1 = y1Val;
x2 = x2Val;
y2 = y2Val;
}
void RECT::~RECT()
{
// Bos
}
/**
* Global Degiskenler
*/
char far *VGAMAP = (char far *) MK_FP(0xA000, 0);
/**
* Prototip Fonksiyonlar
*/
void SetScreen(int screenMode);
void Plot(unsigned int x, unsigned char y, unsigned char color);
void Plot(POINT2D *p2dPoint, unsigned char color);
void DrawLine(int x1, int y1, int x2, int y2, unsigned char color);
void DrawLine(POINT2D *p2dPoint1, POINT2D *p2dPoint2, unsigned char color);
void DrawLine(RECT *rctRect, unsigned char color);
void DrawRectangle(int x1, int y1, int x2, int y2, unsigned char color);
void DrawRectangle(POINT2D *p2dPoint1, POINT2D *p2dPoint2, unsigned char color);
void DrawRectangle(RECT *rctRect, unsigned char color);
void FillRectangle(int x1, int y1, int x2, int y2, unsigned char color);
void FillRectangle(POINT2D *p2dPoint1, POINT2D *p2dPoint2, unsigned char color);
void FillRectangle(RECT *rctRect, unsigned char color);
/**
* Ana Fonksiyon
*/
void main()
{
// Degisken tanimlari
int x, y, i;
POINT2D myPoint1;
POINT2D myPoint2(40, 40);
RECT myRect1;
myPoint1.x = 30;
myPoint1.y = 30;
// 320x200 256 renk ekran moduna gec
SetScreen(VGA13);
// Ekrana birkac ornek nokta bas
Plot(30, 40, 14); // Birinci kullanim bicimi
Plot(&myPoint1, 10); // Ikinci kullanim bicimi
Plot(&myPoint2, 13);
Plot(&POINT2D(40, 30), 12); // Ucuncu kullanim bicimi
// Ekrana birkac ornek cizgi cek
// Cizgilerin baslangic ve bitislerini plot ile isaretle
// Cizgi 1
myPoint1.x = 80; myPoint1.y = 150;
myPoint2.x = 213; myPoint2.y = 48;
DrawLine(&myPoint1, &myPoint2, 12);
Plot(&myPoint1, 15);
Plot(&myPoint2, 15);
// Cizgi 2
myPoint1.x = 80; myPoint1.y = 48;
myPoint2.x = 213; myPoint2.y = 150;
DrawLine(&myPoint1, &myPoint2, 13);
Plot(&myPoint1, 15);
Plot(&myPoint2, 15);
// Cizgi 3
myPoint1.x = 40; myPoint1.y = 110;
myPoint2.x = 77; myPoint2.y = 121;
myRect1 = &RECT(&myPoint1, &myPoint2);
DrawLine(&myRect1, 10);
Plot(&myPoint1, 15);
Plot(&myPoint2, 15);
// Cizgilerden bir daire ciz
for(i = 0; i < 512; i++)
{
x = 250 + 55 * cos((float)i / 256 * M_PI);
y = 100 + 55 * sin((float)i / 256 * M_PI);
DrawLine(250, 100, x, y, 32 + (x & 31));
}
// Ekrana birkac ornek dikdortgen ciz
// Dikdortgen 1
DrawRectangle(70, 170, 10, 183, 9);
// Dikdortgen 2
myPoint1.x = 80; myPoint1.y = 160;
myPoint2.x = 95; myPoint2.y = 190;
DrawRectangle(&myPoint1, &myPoint2, 7);
// Dikdortgen 3
myRect1.x1 = 170; myRect1.y1 = 160;
myRect1.x2 = 105; myRect1.y2 = 190;
DrawRectangle(&myRect1, 6);
// Ekrana birkac ornek ici dolu dikdortgen ciz
// Ici Dolu Dikdortgen 1
FillRectangle(68, 172, 12, 181, 9);
// Ici Dolu Dikdortgen 2
myPoint1.x = 85; myPoint1.y = 165;
myPoint2.x = 90; myPoint2.y = 185;
FillRectangle(&myPoint1, &myPoint2, 4);
// Ici Dolu Dikdortgen 3
myRect1.x1 = 250; myRect1.y1 = 165;
myRect1.x2 = 310; myRect1.y2 = 185;
FillRectangle(&myRect1, 2);
// Bir tusa basilana kadar bekle
getch();
// 80x25 karakter ekran moduna gec
SetScreen(CHAR03);
}
/**
* Name : SetScreen
* Desc : 0x10 interruptini kullanarak ekran modunu degistirir
* Input : (int)screenMode
*/
void SetScreen(int screenMode)
{
union REGS regs;
regs.h.ah = 0;
regs.h.al = screenMode;
int86(INT_VIDEO, ®s, ®s);
}
/**
* Name : Plot
* Desc : Parametreden gelen (x,y) koordinatlarina, yine parametreden
* gelen rengi basar.
* Input : (unsigned int)x
* (unsigned char)y
* (unsigned char)color
*/
void Plot(unsigned int x, unsigned char y, unsigned char color)
{
/********************************
* Formulun Cikarilisi *
*********************************
* (y * 320) + x => *
* (y * 256) + (y * 64) + x => *
* (y * 2^8) + (y * 2^6) + x => *
* (y << 8) + (y << 6) + x *
********************************/
VGAMAP[(y << 8) + (y << 6) + x] = color;
}
/**
* Name : Plot
* Desc : Overloaded Plot fonksiyonu. (x,y) koordinatlarini POINT2D
* sinifindan tek bir degiskenden alir.
* Input : (POINT2D *)p2dPoint
* (unsigned char)color
*/
void Plot(POINT2D *p2dPoint, unsigned char color)
{
VGAMAP[(p2dPoint->y << 8) + (p2dPoint->y << 6) + p2dPoint->x] = color;
}
/**
* Name : DrawLine
* Desc : Parametreden gelen (x1,y1)-(x2,y2) koordinatlari arasina
* cizgi ceker. <math.h>'a ihtiyac duymaz.
* Bresenham algoritmasi kullanir.
* Input : (int)x1
* (int)y1
* (int)x2
* (int)y2
* (unsigned char)color
*/
void DrawLine(int x1, int y1, int x2, int y2, unsigned char color)
{
int dx, dy, x, y, a = 0;
dx = (x1 - x2 > 0) ? x1 - x2 : x2 - x1; // Yatay duzlemdeki genislik
dy = (y1 - y2 > 0) ? y1 - y2 : y2 - y1; // Dikey duzlemdeki uzunluk
if(dx > dy) // Yatay duzlemde genis ise
{
x = x1;
y = y1;
while(1)
{
Plot(x, y, color);
a += dy;
if(a >= dx)
{
a -= dx;
y += (y1 > y2) ? -1 : 1;
}
if(x == x2)
break;
x += (x1 > x2) ? -1 : 1;
}
}
else // Dikey duzlemde uzun ise
{
x = x1;
y = y1;
while(1)
{
Plot(x, y, color);
a += dx;
if(a >= dy)
{
a -= dy;
x += (x1 > x2) ? -1 : 1;
}
if(y == y2)
break;
y += (y1 > y2) ? -1 : 1;
}
}
}
/**
* Name : DrawLine
* Desc : Overloaded DrawLine fonksiyonu. (x1,y1) ve (x2,y2) yerine
* POINT2D sinifindan iki degisken kullanilir.
* Input : (POINT2D *)p2dPoint1
* (POINT2D *)p2dPoint2
* (unsigned char)color
*/
void DrawLine(POINT2D *p2dPoint1, POINT2D *p2dPoint2, unsigned char color)
{
DrawLine(p2dPoint1->x, p2dPoint1->y, p2dPoint2->x, p2dPoint2->y, color);
}
/**
* Name : DrawLine
* Desc : Overloaded DrawLine fonksiyonu. (x1,y1) ve (x2,y2) yerine
* RECT sinifindan bir degisken kullanilir.
* Input : (RECT *)rctRect
* (unsigned char)color
*/
void DrawLine(RECT *rctRect, unsigned char color)
{
DrawLine(rctRect->x1, rctRect->y1, rctRect->x2, rctRect->y2, color);
}
/**
* Name : DrawRectangle
* Desc : Parametreden gelen (x1,y1)-(x2,y2) koordinatlari arasina
* dikdortgen ceker. <math.h>'a ihtiyac duymaz.
* Input : (int)x1
* (int)y1
* (int)x2
* (int)y2
* (unsigned char)color
*/
void DrawRectangle(int x1, int y1, int x2, int y2, unsigned char color)
{
int x, y;
// Yatay cizgiler
if(x1 < x2)
{
for(x = x1; x <= x2; x++)
{
Plot(x, y1, color);
Plot(x, y2, color);
}
}
else
{
for(x = x2; x <= x1; x++)
{
Plot(x, y1, color);
Plot(x, y2, color);
}
}
// Dikey Cizgiler
if(y1 < y2)
{
for(y = y1; y <= y2; y++)
{
Plot(x1, y, color);
Plot(x2, y, color);
}
}
else
{
for(y = y2; y <= y1; y++)
{
Plot(x1, y, color);
Plot(x2, y, color);
}
}
}
/**
* Name : DrawRectangle
* Desc : Overloaded DrawRectangle fonksiyonu. (x1,y1) ve (x2,y2) yerine
* POINT2D sinifindan iki degisken kullanilir.
* Input : (POINT2D *)p2dPoint1
* (POINT2D *)p2dPoint2
* (unsigned char)color
*/
void DrawRectangle(POINT2D *p2dPoint1, POINT2D *p2dPoint2, unsigned char color)
{
DrawRectangle(p2dPoint1->x, p2dPoint1->y, p2dPoint2->x, p2dPoint2->y, color);
}
/**
* Name : DrawRectangle
* Desc : Overloaded DrawRectangle fonksiyonu. (x1,y1) ve (x2,y2) yerine
* RECT sinifindan bir degisken kullanilir.
* Input : (RECT *)rctRect
* (unsigned char)color
*/
void DrawRectangle(RECT *rctRect, unsigned char color)
{
DrawRectangle(rctRect->x1, rctRect->y1, rctRect->x2, rctRect->y2, color);
}
/**
* Name : FillRectangle
* Desc : Parametreden gelen (x1,y1)-(x2,y2) koordinatlari arasina
* ici dolu dikdortgen ceker. <math.h>'a ihtiyac duymaz.
* Input : (int)x1
* (int)y1
* (int)x2
* (int)y2
* (unsigned char)color
*/
void FillRectangle(int x1, int y1, int x2, int y2, unsigned char color)
{
int x, y;
if(y1 < y2)
{
for(y = y1; y <= y2; y++)
{
if(x1 < x2)
{
for(x = x1; x <= x2; x++)
Plot(x, y, color);
}
else
{
for(x = x2; x <= x1; x++)
Plot(x, y, color);
}
}
}
else
{
for(y = y2; y <= y1; y++)
{
if(x1 < x2)
{
for(x = x1; x <= x2; x++)
Plot(x, y, color);
}
else
{
for(x = x2; x <= x1; x++)
Plot(x, y, color);
}
}
}
}
/**
* Name : FillRectangle
* Desc : Overloaded FillRectangle fonksiyonu. (x1,y1) ve (x2,y2) yerine
* POINT2D sinifindan iki degisken kullanilir.
* Input : (POINT2D *)p2dPoint1
* (POINT2D *)p2dPoint2
* (unsigned char)color
*/
void FillRectangle(POINT2D *p2dPoint1, POINT2D *p2dPoint2, unsigned char color)
{
FillRectangle(p2dPoint1->x, p2dPoint1->y, p2dPoint2->x, p2dPoint2->y, color);
}
/**
* Name : FillRectangle
* Desc : Overloaded FillRectangle fonksiyonu. (x1,y1) ve (x2,y2) yerine
* RECT sinifindan bir degisken kullanilir.
* Input : (RECT *)rctRect
* (unsigned char)color
*/
void FillRectangle(RECT *rctRect, unsigned char color)
{
FillRectangle(rctRect->x1, rctRect->y1, rctRect->x2, rctRect->y2, color);
}
-
Code uzadıkça uzuyor. Bundan sonraki versionları upload edip link vericem...
-
Bresenham algoritması kullanan DrawCircle fonksiyonu ekledim. Bu arada tüm yapıyı bir class içine sokmayı ve tüm grafik fonksiyonlarını methodlara çevirmeyi düşünüyorum. Baştan yapmamam hataydı zaten... Ancak POINT2D ve RECT classlarındaki public değişkenleri private yapmaya gerek duymuyorum şimdilik. Zaten 320x200 ekran için long değişken kullanmanın pek de bir anlamı yok, kullanmayıverin :)
Son versiyonu aşağıdaki linkden indirebilirsiniz.
http://www.akaydin.com/misc_files/c_exampl...00412300344.zip (http://www.akaydin.com/misc_files/c_examples/graph13/GRAPH13_200412300344.zip)
-
FillCircle() fonksiyonu eklendi.
http://www.akaydin.com/misc_files/c_exampl...00412301634.zip (http://www.akaydin.com/misc_files/c_examples/graph13/GRAPH13_200412301634.zip)
-
RGB classı, Pallete ve Cycle özellikleri eklendi.
http://www.akaydin.com/misc_files/c_exampl...00412311538.zip (http://www.akaydin.com/misc_files/c_examples/graph13/GRAPH13_200412311538.zip)
-
Ufak bir TODO List yaptım kendime.
ToDO:
Genel Grafik Fonksiyonları:
---------------------------
ClearScreen
GetClip
SetClip
WaitForVerticalRetrace
SetDrawingBuffer
FlipPage
Çizim Fonksiyonları:
--------------------
DrawLines
DrawArc
DrawBezier
DrawBeziers
DrawEllipse
DrawPie
DrawPolygon
DrawPath
DrawCurve
DrawClosedCurve
DrawImage
FillEllipse
FillPie
FillPolygon
FillPath
FillClosedCurve
FillRegion
FloodFill
Ayrıca 3D Grafik fonkisyonları da ekliicem bunlar bittikten sonra.