yeah i could post the source here...
but before i say:
viewing the source wont really help you
...
cause i dit it 'oldStyle' ...
to make game in c++ ... (or any other language) you should use DX ou OpenGl .... its REALLY much more ez.. faster .. you can easyly use hardware accélération..
but here are the different my source files:
this one is Buffer.cpp.. i put everything touching to 2d in this class..... Dot function .... line cls.. and many other ..
#include "BaseType.h"
#include <stdio.h>
#include <alloc.h>
#include <stdlib.h>
#include <dos.h>
#include <Math.h>
const double PI=3.14159265359;
const DWord VidSeg=0xA000;
//########################################################
float ATanFull(float,float);
float DegToRad(float);
int RightEdge[200];
int LeftEdge[200];
double XPerY;
class Buffer
{
public:
void far *BufferPtr;
Buffer(void);
~Buffer(void);
void Sync(void);
void Dot(Word,Word,Byte);
void Cls(void);
void Line(float,float,float,float,Byte);
void FillPoly(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4, Byte color);
void FillTriangle(int x1,int y1,int x2,int y2,int x3,int y3, Byte color);
void ResetEdge(void);
void ChooseSide(int x1, int y1, int x2, int y2);
}
//****************************************
Buffer::Buffer(void)
{
BufferPtr=farmalloc(64000);
}
//****************************************
Buffer::~Buffer(void)
{
}
//****************************************
void Buffer::Sync(void)
{
Word BOff=FP_OFF(BufferPtr);
Word BSeg=FP_SEG(BufferPtr);
asm{
mov cx,32000;
}
LSync:
asm{
mov ax,BSeg;
mov es,ax;
mov ax,32000;
sub ax,cx;
add ax,ax;
add ax,BOff;
mov si,ax
mov dx,es:[si];
mov ax,0A000h;
mov es,ax;
mov ax,32000;
sub ax,cx;
add ax,ax;
mov di,ax;
mov es:[di],dx;
loop LSync;
}
}
//****************************************
void Buffer::Dot(Word X, Word Y, Byte Color)
{
Word OffDot;
Word BOff=FP_OFF(BufferPtr);
Word BSeg=FP_SEG(BufferPtr);
if ((X>319) || (Y<0) || (Y>199) || (Y<0)) goto NoDot;
OffDot=(X+(Y*320));
asm{
mov ax,BSeg;
mov es,ax;
mov ax,BOff;
add ax,OffDot;
mov di,ax;
mov al,Color;
mov al,Color;
mov es:[di],al;
}
NoDot:
}
//***********************************************
void Buffer::Cls(void)
{
Word BOff=FP_OFF(BufferPtr);
Word BSeg=FP_SEG(BufferPtr);
Word OffDot=0;
asm{
mov cx,64000;
}
LCls:
asm{
mov ax,BSeg;
mov es,ax;
mov ax,BOff;
add ax,OffDot;
mov di,ax;
mov ax,0;
mov es:[di],al;
inc OffDot;
loop LCls;
}
}
//******************************************************
void Buffer::Line(float x1,float y1,float x2,float y2,Byte color)
{
if((x1==x2) && (y1==y2))
Dot(x1,y1,color);
else
{
float Dist,Ang,x,y;
Dist=sqrt(((x2-x1)*(x2-x1))+((y2-y1)*(y2-y1)));
Ang=ATanFull((y2-y1),(x2-x1));
for(float i=0;i<=abs(Dist);i++)
{
x=x1+(i*cos(Ang));
y=y1+(i*sin(Ang));
Dot(x,y,color);
}
}
}
//******************************************************
void Buffer::FillPoly(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4, Byte color)
{
FillTriangle(x1,y1,x2,y2,x3,y3,color);
FillTriangle(x1,y1,x3,y3,x4,y4,color);
}
//***************************************************
void Buffer::FillTriangle(int x1,int y1,int x2,int y2,int x3,int y3, Byte color)
{
int MinY=199,MaxY=0;
if(y1>MaxY)
MaxY=y1;
if(y2>MaxY)
MaxY=y2;
if(y3>MaxY)
MaxY=y3;
if(y1<MinY)
MinY=y1;
if(y2<MinY)
MinY=y2;
if(y3<MinY)
MinY=y3;
ResetEdge();
ChooseSide(x1,y1,x2,y2);
ChooseSide(x2,y2,x3,y3);
ChooseSide(x1,y1,x3,y3);
for(int y=MinY;y<=MaxY;y++)
{
Line((int)LeftEdge[y],(int)y,(int)RightEdge[y],(int)y,color);
}
}
//***************************************************
void Buffer::ResetEdge(void)
{
for(int i=0;i<199;i++)
{
RightEdge[i]=0;
LeftEdge[i]=319;
}
}
//***************************************************
void Buffer::ChooseSide(int x1, int y1, int x2, int y2)
{
if(y1==y2)
{
if(x1<x2)
{
RightEdge[y1]=x2;
LeftEdge[y1]=x1;
}
else
{
RightEdge[y1]=x1;
LeftEdge[y1]=x2;
}
}
else
{
if(y1>y2)
{
int temp;
temp=x1;
x1=x2;
x2=temp;
temp=y1;
y1=y2;
y2=temp;
}
XPerY=(double)((double)x2 - (double)x1) / ((double)y2 - (double)y1);
double x12=x1;
for(int y=y1;y<=y2;y++) // For each scanline in the line.
{
if(x12 > RightEdge[y])
RightEdge[y]=x12;
if(x12 < LeftEdge[y])
LeftEdge[y]=x12;
x12=x12+XPerY;
}
}
}
//########################################################
float DegToRad(float Degrees)
{
float Radians;
Radians = Degrees * PI / 180.0;
return Radians;
}
float RadToDeg(float Radians)
{
float Degrees;
Degrees = Radians/(PI/180.0);
return Degrees;
}
//*******************************************************
float ATanFull(float y,float x)
{
float y22=y;
float x22=x;
if(y<0) y22=(0.0-y);
if(x<0) x22=(0.0-x);
if((x>0) && (y>0)) return(atan(y22/x22));
if((x<0) && (y>0)) return(DegToRad(180)-atan(y22/x22));
if((x<0) && (y<0)) return(DegToRad(180)+atan(y22/x22));
if((x>0) && (y<0)) return(DegToRad(360)-atan(y22/x22));
if((x==0) && (y>0)) return(DegToRad(90));
if((x==0) && (y<0)) return(DegToRad(270));
if((y==0) && (x>0)) return(DegToRad(0));
if((y==0) && (x<0)) return(DegToRad(180));
}
here is 3d.cpp
this is the 3d engine..... i mean rotation .. moving .. rendering
camera moving.. and many other things..
#include "BaseType.h"
#include <Math.h>
#include "Buffer.cpp"
#include "Mesh.cpp"
//########################################################
const Vh=200;
const R=1;
const Vw=320;
class T3d
{
public:
DWord NbObject;
Buffer Ecran;
Mesh Object[10];
Mesh Original[10];
int Position[10][4];
int Angle[10][4];
int CamX,CamY,CamZ,CamAngX,CamAngY,OldCamAngY,OldCamAngX;
T3d(void);
~T3d(void);
void AddObject(Mesh);
void PositionObject(int,int,int,int);
void RotateObject(int,float,float,float);
void RotateCamera(int,int);
void PositionCamera(int,int,int);
void Sync(void);
}
//**************************************************
T3d::T3d(void)
{
NbObject=0;
CamX=0;
CamY=0;
CamZ=0;
CamAngX=0;
CamAngY=0;
}
//**************************************************
T3d::~T3d(void)
{
}
//**************************************************
void T3d::PositionObject(int NoObject,int x,int y,int z)
{
}
//**************************************************
void T3d::RotateObject(int NoObject,float x, float y,float z)
{
float TempObject[100][4];
for(int i=1;i<=Original[NoObject].NbPts;i++)
{
TempObject[i][3]=Original[NoObject].Point[i][3];
TempObject[i][2]=Original[NoObject].Point[i][2];
TempObject[i][1]=Original[NoObject].Point[i][1];
}
z=DegToRad(z);
for(i=1;i<=Original[NoObject].NbPts;i++)
{
Object[NoObject].Point[i][1]=(TempObject[i][1]*cos(z))+(TempObject[i][2]*(0-sin(z)));
Object[NoObject].Point[i][2]=(TempObject[i][1]*sin(z))+(TempObject[i][2]*cos(z));
Object[NoObject].Point[i][3]=TempObject[i][3];
}
for(i=1;i<=Original[NoObject].NbPts;i++)
{
TempObject[i][3]=Object[NoObject].Point[i][3];
TempObject[i][2]=Object[NoObject].Point[i][2];
TempObject[i][1]=Object[NoObject].Point[i][1];
}
y=DegToRad(y);
for(i=1;i<=Original[NoObject].NbPts;i++)
{
Object[NoObject].Point[i][1]=(TempObject[i][1]*cos(y))+(TempObject[i][3]*sin(y));
Object[NoObject].Point[i][2]=TempObject[i][2];
Object[NoObject].Point[i][3]=(TempObject[i][1]*(0-sin(y)))+(TempObject[i][3]*cos(y));
}
for(i=1;i<=Original[NoObject].NbPts;i++)
{
TempObject[i][3]=Object[NoObject].Point[i][3];
TempObject[i][2]=Object[NoObject].Point[i][2];
TempObject[i][1]=Object[NoObject].Point[i][1];
}
x=DegToRad(x);
for(i=1;i<=Original[NoObject].NbPts;i++)
{
Object[NoObject].Point[i][1]=TempObject[i][1];
Object[NoObject].Point[i][2]=(TempObject[i][2]*cos(x))+(TempObject[i][3]*(0-sin(x)));
Object[NoObject].Point[i][3]=(TempObject[i][2]*sin(x))+(TempObject[i][3]*cos(x));
}
}
//**************************************************
void T3d::Sync(void)
{
float TempObject[100][4];
float Dot[100][3];
Ecran.Cls();
for(int i=1;i<=NbObject;i++)
{
for(int j=1;j<=Object[i].NbPts;j++)
{
TempObject[j][1]=Object[i].Point[j][1]+Position[i][1]-CamX;
TempObject[j][2]=Object[i].Point[j][2]+Position[i][2]-CamY;
TempObject[j][3]=Object[i].Point[j][3]+Position[i][3]-CamZ;
if(TempObject[j][3]<=0) goto NoRender;
Dot[j][1]=(((TempObject[j][1]/TempObject[j][3])*Vw*R)+(Vw/2.0));
Dot[j][2]=(((TempObject[j][2]/TempObject[j][3])*Vw*(0-R))+(Vh/2.0));
}
for(j=1;j<=Object[i].NbFaces;j++)
{
int P1=Object[i].Join[j][1];
int P2=Object[i].Join[j][2];
int P3=Object[i].Join[j][3];
int P4=Object[i].Join[j][4];
//Ecran.Line(Dot[P1][1],Dot[P1][2],Dot[P2][1],Dot[P2][2],14);
//Ecran.Line(Dot[P2][1],Dot[P2][2],Dot[P3][1],Dot[P3][2],14);
//Ecran.Line(Dot[P3][1],Dot[P3][2],Dot[P4][1],Dot[P4][2],14);
//Ecran.Line(Dot[P4][1],Dot[P4][2],Dot[P1][1],Dot[P1][2],14);
//double x1=TempObject[Object[i].Join[j][1]][1]-CamX;
// double x2=TempObject[Object[i].Join[j][2]][1]-CamX;
//double x3=TempObject[Object[i].Join[j][3]][1]-CamX;
//double y1=TempObject[Object[i].Join[j][1]][2]-CamY;
//double y2=TempObject[Object[i].Join[j][2]][2]-CamY;
//double y3=TempObject[Object[i].Join[j][3]][2]-CamY;
int x1=Dot[P1][1];
int x2=Dot[P2][1];
int x3=Dot[P3][1];
int y1=Dot[P1][2];
int y2=Dot[P2][2];
int y3=Dot[P3][2];
x1=x1-x2;
x2=x3-x2;
y1=y1-y2;
y2=y3-y2;
if ((x1*y2 - x2*y1) < 0)
Ecran.FillPoly(Dot[P1][1],Dot[P1][2],
Dot[P2][1],Dot[P2][2],
Dot[P3][1],Dot[P3][2],
Dot[P4][1],Dot[P4][2],30+(j*4));
}
NoRender:
}
Ecran.Sync();
}
//**************************************************
void T3d::AddObject(Mesh Object1)
{
NbObject=NbObject+1;
Object[NbObject]=Object1;
Original[NbObject]=Object1;
}
//**************************************************
void T3d::RotateCamera(int X,int Y)
{
}
//**************************************************
void T3d::PositionCamera(int X,int Y,int Z)
{
CamX=X;
CamY=Y;
CamZ=Z;
}
//**************************************************
here is Mesh.cpp
this is .. objet definition.. vertex.. polygon...
(quite not complete.. it is fast done.. should be modified soon.)
#include "BaseType.h"
#include <stdlib.h>
#include <time.h>
//***********************************
const NbPoint=8;
const NbFace=6;
class Mesh
{
public:
int NbPts;
int NbFaces;
int Point[NbPoint+1][4];
int Join[NbFace+1][5];
Mesh(void);
~Mesh(void);
}
//************************************
Mesh::Mesh(void)
{
randomize();
NbPts=NbPoint;
NbFaces=NbFace;
char Form='c';
if(Form=='c')
{
Point[1][1]=20;
Point[1][2]=20;
Point[1][3]=20;
Point[2][1]=20;
Point[2][2]=20;
Point[2][3]=-20;
Point[3][1]=-20;
Point[3][2]=20;
Point[3][3]=-20;
Point[4][1]=-20;
Point[4][2]=20;
Point[4][3]=20;
Point[5][1]=20;
Point[5][2]=-20;
Point[5][3]=20;
Point[6][1]=20;
Point[6][2]=-20;
Point[6][3]=-20;
Point[7][1]=-20;
Point[7][2]=-20;
Point[7][3]=-20;
Point[8][1]=-20;
Point[8][2]=-20;
Point[8][3]=20;
Join[1][1]=1;
Join[1][2]=2;
Join[1][3]=3;
Join[1][4]=4;
Join[2][1]=5;
Join[2][2]=8;
Join[2][3]=7;
Join[2][4]=6;
Join[3][1]=1;
Join[3][2]=5;
Join[3][3]=6;
Join[3][4]=2;
Join[4][1]=3;
Join[4][2]=7;
Join[4][3]=8;
Join[4][4]=4;
Join[5][1]=2;
Join[5][2]=6;
Join[5][3]=7;
Join[5][4]=3;
Join[6][1]=4;
Join[6][2]=8;
Join[6][3]=5;
Join[6][4]=1;
}
}
//************************************
Mesh::~Mesh(void)
{
}
and here is the DEMO.cpp ... that you downloaded... who use all these class:
#include "3d.cpp"
//***********************************************
T3d Buffer1;
Mesh Object1;
//***********************************************
int main(void)
{
asm{
mov ax,13h;
int 10h;
}
Buffer1.AddObject(Object1);
Buffer1.Position[1][1]=0;
Buffer1.Position[1][2]=0;
Buffer1.Position[1][3]=200;
int q=0,x=0,y=0,z=0,a=0,b=0;
for(int i=0;i<=(1*360);i++)
{
a=a+1;
if(a>360) a=0;
x=60*sin(DegToRad(a));
y=30*cos(DegToRad(a));
Buffer1.Position[1][1]=x;
Buffer1.Position[1][2]=y;
Buffer1.Sync();
}
a=0;
b=0;
for(i=0;i<=(1*360);i++)
{
a=a+1;
if(a>360) a=0;
x=60*sin(DegToRad(a));
y=30*cos(DegToRad(a));
Buffer1.Position[1][1]=x;
Buffer1.Position[1][2]=y;
Buffer1.RotateObject(1,0,0,a);
Buffer1.Sync();
}
a=0;
b=0;
for(i=0;i<=(1*360);i++)
{
a=a+1;
if(a>360) a=0;
x=60*sin(DegToRad(a));
y=30*cos(DegToRad(a));
Buffer1.Position[1][1]=x;
Buffer1.Position[1][2]=y;
Buffer1.RotateObject(1,a,a,a);
Buffer1.Sync();
}
a=0;
b=0;
for(i=0;i<=(1*360);i++)
{
a=a+1;
b=b+10;
if(a>360) a=0;
if(b>360) b=0;
x=60*sin(DegToRad(a));
y=30*cos(DegToRad(a));
z=200*cos(DegToRad(b));
Buffer1.Position[1][1]=x;
Buffer1.Position[1][2]=y;
Buffer1.Position[1][3]=z+400;
Buffer1.RotateObject(1,a,a,a);
Buffer1.Sync();
}
a=0;
b=0;
Buffer1.Position[1][1]=0;
Buffer1.Position[1][2]=-30;
Buffer1.Position[1][3]=200;
Buffer1.AddObject(Object1);
Buffer1.Position[2][1]=-60;
Buffer1.Position[2][2]=0;
Buffer1.Position[2][3]=200;
Buffer1.AddObject(Object1);
Buffer1.Position[3][1]=60;
Buffer1.Position[3][2]=0;
Buffer1.Position[3][3]=200;
Buffer1.AddObject(Object1);
Buffer1.Position[4][1]=0;
Buffer1.Position[4][2]=30;
Buffer1.Position[4][3]=200;
while(q!=1)
{
a=a+5;
if(a>360) a=0;
Buffer1.RotateObject(1,a,0,0);
Buffer1.RotateObject(2,0,a,0);
Buffer1.RotateObject(3,0,0,a);
Buffer1.RotateObject(4,a,a,a);
Buffer1.Sync();
}
sleep(1000);
return 0;
}
//***********************************************
there is some other file.. but you can understand without them...
you wont be able to compile it.. without them...
if you want to compile it.. ask it ill post the other little files...
if you have any question.. ask