From 6a272d8f0cb0888e8987a330ddbe77fcc00011c0 Mon Sep 17 00:00:00 2001 From: AlexVinS Date: Sun, 6 Nov 2016 00:31:56 +0300 Subject: [PATCH] FoW drawing converted to CAnimation --- client/Graphics.cpp | 60 ++++++++++----------------- client/Graphics.h | 7 ++-- client/gui/CAnimation.cpp | 8 ++-- client/gui/SDL_Extensions.cpp | 6 +-- client/gui/SDL_Extensions.h | 10 ++--- client/mapHandler.cpp | 77 ++++++++++++----------------------- client/mapHandler.h | 6 +-- 7 files changed, 67 insertions(+), 107 deletions(-) diff --git a/client/Graphics.cpp b/client/Graphics.cpp index f22e6b371..750f92586 100644 --- a/client/Graphics.cpp +++ b/client/Graphics.cpp @@ -130,14 +130,6 @@ Graphics::Graphics() { #if 0 - #define GET_DATA(TYPE,DESTINATION,FUNCTION_TO_GET) \ - (std::bind(&setData,&DESTINATION,FUNCTION_TO_GET)) - - #define GET_DEF_ESS(DESTINATION, DEF_NAME) \ - (GET_DATA \ - (CDefEssential*,DESTINATION,\ - std::function(std::bind(CDefHandler::giveDefEss,DEF_NAME)))) - std::vector tasks; //preparing list of graphics to load tasks += std::bind(&Graphics::loadFonts,this); tasks += std::bind(&Graphics::loadPaletteAndColors,this); @@ -165,6 +157,7 @@ void Graphics::load() loadHeroAnimations(); loadHeroFlagAnimations(); + loadFogOfWar(); } void Graphics::loadHeroAnimations() @@ -274,37 +267,6 @@ std::shared_ptr Graphics::loadHeroAnimation(const std::string &name) } } - //todo: apply following commented out optimizations or merge with loadHeroFlagAnimation - -// int pom = 0; //how many groups has been rotated -// for(int o = 7; pom < 6; ++o) -// { -// for(int p = 0; p<6; p++) -// { -// IImage frame = anim->getImage() -// if(anim->ourImages[o].groupNumber == rotations[p].first) -// { -// for(int e=0; e<8; ++e) -// { -// Cimage nci; -// nci.bitmap = CSDL_Ext::verticalFlip(anim->ourImages[o+e].bitmap); -// nci.groupNumber = rotations[p].second; -// nci.imName = std::string(); -// anim->ourImages.push_back(nci); -// if(pom>2) //we need only one frame for groups 13/14/15 -// break; -// } -// if(pom<3) //there are eight frames of animtion of groups 6/7/8 so for speed we'll skip them -// o+=8; -// else //there is only one frame of 1/2/3 -// o+=1; -// ++pom; -// if(p==2 && pom<4) //group1 starts at index 1 -// o = 1; -// } -// } -// } - return anim; } @@ -338,6 +300,26 @@ void Graphics::blueToPlayersAdv(SDL_Surface * sur, PlayerColor player) } } +void Graphics::loadFogOfWar() +{ + fogOfWarFullHide = std::make_shared("TSHRC"); + fogOfWarFullHide->preload(); + fogOfWarPartialHide = std::make_shared("TSHRE"); + fogOfWarPartialHide->preload(); + + static const int rotations [] = {22, 15, 2, 13, 12, 16, 28, 17, 20, 19, 7, 24, 26, 25, 30, 32, 27}; + + size_t size = fogOfWarPartialHide->size(0);//group size after next rotation + + for(const int rotation : rotations) + { + fogOfWarPartialHide->duplicateImage(0, rotation, 0); + IImage * image = fogOfWarPartialHide->getImage(size, 0); + image->verticalFlip(); + size++; + } +} + void Graphics::loadFonts() { const JsonNode config(ResourceID("config/fonts.json")); diff --git a/client/Graphics.h b/client/Graphics.h index f1ce015f9..4683c960d 100644 --- a/client/Graphics.h +++ b/client/Graphics.h @@ -15,7 +15,6 @@ * */ -class CDefEssential; struct SDL_Surface; class CGHeroInstance; class CGTownInstance; @@ -51,7 +50,7 @@ class Graphics std::shared_ptr loadHeroFlagAnimation(const std::string &name); void loadErmuToPicture(); - + void loadFogOfWar(); void loadFonts(); void initializeImageLists(); @@ -80,8 +79,8 @@ public: //all other objects (not hero or boat) std::map< std::string, std::shared_ptr > mapObjectAnimations; - CDefHandler * FoWfullHide; //for Fog of War - CDefHandler * FoWpartialHide; //for For of War + std::shared_ptr fogOfWarFullHide; + std::shared_ptr fogOfWarPartialHide; std::map imageLists; diff --git a/client/gui/CAnimation.cpp b/client/gui/CAnimation.cpp index 77a225c05..55038207c 100644 --- a/client/gui/CAnimation.cpp +++ b/client/gui/CAnimation.cpp @@ -341,7 +341,7 @@ void CDefFile::loadFrame(size_t frame, size_t group, ImageLoader &loader) const //pixel data is not compressed, copy data to surface for (ui32 i=0; iw, dst->h); } - SDL_BlitSurface(src, srcRect, dst, &betterDst); + SDL_UpperBlit(src, srcRect, dst, &betterDst); } } diff --git a/client/gui/SDL_Extensions.h b/client/gui/SDL_Extensions.h index 0f9a5e7d4..9fabf525b 100644 --- a/client/gui/SDL_Extensions.h +++ b/client/gui/SDL_Extensions.h @@ -7,7 +7,7 @@ * Full text of license available in license.txt file, in main folder * */ - + #pragma once #include #include @@ -88,7 +88,7 @@ public: /** green color used for in-game console */ static const SDL_Color GREEN; - + /** default key color for all 8 & 24 bit graphics */ static const SDL_Color DEFAULT_KEY_COLOR; }; @@ -167,7 +167,7 @@ namespace CSDL_Ext } }; - void blitSurface(SDL_Surface * src, SDL_Rect * srcRect, SDL_Surface * dst, SDL_Rect * dstRect); + void blitSurface(SDL_Surface * src, const SDL_Rect * srcRect, SDL_Surface * dst, SDL_Rect * dstRect); void fillRect(SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color); void fillRectBlack(SDL_Surface * dst, SDL_Rect * dstrect); //fill dest image with source texture. @@ -228,10 +228,10 @@ namespace CSDL_Ext template void applyEffectBpp( SDL_Surface * surf, const SDL_Rect * rect, int mode ); void applyEffect(SDL_Surface * surf, const SDL_Rect * rect, int mode); //mode: 0 - sepia, 1 - grayscale - + void startTextInput(SDL_Rect * where); void stopTextInput(); - + void setColorKey(SDL_Surface * surface, SDL_Color color); ///set key-color to 0,255,255 void setDefaultColorKey(SDL_Surface * surface); diff --git a/client/mapHandler.cpp b/client/mapHandler.cpp index 088be9e23..179076e81 100644 --- a/client/mapHandler.cpp +++ b/client/mapHandler.cpp @@ -146,26 +146,11 @@ struct NeighborTilesInfo void CMapHandler::prepareFOWDefs() { - graphics->FoWfullHide = CDefHandler::giveDef("TSHRC.DEF"); - graphics->FoWpartialHide = CDefHandler::giveDef("TSHRE.DEF"); - - //adding necessary rotations - static const int missRot [] = {22, 15, 2, 13, 12, 16, 28, 17, 20, 19, 7, 24, 26, 25, 30, 32, 27}; - - Cimage nw; - for(auto & elem : missRot) - { - nw = graphics->FoWpartialHide->ourImages[elem]; - nw.bitmap = CSDL_Ext::verticalFlip(nw.bitmap); - graphics->FoWpartialHide->ourImages.push_back(nw); - } - //necessaary rotations added - - //alpha - transformation - for(auto & elem : graphics->FoWpartialHide->ourImages) - { - CSDL_Ext::alphaTransform(elem.bitmap); - } + //assume all frames in group 0 + size_t size = graphics->fogOfWarFullHide->size(0); + FoWfullHide.resize(size); + for(size_t frame = 0; frame < size; frame++) + FoWfullHide[frame] = graphics->fogOfWarFullHide->getImage(frame); //initialization of type of full-hide image hideBitmap.resize(sizes.x); @@ -180,10 +165,17 @@ void CMapHandler::prepareFOWDefs() elem[j].resize(sizes.z); for(int k = 0; k < sizes.z; ++k) { - elem[j][k] = CRandomGenerator::getDefault().nextInt(graphics->FoWfullHide->ourImages.size() - 1); + elem[j][k] = CRandomGenerator::getDefault().nextInt(size - 1); } } } + + size = graphics->fogOfWarPartialHide->size(0); + FoWpartialHide.resize(size); + for(size_t frame = 0; frame < size; frame++) + FoWpartialHide[frame] = graphics->fogOfWarPartialHide->getImage(frame); + + } EMapAnimRedrawStatus CMapHandler::drawTerrainRectNew(SDL_Surface * targetSurface, const MapDrawingInfo * info, bool redrawOnlyAnim /* = false */) @@ -903,9 +895,21 @@ void CMapHandler::CMapBlitter::drawRiver(SDL_Surface * targetSurf, const Terrain void CMapHandler::CMapBlitter::drawFow(SDL_Surface * targetSurf) const { + const NeighborTilesInfo neighborInfo(pos, parent->sizes, *info->visibilityMap); + + int retBitmapID = neighborInfo.getBitmapID();// >=0 -> partial hide, <0 - full hide + if (retBitmapID < 0) + retBitmapID = - parent->hideBitmap[pos.x][pos.y][pos.z] - 1; //fully hidden + + const IImage * image = nullptr; + + if (retBitmapID >= 0) + image = parent->FoWpartialHide.at(retBitmapID); + else + image = parent->FoWfullHide.at(-retBitmapID - 1); + Rect destRect(realTileRect); - std::pair hide = getVisBitmap(); - drawElement(EMapCacheType::FOW, hide.first, nullptr, targetSurf, &destRect, hide.second); + drawElement(EMapCacheType::FOW, image, nullptr, targetSurf, &destRect); } void CMapHandler::CMapBlitter::blit(SDL_Surface * targetSurf, const MapDrawingInfo * info) @@ -1175,26 +1179,6 @@ ui8 CMapHandler::CMapBlitter::getHeroFrameGroup(ui8 dir, bool isMoving) const } } -std::pair CMapHandler::CMapBlitter::getVisBitmap() const -{ - const NeighborTilesInfo neighborInfo(pos, parent->sizes, *info->visibilityMap); - - int retBitmapID = neighborInfo.getBitmapID();// >=0 -> partial hide, <0 - full hide - if (retBitmapID < 0) - { - retBitmapID = - parent->hideBitmap[pos.x][pos.y][pos.z] - 1; //fully hidden - } - - if (retBitmapID >= 0) - { - return std::make_pair(graphics->FoWpartialHide->ourImages[retBitmapID].bitmap, true); - } - else - { - return std::make_pair(graphics->FoWfullHide->ourImages[-retBitmapID - 1].bitmap, false); - } -} - bool CMapHandler::updateObjectsFade() { for (auto iter = fadeAnims.begin(); iter != fadeAnims.end(); ) @@ -1512,11 +1496,6 @@ void CMapHandler::updateWater() //shift colors in palettes of water tiles CMapHandler::~CMapHandler() { - delete graphics->FoWfullHide; - delete graphics->FoWpartialHide; -// if (fadingOffscreenBitmapSurface) -// delete fadingOffscreenBitmapSurface; - delete normalBlitter; delete worldViewBlitter; delete puzzleViewBlitter; @@ -1543,8 +1522,6 @@ CMapHandler::~CMapHandler() CMapHandler::CMapHandler() { frameW = frameH = 0; - graphics->FoWfullHide = nullptr; - graphics->FoWpartialHide = nullptr; normalBlitter = new CMapNormalBlitter(this); worldViewBlitter = new CMapWorldViewBlitter(this); puzzleViewBlitter = new CMapPuzzleViewBlitter(this); diff --git a/client/mapHandler.h b/client/mapHandler.h index 016781606..f284be3cc 100644 --- a/client/mapHandler.h +++ b/client/mapHandler.h @@ -267,8 +267,6 @@ class CMapHandler virtual SDL_Rect clip(SDL_Surface * targetSurf) const = 0; virtual ui8 getHeroFrameGroup(ui8 dir, bool isMoving) const; - ///returns appropriate bitmap and info if alpha blitting is necessary - virtual std::pair getVisBitmap() const; virtual ui8 getPhaseShift(const CGObjectInstance *object) const; virtual bool canDrawObject(const CGObjectInstance * obj) const; @@ -378,7 +376,9 @@ public: int offsetX; int offsetY; - //std::set usedHeroes; + //Fog of War cache (not owned) + std::vector FoWfullHide; + std::vector FoWpartialHide; std::vector > terrainGraphics; // [terrain id] [view type] [rotation type] std::vector roadDefs;