Cytopia  0.3
A city building simulation game
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Game.cxx
Go to the documentation of this file.
1 #include "Game.hxx"
3 #include "engine/UIManager.hxx"
6 #include "LOG.hxx"
9 #include "Filesystem.hxx"
10 #include "OSystem.hxx"
11 #include <Map.hxx>
12 #include <MapFunctions.hxx>
13 #include "../game/ui/BuildMenu.hxx"
14 #include "../game/ui/GameTimeMenu.hxx"
15 
16 #include <SDL.h>
17 #include <SDL_ttf.h>
18 
19 #ifdef USE_ANGELSCRIPT
21 #endif
22 
23 #ifdef USE_MOFILEREADER
24 #include "moFileReader.h"
25 #endif
26 
27 #ifdef MICROPROFILE_ENABLED
28 #include "microprofile/microprofile.h"
29 #endif
30 
31 namespace Cytopia
32 {
34 {
35  LOG(LOG_DEBUG) << "Created Game Object";
36  initialize();
37 }
38 
39 void Game::quit()
40 {
41 #ifdef USE_AUDIO
42  auto &m_AudioMixer = AudioMixer::instance();
43  m_AudioMixer.stopAll();
44  if (!Settings::instance().audio3DStatus)
45  {
46  m_AudioMixer.play(SoundtrackID{"NegativeSelect"});
47  }
48  else
49  {
50  m_AudioMixer.play(SoundtrackID{"NegativeSelect"}, Coordinate3D{0, 0, -4});
51  }
52 #endif // USE_AUDIO
53 }
54 
56 {
57 #ifdef USE_MOFILEREADER
58  std::string moFilePath = fs::getBasePath();
59  moFilePath = moFilePath + "languages/" + Settings::instance().gameLanguage + "/Cytopia.mo";
60 
61  if (moFileLib::moFileReaderSingleton::GetInstance().ReadFile(moFilePath.c_str()) == moFileLib::moFileReader::EC_SUCCESS)
62  {
63  LOG(LOG_INFO) << "Loaded MO file " << moFilePath;
64  }
65  else
66  {
67  LOG(LOG_ERROR) << "Failed to load MO file " << moFilePath;
68  }
69 #endif
70 
71  // Register Callbacks
75 
76  // we need to instantiate the Map object to be able to send signals for new / load game
78 
79  LOG(LOG_DEBUG) << "Initialized Game Object";
80 }
81 
82 void Game::run(bool SkipMenu)
83 {
85 
86  SDL_Event event;
87  EventManager &evManager = EventManager::instance();
88 
89  UIManager &uiManager = UIManager::instance();
90  uiManager.init();
91 
92  GameClock &gameClock = GameClock::instance();
93 
94 #ifdef USE_ANGELSCRIPT
95  ScriptEngine &scriptEngine = ScriptEngine::instance();
96  scriptEngine.init();
97  scriptEngine.loadScript(fs::getBasePath() + "/resources/test.as", ScriptCategory::BUILD_IN);
98 #endif
99 
100 #ifdef USE_AUDIO
101  if (!Settings::instance().audio3DStatus)
102  {
103  gameClock.addRealTimeClockTask(
104  []()
105  {
106  AudioMixer::instance().play(AudioTrigger::MainTheme);
107  return false;
108  },
109  0s, 8min);
110  gameClock.addRealTimeClockTask(
111  []()
112  {
113  AudioMixer::instance().play(AudioTrigger::NatureSounds);
114  return false;
115  },
116  0s, 3min);
117  }
118  else
119  {
120  gameClock.addRealTimeClockTask(
121  []()
122  {
123  AudioMixer::instance().play(AudioTrigger::MainTheme, Coordinate3D{0, 0.5, 0.1});
124  return false;
125  },
126  0s, 8min);
127  gameClock.addRealTimeClockTask(
128  []()
129  {
130  AudioMixer::instance().play(AudioTrigger::NatureSounds, Coordinate3D{0, 0, -2});
131  return false;
132  },
133  0s, 3min);
134  }
135 #endif // USE_AUDIO
136 
137  // FPS Counter variables
138  const float fpsIntervall = 1.0; // interval the fps counter is refreshed in seconds.
139  Uint32 fpsLastTime = SDL_GetTicks();
140  Uint32 fpsFrames = 0;
141 
142  uiManager.addPersistentMenu<GameTimeMenu>();
143  uiManager.addPersistentMenu<BuildMenu>();
144 
145  // GameLoop
146  while (!m_shutDown)
147  {
148 #ifdef MICROPROFILE_ENABLED
149  MICROPROFILE_SCOPEI("Map", "Gameloop", MP_GREEN);
150 #endif
151  SDL_RenderClear(WindowManager::instance().getRenderer());
152 
153  evManager.checkEvents(event);
154  gameClock.tick();
155 
156  // render the tileMap
157  if (MapFunctions::instance().getMap())
158  {
160  }
161 
162  // render the ui
163  // TODO: This is only temporary until the new UI is ready. Remove this afterwards
164  if (GameStates::instance().drawUI)
165  {
167  uiManager.drawUI();
168  }
169 #ifdef USE_ANGELSCRIPT
170  ScriptEngine::instance().framestep(1);
171 #endif
172 
173  // we need to instantiate the MapFunctions object so it's ready for new game
175 
176  fpsFrames++;
177 
178  if (fpsLastTime < SDL_GetTicks() - fpsIntervall * 1000)
179  {
180  fpsLastTime = SDL_GetTicks();
181  uiManager.setFPSCounterText("FPS: " + std::to_string(fpsFrames));
182  fpsFrames = 0;
183  }
184 
185 #ifdef MICROPROFILE_ENABLED
186  MicroProfileFlip(nullptr);
187 #endif
188  }
189 }
190 
192 {
193  LOG(LOG_DEBUG) << "In shutdown";
194  TTF_Quit();
195  SDL_Quit();
196 }
197 
198 void Game::newGame(bool generateTerrain)
199 {
200  MapFunctions::instance().newMap(generateTerrain);
202 }
203 
204 void Game::loadGame(const std::string &fileName)
205 {
208 }
209 
210 } // namespace Cytopia
MapFunctions::loadMapFromFile
void loadMapFromFile(const std::string &fileName)
Load Map from file.
Definition: MapFunctions.cxx:684
StrongType< string, struct SoundtrackIDTag >
GameClock
Game clock service. Implement two timers one real time timer and other game time timer.
Definition: GameClock.hxx:18
Cytopia::Game::loadGame
virtual void loadGame(const std::string &fileName)
Definition: Game.cxx:204
Cytopia::Game::run
virtual void run(bool SkipMenu=false)
begins the game
Definition: Game.cxx:82
Camera::centerScreenOnMapCenter
void centerScreenOnMapCenter()
Centers camera on the middle of the map.
Definition: Camera.cxx:75
LOG
Definition: LOG.hxx:32
UIManager
Draws the UI to the screen.
Definition: UIManager.hxx:39
LOG_INFO
@ LOG_INFO
Definition: LOG.hxx:25
UIManager::setFPSCounterText
void setFPSCounterText(const std::string &fps)
Helper function to update the FPS Counter.
Definition: UIManager.cxx:142
EventManager.hxx
LOG.hxx
Cytopia::Game::newGame
virtual void newGame(bool generateTerrain)
Definition: Game.cxx:198
MapFunctions.hxx
UIManager::init
void init()
Parses the UiLayout.json files and creates UI Elements.
Coordinate3D
a 3-dimensional coordinate
Definition: AudioMixer.hxx:35
ScriptEngine.hxx
MapFunctions::getMap
Map * getMap()
Definition: MapFunctions.hxx:16
Camera.hxx
Map.hxx
Map::renderMap
void renderMap() const
Render the elements contained in the Map.
Definition: Map.cxx:102
LOG_ERROR
@ LOG_ERROR
Definition: LOG.hxx:28
Cytopia::Game::m_shutDown
bool m_shutDown
Definition: Game.hxx:57
LOG_DEBUG
@ LOG_DEBUG
Definition: LOG.hxx:26
UIManager::drawUI
void drawUI()
Renders all UI Widgets.
Definition: UIManager.cxx:184
GameClock::tick
void tick(void)
This function provides the tick for both clocks.
Definition: GameClock.cxx:19
SettingsData::gameLanguage
std::string gameLanguage
the code for the current game language
Definition: Settings.hxx:145
OSystem.hxx
GameTimeMenu
Definition: GameTimeMenu.hxx:6
BuildMenu
Definition: BuildMenu.hxx:74
GameStates.hxx
WindowManager.hxx
getBasePath
std::string getBasePath()
Get base path (where Cytopia is being run)
Definition: Filesystem.cxx:77
Filesystem.hxx
Settings.hxx
EventManager::checkEvents
void checkEvents(SDL_Event &event)
Definition: EventManager.cxx:79
Cytopia::Game::m_GamePlay
GamePlay m_GamePlay
Definition: Game.hxx:59
MapFunctions::newMap
void newMap(bool generateTerrain=true)
Creates a new map object with terrain gen.
Definition: MapFunctions.cxx:671
WindowManager::renderScreen
void renderScreen()
Definition: WindowManager.cxx:231
SignalMediator::registerCbLoadGame
void registerCbLoadGame(std::function< void(const std::string &)> const &cb)
Definition: SignalMediator.hxx:36
SignalMediator::registerCbNewGame
void registerCbNewGame(std::function< void(bool)> const &cb)
Definition: SignalMediator.hxx:34
Cytopia::Game::Game
Game()
Creates a game.
Definition: Game.cxx:33
Signal::slot
std::function< R(Args...)> slot(instance &object, R(Class::*method)(Args...))
This function creates a std::function by binding object to the member function pointer method.
Definition: Signal.hxx:167
Cytopia::Game::quit
void quit()
Definition: Game.cxx:39
Cytopia
Definition: Game.cxx:31
UIManager::addPersistentMenu
void addPersistentMenu(GameMenu::Ptr menu)
Definition: UIManager.cxx:174
SignalMediator::registerCbQuitGame
void registerCbQuitGame(std::function< void()> const &cb)
Definition: SignalMediator.hxx:35
AudioMixer::play
void play(const SoundtrackID &id, int effect=AL_EFFECT_NULL)
Plays a Soundtrack given its ID and optionally applies an effect.
Definition: AudioMixer.cxx:158
UIManager.hxx
Singleton< AudioMixer >::instance
static AudioMixer & instance(void)
Get an instance of the singleton.
Definition: Singleton.hxx:15
Cytopia::Game::shutdown
virtual void shutdown()
ends the game
Definition: Game.cxx:191
Game.hxx
EventManager
Definition: EventManager.hxx:12
GamePlay::resetManagers
void resetManagers()
Definition: GamePlay.cxx:3
Cytopia::Game::initialize
virtual void initialize()
starts setting up the game
Definition: Game.cxx:55
GameClock::addRealTimeClockTask
GameClock::ClockTaskHndl addRealTimeClockTask(ClockCbk cbk, DelayType delay, PeriodType period=TimePointZero)
Add new real time clock task.
Definition: GameClock.inl.hxx:5
string
std::string string
Definition: AudioConfig.hxx:14
WindowManager::newImGuiFrame
void newImGuiFrame()
creates and renders a new ImGui frame
Definition: WindowManager.cxx:224