1/*
2 * LegacyClonk
3 *
4 * Copyright (c) 1998-2000, Matthes Bender (RedWolf Design)
5 * Copyright (c) 2017-2021, The LegacyClonk Team and contributors
6 *
7 * Distributed under the terms of the ISC license; see accompanying file
8 * "COPYING" for details.
9 *
10 * "Clonk" is a registered trademark of Matthes Bender, used with permission.
11 * See accompanying file "TRADEMARK" for details.
12 *
13 * To redistribute this file separately, substitute the full license texts
14 * for the above references.
15 */
16
17#pragma once
18
19#include "C4Application.h"
20#include <C4AudioSystem.h>
21#include <C4Config.h>
22#include "C4CurlSystem.h"
23#include <C4Group.h>
24#include "C4Log.h"
25#include <C4MusicSystem.h>
26#include <C4SoundSystem.h>
27#include <C4Components.h>
28#include <C4InteractiveThread.h>
29#include <C4Network2IRC.h>
30#include "C4ThreadPool.h"
31#include "StdApp.h"
32#include <StdWindow.h>
33
34#include <optional>
35
36class C4ToastSystem;
37class CStdDDraw;
38
39class C4Sec1Callback
40{
41public:
42 virtual void OnSec1Timer() = 0;
43 virtual ~C4Sec1Callback() {}
44};
45
46// callback interface for sec1timers
47class C4Sec1TimerCallbackBase
48{
49protected:
50 C4Sec1TimerCallbackBase *pNext;
51 int iRefs;
52
53public:
54 C4Sec1TimerCallbackBase(); // ctor - ref set to 2
55 virtual ~C4Sec1TimerCallbackBase() {}
56 void Release() { if (!--iRefs) delete this; } // release: destruct in next cycle
57
58protected:
59 virtual void OnSec1Timer() = 0;
60 bool IsReleased() { return iRefs <= 1; }
61
62 friend class C4Application;
63};
64
65template <class T> class C4Sec1TimerCallback : public C4Sec1TimerCallbackBase
66{
67private:
68 T *pCallback;
69
70protected:
71 virtual void OnSec1Timer() override { pCallback->OnSec1Timer(); }
72
73public:
74 C4Sec1TimerCallback(T *pCB) : pCallback(pCB) {}
75};
76
77/* Main class to initialize configuration and execute the game */
78
79class C4Application : public CStdApp
80{
81private:
82 // if set, this mission will be launched next
83 StdStrBuf NextMission;
84
85public:
86 C4Application();
87 ~C4Application();
88 // set by ParseCommandLine
89 bool isFullScreen;
90 // set by ParseCommandLine, if neither scenario nor direct join adress has been specified
91 bool UseStartupDialog;
92 // set by ParseCommandLine, for manually applying downloaded update packs
93 StdStrBuf IncomingUpdate;
94 // set by ParseCommandLine, for manually invoking an update check by command line or url
95 bool CheckForUpdates;
96 // Flag for launching editor on quit
97 bool launchEditor;
98 // Flag for restarting the engine at the end
99 bool restartAtEnd;
100 // main System.c4g in working folder
101 C4Group SystemGroup;
102 std::optional<C4ResStrTable> ResStrTable;
103 std::unique_ptr<C4AudioSystem> AudioSystem;
104 std::optional<C4CurlSystem> CurlSystem;
105 C4LogSystem LogSystem;
106 std::optional<C4MusicSystem> MusicSystem;
107 std::optional<C4SoundSystem> SoundSystem;
108 std::unique_ptr<C4ToastSystem> ToastSystem;
109 C4GamePadControl *pGamePadControl;
110 // Thread for interactive processes (automatically starts as needed)
111 C4InteractiveThread InteractiveThread;
112 // IRC client for global chat
113 C4Network2IRCClient IRCClient;
114 // Tick timing
115 unsigned int iLastGameTick, iGameTickDelay, iExtraGameTickDelay;
116 class CStdDDraw *DDraw;
117 virtual int32_t &ScreenWidth() override { return Config.Graphics.ResX; }
118 virtual int32_t &ScreenHeight() override { return Config.Graphics.ResY; }
119 virtual float GetScale() override { return Config.Graphics.Scale / 100.0f; }
120 void Clear() override;
121 void Execute() override;
122 // System.c4g helper funcs
123 bool OpenSystemGroup() { return SystemGroup.IsOpen() || SystemGroup.Open(C4CFN_System); }
124 void DoSec1Timers();
125 void SetGameTickDelay(int iDelay);
126 bool SetResolution(int32_t iNewResX, int32_t iNewResY);
127 bool SetGameFont(const char *szFontFace, int32_t iFontSize);
128
129protected:
130 enum State { C4AS_None, C4AS_PreInit, C4AS_Startup, C4AS_StartGame, C4AS_Game, C4AS_Quit, } AppState;
131
132protected:
133 virtual void DoInit() override;
134 bool OpenGame();
135 bool PreInit();
136 virtual void OnNetworkEvents() override;
137 static bool ProcessCallback(const char *szMessage, int iProcess);
138
139 std::vector<std::unique_ptr<C4Sec1TimerCallbackBase>> sec1TimerCallbacks;
140 void AddSec1Timer(C4Sec1TimerCallbackBase *callback);
141
142 friend class C4Sec1TimerCallbackBase;
143
144 virtual void OnCommand(const char *szCmd) override;
145
146#ifdef WITH_GLIB
147 std::shared_ptr<spdlog::logger> CreateGLibLogger() override;
148#endif
149
150public:
151 virtual void Quit() override;
152 void QuitGame(); // quit game only, but restart application if in fullscreen startup menu mode
153 void SetNextMission(const char *szMissionFilename);
154};
155
156extern C4Application Application;
157