#include "CTileMap.h" CTileMap::CTileMap(void) { CTileMap(0); } CTileMap::CTileMap(int aMapIndex) { mCurrentMap = aMapIndex; mMapImage = new CSurface(CGameConstants::MAP_WIDTH * CGameConstants::TILE_WIDTH, CGameConstants::MAP_HEIGHT * CGameConstants::TILE_HEIGHT); loadMap(mCurrentMap); } CTileMap::~CTileMap(void) { destroy(); } void CTileMap::destroy(void) { delete mMapImage; mMapImage = NULL; } void CTileMap::loadMap(int aMap) { mCurrentMap = aMap; FILE *f; int c; // Load the map. if ((f = fopen("assets/data/maps/map001.bin", "rb")) != NULL) { c = fread(mMap, CGameConstants::MAP_WIDTH * CGameConstants::MAP_HEIGHT, 1, f); fclose(f); } else { fprintf(stdout, "Unable to read map!\n"); } fprintf(stdout, "mMap[15] = %d\n", mMap[0]); fprintf(stdout, "mMap[62] = %d\n", mMap[62]); fprintf(stdout, "mMap[63] = %d\n", mMap[63]); fprintf(stdout, "mMap[64] = %d\n", mMap[64]); CSurface *tilesImage = new CSurface("assets/images/tiles/tileset0.png"); // Dibujar el mapa tileado. for (int i=0; i < CGameConstants::MAP_HEIGHT; i++) { for (int j=0; j < CGameConstants::MAP_WIDTH; j++) { int tile = mMap[i*CGameConstants::MAP_WIDTH+j]; // Calculo la ubicación del tile en la imagen de tiles. int xtile = (tile % CGameConstants::TILE_IMAGE_WIDTH) * CGameConstants::TILE_WIDTH; int ytile = (tile / CGameConstants::TILE_IMAGE_WIDTH) * CGameConstants::TILE_HEIGHT; // Calculo de la posición del tile en pantalla. int x = j * CGameConstants::TILE_WIDTH; int y = i * CGameConstants::TILE_HEIGHT; CSurface::drawImage(mMapImage, tilesImage, x, y, xtile, ytile, CGameConstants::TILE_WIDTH, CGameConstants::TILE_HEIGHT); //CSurface::drawImage(mMapImage, tilesImg, x, y, xtile, ytile, 72, 72); } } delete tilesImage; tilesImage = NULL; } void CTileMap::draw(CSurface *aImgDst, int aX, int aY) { CSurface::drawImage(aImgDst, mMapImage, aX, aY); } int CTileMap::getTile(int aTileX, int aTileY) { return mMap[aTileY*CGameConstants::MAP_WIDTH+aTileX]; } bool CTileMap::isWalkable(int aTileX, int aTileY) { int tile = mMap[aTileY*CGameConstants::MAP_WIDTH+aTileX]; return (tile == 2 || tile == 4 || tile == 1); }