void drawImage(Game *game, byte *image, int texPitch, int sourceX, int sourceY, int sourceWidth, int sourceHeight, int destX, int destY, int destWidth, int destHeight, fixed depth)
{
Graphics *g = (Graphics*)game->graphics;
fixed sampleSizeX = FP16DIV( ITOFP16(1<<sourceWidth) , ITOFP16(destWidth) );
fixed sampleSizeY = FP16DIV( ITOFP16(1<<sourceHeight) , ITOFP16(destHeight) );
fixed samplePosX = 0;
fixed initSamplePosY = 0;
fixed *depthBuffer = g->depthBuffer;
int startX = destX;
int startY = destY;
int endX = startX+destWidth;
int endY = startY+destHeight;
int modX = (1<<sourceWidth)-1;
int modY = (1<<sourceHeight)-1;
int x = 0;
int y = 0;
IDIB *backBuffer = g->backBuffer;
int pitch = backBuffer->nPitch;
byte *screen = NULL;
if(startX < 0)
{
samplePosX = FP16MUL(ITOFP16(-startX) , sampleSizeX);
startX = 0;
}
if(startY < 0)
{
initSamplePosY = FP16MUL(ITOFP16(-startY) , sampleSizeY );
startY = 0;
}
if(endX > game->DeviceInfo.cxScreen)
endX = game->DeviceInfo.cxScreen;
if(endY > game->DeviceInfo.cyScreen)
endY = game->DeviceInfo.cyScreen;
screen = backBuffer->pBmp + startX+(startY*pitch);
for(x = startX; x < endX; ++x)
{
byte *screenPos = screen;
fixed samplePosY = initSamplePosY;
if( depth <= depthBuffer[x] )
{
for(y = startY; y < endY; ++y)
{
int px = (FP16TOI(samplePosX) & modX) + sourceX;
int py = (FP16TOI(samplePosY) & modY) + sourceY;
byte pixel = image[px+(py<<texPitch)];
if(pixel != 255)
*screenPos = pixel;
screenPos += pitch;
samplePosY += sampleSizeY;
}
}
++screen;
samplePosX += sampleSizeX;
}
}