LCOV - code coverage report
Current view: top level - source/tools/atlas/GameInterface/Handlers - GraphicsSetupHandlers.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 0 101 0.0 %
Date: 2026-12-03 01:19:39 Functions: 0 32 0.0 %

          Line data    Source code
       1             : /* Copyright (C) 2026 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 "MessageHandler.h"
      21             : 
      22             : #include "gui/GUIManager.h"
      23             : #include "lib/debug.h"
      24             : #include "lib/external_libraries/libsdl.h"
      25             : #include "lib/sysdep/os.h"
      26             : #include "lib/timer.h"
      27             : #include "ps/CLogger.h"
      28             : #include "ps/CStr.h"
      29             : #include "ps/Filesystem.h"
      30             : #include "ps/GameSetup/Config.h"
      31             : #include "ps/GameSetup/GameSetup.h"
      32             : #include "ps/Input.h"
      33             : #include "ps/Profile.h"
      34             : #include "ps/VideoMode.h"
      35             : #include "renderer/Renderer.h"
      36             : #include "renderer/SceneRenderer.h"
      37             : #include "scriptinterface/ForwardDeclarations.h"
      38             : #include "scriptinterface/Interface.h"
      39             : #include "tools/atlas/GameInterface/ActorViewer.h"
      40             : #include "tools/atlas/GameInterface/CommandProc.h"
      41             : #include "tools/atlas/GameInterface/GameLoop.h"
      42             : #include "tools/atlas/GameInterface/InputProcessor.h"
      43             : #include "tools/atlas/GameInterface/Messages.h"
      44             : #include "tools/atlas/GameInterface/Shareable.h"
      45             : #include "tools/atlas/GameInterface/SharedTypes.h"
      46             : #include "tools/atlas/GameInterface/View.h"
      47             : 
      48             : #include <SDL.h>
      49             : #include <SDL_error.h>
      50             : #include <SDL_events.h>
      51             : #include <SDL_video.h>
      52             : #include <memory>
      53             : #include <optional>
      54             : #include <vector>
      55             : 
      56             : #if OS_WIN
      57             : // We don't include wutil header directly to prevent including Windows headers.
      58             : extern void wutil_SetAppWindow(void* hwnd);
      59             : #endif
      60             : 
      61             : namespace AtlasMessage
      62             : {
      63             : namespace
      64             : {
      65             : InputProcessor g_Input;
      66             : 
      67             : // This keeps track of the last in-game user input.
      68             : // It is used to throttle FPS to save CPU & GPU.
      69             : double last_user_activity;
      70             : 
      71             : // see comment in GameLoop.cpp about ah_display_error before using INIT_HAVE_DISPLAY_ERROR
      72             : const int g_InitFlags = INIT_HAVE_VMODE | INIT_NO_GUI;
      73             : 
      74             : // This isn't used directly. When it's emplaced and when it's reset it does mutate `g_Logger`.
      75             : std::optional<FileLogger> g_FileLogger;
      76             : 
      77             : std::optional<Script::Interface> g_ScriptInterface;
      78             : std::unique_ptr<InputHandlers> g_InputHandlers;
      79             : }
      80             : 
      81           0 : MESSAGEHANDLER(Init)
      82             : {
      83           0 :     g_Quickstart = true;
      84             : 
      85           0 :     InitVfs(g_AtlasGameLoop->args);
      86           0 :     g_FileLogger.emplace();
      87             : 
      88             :     // Mount mods if there are any specified as command line parameters
      89           0 :     if (!Init(g_AtlasGameLoop->args, g_InitFlags | INIT_MODS| INIT_MODS_PUBLIC))
      90             :     {
      91             :         // There are no mods specified on the command line,
      92             :         // but there are in the config file, so mount those.
      93           0 :         ShutdownConfigAndSubsequent();
      94           0 :         ENSURE(Init(g_AtlasGameLoop->args, g_InitFlags));
      95             :     }
      96             : 
      97             :     // Initialise some graphics state for Atlas.
      98             :     // (This must be done after Init loads the config DB,
      99             :     // but before the UI constructs its GL canvases.)
     100           0 :     g_VideoMode.InitNonSDL();
     101           0 : }
     102             : 
     103           0 : MESSAGEHANDLER(InitAppWindow)
     104             : {
     105             : #if OS_WIN
     106             :     wutil_SetAppWindow(msg->handle);
     107             : #endif
     108           0 : }
     109             : 
     110           0 : MESSAGEHANDLER(InitSDL)
     111             : {
     112           0 :     SDL_SetHint(SDL_HINT_NO_SIGNAL_HANDLERS, "1");
     113             :     // When using GLX (Linux), SDL has to load the GL library to find
     114             :     // glXGetProcAddressARB before it can load any extensions.
     115             :     // When running in Atlas, we skip the SDL video initialisation code
     116             :     // which loads the library, and so SDL_GL_GetProcAddress fails (in
     117             :     // ogl.cpp importExtensionFunctions).
     118             :     // So, make sure it's loaded:
     119           0 :     SDL_InitSubSystem(SDL_INIT_VIDEO);
     120             :     // wxWidgets doesn't use a proper approach to dynamically load functions and
     121             :     // doesn't provide GetProcAddr-like function. Technically we need to call
     122             :     // SDL_GL_LoadLibrary inside GL device creation, but that might lead to a
     123             :     // crash on Windows because of a wrong order of initialization between SDL
     124             :     // and wxWidgets context management. So leave the call as is while it works.
     125             :     // Refs:
     126             :     //   http://trac.wxwidgets.org/ticket/9213
     127             :     //   http://trac.wxwidgets.org/ticket/9215
     128           0 :     if (SDL_GL_LoadLibrary(nullptr) && g_Logger)
     129           0 :         LOGERROR("SDL failed to load GL library: '{}'", SDL_GetError());
     130           0 : }
     131             : 
     132           0 : MESSAGEHANDLER(InitGraphics)
     133             : {
     134           0 :     g_VideoMode.CreateBackendDevice(false);
     135             : 
     136           0 :     g_ScriptInterface.emplace("Engine", "GUIManager", *g_ScriptContext);
     137           0 :     g_InputHandlers = InitGraphics(g_AtlasGameLoop->args, g_InitFlags, {}, *g_ScriptContext, *g_ScriptInterface);
     138           0 : }
     139             : 
     140             : 
     141           0 : MESSAGEHANDLER(Shutdown)
     142             : {
     143             :     // Empty the CommandProc, to get rid of its references to entities before
     144             :     // we kill the EntityManager
     145           0 :     GetCommandProc().Destroy();
     146             : 
     147           0 :     AtlasView::DestroyViews();
     148           0 :     g_AtlasGameLoop->view = AtlasView::GetView_None();
     149           0 :     g_InputHandlers.reset();
     150           0 :     ShutdownNetworkAndUI();
     151           0 :     g_InputHandlers.reset();
     152           0 :     g_ScriptInterface.reset();
     153           0 :     ShutdownConfigAndSubsequent();
     154           0 :     g_FileLogger.reset();
     155           0 : }
     156             : 
     157             : 
     158           0 : QUERYHANDLER(Exit)
     159             : {
     160           0 :     g_AtlasGameLoop->running = false;
     161           0 : }
     162             : 
     163             : 
     164           0 : MESSAGEHANDLER(RenderEnable)
     165             : {
     166           0 :     g_AtlasGameLoop->view->SetEnabled(false);
     167           0 :     g_AtlasGameLoop->view = AtlasView::GetView(msg->view);
     168           0 :     g_AtlasGameLoop->view->SetEnabled(true);
     169           0 : }
     170             : 
     171           0 : MESSAGEHANDLER(SetViewParamB)
     172             : {
     173           0 :     AtlasView* view = AtlasView::GetView(msg->view);
     174           0 :     view->SetParam(*msg->name, msg->value);
     175           0 : }
     176             : 
     177           0 : MESSAGEHANDLER(SetViewParamI)
     178             : {
     179           0 :     AtlasView* view = AtlasView::GetView(msg->view);
     180           0 :     view->SetParam(*msg->name, msg->value);
     181           0 : }
     182             : 
     183           0 : MESSAGEHANDLER(SetViewParamC)
     184             : {
     185           0 :     AtlasView* view = AtlasView::GetView(msg->view);
     186           0 :     view->SetParam(*msg->name, msg->value);
     187           0 : }
     188             : 
     189           0 : MESSAGEHANDLER(SetViewParamS)
     190             : {
     191           0 :     AtlasView* view = AtlasView::GetView(msg->view);
     192           0 :     view->SetParam(*msg->name, *msg->value);
     193           0 : }
     194             : 
     195           0 : MESSAGEHANDLER(SetActorViewer)
     196             : {
     197           0 :     if (msg->flushcache)
     198             :     {
     199             :         // TODO EXTREME DANGER: this'll break horribly if any units remain
     200             :         // in existence and use their actors after we've deleted all the actors.
     201             :         // (The actor viewer currently only has one unit at a time, so it's
     202             :         // alright.)
     203             :         // Should replace this with proper actor hot-loading system, or something.
     204             : 
     205           0 :         AtlasView::GetView_Actor()->GetActorViewer().SetActor(L"", "", -1);
     206           0 :         AtlasView::GetView_Actor()->GetActorViewer().UnloadObjects();
     207             : //      vfs_reload_changed_files();
     208             :     }
     209           0 :     AtlasView::GetView_Actor()->SetSpeedMultiplier(msg->speed);
     210           0 :     AtlasView::GetView_Actor()->GetActorViewer().SetActor(*msg->id, *msg->animation, msg->playerID);
     211           0 : }
     212             : 
     213             : //////////////////////////////////////////////////////////////////////////
     214             : 
     215           0 : MESSAGEHANDLER(SetCanvas)
     216             : {
     217             :     // Need to set the canvas size before possibly doing any rendering,
     218             :     // else we'll get GL errors when trying to render to 0x0
     219           0 :     g_VideoMode.UpdateRenderer(msg->width, msg->height);
     220             : 
     221           0 :     g_AtlasGameLoop->glCanvas = msg->canvas;
     222           0 :     Atlas_GLSetCurrent(const_cast<void*>(g_AtlasGameLoop->glCanvas));
     223           0 : }
     224             : 
     225             : 
     226           0 : MESSAGEHANDLER(ResizeScreen)
     227             : {
     228           0 :     g_VideoMode.UpdateRenderer(msg->width, msg->height);
     229             : 
     230             : #if OS_MACOSX
     231             :     // OS X seems to require this to update the GL canvas
     232             :     Atlas_GLSetCurrent(const_cast<void*>(g_AtlasGameLoop->glCanvas));
     233             : #endif
     234           0 : }
     235             : 
     236           0 : QUERYHANDLER(RenderLoop)
     237             : {
     238           0 :     {
     239           0 :         const double time = timer_Time();
     240           0 :         static double last_time = time;
     241           0 :         const double realFrameLength = time-last_time;
     242           0 :         last_time = time;
     243           0 :         ENSURE(realFrameLength >= 0.0);
     244             :         // TODO: filter out big jumps, e.g. when having done a lot of slow
     245             :         // processing in the last frame
     246           0 :         g_AtlasGameLoop->realFrameLength = realFrameLength;
     247             :     }
     248             : 
     249           0 :     if (g_Input.ProcessInput(g_AtlasGameLoop))
     250           0 :         last_user_activity = timer_Time();
     251             : 
     252           0 :     msg->timeSinceActivity = timer_Time() - last_user_activity;
     253             : 
     254           0 :     ReloadChangedFiles();
     255             : 
     256           0 :     RendererIncrementalLoad();
     257             : 
     258             :     // Pump SDL events (e.g. hotkeys)
     259           0 :     for (SDL_Event& ev : g_VideoMode.m_InputManager.PollEvents())
     260           0 :         g_VideoMode.m_InputManager.DispatchEvent(ev);
     261             : 
     262           0 :     if (g_GUI)
     263           0 :         g_GUI->TickObjects();
     264             : 
     265           0 :     g_AtlasGameLoop->view->Update(g_AtlasGameLoop->realFrameLength);
     266             : 
     267           0 :     g_AtlasGameLoop->view->Render();
     268             : 
     269           0 :     if (CProfileManager::IsInitialised())
     270           0 :         g_Profiler.Frame();
     271             : 
     272           0 :     msg->smoothFramerate = g_AtlasGameLoop->view->GetSmoothFramerate();
     273           0 : }
     274             : 
     275             : //////////////////////////////////////////////////////////////////////////
     276             : 
     277           0 : MESSAGEHANDLER(RenderStyle)
     278             : {
     279           0 :     g_Renderer.GetSceneRenderer().SetTerrainRenderMode(msg->wireframe ? EDGED_FACES : SOLID);
     280           0 :     g_Renderer.GetSceneRenderer().SetWaterRenderMode(msg->wireframe ? EDGED_FACES : SOLID);
     281           0 :     g_Renderer.GetSceneRenderer().SetModelRenderMode(msg->wireframe ? EDGED_FACES : SOLID);
     282           0 :     g_Renderer.GetSceneRenderer().SetOverlayRenderMode(msg->wireframe ? EDGED_FACES : SOLID);
     283           0 : }
     284             : 
     285             : } // namespace AtlasMessage

Generated by: LCOV version 1.16