Code:
/class Terrain
{
private: double xCoord, yCoord;
int type; //type of terrain from 0 to 4
MySurface* pMySurface;
RECT sourceRect, destRect;
double width, height;
public:
Terrain(int t, double x1,MySurface* p)
{
xCoord = x1;
yCoord = 0.0;
type = t;
pMySurface = p;
//calculate column and row number of the image
double x,y;
switch(type)
{
case 0: x=0;width=128;y=124;height=132;break;
case 1: x=129;width=61;y=35;height=221;break;
case 2: x=270;width=100;y=185;height=71;break;
case 3: x=383;width=97;y=205;height=51;break;
case 4: x=500;width=140;y=230;height=26;break;
case 5: x=191;width=75;y=35;height=221;break;
}
sourceRect.top=(int)y;
sourceRect.bottom=(int)(y+height);
sourceRect.left=(int)x;
sourceRect.right=(int)(x+width);
LogError("sourceRect");
LogError(x);
LogError(x+width);
}
double getX()
{
return xCoord;
}
double getWidth()
{
return width;
}
int getType()
{
return type;
}
void setType(int t)
{
type = t;
}
void draw()
{
destRect.top = (int)(470-gdViewpointY-height);
destRect.bottom= (int)(600-gdViewpointY);
destRect.left = (int)(xCoord-gdViewpointX);
destRect.right=(int)(xCoord+width-gdViewpointX);
/*destRect.left=(int)xCoord-gdViewpointX+100;
destRect.right=(int)xCoord+width-gdViewpointX;
destRect.bottom=(int)600-gdViewpointY;
destRect.top=(int)470-gdViewpointY;*/
LogError("destRect");
LogError(destRect.left);
LogError(destRect.right);
//to copy from secondary surface to back buffer
pDrawEngine->Blit(destRect,sourceRect,pMySurface);
}
};
Terrain* terrainArr[NUMOFTERRAIN];
int GameInit()
{
.....
srand(time(NULL));
double x=0.0;
terrainArr[0] = new Terrain(rand()%5,x,pgTerrainSurface);
LogError(terrainArr[0]->getType());
x+=terrainArr[0]->getWidth();
for(int i=1;i<NUMOFTERRAIN;i++)
{
terrainArr[i] = new Terrain(rand()%5,x,pgTerrainSurface);
LogError(terrainArr[i]->getType());
x+=terrainArr[i]->getWidth();
LogError(x);
}
count = 1;
x =0.0;
widthDrawn = 0.0;
.....
}
int GameMain{
...
DrawTerrain()
.....}
void DrawTerrain()
{
//if(count==NUMOFTERRAIN)
//count=0;
widthDrawn = 0.0;
for(int i=count;i<NUMOFTERRAIN;i++)
{
LogError("inside for loop in terrain");
terrainArr[i]->draw();
widthDrawn+=terrainArr[i]->getWidth();
count++;
if(widthDrawn>=SCREENWIDTH)
{
LogError("widthDrawn>=SCREENWIDTH");
widthDrawn = 0;
break;
}
}
}