| 1 | #include "background.h" |
| 2 | |
| 3 | #include <base/system.h> |
| 4 | |
| 5 | #include <engine/map.h> |
| 6 | #include <engine/shared/config.h> |
| 7 | |
| 8 | #include <game/client/components/mapimages.h> |
| 9 | #include <game/client/components/maplayers.h> |
| 10 | #include <game/client/gameclient.h> |
| 11 | #include <game/layers.h> |
| 12 | #include <game/localization.h> |
| 13 | |
| 14 | CBackground::CBackground(ERenderType MapType, bool OnlineOnly) : |
| 15 | CMapLayers(MapType, OnlineOnly) |
| 16 | { |
| 17 | m_pLayers = new CLayers; |
| 18 | m_pBackgroundLayers = m_pLayers; |
| 19 | m_pImages = new CMapImages; |
| 20 | m_pBackgroundImages = m_pImages; |
| 21 | m_Loaded = false; |
| 22 | m_aMapName[0] = '\0'; |
| 23 | } |
| 24 | |
| 25 | CBackground::~CBackground() |
| 26 | { |
| 27 | delete m_pBackgroundLayers; |
| 28 | delete m_pBackgroundImages; |
| 29 | } |
| 30 | |
| 31 | CBackgroundEngineMap *CBackground::CreateBGMap() |
| 32 | { |
| 33 | return new CBackgroundEngineMap; |
| 34 | } |
| 35 | |
| 36 | void CBackground::OnInit() |
| 37 | { |
| 38 | m_pBackgroundMap = CreateBGMap(); |
| 39 | m_pMap = m_pBackgroundMap; |
| 40 | |
| 41 | m_pImages->OnInterfacesInit(pClient: GameClient()); |
| 42 | Kernel()->RegisterInterface(pInterface: m_pBackgroundMap); |
| 43 | if(g_Config.m_ClBackgroundEntities[0] != '\0' && str_comp(a: g_Config.m_ClBackgroundEntities, CURRENT_MAP)) |
| 44 | LoadBackground(); |
| 45 | } |
| 46 | |
| 47 | void CBackground::LoadBackground() |
| 48 | { |
| 49 | if(m_Loaded && m_pMap == m_pBackgroundMap) |
| 50 | m_pMap->Unload(); |
| 51 | |
| 52 | m_Loaded = false; |
| 53 | m_pMap = m_pBackgroundMap; |
| 54 | m_pLayers = m_pBackgroundLayers; |
| 55 | m_pImages = m_pBackgroundImages; |
| 56 | |
| 57 | str_copy(dst&: m_aMapName, src: g_Config.m_ClBackgroundEntities); |
| 58 | if(g_Config.m_ClBackgroundEntities[0] != '\0') |
| 59 | { |
| 60 | bool NeedImageLoading = false; |
| 61 | |
| 62 | char aBuf[IO_MAX_PATH_LENGTH]; |
| 63 | str_format(buffer: aBuf, buffer_size: sizeof(aBuf), format: "maps/%s%s" , g_Config.m_ClBackgroundEntities, str_endswith(str: g_Config.m_ClBackgroundEntities, suffix: ".map" ) ? "" : ".map" ); |
| 64 | if(str_comp(a: g_Config.m_ClBackgroundEntities, CURRENT_MAP) == 0) |
| 65 | { |
| 66 | m_pMap = Kernel()->RequestInterface<IEngineMap>(); |
| 67 | if(m_pMap->IsLoaded()) |
| 68 | { |
| 69 | m_pLayers = GameClient()->Layers(); |
| 70 | m_pImages = &GameClient()->m_MapImages; |
| 71 | m_Loaded = true; |
| 72 | } |
| 73 | } |
| 74 | else if(m_pMap->Load(pMapName: aBuf)) |
| 75 | { |
| 76 | m_pLayers->Init(pMap: m_pMap, GameOnly: true); |
| 77 | NeedImageLoading = true; |
| 78 | m_Loaded = true; |
| 79 | } |
| 80 | |
| 81 | if(m_Loaded) |
| 82 | { |
| 83 | if(NeedImageLoading) |
| 84 | { |
| 85 | m_pImages->LoadBackground(pLayers: m_pLayers, pMap: m_pMap); |
| 86 | } |
| 87 | CMapLayers::OnMapLoad(); |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | void CBackground::OnMapLoad() |
| 93 | { |
| 94 | if(str_comp(a: g_Config.m_ClBackgroundEntities, CURRENT_MAP) == 0 || str_comp(a: g_Config.m_ClBackgroundEntities, b: m_aMapName)) |
| 95 | { |
| 96 | LoadBackground(); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | void CBackground::OnRender() |
| 101 | { |
| 102 | if(!m_Loaded) |
| 103 | return; |
| 104 | |
| 105 | if(Client()->State() != IClient::STATE_ONLINE && Client()->State() != IClient::STATE_DEMOPLAYBACK) |
| 106 | return; |
| 107 | |
| 108 | if(g_Config.m_ClOverlayEntities != 100) |
| 109 | return; |
| 110 | |
| 111 | CMapLayers::OnRender(); |
| 112 | } |
| 113 | |