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