#include "allegro.h" //define some convenient constants #define MODE GFX_AUTODETECT_WINDOWED #define WIDTH 640 #define HEIGHT 480 #define STEP 8 //very important! double check these values! // THEY MUST MATCH WHAT YOU EXPORTED // Number of pixels per tile. #define TILEW 32 #define TILEH 32 // 20 columns across is Mappy's default for saving tile bitmaps. #define COLS 20 // The size of your terrain map, in tiles. #define MAP_ACROSS 31 #define MAP_DOWN 33 #define MAPW MAP_ACROSS * TILEW #define MAPH MAP_DOWN * TILEH int map[] = {/* 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4, 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4, 4,4,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,4,4, 4,4,1,2,2,2,2,2,2,37,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,1,4,4, 4,4,1,2,2,2,2,2,2,38,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,1,4,4, 4,4,1,2,2,2,2,2,2,39,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,1,4,4, 4,4,1,2,2,2,2,2,2,39,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,1,4,4, 4,4,1,2,2,2,2,2,2,3,3,3,3,3,3,27,27,29,3,3,3,3,3,3,3,3,3,3,1,4,4, 4,4,1,2,2,2,2,2,3,3,3,3,3,3,27,27,30,3,3,3,3,3,3,3,3,3,3,3,1,4,4, 4,4,1,15,35,36,36,3,3,3,3,3,3,27,27,27,29,3,3,3,3,3,3,3,3,3,3,3,1,4,4, 4,4,1,3,3,3,3,3,3,3,3,3,3,27,27,30,3,3,3,3,3,3,3,3,3,3,3,3,1,4,4, 4,4,1,3,3,3,3,3,3,3,3,3,27,27,28,3,3,3,3,3,3,3,3,3,3,3,3,3,1,4,4, 4,4,1,3,3,3,3,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3,3,3,3,3,3,38,2,2,2,2,2,2,1,4,4, 4,4,1,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,39,2,2,2,2,2,2,1,4,4, 4,4,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,4,4, 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4, 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4 */ // YOUR MAPPY GENERATED TEXT GOES HERE }; //temp bitmap BITMAP *tiles; //virtual background buffer BITMAP *scroll; //position variables int x=0, y=0, n; int tilex, tiley; //reuse our friendly tile grabber from chapter 9 BITMAP *grabframe(BITMAP *source, int width, int height, int startx, int starty, int columns, int frame) { BITMAP *temp = create_bitmap(width,height); int x = startx + (frame % columns) * width; int y = starty + (frame / columns) * height; blit(source,temp,x,y,0,0,width,height); return temp; } //main function void main(void) { //initialize allegro allegro_init(); install_keyboard(); install_timer(); srand(time(NULL)); set_color_depth(16); //set video mode if (set_gfx_mode(MODE, WIDTH, HEIGHT, 0, 0) != 0) { set_gfx_mode(GFX_TEXT, 0, 0, 0, 0); allegro_message(allegro_error); return; } //create the virtual background scroll = create_bitmap(MAPW, MAPH); if (scroll == NULL) { set_gfx_mode(GFX_TEXT, 0, 0, 0, 0); allegro_message("Error creating virtual background"); return; } //load the tile bitmap // YOUR FILENAME WOULD GO HERE INSTEAD! tiles = load_bitmap("maptiles.bmp", NULL); if (tiles == NULL) { set_gfx_mode(GFX_TEXT, 0, 0, 0, 0); allegro_message("Error loading maptiles.bmp file"); return; } //now draw tiles on virtual background for (tiley=0; tiley < scroll->h; tiley+=TILEH) { for (tilex=0; tilex < scroll->w; tilex+=TILEW) { //use the result of grabframe directly in blitter blit(grabframe(tiles, TILEW, TILEH, 0, 0, COLS, map[n++]), scroll, 0, 0, tilex, tiley, TILEW, TILEH); } } //main loop while (!key[KEY_ESC]) { //check right arrow if (key[KEY_RIGHT]) { x += STEP; if (x > scroll->w - WIDTH) x = scroll->w - WIDTH; } //check left arrow if (key[KEY_LEFT]) { x -= STEP; if (x < 0) x = 0; } //check down arrow if (key[KEY_DOWN]) { y += STEP; if (y > scroll->h - HEIGHT) y = scroll->h - HEIGHT; } //check up arrow if (key[KEY_UP]) { y -= STEP; if (y < 0) y = 0; } //draw the scroll window portion of the virtual buffer blit(scroll, screen, x, y, 0, 0, WIDTH-1, HEIGHT-1); //display status info text_mode(-1); textprintf(screen,font,0,0,makecol(0,0,0), "Position = (%d,%d)", x, y); //slow it down rest(20); } destroy_bitmap(scroll); destroy_bitmap(tiles); return; } END_OF_MAIN();