Alright, here goes (I've excluded my main() even though it's very short):
Also, I'm able to compile my program now that I've moved my structure out from gameLoop to the top of my code as a global variable, the only problem is I don't get any images loading anymore which I assume has to do with the fact that I'm destroying the data at the end of my loadTiles() function, before the drawTiles() function.
This seems wrong to me, but should I place both my loadTiles() and drawTiles() functions INSIDE my gameLoop() function? I've always liked keeping everything separate, but I've never made a game before...
Code:
//create the world map
struct MAP {
BITMAP *map[40][30];
int tileID[40][30];
};
MAP worldMap;
void loadTiles(int worldX, int worldY){
//load the tiles from tiles.dat
DATAFILE *tiles = load_datafile("tiles.dat");
//load the tiles into memory
BITMAP *mapTile_grass = (BITMAP*)tiles[Grass].dat;
BITMAP *mapTile_mountain = (BITMAP*)tiles[Mountain].dat;
BITMAP *mapTile_town = (BITMAP*)tiles[Town].dat;
//load world map from worldmap.txt
ifstream mapInput("worldmaplarge000.txt");
for(int y=0; y<worldY; y++){
for(int x=0; x<worldX; x++){
mapInput >> worldMap.tileID[x][y];
}
}
mapInput.close();
//load tiles to the proper coordinates
for(int y=0; y<worldY; y++){
for(int x=0; x<worldX; x++){
if (worldMap.tileID[x][y] == 0){
worldMap.map[x][y] = mapTile_grass;
}
else if (worldMap.tileID[x][y] == 1){
worldMap.map[x][y] = mapTile_mountain;
}
else if (worldMap.tileID[x][y] == 2){
worldMap.map[x][y] = mapTile_town;
}
}
}
//unload data before exit
destroy_bitmap(mapTile_grass);
destroy_bitmap(mapTile_mountain);
destroy_bitmap(mapTile_town);
unload_datafile(tiles);
return;
}
void drawTiles(int screenX, int screenY){
//draw the map into the buffer
BITMAP *buffer = create_bitmap(320, 240);
//draw tiles to the screen
acquire_screen();
for(int y=0; y<screenY; y++)
{
for(int x=0; x<screenX; x++)
{
draw_sprite(buffer, worldMap.map[x][y], x*16, y*16);
blit(buffer, screen, 0, 0, 0, 0, 320, 240);
}
}
release_screen();
//unload data before exit
destroy_bitmap(buffer);
return;
}
void gameLoop(){
//map coordinates
int worldX=40;
int worldY=30;
int screenX=20;
int screenY=15;
//load the tiles into memory
loadTiles(worldX, worldY);
//drawTiles to the screen
drawTiles(screenX, screenY);
//quit on keypress
readkey();
return;
}