Code:
#include <allegro.h>
#include <string>
#include <fstream>
using namespace std;
void drawMap(BITMAP *b, int sx, int sy);
void loadMap(string s);
int map[100][100];
BITMAP *buffer;
BITMAP *tiles[3];
BITMAP *player;
void main(void)
{
allegro_init();
install_keyboard();
install_timer();
set_color_depth(8);
set_gfx_mode(GFX_AUTODETECT, 320, 240, 0, 0);
PALLETE pal;
tiles[0] = load_bitmap("textures/grass.gif", pal);
tiles[1] = load_bitmap("textures/tree.gif", pal);
tiles[2] = load_bitmap("textures/bush.gif", pal);
player = load_bitmap("sprites/sprite1.gif", pal);
buffer = create_bitmap(320, 240);
loadMap("maps/map1.txt");
int x = 50, y = 50;
int xs = 0, ys = 0;
int up = false, down = false, left = false, right = false;
while(!key[KEY_ESC]) {
drawMap(buffer, x, y);
draw_sprite(buffer, player, (320 / 2), (240 / 2));
blit(buffer, screen, 0, 0, 0, 0, 16, 16);
poll_keyboard();
if(key[KEY_UP]) up = true; else up = false;
if(key[KEY_DOWN]) down = true; else down = false;
if(key[KEY_LEFT]) left = true; else left = false;
if(key[KEY_RIGHT]) right = true; else right = false;
if (up | down | left | right) {
for(int i = 0; i != 16; i++) {
if(up) ys -= i;
if(down) ys += i;
if(left) xs -= i;
if(right) xs += i;
drawMap(buffer, x, y);
draw_sprite(buffer, player, (320 / 2), (240 / 2));
blit(buffer, screen, 0, 0, xs - 16, ys - 16, 16, 16);
}
xs = 0;
ys = 0;
if(up) y--;
if(down) y++;
if(left) x--;
if(right) x++;
drawMap(buffer, x, y);
draw_sprite(buffer, player, (320 / 2), (240 / 2));
blit(buffer, screen, 0, 0, 0, 0, 16, 16);
}
}
}
END_OF_MAIN();
void drawMap(BITMAP *b, int sx, int sy)
{
for(int x = sx; x != (((240/26)+2)+sy); x++) {
for(int y = sy; y != (((240/16)+2)+sy); y++) {
blit(tiles[map[x][y]], buffer, 0, 0, (x * 16), (y * 16), 16, 16);
}
}
}
void loadMap(string s)
{
ifstream file_in;
file_in.open(s.c_str());
int total_x = 0;
int total_y = 0;
file_in >> total_x >> total_y;
for(int x = 0; x != total_x; x++) {
for (int y = 0; y != total_y; y++) {
file_in >> map[x][y];
}
}
file_in.close();
}
It now compiles, but when I run the program it closes right away. Now I have to figure out how to fix that little problem.