Программирование игр для Windows. Советы профессионала


Смещение мозаичных изображений - часть 6


{

free(MemBuf);

free(BackGroundBmp) ;

free(ForeGroundBmp) ;

FreeTiles(};

}

//

void DrawLayers()

{ OpaqueBlt(BackGroundBmp,0,100,background) ;

TransparentBIt(ForeGroundBmp,50,100,foreground) ;

DrawTiles(position,54) ;

}

//

void AnimLoop() {

while(KeyScan != ESC_PRESSED)

{

switch(KeyScan)

{ case RIGHT_ARROW_PRESSED:

position+=4;

if(position > TOTAL_SCROLL) {

position=TOTAL_SCROLL;

break;

}

background-=1;

if(background < 1)

background+=VIEW_WIDTH;

foreground-=2; if(foreground < 1)

foreground+=VIEW_WIDTH;

break;

case LEFT_ARROW_PRESSED:

position-=4;

if(position < 0) {

position=0;

break;

}

background+=1;

if(background > VIEW_WIDTH-1) background-=VIEW_WIDTH;

foreground+=2 ;

if (foreground > VIEW_WIDTH-1) foreground-=VIEW_WIDTH;

break;

default:

break;

} DrawLayers();

memcpy(VideoRam,MemBuf,MEMBLK) ;

frames++;

} }

//

void Initialize()

{

position=0;

InitVideo(} ;

InitKeyboard();

if(!InitBitmaps())

{

Cleanup();

printf("\nError loading bitmaps\n");

exit(l);

} ReadTileMap("tilemap.dat");

ReadTiles();

}

//               void Cleanup() {

RestoreVideo() ;

RestoreKeyboard();

FreeMem();

}

void ReadTiles(void)

{

PcxFile pcx;

char buf[80];

int i,result;

tiles[0]=NULL;

for(i=l;i<=NUM_TILES;i++)

{

sprintf(buf,"t%d.pcx",i);

result=ReadPcxFile(buf,&pcx);

if(result != PCX_OK) ( printf("\nerror reading file: %s\n",buf);

exit(1);

} tiles[i]=pcx.bitmap;

} }

void FreeTiles() { int i;

for(i=0;i<NUM_TILES;i++) free(tiles[i]) ;

}

void ReadTileMap(char *filename)

{

int i;

FILE *f;

f=fopen(filename,"rt") ;

for (i=0; i<TILES__TOTAL; i-H-) {

fscanf(f,"%d",&(tilemap[i])) ;

}

fclose(f);

}

//

void DrawTile(char *bmp,int x,int y,int offset, int width)

{

char *dest;

int i;

if(bmp == NULL) return;

dest=MemBuf+y*VIEW_WIDTH+x;

bmp+=offset;

for(i=0;i<TILE_HEIGHT;i++) {

memcpy(dest,bmp,width);

dest+=VIEW_WIDTH;

bmp+=TILE_WIDTH;

} }

//

void DrawTiles(int VirtualX,int Starty)

{

int i,x,index,offset,row,limit;

index=VirtualX>>SHIFT;

offset=VirtualX - (index<<SHIFT) ;

limit=TILES_PER_ROW;

if(offset==0)

limit--;

for(row=Starty;row<Starty+TILE_HEIGHT*TILE_ROWS;

row+=TILE_HEIGHT) {

x=TILE_WIDTH-of£set;

DrawTile(tiles[tilemap[index]],0,row,offset,

TILE_WIDTH-offset);

for(i=index+l;i<index+limit;i++)

{

DrawTile(tiles [tilemap [i]], x, row, 0, TILE_WIDTH) ;

x+=TILE_WIDTH;

} DrawTile(tiles [tilemap[i] ] ,x, row, 0,offset);

index+=TILE_COLS;

}

}

//

int main() { clock_t begin,fini;

Initialize() ;

begin=clock();

AnimLoop() ;

fini=clock() ;

Cleanup() ;

printf("Frames: %d\nfps: %f\n",frames,

(float)CLK_TCK*frames/(fini-begin)) ;

return 0;

)




Начало  Назад  Вперед



Книжный магазин