| 1 | /* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */ |
| 2 | /* If you are missing that file, acquire a complete release at teeworlds.com. */ |
| 3 | #include "mapimages.h" |
| 4 | |
| 5 | #include <base/dbg.h> |
| 6 | #include <base/log.h> |
| 7 | #include <base/math.h> |
| 8 | #include <base/mem.h> |
| 9 | |
| 10 | #include <engine/gfx/image_manipulation.h> |
| 11 | #include <engine/graphics.h> |
| 12 | #include <engine/map.h> |
| 13 | #include <engine/storage.h> |
| 14 | #include <engine/textrender.h> |
| 15 | |
| 16 | #include <generated/client_data.h> |
| 17 | |
| 18 | #include <game/client/gameclient.h> |
| 19 | #include <game/layers.h> |
| 20 | #include <game/localization.h> |
| 21 | #include <game/mapitems.h> |
| 22 | |
| 23 | CMapImages::CMapImages() |
| 24 | { |
| 25 | m_Count = 0; |
| 26 | std::fill(first: std::begin(arr&: m_aEntitiesIsLoaded), last: std::end(arr&: m_aEntitiesIsLoaded), value: false); |
| 27 | m_SpeedupArrowIsLoaded = false; |
| 28 | m_TuneColorsIsLoaded = false; |
| 29 | |
| 30 | str_copy(dst&: m_aEntitiesPath, src: "editor/entities_clear" ); |
| 31 | |
| 32 | static_assert(std::size(gs_apModEntitiesNames) == MAP_IMAGE_MOD_TYPE_COUNT, "Mod name string count is not equal to mod type count" ); |
| 33 | } |
| 34 | |
| 35 | void CMapImages::OnInit() |
| 36 | { |
| 37 | m_TextureScale = g_Config.m_ClTextEntitiesSize; |
| 38 | InitOverlayTextures(); |
| 39 | |
| 40 | if(str_comp(a: g_Config.m_ClAssetsEntities, b: "default" ) == 0) |
| 41 | str_copy(dst&: m_aEntitiesPath, src: "editor/entities_clear" ); |
| 42 | else |
| 43 | { |
| 44 | str_format(buffer: m_aEntitiesPath, buffer_size: sizeof(m_aEntitiesPath), format: "assets/entities/%s" , g_Config.m_ClAssetsEntities); |
| 45 | } |
| 46 | |
| 47 | Console()->Chain(pName: "cl_text_entities_size" , pfnChainFunc: ConchainClTextEntitiesSize, pUser: this); |
| 48 | } |
| 49 | |
| 50 | void CMapImages::Unload() |
| 51 | { |
| 52 | // unload all textures |
| 53 | for(int i = 0; i < m_Count; i++) |
| 54 | { |
| 55 | Graphics()->UnloadTexture(pIndex: &m_aTextures[i]); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | void CMapImages::OnMapLoadImpl(class CLayers *pLayers, IMap *pMap) |
| 60 | { |
| 61 | Unload(); |
| 62 | |
| 63 | int Start; |
| 64 | pMap->GetType(Type: MAPITEMTYPE_IMAGE, pStart: &Start, pNum: &m_Count); |
| 65 | m_Count = std::clamp<int>(val: m_Count, lo: 0, hi: MAX_MAPIMAGES); |
| 66 | |
| 67 | unsigned char aTextureUsedByTileOrQuadLayerFlag[MAX_MAPIMAGES] = {0}; // 0: nothing, 1(as flag): tile layer, 2(as flag): quad layer |
| 68 | for(int GroupIndex = 0; GroupIndex < pLayers->NumGroups(); GroupIndex++) |
| 69 | { |
| 70 | const CMapItemGroup *pGroup = pLayers->GetGroup(Index: GroupIndex); |
| 71 | if(!pGroup) |
| 72 | { |
| 73 | continue; |
| 74 | } |
| 75 | |
| 76 | for(int LayerIndex = 0; LayerIndex < pGroup->m_NumLayers; LayerIndex++) |
| 77 | { |
| 78 | const CMapItemLayer *pLayer = pLayers->GetLayer(Index: pGroup->m_StartLayer + LayerIndex); |
| 79 | if(!pLayer) |
| 80 | { |
| 81 | continue; |
| 82 | } |
| 83 | |
| 84 | if(pLayer->m_Type == LAYERTYPE_TILES) |
| 85 | { |
| 86 | const CMapItemLayerTilemap *pLayerTilemap = reinterpret_cast<const CMapItemLayerTilemap *>(pLayer); |
| 87 | if(pLayerTilemap->m_Image >= 0 && pLayerTilemap->m_Image < m_Count) |
| 88 | { |
| 89 | aTextureUsedByTileOrQuadLayerFlag[pLayerTilemap->m_Image] |= 1; |
| 90 | } |
| 91 | } |
| 92 | else if(pLayer->m_Type == LAYERTYPE_QUADS) |
| 93 | { |
| 94 | const CMapItemLayerQuads *pLayerQuads = reinterpret_cast<const CMapItemLayerQuads *>(pLayer); |
| 95 | if(pLayerQuads->m_Image >= 0 && pLayerQuads->m_Image < m_Count) |
| 96 | { |
| 97 | aTextureUsedByTileOrQuadLayerFlag[pLayerQuads->m_Image] |= 2; |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | // load new textures |
| 104 | bool ShowWarning = false; |
| 105 | for(int i = 0; i < m_Count; i++) |
| 106 | { |
| 107 | if(aTextureUsedByTileOrQuadLayerFlag[i] == 0) |
| 108 | { |
| 109 | // skip loading unused images |
| 110 | continue; |
| 111 | } |
| 112 | |
| 113 | const int LoadFlag = (((aTextureUsedByTileOrQuadLayerFlag[i] & 1) != 0) ? Graphics()->TextureLoadFlags() : 0) | (((aTextureUsedByTileOrQuadLayerFlag[i] & 2) != 0) ? 0 : (Graphics()->HasTextureArraysSupport() ? IGraphics::TEXLOAD_NO_2D_TEXTURE : 0)); |
| 114 | const CMapItemImage_v2 *pImg = static_cast<const CMapItemImage_v2 *>(pMap->GetItem(Index: Start + i)); |
| 115 | |
| 116 | const char *pName = pMap->GetDataString(Index: pImg->m_ImageName); |
| 117 | if(pName == nullptr || pName[0] == '\0') |
| 118 | { |
| 119 | if(pImg->m_External) |
| 120 | { |
| 121 | log_error("mapimages" , "Failed to load map image %d: failed to load name." , i); |
| 122 | ShowWarning = true; |
| 123 | continue; |
| 124 | } |
| 125 | pName = "(error)" ; |
| 126 | } |
| 127 | |
| 128 | if(pImg->m_Version > 1 && pImg->m_MustBe1 != 1) |
| 129 | { |
| 130 | log_error("mapimages" , "Failed to load map image %d '%s': invalid map image type." , i, pName); |
| 131 | ShowWarning = true; |
| 132 | continue; |
| 133 | } |
| 134 | |
| 135 | if(pImg->m_External) |
| 136 | { |
| 137 | char aPath[IO_MAX_PATH_LENGTH]; |
| 138 | bool Translated = false; |
| 139 | if(Client()->IsSixup()) |
| 140 | { |
| 141 | Translated = |
| 142 | !str_comp(a: pName, b: "grass_doodads" ) || |
| 143 | !str_comp(a: pName, b: "grass_main" ) || |
| 144 | !str_comp(a: pName, b: "winter_main" ) || |
| 145 | !str_comp(a: pName, b: "generic_shadows" ) || |
| 146 | !str_comp(a: pName, b: "generic_unhookable" ) || |
| 147 | !str_comp(a: pName, b: "easter" ); |
| 148 | } |
| 149 | str_format(buffer: aPath, buffer_size: sizeof(aPath), format: "mapres/%s%s.png" , pName, Translated ? "_0.7" : "" ); |
| 150 | m_aTextures[i] = Graphics()->LoadTexture(pFilename: aPath, StorageType: IStorage::TYPE_ALL, Flags: LoadFlag); |
| 151 | } |
| 152 | else |
| 153 | { |
| 154 | CImageInfo ImageInfo; |
| 155 | ImageInfo.m_Width = pImg->m_Width; |
| 156 | ImageInfo.m_Height = pImg->m_Height; |
| 157 | ImageInfo.m_Format = CImageInfo::FORMAT_RGBA; |
| 158 | ImageInfo.m_pData = static_cast<uint8_t *>(pMap->GetData(Index: pImg->m_ImageData)); |
| 159 | if(ImageInfo.m_pData && (size_t)pMap->GetDataSize(Index: pImg->m_ImageData) >= ImageInfo.DataSize()) |
| 160 | { |
| 161 | char aTexName[IO_MAX_PATH_LENGTH]; |
| 162 | str_format(buffer: aTexName, buffer_size: sizeof(aTexName), format: "embedded: %s" , pName); |
| 163 | m_aTextures[i] = Graphics()->LoadTextureRaw(Image: ImageInfo, Flags: LoadFlag, pTexName: aTexName); |
| 164 | pMap->UnloadData(Index: pImg->m_ImageData); |
| 165 | } |
| 166 | else |
| 167 | { |
| 168 | pMap->UnloadData(Index: pImg->m_ImageData); |
| 169 | log_error("mapimages" , "Failed to load map image %d: failed to load data." , i); |
| 170 | ShowWarning = true; |
| 171 | continue; |
| 172 | } |
| 173 | } |
| 174 | pMap->UnloadData(Index: pImg->m_ImageName); |
| 175 | ShowWarning = ShowWarning || m_aTextures[i].IsNullTexture(); |
| 176 | } |
| 177 | if(ShowWarning) |
| 178 | { |
| 179 | Client()->AddWarning(Warning: SWarning(Localize(pStr: "Some map images could not be loaded. Check the local console for details." ))); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | void CMapImages::OnMapLoad() |
| 184 | { |
| 185 | OnMapLoadImpl(pLayers: GameClient()->Layers(), pMap: GameClient()->Map()); |
| 186 | } |
| 187 | |
| 188 | void CMapImages::LoadBackground(class CLayers *pLayers, class IMap *pMap) |
| 189 | { |
| 190 | OnMapLoadImpl(pLayers, pMap); |
| 191 | } |
| 192 | |
| 193 | static EMapImageModType GetEntitiesModType(const CGameInfo &GameInfo) |
| 194 | { |
| 195 | if(GameInfo.m_EntitiesFDDrace) |
| 196 | return MAP_IMAGE_MOD_TYPE_FDDRACE; |
| 197 | else if(GameInfo.m_EntitiesDDNet) |
| 198 | return MAP_IMAGE_MOD_TYPE_DDNET; |
| 199 | else if(GameInfo.m_EntitiesDDRace) |
| 200 | return MAP_IMAGE_MOD_TYPE_DDRACE; |
| 201 | else if(GameInfo.m_EntitiesRace) |
| 202 | return MAP_IMAGE_MOD_TYPE_RACE; |
| 203 | else if(GameInfo.m_EntitiesBW) |
| 204 | return MAP_IMAGE_MOD_TYPE_BLOCKWORLDS; |
| 205 | else if(GameInfo.m_EntitiesFNG) |
| 206 | return MAP_IMAGE_MOD_TYPE_FNG; |
| 207 | else if(GameInfo.m_EntitiesVanilla) |
| 208 | return MAP_IMAGE_MOD_TYPE_VANILLA; |
| 209 | else |
| 210 | return MAP_IMAGE_MOD_TYPE_DDNET; |
| 211 | } |
| 212 | |
| 213 | static bool IsValidTile(int LayerType, bool EntitiesAreMasked, EMapImageModType EntitiesModType, int TileIndex) |
| 214 | { |
| 215 | if(TileIndex == TILE_AIR) |
| 216 | return false; |
| 217 | if(!EntitiesAreMasked) |
| 218 | return true; |
| 219 | |
| 220 | if(EntitiesModType == MAP_IMAGE_MOD_TYPE_DDNET || EntitiesModType == MAP_IMAGE_MOD_TYPE_DDRACE) |
| 221 | { |
| 222 | if(EntitiesModType == MAP_IMAGE_MOD_TYPE_DDNET || TileIndex != TILE_SPEED_BOOST_OLD) |
| 223 | { |
| 224 | if(LayerType == MAP_IMAGE_ENTITY_LAYER_TYPE_ALL_EXCEPT_SWITCH && |
| 225 | !IsValidGameTile(Index: TileIndex) && |
| 226 | !IsValidFrontTile(Index: TileIndex) && |
| 227 | !IsValidSpeedupTile(Index: TileIndex) && |
| 228 | !IsValidTeleTile(Index: TileIndex) && |
| 229 | !IsValidTuneTile(Index: TileIndex)) |
| 230 | { |
| 231 | return false; |
| 232 | } |
| 233 | else if(LayerType == MAP_IMAGE_ENTITY_LAYER_TYPE_SWITCH && |
| 234 | !IsValidSwitchTile(Index: TileIndex)) |
| 235 | { |
| 236 | return false; |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | else if(EntitiesModType == MAP_IMAGE_MOD_TYPE_RACE && IsCreditsTile(TileIndex)) |
| 241 | { |
| 242 | return false; |
| 243 | } |
| 244 | else if(EntitiesModType == MAP_IMAGE_MOD_TYPE_FNG && IsCreditsTile(TileIndex)) |
| 245 | { |
| 246 | return false; |
| 247 | } |
| 248 | else if(EntitiesModType == MAP_IMAGE_MOD_TYPE_VANILLA && IsCreditsTile(TileIndex)) |
| 249 | { |
| 250 | return false; |
| 251 | } |
| 252 | return true; |
| 253 | } |
| 254 | |
| 255 | IGraphics::CTextureHandle CMapImages::GetEntities(EMapImageEntityLayerType EntityLayerType) |
| 256 | { |
| 257 | const bool EntitiesAreMasked = !GameClient()->m_GameInfo.m_DontMaskEntities; |
| 258 | const EMapImageModType EntitiesModType = GetEntitiesModType(GameInfo: GameClient()->m_GameInfo); |
| 259 | |
| 260 | if(!m_aEntitiesIsLoaded[(EntitiesModType * 2) + (int)EntitiesAreMasked]) |
| 261 | { |
| 262 | m_aEntitiesIsLoaded[(EntitiesModType * 2) + (int)EntitiesAreMasked] = true; |
| 263 | |
| 264 | int TextureLoadFlag = 0; |
| 265 | if(Graphics()->HasTextureArraysSupport()) |
| 266 | TextureLoadFlag = Graphics()->TextureLoadFlags() | IGraphics::TEXLOAD_NO_2D_TEXTURE; |
| 267 | |
| 268 | CImageInfo ImgInfo; |
| 269 | char aPath[IO_MAX_PATH_LENGTH]; |
| 270 | str_format(buffer: aPath, buffer_size: sizeof(aPath), format: "%s/%s.png" , m_aEntitiesPath, gs_apModEntitiesNames[EntitiesModType]); |
| 271 | Graphics()->LoadPng(Image&: ImgInfo, pFilename: aPath, StorageType: IStorage::TYPE_ALL); |
| 272 | |
| 273 | // try as single ddnet replacement |
| 274 | if(ImgInfo.m_pData == nullptr && EntitiesModType == MAP_IMAGE_MOD_TYPE_DDNET) |
| 275 | { |
| 276 | str_format(buffer: aPath, buffer_size: sizeof(aPath), format: "%s.png" , m_aEntitiesPath); |
| 277 | Graphics()->LoadPng(Image&: ImgInfo, pFilename: aPath, StorageType: IStorage::TYPE_ALL); |
| 278 | } |
| 279 | |
| 280 | // try default |
| 281 | if(ImgInfo.m_pData == nullptr) |
| 282 | { |
| 283 | str_format(buffer: aPath, buffer_size: sizeof(aPath), format: "editor/entities_clear/%s.png" , gs_apModEntitiesNames[EntitiesModType]); |
| 284 | Graphics()->LoadPng(Image&: ImgInfo, pFilename: aPath, StorageType: IStorage::TYPE_ALL); |
| 285 | } |
| 286 | |
| 287 | if(ImgInfo.m_pData != nullptr) |
| 288 | { |
| 289 | CImageInfo BuildImageInfo; |
| 290 | BuildImageInfo.m_Width = ImgInfo.m_Width; |
| 291 | BuildImageInfo.m_Height = ImgInfo.m_Height; |
| 292 | BuildImageInfo.m_Format = ImgInfo.m_Format; |
| 293 | BuildImageInfo.Allocate(); // allocate already transparent image |
| 294 | |
| 295 | // convert tune tile to gray |
| 296 | const size_t CopyWidth = ImgInfo.m_Width / 16; |
| 297 | const size_t CopyHeight = ImgInfo.m_Height / 16; |
| 298 | const size_t TuneTileX = static_cast<size_t>(TILE_TUNE % 16) * CopyWidth; |
| 299 | const size_t TuneTileY = static_cast<size_t>(TILE_TUNE / 16) * CopyHeight; |
| 300 | |
| 301 | ConvertToGrayscaleRect(Image: ImgInfo, StartX: TuneTileX, StartY: TuneTileY, Width: CopyWidth, Height: CopyHeight); |
| 302 | |
| 303 | // build game layer |
| 304 | for(int LayerType = 0; LayerType < MAP_IMAGE_ENTITY_LAYER_TYPE_COUNT; ++LayerType) |
| 305 | { |
| 306 | dbg_assert(!m_aaEntitiesTextures[(EntitiesModType * 2) + (int)EntitiesAreMasked][LayerType].IsValid(), "entities texture already loaded when it should not be" ); |
| 307 | |
| 308 | // set everything transparent |
| 309 | mem_zero(block: BuildImageInfo.m_pData, size: BuildImageInfo.DataSize()); |
| 310 | |
| 311 | for(int i = 0; i < 256; ++i) |
| 312 | { |
| 313 | int TileIndex = i; |
| 314 | if(IsValidTile(LayerType, EntitiesAreMasked, EntitiesModType, TileIndex)) |
| 315 | { |
| 316 | if(LayerType == MAP_IMAGE_ENTITY_LAYER_TYPE_SWITCH && TileIndex == TILE_SWITCHTIMEDOPEN) |
| 317 | { |
| 318 | TileIndex = 8; |
| 319 | } |
| 320 | |
| 321 | const size_t OffsetX = (size_t)(TileIndex % 16) * CopyWidth; |
| 322 | const size_t OffsetY = (size_t)(TileIndex / 16) * CopyHeight; |
| 323 | BuildImageInfo.CopyRectFrom(SrcImage: ImgInfo, SrcX: OffsetX, SrcY: OffsetY, Width: CopyWidth, Height: CopyHeight, DestX: OffsetX, DestY: OffsetY); |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | m_aaEntitiesTextures[(EntitiesModType * 2) + (int)EntitiesAreMasked][LayerType] = Graphics()->LoadTextureRaw(Image: BuildImageInfo, Flags: TextureLoadFlag, pTexName: aPath); |
| 328 | } |
| 329 | |
| 330 | BuildImageInfo.Free(); |
| 331 | |
| 332 | // build tune map from the tune tile |
| 333 | if(Graphics()->HasTextureArraysSupport()) |
| 334 | { |
| 335 | CImageInfo TuneMapInfo; |
| 336 | TuneMapInfo.m_Width = ImgInfo.m_Width; |
| 337 | TuneMapInfo.m_Height = ImgInfo.m_Height; |
| 338 | TuneMapInfo.m_Format = ImgInfo.m_Format; |
| 339 | TuneMapInfo.AllocateFillZero(); |
| 340 | |
| 341 | for(int TileIndex = 1; TileIndex < 256; ++TileIndex) |
| 342 | { |
| 343 | size_t StartX = CopyWidth * (TileIndex % 16); |
| 344 | size_t StartY = CopyHeight * (TileIndex / 16); |
| 345 | TuneMapInfo.CopyRectFrom(SrcImage: ImgInfo, SrcX: TuneTileX, SrcY: TuneTileY, Width: CopyWidth, Height: CopyHeight, DestX: StartX, DestY: StartY); |
| 346 | float Hue = std::fmod(x: (TileIndex - 1) * normalized_golden_angle, y: 1.0f); |
| 347 | ColorizeWithHueRect(Image&: TuneMapInfo, Hue, Sat: 0.75f, StartX, StartY, Width: CopyWidth, Height: CopyHeight); |
| 348 | } |
| 349 | m_TuneColorMapTexture = Graphics()->LoadTextureRawMove(Image&: TuneMapInfo, Flags: TextureLoadFlag); |
| 350 | m_TuneColorsIsLoaded = true; |
| 351 | } |
| 352 | |
| 353 | ImgInfo.Free(); |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | return m_aaEntitiesTextures[(EntitiesModType * 2) + (int)EntitiesAreMasked][EntityLayerType]; |
| 358 | } |
| 359 | |
| 360 | IGraphics::CTextureHandle CMapImages::GetSpeedupArrow() |
| 361 | { |
| 362 | if(!m_SpeedupArrowIsLoaded) |
| 363 | { |
| 364 | int TextureLoadFlag = Graphics()->TextureLoadFlags() | IGraphics::TEXLOAD_NO_2D_TEXTURE; |
| 365 | m_SpeedupArrowTexture = Graphics()->LoadTexture(pFilename: "editor/speed_arrow_array.png" , StorageType: IStorage::TYPE_ALL, Flags: TextureLoadFlag); |
| 366 | m_SpeedupArrowIsLoaded = true; |
| 367 | } |
| 368 | return m_SpeedupArrowTexture; |
| 369 | } |
| 370 | |
| 371 | IGraphics::CTextureHandle CMapImages::GetTuneColors() |
| 372 | { |
| 373 | if(Graphics()->HasTextureArraysSupport()) |
| 374 | { |
| 375 | if(!m_TuneColorsIsLoaded) |
| 376 | { |
| 377 | // load entities, this also loads the tune map |
| 378 | GetEntities(EntityLayerType: EMapImageEntityLayerType::MAP_IMAGE_ENTITY_LAYER_TYPE_ALL_EXCEPT_SWITCH); |
| 379 | dbg_assert(m_TuneColorsIsLoaded, "Entities did not load the tune color map" ); |
| 380 | } |
| 381 | return m_TuneColorMapTexture; |
| 382 | } |
| 383 | else |
| 384 | { |
| 385 | return GetEntities(EntityLayerType: MAP_IMAGE_ENTITY_LAYER_TYPE_ALL_EXCEPT_SWITCH); |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | IGraphics::CTextureHandle CMapImages::GetOverlayBottom() |
| 390 | { |
| 391 | return m_OverlayBottomTexture; |
| 392 | } |
| 393 | |
| 394 | IGraphics::CTextureHandle CMapImages::GetOverlayTop() |
| 395 | { |
| 396 | return m_OverlayTopTexture; |
| 397 | } |
| 398 | |
| 399 | IGraphics::CTextureHandle CMapImages::GetOverlayCenter() |
| 400 | { |
| 401 | return m_OverlayCenterTexture; |
| 402 | } |
| 403 | |
| 404 | void CMapImages::ChangeEntitiesPath(const char *pPath) |
| 405 | { |
| 406 | if(str_comp(a: pPath, b: "default" ) == 0) |
| 407 | str_copy(dst&: m_aEntitiesPath, src: "editor/entities_clear" ); |
| 408 | else |
| 409 | { |
| 410 | str_format(buffer: m_aEntitiesPath, buffer_size: sizeof(m_aEntitiesPath), format: "assets/entities/%s" , pPath); |
| 411 | } |
| 412 | |
| 413 | for(int ModType = 0; ModType < MAP_IMAGE_MOD_TYPE_COUNT * 2; ++ModType) |
| 414 | { |
| 415 | if(m_aEntitiesIsLoaded[ModType]) |
| 416 | { |
| 417 | for(int LayerType = 0; LayerType < MAP_IMAGE_ENTITY_LAYER_TYPE_COUNT; ++LayerType) |
| 418 | { |
| 419 | Graphics()->UnloadTexture(pIndex: &m_aaEntitiesTextures[ModType][LayerType]); |
| 420 | } |
| 421 | m_aEntitiesIsLoaded[ModType] = false; |
| 422 | } |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | void CMapImages::ConchainClTextEntitiesSize(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData) |
| 427 | { |
| 428 | pfnCallback(pResult, pCallbackUserData); |
| 429 | if(pResult->NumArguments()) |
| 430 | { |
| 431 | CMapImages *pThis = static_cast<CMapImages *>(pUserData); |
| 432 | pThis->SetTextureScale(g_Config.m_ClTextEntitiesSize); |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | void CMapImages::SetTextureScale(int Scale) |
| 437 | { |
| 438 | if(m_TextureScale == Scale) |
| 439 | return; |
| 440 | |
| 441 | m_TextureScale = Scale; |
| 442 | |
| 443 | if(Graphics() && m_OverlayCenterTexture.IsValid()) // check if component was initialized |
| 444 | { |
| 445 | // reinitialize component |
| 446 | Graphics()->UnloadTexture(pIndex: &m_OverlayBottomTexture); |
| 447 | Graphics()->UnloadTexture(pIndex: &m_OverlayTopTexture); |
| 448 | Graphics()->UnloadTexture(pIndex: &m_OverlayCenterTexture); |
| 449 | |
| 450 | InitOverlayTextures(); |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | int CMapImages::GetTextureScale() const |
| 455 | { |
| 456 | return m_TextureScale; |
| 457 | } |
| 458 | |
| 459 | IGraphics::CTextureHandle CMapImages::UploadEntityLayerText(int TextureSize, int MaxWidth, int YOffset) |
| 460 | { |
| 461 | CImageInfo TextImage; |
| 462 | TextImage.m_Width = 1024; |
| 463 | TextImage.m_Height = 1024; |
| 464 | TextImage.m_Format = CImageInfo::FORMAT_RGBA; |
| 465 | TextImage.AllocateFillZero(); |
| 466 | |
| 467 | UpdateEntityLayerText(TextImage, TextureSize, MaxWidth, YOffset, NumbersPower: 0); |
| 468 | UpdateEntityLayerText(TextImage, TextureSize, MaxWidth, YOffset, NumbersPower: 1); |
| 469 | UpdateEntityLayerText(TextImage, TextureSize, MaxWidth, YOffset, NumbersPower: 2, MaxNumber: 255); |
| 470 | |
| 471 | const int TextureLoadFlag = Graphics()->TextureLoadFlags() | IGraphics::TEXLOAD_NO_2D_TEXTURE; |
| 472 | return Graphics()->LoadTextureRawMove(Image&: TextImage, Flags: TextureLoadFlag); |
| 473 | } |
| 474 | |
| 475 | void CMapImages::UpdateEntityLayerText(CImageInfo &TextImage, int TextureSize, int MaxWidth, int YOffset, int NumbersPower, int MaxNumber) |
| 476 | { |
| 477 | char aBuf[4]; |
| 478 | int DigitsCount = NumbersPower + 1; |
| 479 | |
| 480 | int CurrentNumber = std::pow(x: 10, y: NumbersPower); |
| 481 | |
| 482 | if(MaxNumber == -1) |
| 483 | MaxNumber = CurrentNumber * 10 - 1; |
| 484 | |
| 485 | str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "%d" , CurrentNumber); |
| 486 | |
| 487 | int CurrentNumberSuitableFontSize = TextRender()->AdjustFontSize(pText: aBuf, TextLength: DigitsCount, MaxSize: TextureSize, MaxWidth); |
| 488 | int UniversalSuitableFontSize = CurrentNumberSuitableFontSize * 0.92f; // should be smoothed enough to fit any digits combination |
| 489 | |
| 490 | YOffset += ((TextureSize - UniversalSuitableFontSize) / 2); |
| 491 | |
| 492 | for(; CurrentNumber <= MaxNumber; ++CurrentNumber) |
| 493 | { |
| 494 | str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "%d" , CurrentNumber); |
| 495 | |
| 496 | float x = (CurrentNumber % 16) * 64; |
| 497 | float y = (CurrentNumber / 16) * 64; |
| 498 | |
| 499 | int ApproximateTextWidth = TextRender()->CalculateTextWidth(pText: aBuf, TextLength: DigitsCount, FontWidth: 0, FontSize: UniversalSuitableFontSize); |
| 500 | int XOffSet = (MaxWidth - std::clamp(val: ApproximateTextWidth, lo: 0, hi: MaxWidth)) / 2; |
| 501 | |
| 502 | TextRender()->UploadEntityLayerText(TextImage, TexSubWidth: (TextImage.m_Width / 16) - XOffSet, TexSubHeight: (TextImage.m_Height / 16) - YOffset, pText: aBuf, Length: DigitsCount, x: x + XOffSet, y: y + YOffset, FontSize: UniversalSuitableFontSize); |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | void CMapImages::InitOverlayTextures() |
| 507 | { |
| 508 | int TextureSize = 64 * m_TextureScale / 100; |
| 509 | TextureSize = std::clamp(val: TextureSize, lo: 2, hi: 64); |
| 510 | int TextureToVerticalCenterOffset = (64 - TextureSize) / 2 + TextureSize * 0.1f; // should be used to move texture to the center of 64 pixels area |
| 511 | |
| 512 | if(!m_OverlayBottomTexture.IsValid()) |
| 513 | { |
| 514 | m_OverlayBottomTexture = UploadEntityLayerText(TextureSize: TextureSize / 2, MaxWidth: 64, YOffset: 32 + TextureToVerticalCenterOffset / 2); |
| 515 | } |
| 516 | |
| 517 | if(!m_OverlayTopTexture.IsValid()) |
| 518 | { |
| 519 | m_OverlayTopTexture = UploadEntityLayerText(TextureSize: TextureSize / 2, MaxWidth: 64, YOffset: TextureToVerticalCenterOffset / 2); |
| 520 | } |
| 521 | |
| 522 | if(!m_OverlayCenterTexture.IsValid()) |
| 523 | { |
| 524 | m_OverlayCenterTexture = UploadEntityLayerText(TextureSize, MaxWidth: 64, YOffset: TextureToVerticalCenterOffset); |
| 525 | } |
| 526 | } |
| 527 | |