FIFE
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
engine.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2013 by the FIFE team *
3  * http://www.fifengine.net *
4  * This file is part of FIFE. *
5  * *
6  * FIFE is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20  ***************************************************************************/
21 
22 // Standard C++ library includes
23 #include <iostream>
24 #include <algorithm>
25 
26 // 3rd party library includes
27 #include <SDL.h>
28 #include <SDL_ttf.h>
29 
30 // FIFE includes
31 // These includes are split up in two parts, separated by one empty line
32 // First block: files included from the FIFE root src directory
33 // Second block: files included from the same folder
34 #include "util/base/exception.h"
35 #include "util/log/logger.h"
36 #include "util/time/timemanager.h"
37 #include "audio/soundmanager.h"
38 #include "gui/guimanager.h"
39 #include "vfs/vfs.h"
40 #include "vfs/vfsdirectory.h"
41 #include "vfs/directoryprovider.h"
42 #ifdef HAVE_ZIP
43 #include "vfs/zip/zipprovider.h"
44 #endif
46 #include "video/imagemanager.h"
47 #include "audio/soundclipmanager.h"
48 #include "video/renderbackend.h"
49 #include "video/cursor.h"
50 #include "video/devicecaps.h"
51 #ifdef HAVE_OPENGL
55 #endif
59 #include "model/model.h"
75 #include "video/image.h"
76 #include "engine.h"
77 
78 #ifdef USE_COCOA
79 
80 #include <objc/message.h>
81 #include <dlfcn.h>
82 
83 int32_t main(int32_t argc, char **argv)
84 {
85  return 0;
86 }
87 #endif
88 
89 namespace FIFE {
90  static Logger _log(LM_CONTROLLER);
91 
93  m_renderbackend(0),
94  m_guimanager(0),
95  m_eventmanager(0),
96  m_soundmanager(0),
97  m_timemanager(0),
98  m_imagemanager(0),
99  m_soundclipmanager(0),
100  m_vfs(0),
101  m_model(0),
102  m_logmanager(0),
103  m_cursor(0),
104  m_settings(),
105  m_devcaps(),
106  m_offrenderer(0),
107  m_changelisteners() {
108 #ifdef USE_COCOA
109  // The next lines ensure that Cocoa is initialzed correctly.
110  // This is needed for SDL to function properly on MAC OS X.
111  void* cocoa_lib;
112  cocoa_lib = dlopen( "/System/Library/Frameworks/Cocoa.framework/Cocoa", RTLD_LAZY );
113  void (*nsappload)(void);
114  nsappload = (void(*)()) dlsym( cocoa_lib, "NSApplicationLoad");
115  nsappload();
116 
117  // Create an autorelease pool, so autoreleased SDL objects don't leak.
118  objc_object *NSAutoreleasePool = objc_getClass("NSAutoreleasePool");
119  m_autoreleasePool =
120  objc_msgSend(NSAutoreleasePool, sel_registerName("new"));
121 #endif
123  }
124 
126  return m_settings;
127  }
128 
130  return m_devcaps;
131  }
132 
134  m_cursor->invalidate();
135 
137 
139 
140  if (m_guimanager) {
141  m_guimanager->resizeTopContainer(0,0,mode.getWidth(), mode.getHeight());
142  }
143 
144  std::vector<IEngineChangeListener*>::iterator i = m_changelisteners.begin();
145  while (i != m_changelisteners.end()) {
146  (*i)->onScreenModeChanged(mode);
147  ++i;
148  }
149  }
150 
151  void Engine::init() {
152  m_destroyed = false;
153 
154  FL_LOG(_log, "================== Engine initialize start =================");
155  m_timemanager = new TimeManager();
156  FL_LOG(_log, "Time manager created");
157 
158  FL_LOG(_log, "Creating VFS");
159  m_vfs = new VFS();
160 
161  FL_LOG(_log, "Adding root directory to VFS");
164 #ifdef HAVE_ZIP
165  FL_LOG(_log, "Adding zip provider to VFS");
166  m_vfs->addProvider( new ZipProvider() );
167 #endif
168  //m_vfs->addProvider(ProviderDAT2());
169  //m_vfs->addProvider(ProviderDAT1());
170  FL_LOG(_log, "Engine pre-init done");
171 
172  // If failed to init SDL throw exception.
173  if (SDL_Init(SDL_INIT_NOPARACHUTE | SDL_INIT_TIMER) < 0) {
174  throw SDLException(SDL_GetError());
175  }
176 
177  SDL_EnableUNICODE(1);
178  SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
179  TTF_Init();
180 
181  FL_LOG(_log, "Creating event manager");
185 
186  FL_LOG(_log, "Creating resource managers");
187 
190 
191  FL_LOG(_log, "Creating render backend");
192  std::string rbackend(m_settings.getRenderBackend());
193  if (rbackend == "SDL") {
195  FL_LOG(_log, "SDL Render backend created");
196  } else {
197 #ifdef HAVE_OPENGL
198  if (rbackend == "OpenGLe") {
200  FL_LOG(_log, "OpenGLe Render backend created");
201  FL_LOG(_log, "This is highly experimental so bear in mind some features may not work/work correctly.");
202  } else {
204  FL_LOG(_log, "OpenGL Render backend created");
205  }
206 #else
208  // Remember the choice so we pick the right graphics class.
209  rbackend = "SDL";
210  FL_WARN(_log, "Tried to select OpenGL, even though it is not compiled into the engine. Falling back to SDL Render backend");
211 #endif
212  }
213  FL_LOG(_log, "Initializing render backend");
215  // we always set this to false
216  //m_renderbackend->setAlphaOptimizerEnabled(false);
220 
224  }
225 
226  std::string driver = m_settings.getVideoDriver();
227  std::vector<std::string> drivers = m_devcaps.getAvailableDrivers();
228 
229  if (driver != ""){
230  if (std::find (drivers.begin(), drivers.end(), driver) == drivers.end()) {
231  FL_WARN(_log, "Selected driver is not supported for your Operating System! Reverting to default driver.");
232  driver = "";
233  }
234  }
235 
236  m_renderbackend->init(driver);
237 
238  FL_LOG(_log, "Querying device capabilities");
240 
242 
246  bpp,
247  rbackend,
249 
250  FL_LOG(_log, "Creating main screen");
252  m_screenMode,
255  FL_LOG(_log, "Main screen created");
256 
257 #ifdef HAVE_OPENGL
258  if (m_settings.getLightingModel() != 0) {
260  }
261 
262 #endif
263  SDL_EnableUNICODE(1);
264 
265  FL_LOG(_log, "Creating sound manager");
267  m_soundmanager->setVolume(static_cast<float>(m_settings.getInitialVolume()) / 10);
268 
269  FL_LOG(_log, "Creating renderers");
272  m_renderers.push_back(new InstanceRenderer(m_renderbackend, 10));
273  m_renderers.push_back(new GridRenderer(m_renderbackend, 20));
277  m_renderers.push_back(new QuadTreeRenderer(m_renderbackend, 60));
278  m_renderers.push_back(new CoordinateRenderer(m_renderbackend, 70));
279  m_renderers.push_back(new GenericRenderer(m_renderbackend, 80));
280  m_renderers.push_back(new LightRenderer(m_renderbackend, 90));
281  m_renderers.push_back(new CellRenderer(m_renderbackend, 100));
282 
283  FL_LOG(_log, "Creating model");
285  FL_LOG(_log, "Adding pathers to model");
287  FL_LOG(_log, "Adding grid prototypes to model");
289  m_model->adoptCellGrid(new HexGrid());
290 
292  FL_LOG(_log, "Engine intialized");
293  }
294 
296  if( !m_destroyed ) {
297  destroy();
298  }
299  }
300 
302  FL_LOG(_log, "Destructing engine");
303  delete m_cursor;
304  delete m_model;
305  delete m_soundmanager;
306  delete m_guimanager;
307 
308  delete m_imagemanager;
309  delete m_soundclipmanager;
310 // delete m_eventmanager;
311 
312  // properly remove all the renderers created during init
313  delete m_offrenderer;
314  delete m_targetrenderer;
315  std::vector<RendererBase*>::iterator rendererIter = m_renderers.begin();
316  for ( ; rendererIter != m_renderers.end(); ++rendererIter)
317  {
318  delete *rendererIter;
319  }
320  m_renderers.clear();
321 
322  delete m_renderbackend;
323  delete m_vfs;
324  delete m_timemanager;
325 
326  TTF_Quit();
327  SDL_Quit();
328 
329 #ifdef USE_COCOA
330  objc_msgSend(m_autoreleasePool, sel_registerName("release"));
331 #endif
332 
333  FL_LOG(_log, "================== Engine destructed ==================");
334  m_destroyed = true;
335  //delete m_logmanager;
336  }
339  }
340 
341  void Engine::pump() {
345 
347  if (m_model->getMapCount() == 0) {
350  } else {
351  m_model->update();
352  }
353 
354  if (m_guimanager) {
355  m_guimanager->turn();
356  }
357 
358  m_cursor->draw();
360  }
361 
363  // nothing here at the moment..
364  }
365 
367  m_changelisteners.push_back(listener);
368  }
369 
371  std::vector<IEngineChangeListener*>::iterator i = m_changelisteners.begin();
372  while (i != m_changelisteners.end()) {
373  if ((*i) == listener) {
374  m_changelisteners.erase(i);
375  return;
376  }
377  ++i;
378  }
379  }
380 }//FIFE
381 
382 /* vim: set noexpandtab: set shiftwidth=2: set tabstop=2: */
EngineSettings m_settings
Definition: engine.h:213
#define FL_WARN(logger, msg)
Definition: logger.h:72
void pump()
Runs one cycle for the engine.
Definition: engine.cpp:341
This class defines the engine settings on engine init.
virtual void turn()=0
Performs the GUI logic and draws the GUI accordingly.
void adoptCellGrid(CellGrid *grid)
Adds cellgrid to model.
Definition: model.cpp:92
ImageManager * m_imagemanager
Definition: engine.h:203
void addProvider(VFSSourceProvider *provider)
add new VFSSourceProvider
Definition: vfs.cpp:66
void addChangeListener(IEngineChangeListener *listener)
Adds new change listener.
Definition: engine.cpp:366
ImageManager.
Definition: imagemanager.h:54
bool isColorKeyEnabled() const
Gets whether the colorkey feature is in use.
RenderBackend * m_renderbackend
Definition: engine.h:198
The main class of the SDL-based renderer.
void finalizePumping()
Finalizes the continuous processing of the engine Call this only once in your program, after you have called initializePumping + (pump() * N times)
Definition: engine.cpp:362
std::vector< IEngineChangeListener * > m_changelisteners
Definition: engine.h:222
Engine()
Constructor.
Definition: engine.cpp:92
void destroy()
Explicit destruction of engine.
Definition: engine.cpp:301
virtual void createMainScreen(const ScreenMode &mode, const std::string &title, const std::string &icon)=0
Creates the mainscreen (the display window).
void update()
Called periodically to update events on model.
Definition: model.cpp:271
static Logger _log(LM_AUDIO)
bool isFrameLimitEnabled() const
Gets whether the frame limiter is in use.
bool m_destroyed
Definition: engine.h:211
TargetRenderer * m_targetrenderer
Definition: engine.h:219
void setColorKeyEnabled(bool colorkeyenable)
Sets whether to use the colorkey feature.
void setMouseAccelerationEnabled(bool acceleration)
Sets mouse acceleration if mouse acceleration is enabled, then the mouse sensitivity is used as speed...
void setFramebufferEnabled(bool enabled)
Enables or disable the usage of the framebuffer, if available.
uint16_t getScreenHeight() const
Gets screen height (pixels)
virtual void init(const std::string &driver)=0
Initializes the backend.
const SDL_Color & getColorKey() const
Gets the global colorkey setting.
SoundManager * m_soundmanager
Definition: engine.h:201
const std::string & getVideoDriver() const
void initializePumping()
Initializes the continuous processing of the engine Call this only once in your program.
Definition: engine.cpp:337
virtual void clearBackBuffer()=0
Forces a clear of the backbuffer.
ScreenMode getNearestScreenMode(uint16_t width, uint16_t height, uint16_t bpp, const std::string &renderer, bool fs) const
Gets the nearest valid screen mode based on the arguments passed.
Definition: devicecaps.cpp:277
const std::string & getWindowTitle() const
Gets the current window title.
bool isGLUseNPOT() const
Tells if OpenGL renderbackend should use NPOT Textures.
DeviceCaps m_devcaps
Definition: engine.h:214
uint16_t getScreenWidth() const
Gets screen width (pixels)
SoundClipManager.
void init()
Initializes the engine.
Definition: engine.cpp:151
EngineSettings & getSettings()
Gets settings class for engine.
Definition: engine.cpp:125
float getMouseSensitivity() const
Gets mouse sensitivity.
LogManager * m_logmanager
Definition: engine.h:208
void invalidate()
Definition: cursor.cpp:184
bool isGLCompressImages() const
Tells if images are compress by video driver in OpenGL renderbackend.
virtual void draw()
draws cursor on screen
Definition: cursor.cpp:195
Provider for OS directories.
void setVolume(float vol)
Sets the Master Volume.
Definition: soundmanager.h:76
std::vector< std::string > getAvailableDrivers() const
Gets the available graphics drivers for your operating system.
Definition: devicecaps.h:134
virtual void startFrame()
Called when a new frame starts.
uint32_t getMapCount() const
Return the number of maps in this model.
Definition: model.cpp:131
void setFrameLimit(uint16_t framelimit)
Sets the frame limit.
void setNPOTEnabled(bool enabled)
Enables or disable the usage of npot, if available.
float getInitialVolume() const
Gets initial engine sound volume.
const DeviceCaps & getDeviceCaps() const
Gets device capabilities.
Definition: engine.cpp:129
void removeChangeListener(IEngineChangeListener *listener)
Removes associated change listener.
Definition: engine.cpp:370
const std::string & getRenderBackend() const
Gets currently set renderbackend name.
EventManager * m_eventmanager
Definition: engine.h:200
uint8_t getBitsPerPixel() const
Gets currently set bits per pixel value.
TimeManager * m_timemanager
Definition: engine.h:202
uint16_t getHeight() const
Returns the height of the screen mode.
Definition: devicecaps.h:66
void addSource(VFSSource *source)
Add a new VFSSource.
Definition: vfs.cpp:110
virtual void resizeTopContainer(uint32_t x, uint32_t y, uint32_t width, uint32_t height)=0
Resizes the top container.
A model is a facade for everything in the model.
Definition: model.h:53
CellSelectionRenderer renders a frame around selected cells.
OffRenderer * m_offrenderer
Definition: engine.h:218
unsigned short uint16_t
Definition: core.h:39
uint16_t getWidth() const
Returns the width of the screen mode.
Definition: devicecaps.h:60
IGUIManager * m_guimanager
Definition: engine.h:199
virtual void invalidateAll()
Cursor * m_cursor
Definition: engine.h:210
Event Manager manages all events related to FIFE.
Definition: eventmanager.h:66
Time Manager.
Definition: timemanager.h:50
virtual ~Engine()
Destructor.
Definition: engine.cpp:295
#define FL_LOG(logger, msg)
Definition: logger.h:71
bool isMouseAccelerationEnabled() const
Returns if mouse acceleration is enabled or not.
virtual void endFrame()
Called when a frame is finished and ready to be displayed.
static LogManager * instance()
Returns instance to log manager.
Definition: logger.cpp:65
void fillDeviceCaps()
Should be called AFTER SDL_Init() has been called.
Definition: devicecaps.cpp:154
uint16_t getFrameLimit() const
Gets the frame limit.
Cursor class manages mouse cursor handling.
Definition: cursor.h:84
bool isGLUseFramebuffer() const
Tells if OpenGL renderbackend should use FramebufferObject.
void setFrameLimitEnabled(bool limited)
Sets whether to use the frame limiter.
std::vector< RendererBase * > m_renderers
Definition: engine.h:220
the main VFS (virtual file system) class
Definition: vfs.h:58
bool isFullScreen() const
True, if set to fullscreen.
ScreenMode m_screenMode
Definition: engine.h:216
void adoptPather(IPather *pather)
Adds pather to model.
Definition: model.cpp:77
void update()
Called once a frame and updates the timer objects and events.
Definition: timemanager.cpp:51
The main class of the OpenGL-based experimental renderer.
uint32_t getLightingModel() const
Gets the currently set light model.
The most basic VFSSource for &quot;normal&quot; filesystems.
Definition: vfsdirectory.h:44
A VFS provider for Zip archives.
Definition: zipprovider.h:42
const std::string & getWindowIcon() const
Gets the icon in the window title bar.
Model * m_model
Definition: engine.h:207
void setMouseSensitivity(float sensitivity)
Sets mouse sensitivity The sensitivity is limited to the range -0.99 - 10.0.
void processEvents()
Process the SDL event queue.
SoundClipManager * m_soundclipmanager
Definition: engine.h:204
void setImageCompressingEnabled(bool enabled)
Enables or disable compressing images by video driver.
VFS * m_vfs
Definition: engine.h:206
virtual void setScreenMode(const ScreenMode &mode)=0
Sets the mainscreen display mode.
The main class of the OpenGL-based renderer.
virtual void setLightingModel(uint32_t lighting)=0
Initializes the light.
void changeScreenMode(const ScreenMode &mode)
Changes the screen mode.
Definition: engine.cpp:133