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
54 #endif
58 #include "model/model.h"
74 #include "video/image.h"
75 #include "engine.h"
76 
77 #ifdef USE_COCOA
78 
79 #include <objc/message.h>
80 #include <dlfcn.h>
81 
82 int32_t main(int32_t argc, char **argv)
83 {
84  return 0;
85 }
86 #endif
87 
88 namespace FIFE {
89  static Logger _log(LM_CONTROLLER);
90 
92  m_renderbackend(0),
93  m_guimanager(0),
94  m_eventmanager(0),
95  m_soundmanager(0),
96  m_timemanager(0),
97  m_imagemanager(0),
98  m_soundclipmanager(0),
99  m_vfs(0),
100  m_model(0),
101  m_logmanager(0),
102  m_cursor(0),
103  m_destroyed(false),
104  m_settings(),
105  m_devcaps(),
106  m_offrenderer(0),
107  m_targetrenderer(0),
108  m_changelisteners() {
109 #ifdef USE_COCOA
110  // The next lines ensure that Cocoa is initialzed correctly.
111  // This is needed for SDL to function properly on MAC OS X.
112  void* cocoa_lib;
113  cocoa_lib = dlopen( "/System/Library/Frameworks/Cocoa.framework/Cocoa", RTLD_LAZY );
114  void (*nsappload)(void);
115  nsappload = (void(*)()) dlsym( cocoa_lib, "NSApplicationLoad");
116  nsappload();
117 
118  // Create an autorelease pool, so autoreleased SDL objects don't leak.
119 #ifdef OSX_109
120  Class NSAutoreleasePool = objc_getClass("NSAutoreleasePool");
121  m_autoreleasePool = class_createInstance(NSAutoreleasePool, 0);
122 #else
123  objc_object *NSAutoreleasePool = objc_getClass("NSAutoreleasePool");
124  m_autoreleasePool =
125  objc_msgSend(NSAutoreleasePool, sel_registerName("new"));
126 #endif
127 #endif
129  }
130 
132  return m_settings;
133  }
134 
136  return m_devcaps;
137  }
138 
140  m_cursor->invalidate();
141 
143 
145 
146  if (m_guimanager) {
147  m_guimanager->resizeTopContainer(0,0,mode.getWidth(), mode.getHeight());
148  }
149 
150  std::vector<IEngineChangeListener*>::iterator i = m_changelisteners.begin();
151  while (i != m_changelisteners.end()) {
152  (*i)->onScreenModeChanged(mode);
153  ++i;
154  }
155  }
156 
157  void Engine::init() {
158  m_destroyed = false;
159 
160  FL_LOG(_log, "================== Engine initialize start =================");
161  m_timemanager = new TimeManager();
162  FL_LOG(_log, "Time manager created");
163 
164  FL_LOG(_log, "Creating VFS");
165  m_vfs = new VFS();
166 
167  FL_LOG(_log, "Adding root directory to VFS");
170 #ifdef HAVE_ZIP
171  FL_LOG(_log, "Adding zip provider to VFS");
172  m_vfs->addProvider( new ZipProvider() );
173 #endif
174  //m_vfs->addProvider(ProviderDAT2());
175  //m_vfs->addProvider(ProviderDAT1());
176  FL_LOG(_log, "Engine pre-init done");
177 
178  // If failed to init SDL throw exception.
179  if (SDL_Init(SDL_INIT_NOPARACHUTE | SDL_INIT_TIMER) < 0) {
180  throw SDLException(SDL_GetError());
181  }
182 
183  SDL_EnableUNICODE(1);
184  SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
185  TTF_Init();
186 
187  FL_LOG(_log, "Creating event manager");
191 
192  FL_LOG(_log, "Creating resource managers");
193 
196 
197  FL_LOG(_log, "Creating render backend");
198  std::string rbackend(m_settings.getRenderBackend());
199  if (rbackend == "SDL") {
201  FL_LOG(_log, "SDL Render backend created");
202  } else {
203 #ifdef HAVE_OPENGL
205  FL_LOG(_log, "OpenGL Render backend created");
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);
228  }
229 
230  std::string driver = m_settings.getVideoDriver();
231  std::vector<std::string> drivers = m_devcaps.getAvailableDrivers();
232 
233  if (driver != ""){
234  if (std::find (drivers.begin(), drivers.end(), driver) == drivers.end()) {
235  FL_WARN(_log, "Selected driver is not supported for your Operating System! Reverting to default driver.");
236  driver = "";
237  }
238  }
239 
240  m_renderbackend->init(driver);
241 
242  FL_LOG(_log, "Querying device capabilities");
244 
246 
250  bpp,
251  rbackend,
253 
254  FL_LOG(_log, "Creating main screen");
256  m_screenMode,
259  FL_LOG(_log, "Main screen created");
260 
261 #ifdef HAVE_OPENGL
262  if (m_settings.getLightingModel() != 0) {
264  }
265 
266 #endif
267  SDL_EnableUNICODE(1);
268 
269  FL_LOG(_log, "Creating sound manager");
271  m_soundmanager->setVolume(static_cast<float>(m_settings.getInitialVolume()) / 10);
272 
273  FL_LOG(_log, "Creating renderers");
276  m_renderers.push_back(new InstanceRenderer(m_renderbackend, 10));
277  m_renderers.push_back(new GridRenderer(m_renderbackend, 20));
281  m_renderers.push_back(new QuadTreeRenderer(m_renderbackend, 60));
282  m_renderers.push_back(new CoordinateRenderer(m_renderbackend, 70));
283  m_renderers.push_back(new GenericRenderer(m_renderbackend, 80));
284  m_renderers.push_back(new LightRenderer(m_renderbackend, 90));
285  m_renderers.push_back(new CellRenderer(m_renderbackend, 100));
286 
287  FL_LOG(_log, "Creating model");
289  FL_LOG(_log, "Adding pathers to model");
291  FL_LOG(_log, "Adding grid prototypes to model");
293  m_model->adoptCellGrid(new HexGrid());
294 
296  FL_LOG(_log, "Engine intialized");
297  }
298 
300  if( !m_destroyed ) {
301  destroy();
302  }
303  }
304 
306  FL_LOG(_log, "Destructing engine");
307  delete m_cursor;
308  delete m_model;
309  delete m_soundmanager;
310  delete m_guimanager;
311 
312  delete m_imagemanager;
313  delete m_soundclipmanager;
314 // delete m_eventmanager;
315 
316  // properly remove all the renderers created during init
317  delete m_offrenderer;
318  delete m_targetrenderer;
319  std::vector<RendererBase*>::iterator rendererIter = m_renderers.begin();
320  for ( ; rendererIter != m_renderers.end(); ++rendererIter)
321  {
322  delete *rendererIter;
323  }
324  m_renderers.clear();
325 
326  delete m_renderbackend;
327  delete m_vfs;
328  delete m_timemanager;
329 
330  TTF_Quit();
331  SDL_Quit();
332 
333 #ifdef USE_COCOA
334  objc_msgSend(m_autoreleasePool, sel_registerName("release"));
335 #endif
336 
337  FL_LOG(_log, "================== Engine destructed ==================");
338  m_destroyed = true;
339  //delete m_logmanager;
340  }
343  }
344 
345  void Engine::pump() {
349 
351  if (m_model->getActiveCameraCount() == 0) {
354  } else {
355  m_model->update();
356  }
357 
358  if (m_guimanager) {
359  m_guimanager->turn();
360  }
361 
362  m_cursor->draw();
364  }
365 
367  // nothing here at the moment..
368  }
369 
371  m_changelisteners.push_back(listener);
372  }
373 
375  std::vector<IEngineChangeListener*>::iterator i = m_changelisteners.begin();
376  while (i != m_changelisteners.end()) {
377  if ((*i) == listener) {
378  m_changelisteners.erase(i);
379  return;
380  }
381  ++i;
382  }
383  }
384 }//FIFE
385 
386 /* 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
bool isGLUseMonochrome() const
Tells if OpenGL renderbackend should render only monochrome.
void pump()
Runs one cycle for the engine.
Definition: engine.cpp:345
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:370
ImageManager.
Definition: imagemanager.h:54
void setMonochromeEnabled(bool enabled)
Enables or disables monochrome rendering.
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:366
std::vector< IEngineChangeListener * > m_changelisteners
Definition: engine.h:222
Engine()
Constructor.
Definition: engine.cpp:91
void destroy()
Explicit destruction of engine.
Definition: engine.cpp:305
uint32_t getActiveCameraCount() const
Return the number of enabled cameras in this model.
Definition: model.cpp:140
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:280
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:341
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:157
EngineSettings & getSettings()
Gets settings class for engine.
Definition: engine.cpp:131
float getMouseSensitivity() const
Gets mouse sensitivity.
LogManager * m_logmanager
Definition: engine.h:208
void invalidate()
Definition: cursor.cpp:184
bool isGLUseMipmapping() const
Tells if OpenGL renderbackend should use mipmapping.
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
TextureFiltering getGLTextureFiltering() const
Gets current texture filter which uses OpenGL.
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.
void setMipmappingEnabled(bool enabled)
Enables or disables the usage of mipmapping.
void setAlphaTestValue(float alpha)
Sets the value for alpha test.
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:135
void removeChangeListener(IEngineChangeListener *listener)
Removes associated change listener.
Definition: engine.cpp:374
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.
void setTextureFiltering(TextureFiltering filter)
Sets the texture filtering method.
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
void setDepthBufferEnabled(bool enabled)
Enables or disables depth buffer rendering.
Event Manager manages all events related to FIFE.
Definition: eventmanager.h:67
Time Manager.
Definition: timemanager.h:50
virtual ~Engine()
Destructor.
Definition: engine.cpp:299
#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.
float getGLAlphaTestValue() const
Gets current alpha test value which uses OpenGL.
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
uint32_t getLightingModel() const
Gets the currently set light model.
The most basic VFSSource for "normal" filesystems.
Definition: vfsdirectory.h:44
A VFS provider for Zip archives.
Definition: zipprovider.h:43
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.
bool isGLUseDepthBuffer() const
Tells if OpenGL renderbackend should use depth buffer.
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:139