Line data Source code
1 : /* Copyright (C) 2025 Wildfire Games. 2 : * This file is part of 0 A.D. 3 : * 4 : * 0 A.D. is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 2 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * 0 A.D. is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : 18 : #include "precompiled.h" 19 : 20 : #include "GameLoop.h" 21 : 22 : #include "graphics/TextureManager.h" 23 : #include "lib/debug.h" 24 : #include "lib/timer.h" 25 : #include "ps/DllLoader.h" 26 : #include "ps/GameSetup/Paths.h" 27 : #include "ps/ThreadUtil.h" 28 : #include "renderer/Renderer.h" 29 : #include "tools/atlas/GameInterface/MessagePasser.h" 30 : #include "tools/atlas/GameInterface/MessagePasserImpl.h" 31 : #include "tools/atlas/GameInterface/SharedMemory.h" 32 : #include "tools/atlas/GameInterface/View.h" 33 : 34 : #include <cstddef> 35 : #include <string> 36 : 37 : using namespace AtlasMessage; 38 : 39 : namespace AtlasMessage 40 : { 41 : extern void RegisterHandlers(); 42 : } 43 : 44 : // Loaded from DLL: 45 : void (*Atlas_StartWindow)(const wchar_t* type); 46 : void (*Atlas_SetDataDirectory)(const wchar_t* path); 47 : void (*Atlas_SetConfigDirectory)(const wchar_t* path); 48 : void (*Atlas_SetMessagePasser)(MessagePasser*); 49 : void (*Atlas_GLSetCurrent)(void* cavas); 50 : void (*Atlas_GLSwapBuffers)(void* canvas); 51 : void (*Atlas_DisplayError)(const wchar_t* text, size_t flags); 52 : namespace AtlasMessage 53 : { 54 : void* (*ShareableMallocFptr)(size_t); 55 : void (*ShareableFreeFptr)(void*); 56 : } 57 : 58 : 59 : MessagePasser* AtlasMessage::g_MessagePasser = NULL; 60 : 61 : 62 : static GameLoopState state; 63 : GameLoopState* g_AtlasGameLoop = &state; 64 : 65 0 : void RendererIncrementalLoad() 66 : { 67 : // TODO: shouldn't duplicate this code from main.cpp 68 : 69 0 : if (!CRenderer::IsInitialised()) 70 : return; 71 : 72 0 : const double maxTime = 0.1f; 73 : 74 0 : double startTime = timer_Time(); 75 0 : bool more; 76 0 : do { 77 0 : more = g_Renderer.GetTextureManager().MakeProgress(); 78 : } 79 0 : while (more && timer_Time() - startTime < maxTime); 80 : } 81 : 82 0 : bool BeginAtlas(const CmdLineArgs& args, const DllLoader& dll) 83 : { 84 : // Load required symbols from the DLL 85 0 : try 86 : { 87 0 : dll.LoadSymbol("Atlas_StartWindow", Atlas_StartWindow); 88 0 : dll.LoadSymbol("Atlas_SetMessagePasser", Atlas_SetMessagePasser); 89 0 : dll.LoadSymbol("Atlas_SetDataDirectory", Atlas_SetDataDirectory); 90 0 : dll.LoadSymbol("Atlas_SetConfigDirectory", Atlas_SetConfigDirectory); 91 0 : dll.LoadSymbol("Atlas_GLSetCurrent", Atlas_GLSetCurrent); 92 0 : dll.LoadSymbol("Atlas_GLSwapBuffers", Atlas_GLSwapBuffers); 93 0 : dll.LoadSymbol("Atlas_DisplayError", Atlas_DisplayError); 94 0 : dll.LoadSymbol("ShareableMalloc", ShareableMallocFptr); 95 0 : dll.LoadSymbol("ShareableFree", ShareableFreeFptr); 96 : } 97 0 : catch (PSERROR_DllLoader&) 98 : { 99 0 : debug_warn(L"Failed to initialize DLL"); 100 0 : return false; 101 0 : } 102 : 103 : // Construct a message passer for communicating with Atlas 104 : // (here so that its scope lasts beyond the game thread) 105 0 : MessagePasserImpl msgPasser; 106 0 : AtlasMessage::g_MessagePasser = &msgPasser; 107 : 108 : // Pass our message handler to Atlas 109 0 : Atlas_SetMessagePasser(&msgPasser); 110 : 111 : // Tell Atlas the location of the data directory 112 0 : const Paths paths(args); 113 0 : Atlas_SetDataDirectory(paths.RData().string().c_str()); 114 : 115 : // Tell Atlas the location of the user config directory 116 0 : Atlas_SetConfigDirectory(paths.Config().string().c_str()); 117 : 118 0 : RegisterHandlers(); 119 : 120 0 : state.args = args; 121 0 : state.running = true; 122 0 : state.view = AtlasView::GetView_None(); 123 0 : state.glCanvas = NULL; 124 : 125 : // Start Atlas UI on main thread 126 : // (required for wxOSX/Cocoa compatibility - see https://gitea.wildfiregames.com/0ad/0ad/issues/500) 127 0 : Atlas_StartWindow(L"ScenarioEditor"); 128 : 129 : // TODO: delete all remaining messages, to avoid memory leak warnings 130 : 131 : // Restore main thread 132 0 : Threading::SetMainThread(); 133 : 134 : // Clean up 135 0 : AtlasView::DestroyViews(); 136 0 : AtlasMessage::g_MessagePasser = NULL; 137 : 138 0 : return true; 139 : }