red box = solid tile represented by a 1.
green = passibile tile reprented by a 0.
I'm using Visual C++ and the allegro sdk.
Thank you for your time
#include <allegro.h>
using namespace std;
#define ScreenWidth 800
#define ScreenHeight 600
volatile long timer = 0;
void Increment(){timer++;}
END_OF_FUNCTION(Increment);
int map[10][10] = {{1,1,1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,1,1,0,1,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,1,1,1,1,0,0,1},
{1,0,0,1,0,0,1,0,0,1},
{1,0,0,1,1,1,1,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1},
};
int main()
{
allegro_init();
install_keyboard();
install_timer();
set_color_depth(32);
set_gfx_mode(GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0);
set_window_title("Collision detection");
LOCK_FUNCTION(Increment);
LOCK_VARIABLE(counter);
install_int_ex(Increment, BPS_TO_TIMER(60));
BITMAP *Buffer = create_bitmap(800, 600);
bool done = false;
int x = 64, y = 64;
//Game Loop
while(!done)
{
int px = x / 32;
int py = y / 32;
int px2 = (x+32)/ 32;
int py2 = (y+32)/ 32;
//Update Loop
while(timer > 0)
{
if(key[KEY_ESC])
done = true;
if(key[KEY_UP])
{
if(map[px][py]==0 && map[px2][py]==0)
y -= 1;
}
if(key[KEY_RIGHT])
{
if(map[px2][py]==0 && map[px2][py2]==0)
x += 1;
}
if(key[KEY_LEFT])
{
if(map[px][py]==0 && map[px][py2]==0)
x -= 1;
}
if(key[KEY_DOWN])
{
if(map[px][py2]==0 && map[px2][py2]==0)
y += 1;
}
timer--;
}
//Draw tiles
for(int a = 0; a < 10; a++)
{
for(int b = 0; b < 10; b++)
{
if(map[a]== 0)
rectfill(Buffer, a*32,b*32, a*32+32, b*32+32, makecol(0,255,0));
else
rectfill(Buffer, a*32,b*32, a*32+32, b*32+32, makecol(200,0,0));
}
}
textprintf(Buffer, font, 400, 530, 255, "X: %i Y: %i", x, y);
textprintf(Buffer, font, 400, 550, 255, "Integer: %i, %i", px, py);
textprintf(Buffer, font, 400, 570, 255, "Map tile: %i", map[px][py]);
rect(Buffer, x,y,x+32,y+32, 255);
blit(Buffer, screen, 0,0,0,0,800, 600);
clear_bitmap(Buffer);
}
destroy_bitmap(Buffer);
allegro_exit();
return 0;
}
END_OF_MAIN()