1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-02-03 13:01:33 +02:00

Internal classes of mapHandler are now in global namespace

This commit is contained in:
Ivan Savenko 2023-02-10 16:50:05 +02:00
parent cc0c3e5bc9
commit 89c29206f1
2 changed files with 220 additions and 212 deletions

View File

@ -360,7 +360,7 @@ void CMapHandler::init()
logGlobal->info("\tMaking object rects: %d ms", th.getDiff());
}
CMapHandler::CMapBlitter *CMapHandler::resolveBlitter(const MapDrawingInfo * info) const
CMapBlitter *CMapHandler::resolveBlitter(const MapDrawingInfo * info) const
{
if (info->scaled)
return worldViewBlitter;
@ -370,12 +370,12 @@ CMapHandler::CMapBlitter *CMapHandler::resolveBlitter(const MapDrawingInfo * inf
return normalBlitter;
}
void CMapHandler::CMapNormalBlitter::drawElement(EMapCacheType cacheType, std::shared_ptr<IImage> source, Rect * sourceRect, SDL_Surface * targetSurf, Rect * destRect) const
void CMapNormalBlitter::drawElement(EMapCacheType cacheType, std::shared_ptr<IImage> source, Rect * sourceRect, SDL_Surface * targetSurf, Rect * destRect) const
{
source->draw(targetSurf, destRect, sourceRect);
}
void CMapHandler::CMapNormalBlitter::init(const MapDrawingInfo * drawingInfo)
void CMapNormalBlitter::init(const MapDrawingInfo * drawingInfo)
{
info = drawingInfo;
// Width and height of the portion of the map to process. Units in tiles.
@ -426,7 +426,7 @@ void CMapHandler::CMapNormalBlitter::init(const MapDrawingInfo * drawingInfo)
tileCount.y = parent->sizes.y + parent->frameH - topTile.y;
}
Rect CMapHandler::CMapNormalBlitter::clip(SDL_Surface * targetSurf) const
Rect CMapNormalBlitter::clip(SDL_Surface * targetSurf) const
{
Rect prevClip;
CSDL_Ext::getClipRect(targetSurf, prevClip);
@ -434,7 +434,7 @@ Rect CMapHandler::CMapNormalBlitter::clip(SDL_Surface * targetSurf) const
return prevClip;
}
CMapHandler::CMapNormalBlitter::CMapNormalBlitter(CMapHandler * parent)
CMapNormalBlitter::CMapNormalBlitter(CMapHandler * parent)
: CMapBlitter(parent)
{
tileSize = 32;
@ -442,7 +442,7 @@ CMapHandler::CMapNormalBlitter::CMapNormalBlitter(CMapHandler * parent)
defaultTileRect = Rect(0, 0, tileSize, tileSize);
}
std::shared_ptr<IImage> CMapHandler::CMapWorldViewBlitter::objectToIcon(Obj id, si32 subId, PlayerColor owner) const
std::shared_ptr<IImage> CMapWorldViewBlitter::objectToIcon(Obj id, si32 subId, PlayerColor owner) const
{
int ownerIndex = 0;
if(owner < PlayerColor::PLAYER_LIMIT)
@ -476,7 +476,7 @@ std::shared_ptr<IImage> CMapHandler::CMapWorldViewBlitter::objectToIcon(Obj id,
return std::shared_ptr<IImage>();
}
void CMapHandler::CMapWorldViewBlitter::calculateWorldViewCameraPos()
void CMapWorldViewBlitter::calculateWorldViewCameraPos()
{
bool outsideLeft = topTile.x < 0;
bool outsideTop = topTile.y < 0;
@ -508,7 +508,7 @@ void CMapHandler::CMapWorldViewBlitter::calculateWorldViewCameraPos()
topTile.y = parent->sizes.y - tileCount.y;
}
void CMapHandler::CMapWorldViewBlitter::drawElement(EMapCacheType cacheType, std::shared_ptr<IImage> source, Rect * sourceRect, SDL_Surface * targetSurf, Rect * destRect) const
void CMapWorldViewBlitter::drawElement(EMapCacheType cacheType, std::shared_ptr<IImage> source, Rect * sourceRect, SDL_Surface * targetSurf, Rect * destRect) const
{
auto scaled = parent->cache.requestWorldViewCacheOrCreate(cacheType, source);
@ -516,7 +516,7 @@ void CMapHandler::CMapWorldViewBlitter::drawElement(EMapCacheType cacheType, std
scaled->draw(targetSurf, destRect, sourceRect);
}
void CMapHandler::CMapWorldViewBlitter::drawTileOverlay(SDL_Surface * targetSurf, const TerrainTile2 & tile) const
void CMapWorldViewBlitter::drawTileOverlay(SDL_Surface * targetSurf, const TerrainTile2 & tile) const
{
auto drawIcon = [this,targetSurf](Obj id, si32 subId, PlayerColor owner)
{
@ -548,7 +548,7 @@ void CMapHandler::CMapWorldViewBlitter::drawTileOverlay(SDL_Surface * targetSurf
}
}
void CMapHandler::CMapWorldViewBlitter::drawOverlayEx(SDL_Surface * targetSurf)
void CMapWorldViewBlitter::drawOverlayEx(SDL_Surface * targetSurf)
{
if(nullptr == info->additionalIcons)
return;
@ -580,7 +580,7 @@ void CMapHandler::CMapWorldViewBlitter::drawOverlayEx(SDL_Surface * targetSurf)
}
}
void CMapHandler::CMapWorldViewBlitter::drawHeroFlag(SDL_Surface * targetSurf, std::shared_ptr<IImage> source, Rect * sourceRect, Rect * destRect, bool moving) const
void CMapWorldViewBlitter::drawHeroFlag(SDL_Surface * targetSurf, std::shared_ptr<IImage> source, Rect * sourceRect, Rect * destRect, bool moving) const
{
if (moving)
return;
@ -588,7 +588,7 @@ void CMapHandler::CMapWorldViewBlitter::drawHeroFlag(SDL_Surface * targetSurf, s
CMapBlitter::drawHeroFlag(targetSurf, source, sourceRect, destRect, false);
}
void CMapHandler::CMapWorldViewBlitter::drawObject(SDL_Surface * targetSurf, std::shared_ptr<IImage> source, Rect * sourceRect, bool moving) const
void CMapWorldViewBlitter::drawObject(SDL_Surface * targetSurf, std::shared_ptr<IImage> source, Rect * sourceRect, bool moving) const
{
if (moving)
return;
@ -597,7 +597,7 @@ void CMapHandler::CMapWorldViewBlitter::drawObject(SDL_Surface * targetSurf, std
CMapBlitter::drawObject(targetSurf, source, &scaledSourceRect, false);
}
void CMapHandler::CMapBlitter::drawTileTerrain(SDL_Surface * targetSurf, const TerrainTile & tinfo, const TerrainTile2 & tile) const
void CMapBlitter::drawTileTerrain(SDL_Surface * targetSurf, const TerrainTile & tinfo, const TerrainTile2 & tile) const
{
Rect destRect(realTileRect);
@ -612,7 +612,7 @@ void CMapHandler::CMapBlitter::drawTileTerrain(SDL_Surface * targetSurf, const T
drawElement(EMapCacheType::TERRAIN, parent->terrainImages[terrainName][tinfo.terView][rotation], nullptr, targetSurf, &destRect);
}
void CMapHandler::CMapWorldViewBlitter::init(const MapDrawingInfo * drawingInfo)
void CMapWorldViewBlitter::init(const MapDrawingInfo * drawingInfo)
{
info = drawingInfo;
parent->cache.updateWorldViewScale(info->scale);
@ -633,7 +633,7 @@ void CMapHandler::CMapWorldViewBlitter::init(const MapDrawingInfo * drawingInfo)
calculateWorldViewCameraPos();
}
Rect CMapHandler::CMapWorldViewBlitter::clip(SDL_Surface * targetSurf) const
Rect CMapWorldViewBlitter::clip(SDL_Surface * targetSurf) const
{
Rect prevClip;
@ -649,12 +649,12 @@ Rect CMapHandler::CMapWorldViewBlitter::clip(SDL_Surface * targetSurf) const
return prevClip;
}
CMapHandler::CMapWorldViewBlitter::CMapWorldViewBlitter(CMapHandler * parent)
CMapWorldViewBlitter::CMapWorldViewBlitter(CMapHandler * parent)
: CMapBlitter(parent)
{
}
void CMapHandler::CMapPuzzleViewBlitter::drawObjects(SDL_Surface * targetSurf, const TerrainTile2 & tile) const
void CMapPuzzleViewBlitter::drawObjects(SDL_Surface * targetSurf, const TerrainTile2 & tile) const
{
CMapBlitter::drawObjects(targetSurf, tile);
@ -666,12 +666,12 @@ void CMapHandler::CMapPuzzleViewBlitter::drawObjects(SDL_Surface * targetSurf, c
}
}
void CMapHandler::CMapPuzzleViewBlitter::postProcessing(SDL_Surface * targetSurf) const
void CMapPuzzleViewBlitter::postProcessing(SDL_Surface * targetSurf) const
{
CSDL_Ext::applyEffect(targetSurf, info->drawBounds, static_cast<int>(!ADVOPT.puzzleSepia));
}
bool CMapHandler::CMapPuzzleViewBlitter::canDrawObject(const CGObjectInstance * obj) const
bool CMapPuzzleViewBlitter::canDrawObject(const CGObjectInstance * obj) const
{
if (!CMapBlitter::canDrawObject(obj))
return false;
@ -686,43 +686,43 @@ bool CMapHandler::CMapPuzzleViewBlitter::canDrawObject(const CGObjectInstance *
return true;
}
CMapHandler::CMapPuzzleViewBlitter::CMapPuzzleViewBlitter(CMapHandler * parent)
CMapPuzzleViewBlitter::CMapPuzzleViewBlitter(CMapHandler * parent)
: CMapNormalBlitter(parent)
{
unblittableObjects.push_back(Obj::HOLE);
}
CMapHandler::CMapBlitter::CMapBlitter(CMapHandler * p)
CMapBlitter::CMapBlitter(CMapHandler * p)
:parent(p), tileSize(0), halfTileSizeCeil(0), info(nullptr)
{
}
CMapHandler::CMapBlitter::~CMapBlitter() = default;
CMapBlitter::~CMapBlitter() = default;
void CMapHandler::CMapBlitter::drawFrame(SDL_Surface * targetSurf) const
void CMapBlitter::drawFrame(SDL_Surface * targetSurf) const
{
Rect destRect(realTileRect);
drawElement(EMapCacheType::FRAME, parent->egdeImages[parent->edgeFrames[pos.x][pos.y][topTile.z]], nullptr, targetSurf, &destRect);
}
void CMapHandler::CMapBlitter::drawOverlayEx(SDL_Surface * targetSurf)
void CMapBlitter::drawOverlayEx(SDL_Surface * targetSurf)
{
//nothing to do here
}
void CMapHandler::CMapBlitter::drawHeroFlag(SDL_Surface * targetSurf, std::shared_ptr<IImage> source, Rect * sourceRect, Rect * destRect, bool moving) const
void CMapBlitter::drawHeroFlag(SDL_Surface * targetSurf, std::shared_ptr<IImage> source, Rect * sourceRect, Rect * destRect, bool moving) const
{
drawElement(EMapCacheType::HERO_FLAGS, source, sourceRect, targetSurf, destRect);
}
void CMapHandler::CMapBlitter::drawObject(SDL_Surface * targetSurf, std::shared_ptr<IImage> source, Rect * sourceRect, bool moving) const
void CMapBlitter::drawObject(SDL_Surface * targetSurf, std::shared_ptr<IImage> source, Rect * sourceRect, bool moving) const
{
Rect dstRect(realTileRect);
drawElement(EMapCacheType::OBJECTS, source, sourceRect, targetSurf, &dstRect);
}
void CMapHandler::CMapBlitter::drawObjects(SDL_Surface * targetSurf, const TerrainTile2 & tile) const
void CMapBlitter::drawObjects(SDL_Surface * targetSurf, const TerrainTile2 & tile) const
{
auto & objects = tile.objects;
for(auto & object : objects)
@ -782,7 +782,7 @@ void CMapHandler::CMapBlitter::drawObjects(SDL_Surface * targetSurf, const Terra
}
}
void CMapHandler::CMapBlitter::drawRoad(SDL_Surface * targetSurf, const TerrainTile & tinfo, const TerrainTile * tinfoUpper) const
void CMapBlitter::drawRoad(SDL_Surface * targetSurf, const TerrainTile & tinfo, const TerrainTile * tinfoUpper) const
{
if (tinfoUpper && tinfoUpper->roadType->getId() != Road::NO_ROAD)
{
@ -803,14 +803,14 @@ void CMapHandler::CMapBlitter::drawRoad(SDL_Surface * targetSurf, const TerrainT
}
}
void CMapHandler::CMapBlitter::drawRiver(SDL_Surface * targetSurf, const TerrainTile & tinfo) const
void CMapBlitter::drawRiver(SDL_Surface * targetSurf, const TerrainTile & tinfo) const
{
Rect destRect(realTileRect);
ui8 rotation = (tinfo.extTileFlags >> 2) % 4;
drawElement(EMapCacheType::RIVERS, parent->riverImages[tinfo.riverType->getJsonKey()][tinfo.riverDir][rotation], nullptr, targetSurf, &destRect);
}
void CMapHandler::CMapBlitter::drawFow(SDL_Surface * targetSurf) const
void CMapBlitter::drawFow(SDL_Surface * targetSurf) const
{
const NeighborTilesInfo neighborInfo(pos, parent->sizes, info->visibilityMap);
@ -829,7 +829,7 @@ void CMapHandler::CMapBlitter::drawFow(SDL_Surface * targetSurf) const
drawElement(EMapCacheType::FOW, image, nullptr, targetSurf, &destRect);
}
void CMapHandler::CMapBlitter::blit(SDL_Surface * targetSurf, const MapDrawingInfo * info)
void CMapBlitter::blit(SDL_Surface * targetSurf, const MapDrawingInfo * info)
{
init(info);
auto prevClip = clip(targetSurf);
@ -950,14 +950,14 @@ void CMapHandler::CMapBlitter::blit(SDL_Surface * targetSurf, const MapDrawingIn
CSDL_Ext::setClipRect(targetSurf, prevClip);
}
CMapHandler::AnimBitmapHolder CMapHandler::CMapBlitter::findHeroBitmap(const CGHeroInstance * hero, int anim) const
AnimBitmapHolder CMapBlitter::findHeroBitmap(const CGHeroInstance * hero, int anim) const
{
if(hero && hero->moveDir && hero->type) //it's hero or boat
{
if(hero->tempOwner >= PlayerColor::PLAYER_LIMIT) //Neutral hero?
{
logGlobal->error("A neutral hero (%s) at %s. Should not happen!", hero->getNameTranslated(), hero->pos.toString());
return CMapHandler::AnimBitmapHolder();
return AnimBitmapHolder();
}
//pick graphics of hero (or boat if hero is sailing)
@ -978,23 +978,23 @@ CMapHandler::AnimBitmapHolder CMapHandler::CMapBlitter::findHeroBitmap(const CGH
//get flag overlay only if we have main image
auto flagImage = findFlagBitmap(hero, anim, &hero->tempOwner, group);
return CMapHandler::AnimBitmapHolder(heroImage, flagImage, moving);
return AnimBitmapHolder(heroImage, flagImage, moving);
}
}
return CMapHandler::AnimBitmapHolder();
return AnimBitmapHolder();
}
CMapHandler::AnimBitmapHolder CMapHandler::CMapBlitter::findBoatBitmap(const CGBoat * boat, int anim) const
AnimBitmapHolder CMapBlitter::findBoatBitmap(const CGBoat * boat, int anim) const
{
auto animation = graphics->boatAnimations.at(boat->subID);
int group = getHeroFrameGroup(boat->direction, false);
if(animation->size(group) > 0)
return CMapHandler::AnimBitmapHolder(animation->getImage(anim % animation->size(group), group));
return AnimBitmapHolder(animation->getImage(anim % animation->size(group), group));
else
return CMapHandler::AnimBitmapHolder();
return AnimBitmapHolder();
}
std::shared_ptr<IImage> CMapHandler::CMapBlitter::findFlagBitmap(const CGHeroInstance * hero, int anim, const PlayerColor * color, int group) const
std::shared_ptr<IImage> CMapBlitter::findFlagBitmap(const CGHeroInstance * hero, int anim, const PlayerColor * color, int group) const
{
if(!hero)
return std::shared_ptr<IImage>();
@ -1004,12 +1004,12 @@ std::shared_ptr<IImage> CMapHandler::CMapBlitter::findFlagBitmap(const CGHeroIns
return findHeroFlagBitmap(hero, anim, color, group);
}
std::shared_ptr<IImage> CMapHandler::CMapBlitter::findHeroFlagBitmap(const CGHeroInstance * hero, int anim, const PlayerColor * color, int group) const
std::shared_ptr<IImage> CMapBlitter::findHeroFlagBitmap(const CGHeroInstance * hero, int anim, const PlayerColor * color, int group) const
{
return findFlagBitmapInternal(graphics->heroFlagAnimations.at(color->getNum()), anim, group, hero->moveDir, !hero->isStanding);
}
std::shared_ptr<IImage> CMapHandler::CMapBlitter::findBoatFlagBitmap(const CGBoat * boat, int anim, const PlayerColor * color, int group, ui8 dir) const
std::shared_ptr<IImage> CMapBlitter::findBoatFlagBitmap(const CGBoat * boat, int anim, const PlayerColor * color, int group, ui8 dir) const
{
int boatType = boat->subID;
if(boatType < 0 || boatType >= graphics->boatFlagAnimations.size())
@ -1031,7 +1031,7 @@ std::shared_ptr<IImage> CMapHandler::CMapBlitter::findBoatFlagBitmap(const CGBoa
return findFlagBitmapInternal(subtypeFlags.at(colorIndex), anim, group, dir, false);
}
std::shared_ptr<IImage> CMapHandler::CMapBlitter::findFlagBitmapInternal(std::shared_ptr<CAnimation> animation, int anim, int group, ui8 dir, bool moving) const
std::shared_ptr<IImage> CMapBlitter::findFlagBitmapInternal(std::shared_ptr<CAnimation> animation, int anim, int group, ui8 dir, bool moving) const
{
size_t groupSize = animation->size(group);
if(groupSize == 0)
@ -1043,10 +1043,10 @@ std::shared_ptr<IImage> CMapHandler::CMapBlitter::findFlagBitmapInternal(std::sh
return animation->getImage((anim / 4) % groupSize, group);
}
CMapHandler::AnimBitmapHolder CMapHandler::CMapBlitter::findObjectBitmap(const CGObjectInstance * obj, int anim) const
AnimBitmapHolder CMapBlitter::findObjectBitmap(const CGObjectInstance * obj, int anim) const
{
if (!obj)
return CMapHandler::AnimBitmapHolder();
return AnimBitmapHolder();
if (obj->ID == Obj::HERO)
return findHeroBitmap(static_cast<const CGHeroInstance*>(obj), anim);
if (obj->ID == Obj::BOAT)
@ -1056,18 +1056,18 @@ CMapHandler::AnimBitmapHolder CMapHandler::CMapBlitter::findObjectBitmap(const C
std::shared_ptr<CAnimation> animation = graphics->getAnimation(obj);
size_t groupSize = animation->size();
if(groupSize == 0)
return CMapHandler::AnimBitmapHolder();
return AnimBitmapHolder();
auto bitmap = animation->getImage((anim + getPhaseShift(obj)) % groupSize);
if(!bitmap)
return CMapHandler::AnimBitmapHolder();
return AnimBitmapHolder();
bitmap->setFlagColor(obj->tempOwner);
return CMapHandler::AnimBitmapHolder(bitmap);
return AnimBitmapHolder(bitmap);
}
ui8 CMapHandler::CMapBlitter::getPhaseShift(const CGObjectInstance *object) const
ui8 CMapBlitter::getPhaseShift(const CGObjectInstance *object) const
{
auto i = parent->animationPhase.find(object);
if(i == parent->animationPhase.end())
@ -1080,13 +1080,13 @@ ui8 CMapHandler::CMapBlitter::getPhaseShift(const CGObjectInstance *object) cons
return i->second;
}
bool CMapHandler::CMapBlitter::canDrawObject(const CGObjectInstance * obj) const
bool CMapBlitter::canDrawObject(const CGObjectInstance * obj) const
{
//checking if object has non-empty graphic on this tile
return obj->ID == Obj::HERO || obj->coveringAt(pos.x, pos.y);
}
bool CMapHandler::CMapBlitter::canDrawCurrentTile() const
bool CMapBlitter::canDrawCurrentTile() const
{
if(settings["session"]["spectate"].Bool())
return true;
@ -1095,7 +1095,7 @@ bool CMapHandler::CMapBlitter::canDrawCurrentTile() const
return !neighbors.areAllHidden();
}
ui8 CMapHandler::CMapBlitter::getHeroFrameGroup(ui8 dir, bool isMoving) const
ui8 CMapBlitter::getHeroFrameGroup(ui8 dir, bool isMoving) const
{
if(isMoving)
{
@ -1403,26 +1403,26 @@ void CMapHandler::discardWorldViewCache()
cache.discardWorldViewCache();
}
CMapHandler::CMapCache::CMapCache()
CMapCache::CMapCache()
{
worldViewCachedScale = 0;
}
void CMapHandler::CMapCache::discardWorldViewCache()
void CMapCache::discardWorldViewCache()
{
for(auto & cache : data)
cache.clear();
logAnim->debug("Discarded world view cache");
}
void CMapHandler::CMapCache::updateWorldViewScale(float scale)
void CMapCache::updateWorldViewScale(float scale)
{
if (fabs(scale - worldViewCachedScale) > 0.001f)
discardWorldViewCache();
worldViewCachedScale = scale;
}
std::shared_ptr<IImage> CMapHandler::CMapCache::requestWorldViewCacheOrCreate(CMapHandler::EMapCacheType type, std::shared_ptr<IImage> fullSurface)
std::shared_ptr<IImage> CMapCache::requestWorldViewCacheOrCreate(EMapCacheType type, std::shared_ptr<IImage> fullSurface)
{
intptr_t key = (intptr_t) (fullSurface.get());
auto & cache = data[(ui8)type];

View File

@ -37,6 +37,7 @@ struct SDL_Surface;
class CAnimation;
class IImage;
class CFadeAnimation;
class CMapHandler;
enum class EWorldViewIcon
{
@ -161,163 +162,168 @@ private:
int offset;
std::vector<T> inver;
};
enum class EMapCacheType : ui8
{
TERRAIN, OBJECTS, ROADS, RIVERS, FOW, HEROES, HERO_FLAGS, FRAME, AFTER_LAST
};
/// temporarily caches rescaled frames for map world view redrawing
class CMapCache
{
std::array< std::map<intptr_t, std::shared_ptr<IImage>>, (ui8)EMapCacheType::AFTER_LAST> data;
float worldViewCachedScale;
public:
CMapCache();
/// destroys all cached data (frees surfaces)
void discardWorldViewCache();
/// updates scale and determines if currently cached data is still valid
void updateWorldViewScale(float scale);
/// asks for cached data; @returns cached data if found, new scaled surface otherwise, may return nullptr in case of scaling error
std::shared_ptr<IImage> requestWorldViewCacheOrCreate(EMapCacheType type, std::shared_ptr<IImage> fullSurface);
};
/// helper struct to pass around resolved bitmaps of an object; images can be nullptr if object doesn't have bitmap of that type
struct AnimBitmapHolder
{
std::shared_ptr<IImage> objBitmap; // main object bitmap
std::shared_ptr<IImage> flagBitmap; // flag bitmap for the object (probably only for heroes and boats with heroes)
bool isMoving; // indicates if the object is moving (again, heroes/boats only)
AnimBitmapHolder(std::shared_ptr<IImage> objBitmap_ = nullptr, std::shared_ptr<IImage> flagBitmap_ = nullptr, bool moving = false)
: objBitmap(objBitmap_),
flagBitmap(flagBitmap_),
isMoving(moving)
{}
};
class CMapBlitter
{
protected:
const int FRAMES_PER_MOVE_ANIM_GROUP = 8;
CMapHandler * parent; // ptr to enclosing map handler; generally for legacy reasons, probably could/should be refactored out of here
int tileSize; // size of a tile drawn on map [in pixels]
int halfTileSizeCeil; // half of the tile size, rounded up
int3 tileCount; // number of tiles in current viewport
int3 topTile; // top-left tile of the viewport
int3 initPos; // starting drawing position [in pixels]
int3 pos; // current position [in tiles]
int3 realPos; // current position [in pixels]
Rect realTileRect; // default rect based on current pos: [realPos.x, realPos.y, tileSize, tileSize]
Rect defaultTileRect; // default rect based on 0: [0, 0, tileSize, tileSize]
const MapDrawingInfo * info; // data for drawing passed from outside
/// general drawing method, called internally by more specialized ones
virtual void drawElement(EMapCacheType cacheType, std::shared_ptr<IImage> source, Rect * sourceRect, SDL_Surface * targetSurf, Rect * destRect) const = 0;
// first drawing pass
/// draws terrain bitmap (or custom bitmap if applicable) on current tile
virtual void drawTileTerrain(SDL_Surface * targetSurf, const TerrainTile & tinfo, const TerrainTile2 & tile) const;
/// draws a river segment on current tile
virtual void drawRiver(SDL_Surface * targetSurf, const TerrainTile & tinfo) const;
/// draws a road segment on current tile
virtual void drawRoad(SDL_Surface * targetSurf, const TerrainTile & tinfo, const TerrainTile * tinfoUpper) const;
/// draws all objects on current tile (higher-level logic, unlike other draw*** methods)
virtual void drawObjects(SDL_Surface * targetSurf, const TerrainTile2 & tile) const;
virtual void drawObject(SDL_Surface * targetSurf, std::shared_ptr<IImage> source, Rect * sourceRect, bool moving) const;
virtual void drawHeroFlag(SDL_Surface * targetSurf, std::shared_ptr<IImage> source, Rect * sourceRect, Rect * destRect, bool moving) const;
// second drawing pass
/// current tile: draws overlay over the map, used to draw world view icons
virtual void drawTileOverlay(SDL_Surface * targetSurf, const TerrainTile2 & tile) const = 0;
/// draws fog of war on current tile
virtual void drawFow(SDL_Surface * targetSurf) const;
/// draws map border frame on current position
virtual void drawFrame(SDL_Surface * targetSurf) const;
/// draws additional icons (for VIEW_AIR, VIEW_EARTH spells atm)
virtual void drawOverlayEx(SDL_Surface * targetSurf);
// third drawing pass
/// custom post-processing, if needed (used by puzzle view)
virtual void postProcessing(SDL_Surface * targetSurf) const {}
// misc methods
/// initializes frame-drawing (called at the start of every redraw)
virtual void init(const MapDrawingInfo * drawingInfo) = 0;
/// calculates clip region for map viewport
virtual Rect clip(SDL_Surface * targetSurf) const = 0;
virtual ui8 getHeroFrameGroup(ui8 dir, bool isMoving) const;
virtual ui8 getPhaseShift(const CGObjectInstance *object) const;
virtual bool canDrawObject(const CGObjectInstance * obj) const;
virtual bool canDrawCurrentTile() const;
// internal helper methods to choose correct bitmap(s) for object; called internally by findObjectBitmap
AnimBitmapHolder findHeroBitmap(const CGHeroInstance * hero, int anim) const;
AnimBitmapHolder findBoatBitmap(const CGBoat * hero, int anim) const;
std::shared_ptr<IImage> findFlagBitmap(const CGHeroInstance * obj, int anim, const PlayerColor * color, int group) const;
std::shared_ptr<IImage> findHeroFlagBitmap(const CGHeroInstance * obj, int anim, const PlayerColor * color, int group) const;
std::shared_ptr<IImage> findBoatFlagBitmap(const CGBoat * obj, int anim, const PlayerColor * color, int group, ui8 dir) const;
std::shared_ptr<IImage> findFlagBitmapInternal(std::shared_ptr<CAnimation> animation, int anim, int group, ui8 dir, bool moving) const;
public:
CMapBlitter(CMapHandler * p);
virtual ~CMapBlitter();
void blit(SDL_Surface * targetSurf, const MapDrawingInfo * info);
/// helper method that chooses correct bitmap(s) for given object
AnimBitmapHolder findObjectBitmap(const CGObjectInstance * obj, int anim) const;
};
class CMapNormalBlitter : public CMapBlitter
{
protected:
void drawElement(EMapCacheType cacheType, std::shared_ptr<IImage> source, Rect * sourceRect, SDL_Surface * targetSurf, Rect * destRect) const override;
void drawTileOverlay(SDL_Surface * targetSurf,const TerrainTile2 & tile) const override {}
void init(const MapDrawingInfo * info) override;
Rect clip(SDL_Surface * targetSurf) const override;
public:
CMapNormalBlitter(CMapHandler * parent);
virtual ~CMapNormalBlitter(){}
};
class CMapWorldViewBlitter : public CMapBlitter
{
private:
std::shared_ptr<IImage> objectToIcon(Obj id, si32 subId, PlayerColor owner) const;
protected:
void drawElement(EMapCacheType cacheType, std::shared_ptr<IImage> source, Rect * sourceRect, SDL_Surface * targetSurf, Rect * destRect) const override;
void drawTileOverlay(SDL_Surface * targetSurf, const TerrainTile2 & tile) const override;
void drawHeroFlag(SDL_Surface * targetSurf, std::shared_ptr<IImage> source, Rect * sourceRect, Rect * destRect, bool moving) const override;
void drawObject(SDL_Surface * targetSurf, std::shared_ptr<IImage> source, Rect * sourceRect, bool moving) const override;
void drawFrame(SDL_Surface * targetSurf) const override {}
void drawOverlayEx(SDL_Surface * targetSurf) override;
void init(const MapDrawingInfo * info) override;
Rect clip(SDL_Surface * targetSurf) const override;
ui8 getPhaseShift(const CGObjectInstance *object) const override { return 0u; }
void calculateWorldViewCameraPos();
public:
CMapWorldViewBlitter(CMapHandler * parent);
virtual ~CMapWorldViewBlitter(){}
};
class CMapPuzzleViewBlitter : public CMapNormalBlitter
{
std::vector<int> unblittableObjects;
void drawObjects(SDL_Surface * targetSurf, const TerrainTile2 & tile) const override;
void drawFow(SDL_Surface * targetSurf) const override {} // skipping FoW in puzzle view
void postProcessing(SDL_Surface * targetSurf) const override;
bool canDrawObject(const CGObjectInstance * obj) const override;
bool canDrawCurrentTile() const override { return true; }
public:
CMapPuzzleViewBlitter(CMapHandler * parent);
};
class CMapHandler
{
enum class EMapCacheType : ui8
{
TERRAIN, OBJECTS, ROADS, RIVERS, FOW, HEROES, HERO_FLAGS, FRAME, AFTER_LAST
};
/// temporarily caches rescaled frames for map world view redrawing
class CMapCache
{
std::array< std::map<intptr_t, std::shared_ptr<IImage>>, (ui8)EMapCacheType::AFTER_LAST> data;
float worldViewCachedScale;
public:
CMapCache();
/// destroys all cached data (frees surfaces)
void discardWorldViewCache();
/// updates scale and determines if currently cached data is still valid
void updateWorldViewScale(float scale);
/// asks for cached data; @returns cached data if found, new scaled surface otherwise, may return nullptr in case of scaling error
std::shared_ptr<IImage> requestWorldViewCacheOrCreate(EMapCacheType type, std::shared_ptr<IImage> fullSurface);
};
/// helper struct to pass around resolved bitmaps of an object; images can be nullptr if object doesn't have bitmap of that type
struct AnimBitmapHolder
{
std::shared_ptr<IImage> objBitmap; // main object bitmap
std::shared_ptr<IImage> flagBitmap; // flag bitmap for the object (probably only for heroes and boats with heroes)
bool isMoving; // indicates if the object is moving (again, heroes/boats only)
AnimBitmapHolder(std::shared_ptr<IImage> objBitmap_ = nullptr, std::shared_ptr<IImage> flagBitmap_ = nullptr, bool moving = false)
: objBitmap(objBitmap_),
flagBitmap(flagBitmap_),
isMoving(moving)
{}
};
class CMapBlitter
{
protected:
const int FRAMES_PER_MOVE_ANIM_GROUP = 8;
CMapHandler * parent; // ptr to enclosing map handler; generally for legacy reasons, probably could/should be refactored out of here
int tileSize; // size of a tile drawn on map [in pixels]
int halfTileSizeCeil; // half of the tile size, rounded up
int3 tileCount; // number of tiles in current viewport
int3 topTile; // top-left tile of the viewport
int3 initPos; // starting drawing position [in pixels]
int3 pos; // current position [in tiles]
int3 realPos; // current position [in pixels]
Rect realTileRect; // default rect based on current pos: [realPos.x, realPos.y, tileSize, tileSize]
Rect defaultTileRect; // default rect based on 0: [0, 0, tileSize, tileSize]
const MapDrawingInfo * info; // data for drawing passed from outside
/// general drawing method, called internally by more specialized ones
virtual void drawElement(EMapCacheType cacheType, std::shared_ptr<IImage> source, Rect * sourceRect, SDL_Surface * targetSurf, Rect * destRect) const = 0;
// first drawing pass
/// draws terrain bitmap (or custom bitmap if applicable) on current tile
virtual void drawTileTerrain(SDL_Surface * targetSurf, const TerrainTile & tinfo, const TerrainTile2 & tile) const;
/// draws a river segment on current tile
virtual void drawRiver(SDL_Surface * targetSurf, const TerrainTile & tinfo) const;
/// draws a road segment on current tile
virtual void drawRoad(SDL_Surface * targetSurf, const TerrainTile & tinfo, const TerrainTile * tinfoUpper) const;
/// draws all objects on current tile (higher-level logic, unlike other draw*** methods)
virtual void drawObjects(SDL_Surface * targetSurf, const TerrainTile2 & tile) const;
virtual void drawObject(SDL_Surface * targetSurf, std::shared_ptr<IImage> source, Rect * sourceRect, bool moving) const;
virtual void drawHeroFlag(SDL_Surface * targetSurf, std::shared_ptr<IImage> source, Rect * sourceRect, Rect * destRect, bool moving) const;
// second drawing pass
/// current tile: draws overlay over the map, used to draw world view icons
virtual void drawTileOverlay(SDL_Surface * targetSurf, const TerrainTile2 & tile) const = 0;
/// draws fog of war on current tile
virtual void drawFow(SDL_Surface * targetSurf) const;
/// draws map border frame on current position
virtual void drawFrame(SDL_Surface * targetSurf) const;
/// draws additional icons (for VIEW_AIR, VIEW_EARTH spells atm)
virtual void drawOverlayEx(SDL_Surface * targetSurf);
// third drawing pass
/// custom post-processing, if needed (used by puzzle view)
virtual void postProcessing(SDL_Surface * targetSurf) const {}
// misc methods
/// initializes frame-drawing (called at the start of every redraw)
virtual void init(const MapDrawingInfo * drawingInfo) = 0;
/// calculates clip region for map viewport
virtual Rect clip(SDL_Surface * targetSurf) const = 0;
virtual ui8 getHeroFrameGroup(ui8 dir, bool isMoving) const;
virtual ui8 getPhaseShift(const CGObjectInstance *object) const;
virtual bool canDrawObject(const CGObjectInstance * obj) const;
virtual bool canDrawCurrentTile() const;
// internal helper methods to choose correct bitmap(s) for object; called internally by findObjectBitmap
AnimBitmapHolder findHeroBitmap(const CGHeroInstance * hero, int anim) const;
AnimBitmapHolder findBoatBitmap(const CGBoat * hero, int anim) const;
std::shared_ptr<IImage> findFlagBitmap(const CGHeroInstance * obj, int anim, const PlayerColor * color, int group) const;
std::shared_ptr<IImage> findHeroFlagBitmap(const CGHeroInstance * obj, int anim, const PlayerColor * color, int group) const;
std::shared_ptr<IImage> findBoatFlagBitmap(const CGBoat * obj, int anim, const PlayerColor * color, int group, ui8 dir) const;
std::shared_ptr<IImage> findFlagBitmapInternal(std::shared_ptr<CAnimation> animation, int anim, int group, ui8 dir, bool moving) const;
public:
CMapBlitter(CMapHandler * p);
virtual ~CMapBlitter();
void blit(SDL_Surface * targetSurf, const MapDrawingInfo * info);
/// helper method that chooses correct bitmap(s) for given object
AnimBitmapHolder findObjectBitmap(const CGObjectInstance * obj, int anim) const;
};
class CMapNormalBlitter : public CMapBlitter
{
protected:
void drawElement(EMapCacheType cacheType, std::shared_ptr<IImage> source, Rect * sourceRect, SDL_Surface * targetSurf, Rect * destRect) const override;
void drawTileOverlay(SDL_Surface * targetSurf,const TerrainTile2 & tile) const override {}
void init(const MapDrawingInfo * info) override;
Rect clip(SDL_Surface * targetSurf) const override;
public:
CMapNormalBlitter(CMapHandler * parent);
virtual ~CMapNormalBlitter(){}
};
class CMapWorldViewBlitter : public CMapBlitter
{
private:
std::shared_ptr<IImage> objectToIcon(Obj id, si32 subId, PlayerColor owner) const;
protected:
void drawElement(EMapCacheType cacheType, std::shared_ptr<IImage> source, Rect * sourceRect, SDL_Surface * targetSurf, Rect * destRect) const override;
void drawTileOverlay(SDL_Surface * targetSurf, const TerrainTile2 & tile) const override;
void drawHeroFlag(SDL_Surface * targetSurf, std::shared_ptr<IImage> source, Rect * sourceRect, Rect * destRect, bool moving) const override;
void drawObject(SDL_Surface * targetSurf, std::shared_ptr<IImage> source, Rect * sourceRect, bool moving) const override;
void drawFrame(SDL_Surface * targetSurf) const override {}
void drawOverlayEx(SDL_Surface * targetSurf) override;
void init(const MapDrawingInfo * info) override;
Rect clip(SDL_Surface * targetSurf) const override;
ui8 getPhaseShift(const CGObjectInstance *object) const override { return 0u; }
void calculateWorldViewCameraPos();
public:
CMapWorldViewBlitter(CMapHandler * parent);
virtual ~CMapWorldViewBlitter(){}
};
class CMapPuzzleViewBlitter : public CMapNormalBlitter
{
std::vector<int> unblittableObjects;
void drawObjects(SDL_Surface * targetSurf, const TerrainTile2 & tile) const override;
void drawFow(SDL_Surface * targetSurf) const override {} // skipping FoW in puzzle view
void postProcessing(SDL_Surface * targetSurf) const override;
bool canDrawObject(const CGObjectInstance * obj) const override;
bool canDrawCurrentTile() const override { return true; }
public:
CMapPuzzleViewBlitter(CMapHandler * parent);
};
friend class CMapBlitter;
friend class CMapNormalBlitter;
friend class CMapWorldViewBlitter;
CMapCache cache;
CMapBlitter * normalBlitter;
@ -335,7 +341,8 @@ class CMapHandler
void initBorderGraphics();
void initTerrainGraphics();
void prepareFOWDefs();
public:
public: //TODO: make private
boost::multi_array<TerrainTile2, 3> ttiles; //informations about map tiles [z][x][y]
int3 sizes; //map size (x = width, y = height, z = number of levels)
const CMap * map;
@ -356,7 +363,7 @@ public:
int offsetY;
//terrain graphics
private:
//FIXME: unique_ptr should be enough, but fails to compile in MSVS 2013
typedef std::map<std::string, std::array<std::shared_ptr<CAnimation>, 4>> TFlippedAnimations; //[type, rotation]
typedef std::map<std::string, std::vector<std::array<std::shared_ptr<IImage>, 4>>> TFlippedCache;//[type, view type, rotation]
@ -383,6 +390,7 @@ public:
mutable std::map<const CGObjectInstance*, ui8> animationPhase;
public:
CMapHandler();
~CMapHandler();