Ejemplo n.º 1
0
GAE_Tiled_t* GAE_TiledParser_draw(GAE_Tiled_t* tilemap, GAE_Renderer_t* renderer, const unsigned int layerId) {
	unsigned int y = 0U;
	unsigned int x = 0U;
	
	GAE_Tiled_Layer_t* layer = (GAE_Tiled_Layer_t*)GAE_Array_get(tilemap->layers, layerId);
	GAE_Tiled_Tileset_t* tileset = getTileset(tilemap, *(unsigned int*)GAE_Array_begin(layer->data));
	
	const unsigned int margin = tileset->margin;
	const unsigned int srcWidth = tileset->tileWidth + margin;
	const unsigned int srcHeight = tileset->tileHeight + margin;
	const unsigned int dstWidth = tilemap->tileWidth + margin;
	const unsigned int dstHeight = tilemap->tileHeight + margin;
	const unsigned int tilemapWidthInTiles = (tileset->imageWidth / srcWidth);
	const unsigned int offsetX = layer->x;
	const unsigned int offsetY = layer->y;
	
	#define ROWCOL(x,y,width) (x * width + y)
	
	for (y = 0U; y < layer->height; ++y) {
		for (x = 0U; x < layer->width; ++x) {
			const unsigned int tileId = *(unsigned int*)GAE_Array_get(layer->data, ROWCOL(y, x, layer->width)) - 1U;
			SDL_Rect src;
			SDL_Rect dst;
			
			src.x = (tileId % tilemapWidthInTiles) * srcWidth;
			src.y = (tileId / tilemapWidthInTiles) * srcWidth;
			src.w = srcWidth;
			src.h = srcHeight;

			dst.x = (x * dstWidth) + offsetX;
			dst.y = (y * dstHeight) + offsetY;
			dst.w = dstWidth;
			dst.h = dstHeight;

			tileset->image->srcRect = &src;
			tileset->image->dstRect = &dst;
			GAE_Renderer_drawSprite(renderer, tileset->image);
		}
	}
	
	#undef ROWCOL
	
	return tilemap;
}
Ejemplo n.º 2
0
void SDL_TMXMap::Populate_Map(SDL_Renderer *Render)
{
    if( MapSurf || getMapFile().length())
    {
        if (!MapSurf)
        {
            std::ifstream *in_xml = new ifstream(getMapFile().c_str(), ifstream::in);
            if(!in_xml->is_open())
            {
                std::cout << "No way to Populate Map. Quitting..." <<endl;
                return;
            }
            std::stringstream *ss_xml = new std::stringstream;
            *ss_xml << in_xml->rdbuf();
            char *xml_data = new char[ss_xml->str().length()+1]();
            strcpy(xml_data, ss_xml->str().c_str());
            in_xml->close();
            delete ss_xml;
            Load_Map(xml_data);
            MapSurf = SDL_CreateRGBSurface(0, getWidth()*getTileWidth(), getHeight()*getTileHeight(), 32, rmask, gmask, bmask, amask);
        }

        string ResDir = "Resources\\";
        SDL_Surface **TileSurf = new SDL_Surface*[getNumTilesets()];
        for ( unsigned int fn = 0; fn < getNumTilesets(); fn++)
        {
            //eventually, there will be a means to load multiple images per tileset. TODO: Make a loop that allows it.
            //TODO: Write it so that it also works with
            string Filenname = ResDir + getTileset(fn).getImage(0).getFilename();
            TileSurf[fn] = IMG_Load(Filenname.c_str());
            if (!TileSurf[fn]) return;
        }

        unsigned long color = getTileset(0).getImage(0).getTrans();
        SDL_Color CKey;
        CKey.r = (color & 0xFF0000)   >> 16;
        CKey.g = (color & 0x00FF00)   >> 8 ;
        CKey.b = (color & 0x0000FF)        ;
        SDL_SetColorKey( MapSurf , SDL_TRUE , SDL_MapRGB(TileSurf[0]->format , CKey.r , CKey.g , CKey.b));
        SDL_Rect SrcRect;
        SDL_Rect DstRect;
        DstRect.h = getTileHeight();
        DstRect.w = getTileWidth();
        unsigned char **in_data  = new unsigned char* [getNumLayers()];
        unsigned char **out_data = new unsigned char* [getNumLayers()];
        unsigned int **tiledata = (unsigned int **) out_data;
        for(unsigned i = 0; i < getNumLayers(); i++)
        {
            in_data[i]  = new unsigned char [getLayer(i).getData().getData().size()];
            out_data[i] = new unsigned char [getWidth()*getHeight()*4];
            memcpy( in_data[i] , getLayer(i).getData().getData().c_str() , getLayer(i).getData().getData().size() );
            unsigned char *dec_data = new unsigned char[( getLayer(i).getData().getData().size())];
            TMX_Decode( in_data[i] , dec_data , getLayer(i).getData().getData().size() );
            TMX_Uncompress( dec_data , out_data[i] , ( getLayer(i).getData().getData().size() ), getWidth() * getHeight() * 4,
                           getLayer(i).getData().getCompression().c_str());
        }

        delete[]in_data;

        for (unsigned l = 0; l < TMX_Map::getNumLayers(); l++)
        {

            LayerSurf.push_back(SDL_CreateRGBSurface(0, getWidth()*getTileWidth(), getHeight()*getTileHeight(), 32, rmask, gmask, bmask, amask));
            SDL_SetSurfaceAlphaMod(LayerSurf[l], getLayer(l).getOpacity() * 255);
            for(unsigned int y = 0; y < getHeight(); y++)
            {
                DstRect.y = y * getTileHeight();
                for (unsigned int x = 0; x  < getWidth(); x++)
                {
                    if(tiledata[l][y*getWidth()+x])
                    {
                        DstRect.x = x * getTileWidth();
                        unsigned tilesetindex = findTileset(tiledata[l][y*getWidth()+x]);
                        unsigned srcindex = tiledata[l][y*getWidth()+x] - getTileset(tilesetindex).getGID();
                        SrcRect.h = getTileset(tilesetindex).getTileHeight();
                        SrcRect.w = getTileset(tilesetindex).getTileWidth();
                        float Float_TPL = (float) getTileset(tilesetindex).getImage(0).getWidth()
                                                  / (getTileset(tilesetindex).getTileWidth() + getTileset(tilesetindex).getSpacing());
                        unsigned long Tiles_Per_Line = (long) ceil(Float_TPL);
                        SrcRect.x = srcindex % Tiles_Per_Line * (getTileset(tilesetindex).getTileWidth() + getTileset(tilesetindex).getSpacing());
                        SrcRect.y = srcindex / Tiles_Per_Line * (getTileset(tilesetindex).getTileWidth() + getTileset(tilesetindex).getSpacing());
                        if( SDL_BlitSurface(TileSurf[tilesetindex], &SrcRect, LayerSurf[l], &DstRect) )
						{
							cout << "SDL_BlitSurface Failed: " << SDL_GetError() << endl;
							// if for some reason the application does not implode and instead run this code, we need to know why it crapped us.
							exit (-2);
						}
                    }
                }
            }
            if(getLayer(l).isVisible()) SDL_BlitSurface(LayerSurf[l],NULL, MapSurf, NULL);
        }

        delete[] tiledata;
        for (unsigned l = 0; l < getNumTilesets(); l++)
            SDL_FreeSurface(TileSurf[l]);

        MapTex = SDL_CreateTextureFromSurface(Render, MapSurf);
        SDL_FreeSurface(MapSurf);
    }
    else std::cout << "No way to Populate Map. Quitting..." << endl;