// Draw a tile at a location on the screen determined by the tile // position and the base X and Y co-ordinates // OVERRIDE THIS TO DRAW YOUR TILE!!! // Draw a tile void Demo4TileManager::DrawTileAt( BaseEngine* pEngine, SDL_Surface* pSurface, int iMapX, int iMapY, int iStartPositionScreenX, int iStartPositionScreenY ) { // Base class implementation just draws some grey tiles /* pEngine->DrawRectangle( iStartPositionScreenX, iStartPositionScreenY, iStartPositionScreenX + GetTileWidth() - 1, iStartPositionScreenY + GetTileHeight() - 1, pEngine->GetColour( GetValue(iMapX,iMapY) ), pSurface ); */ // Base class implementation just draws some grey tiles switch( GetValue(iMapX,iMapY) ) { case 0: case 1: pEngine->DrawRectangle( iStartPositionScreenX, iStartPositionScreenY, iStartPositionScreenX + GetTileWidth() - 1, iStartPositionScreenY + GetTileHeight() - 1, pEngine->GetColour( GetValue(iMapX,iMapY) ), pSurface ); break; case 2: case 3: case 4: case 5: case 6: case 7: case 8: pEngine->DrawRectangle( iStartPositionScreenX, iStartPositionScreenY, iStartPositionScreenX + GetTileWidth() - 1, iStartPositionScreenY + GetTileHeight() - 1, pEngine->GetColour( 0 ), pSurface ); pEngine->DrawOval( iStartPositionScreenX+GetValue(iMapX,iMapY)*2+5, iStartPositionScreenY+GetValue(iMapX,iMapY)*2+5, iStartPositionScreenX + GetTileWidth() - GetValue(iMapX,iMapY)*2 -6, iStartPositionScreenY + GetTileHeight() - GetValue(iMapX,iMapY)*2 -6, pEngine->GetColour( GetValue(iMapX,iMapY) ), pSurface ); break; } }
void MyTileManager::DrawTileAt(BaseEngine* pEngine, SDL_Surface* pSurface, int iMapX, int iMapY, int iStartPositionScreenX, int iStartPositionScreenY) { pEngine->DrawRectangle( iStartPositionScreenX, iStartPositionScreenY, iStartPositionScreenX + GetTileWidth() - 1, iStartPositionScreenY + GetTileHeight() - 1, pEngine->GetColour( GetValue(iMapX,iMapY) ), pSurface); }
// Draw a tile at a location on the screen determined by the tile // position and the base X and Y co-ordinates // OVERRIDE THIS TO DRAW YOUR TILE!!! // Draw a tile void Demo2TileManager::DrawTileAt( BaseEngine* pEngine, SDL_Surface* pSurface, int iMapX, int iMapY, int iStartPositionScreenX, int iStartPositionScreenY ) { // Base class implementation just draws some grey tiles pEngine->DrawRectangle( iStartPositionScreenX, iStartPositionScreenY, iStartPositionScreenX + GetTileWidth() - 1, iStartPositionScreenY + GetTileHeight() - 1, pEngine->GetColour( GetValue(iMapX,iMapY) ), pSurface ); }
// Draw all tiles, from start to end position void TileManager::UpdateTileAt( BaseEngine* pEngine, int iMapX, int iMapY, int iNewTileValue, int iScreenLeftTilePosition, int iScreenTopTilePosition ) { // Set the new Tile value SetValue( iMapX, iMapY, iNewTileValue ); // Draw the tile to the background DrawTileAt( pEngine, pEngine->GetBackground(), iMapX, iMapY, iScreenLeftTilePosition, iScreenTopTilePosition ); // Copy the background to the foreground pEngine->CopyBackgroundPixels( iScreenLeftTilePosition, iScreenTopTilePosition, GetTileWidth(), GetTileHeight() ); // Update the screen - so that it gets redrawn SDL_Rect* pRect = pEngine->GetNextUpdateRect(); pRect->x = iScreenLeftTilePosition; pRect->y = iScreenTopTilePosition; pRect->h = GetTileHeight(); pRect->w = GetTileWidth(); }
// Draw all tiles, from start to end position void TileManager::DrawAllTilesAt( BaseEngine* pEngine, SDL_Surface* pSurface, int iMapXStart, int iMapYStart, int iMapXEnd, int iMapYEnd, int iStartPositionScreenX, int iStartPositionScreenY ) const { for ( int iTX = iMapXStart ; iTX <= iMapXEnd ; iTX++ ) for ( int iTY = iMapYStart ; iTY <= iMapYEnd ; iTY++ ) { DrawTileAt( pEngine, pSurface, iTX, iTY, iStartPositionScreenX+GetTileWidth()*(iTX-iMapXStart), iStartPositionScreenY+GetTileHeight()*(iTY-iMapYStart) ); } }
// Override this to draw tiles. void TileManager::DrawTileAt( BaseEngine* pEngine, SDL_Surface* pSurface, int iMapX, int iMapY, int iStartPositionScreenX, int iStartPositionScreenY ) const { // Base class implementation just draws some grey tiles unsigned int iColour = 0x101010 * ((iMapX+iMapY+GetValue(iMapX,iMapY))%16); pEngine->DrawRectangle( iStartPositionScreenX, iStartPositionScreenY, iStartPositionScreenX + GetTileWidth() - 1, iStartPositionScreenY + GetTileHeight() - 1, iColour, pSurface ); }
void Nxs13uTileManager::DrawTileAt( BaseEngine* pEngine, SDL_Surface* pSurface, int iMapX, int iMapY, int iStartPositionScreenX, int iStartPositionScreenY) const { //draws tiles for the home page pEngine->DrawRectangle( iStartPositionScreenX, iStartPositionScreenY, iStartPositionScreenX + GetTileWidth(), iStartPositionScreenY + GetTileHeight(), pEngine->GetColour(0xF5D9D9), pSurface); }
// Draw all tiles, from start to end position void TileManager::UpdateTileAt( BaseEngine* pEngine, int iMapX, int iMapY, int iNewTileValue, int iScreenLeftTilePosition, int iScreenTopTilePosition ) { // Set the new Tile value SetValue( iMapX, iMapY, iNewTileValue ); // Draw the tile to the background DrawTileAt( pEngine, pEngine->GetBackground(), iMapX, iMapY, iScreenLeftTilePosition, iScreenTopTilePosition ); // Copy the background to the foreground pEngine->CopyBackgroundPixels( iScreenLeftTilePosition, iScreenTopTilePosition, GetTileWidth(), GetTileHeight() ); }
void World::openMap(std::string mapName,int callbackRef) { _map.ParseFile(single<ResourceManager>().getMap(mapName).tmxFilePath()); //set the background color unsigned short r, g, b; auto c = Color{}; if (!_map.GetBackgroundColor().compare("")) { r = 0x00; g = 0x00; b = 0x00; } else { hexToRgb(_map.GetBackgroundColor(), c.r, c.g, c.b); } c.a = 0xFF; //load the tilesets into the resource manager const std::vector<Tmx::Tileset*> tilesets = _map.GetTilesets(); for (auto ts : tilesets) { auto tsimage = ts->GetImage(); auto texture = new Texture{}; texture->setName(ts->GetName()); texture->setFileSource("./assets/maps/tilesets/" + stripPath(tsimage->GetSource())); texture->initialize(); single<ResourceManager>().addTexture(std::unique_ptr<Texture>(texture)); } auto& lua = single<Core>().lua(); const std::vector<Tmx::Layer*> layers = _map.GetLayers(); for (auto layer : layers) { int x1, y1; int u1, v1; int layerWidth = layer->GetWidth(); int layerHeight = layer->GetHeight(); // so much shit code single<Core>().getSystemByName<PhysicsSystem>("Physics")->setGravity(Vec2{ layer->GetProperties().GetFloatProperty("defaultGravityX"), layer->GetProperties().GetFloatProperty("defaultGravityY") }); for (int y = 0; y < layerHeight; y++) { for (int x = 0; x < layerWidth; x++) { int tileId = layer->GetTileId(x, layerHeight - y - 1); int tilesetId = layer->GetTileTilesetIndex(x, layerHeight - y - 1); if (tilesetId >= 0) { auto thisSet = _map.GetTileset(tilesetId); auto tile = _map.GetTileset(tilesetId)->GetTile(tileId); //figure out which section of the texture this tile is auto setw = (thisSet->GetImage()->GetWidth() / thisSet->GetTileWidth()); auto seth = (thisSet->GetImage()->GetHeight() / thisSet->GetTileHeight()); u1 = tileId % setw; v1 = (tileId - u1) / setw; u1 *= thisSet->GetTileWidth(); v1 *= thisSet->GetTileHeight(); x1 = x * _map.GetTileWidth(); y1 = y * _map.GetTileHeight(); auto luaTile = LuaTile{}; luaTile.position[0] = x1; luaTile.position[1] = y1; luaTile.position[2] = 0; luaTile.dimensions[0] = thisSet->GetTileWidth(); luaTile.dimensions[1] = thisSet->GetTileHeight(); luaTile.sourceTextureRect[0] = u1; luaTile.sourceTextureRect[1] = v1; luaTile.sourceTextureRect[2] = thisSet->GetTileWidth(); luaTile.sourceTextureRect[3] = thisSet->GetTileHeight(); luaTile.textureName = thisSet->GetName(); auto& tileProperties = tile->GetProperties(); luaTile.tileProperties = tileProperties.GetList(); lua.call(callbackRef, luaTile); } } //end layer X loop } //end layer Y loop } //end layers loop for (auto objGroup : _map.GetObjectGroups()) { //get the associated tile layer auto tileLayer = objGroup->GetProperties().GetLiteralProperty("tileLayer"); auto physicsSystem = single<Core>().getSystemByName<PhysicsSystem>("Physics"); for (auto obj : objGroup->GetObjects()) { debug("Object at ", obj->GetX(), ", ", obj->GetY()); auto luaTile = LuaTile{}; luaTile.position[0] = obj->GetX(); luaTile.position[1] = _map.GetTileHeight() * objGroup->GetHeight() - obj->GetY() - obj->GetHeight(); luaTile.position[2] = 0; luaTile.dimensions[0] = obj->GetWidth(); luaTile.dimensions[1] = obj->GetHeight(); luaTile.isObject = true; luaTile.tileProperties = obj->GetProperties().GetList(); luaTile.tileProperties["name"] = obj->GetName(); luaTile.tileProperties["objectType"] = obj->GetType(); lua.call(callbackRef, luaTile); } //end objects loop } //end object layers loop lua.removeReference(callbackRef); }