comparison engine/core/controller/engine.cpp @ 0:4a0efb7baf70

* Datasets becomes the new trunk and retires after that :-)
author mvbarracuda@33b003aa-7bff-0310-803a-e67f0ece8222
date Sun, 29 Jun 2008 18:44:17 +0000
parents
children ae46cee19e76
comparison
equal deleted inserted replaced
-1:000000000000 0:4a0efb7baf70
1 /***************************************************************************
2 * Copyright (C) 2005-2008 by the FIFE team *
3 * http://www.fifengine.de *
4 * This file is part of FIFE. *
5 * *
6 * FIFE is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program 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 *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the *
18 * Free Software Foundation, Inc., *
19 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
20 ***************************************************************************/
21
22 // Standard C++ library includes
23 #include <iostream>
24
25 // 3rd party library includes
26 #include <SDL.h>
27 #include <SDL_ttf.h>
28
29 // FIFE includes
30 // These includes are split up in two parts, separated by one empty line
31 // First block: files included from the FIFE root src directory
32 // Second block: files included from the same folder
33 #include "util/base/exception.h"
34 #include "util/log/logger.h"
35 #include "util/time/timemanager.h"
36 #include "audio/soundmanager.h"
37 #include "gui/console/console.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
45 #include "eventchannel/eventmanager.h"
46 #include "video/imagepool.h"
47 #include "video/animationpool.h"
48 #include "audio/soundclippool.h"
49 #include "video/renderbackend.h"
50 #include "video/cursor.h"
51 #ifdef HAVE_OPENGL
52 #include "video/opengl/renderbackendopengl.h"
53 #include "gui/base/opengl/opengl_gui_graphics.h"
54 #endif
55 #include "gui/base/sdl/sdl_gui_graphics.h"
56 #include "gui/base/gui_font.h"
57 #include "video/sdl/renderbackendsdl.h"
58 #include "video/fonts/abstractfont.h"
59 #include "loaders/native/video_loaders/subimage_loader.h"
60 #include "loaders/native/video_loaders/image_loader.h"
61 #include "loaders/native/audio_loaders/ogg_loader.h"
62 //#include "loaders/fallout/model_loaders/dat1.h"
63 //#include "loaders/fallout/model_loaders/dat2.h"
64 #include "model/model.h"
65 #include "pathfinder/linearpather/linearpather.h"
66 #include "pathfinder/routepather/routepather.h"
67
68 #include "view/view.h"
69 #include "view/renderers/camerazonerenderer.h"
70 #include "view/renderers/quadtreerenderer.h"
71 #include "view/renderers/gridrenderer.h"
72 #include "view/renderers/instancerenderer.h"
73 #include "view/renderers/coordinaterenderer.h"
74 #include "view/renderers/floatingtextrenderer.h"
75 #include "view/renderers/cellselectionrenderer.h"
76 #include "view/renderers/blockinginforenderer.h"
77 #include "engine.h"
78
79 #ifdef USE_COCOA
80 #include <dlfcn.h>
81
82 int main(int argc, char **argv)
83 {
84 return 0;
85 }
86 #endif
87
88 namespace FIFE {
89 static Logger _log(LM_CONTROLLER);
90
91 Engine::Engine():
92 m_renderbackend(0),
93 m_guimanager(0),
94 m_eventmanager(0),
95 m_soundmanager(0),
96 m_timemanager(0),
97 m_imagepool(0),
98 m_animpool(0),
99 m_soundclippool(0),
100 m_vfs(0),
101 m_model(0),
102 m_gui_graphics(0),
103 m_view(0),
104 m_logmanager(0),
105 m_cursor(0),
106 m_settings() {
107 #ifdef USE_COCOA
108 // The next lines ensure that Cocoa is initialzed correctly.
109 // This is needed for SDL to function properly on MAC OS X.
110 void* cocoa_lib;
111 cocoa_lib = dlopen( "/System/Library/Frameworks/Cocoa.framework/Cocoa", RTLD_LAZY );
112 void (*nsappload)(void);
113 nsappload = (void(*)()) dlsym( cocoa_lib, "NSApplicationLoad");
114 nsappload();
115 #endif
116 preInit();
117 }
118
119 EngineSettings& Engine::getSettings() {
120 return m_settings;
121 }
122
123 void Engine::preInit() {
124 m_logmanager = LogManager::instance();
125
126 FL_LOG(_log, "================== Engine pre-init start =================");
127 m_timemanager = new TimeManager();
128 FL_LOG(_log, "Time manager created");
129
130 FL_LOG(_log, "Creating VFS");
131 m_vfs = new VFS();
132
133 FL_LOG(_log, "Adding root directory to VFS");
134 m_vfs->addSource( new VFSDirectory(m_vfs) );
135 m_vfs->addProvider( new DirectoryProvider() );
136 #ifdef HAVE_ZIP
137 FL_LOG(_log, "Adding zip provider to VFS");
138 m_vfs->addProvider( new ZipProvider() );
139 #endif
140 //m_vfs->addProvider(ProviderDAT2());
141 //m_vfs->addProvider(ProviderDAT1());
142 FL_LOG(_log, "Engine pre-init done");
143 }
144
145 void Engine::init() {
146 FL_LOG(_log, "Engine initialize start");
147 m_settings.validate();
148 FL_LOG(_log, "Engine settings validated");
149
150 // If failed to init SDL throw exception.
151 if (SDL_Init(SDL_INIT_NOPARACHUTE | SDL_INIT_TIMER) < 0) {
152 throw SDLException(SDL_GetError());
153 }
154
155 SDL_EnableUNICODE(1);
156 SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
157 TTF_Init();
158
159 FL_LOG(_log, "Creating event manager");
160 m_eventmanager = new EventManager();
161
162 FL_LOG(_log, "Creating pools");
163 m_imagepool = new ImagePool();
164 m_animpool = new AnimationPool();
165 m_soundclippool = new SoundClipPool();
166 m_imagepool->addResourceLoader(new SubImageLoader());
167 m_imagepool->addResourceLoader(new ImageLoader(m_vfs));
168 m_soundclippool->addResourceLoader(new OggLoader(m_vfs));
169
170 FL_LOG(_log, "Creating render backend");
171 std::string rbackend(m_settings.getRenderBackend());
172 if (rbackend == "SDL") {
173 m_renderbackend = new RenderBackendSDL();
174 FL_LOG(_log, "SDL Render backend created");
175 } else {
176 #ifdef HAVE_OPENGL
177 m_renderbackend = new RenderBackendOpenGL();
178 FL_LOG(_log, "OpenGL Render backend created");
179 #else
180 m_renderbackend = new RenderBackendSDL();
181 // Remember the choice so we pick the right graphics class.
182 rbackend = "SDL";
183 FL_WARN(_log, "Tried to select OpenGL, even though it is not compiled into the engine. Falling back to SDL Render backend");
184 #endif
185 }
186 FL_LOG(_log, "Initializing render backend");
187 m_renderbackend->setChunkingSize(m_settings.getImageChunkingSize());
188 m_renderbackend->init();
189
190 FL_LOG(_log, "Creating main screen");
191 m_renderbackend->createMainScreen(
192 m_settings.getScreenWidth(),
193 m_settings.getScreenHeight(),
194 static_cast<unsigned char>(m_settings.getBitsPerPixel()),
195 m_settings.isFullScreen());
196 FL_LOG(_log, "Main screen created");
197
198 #ifdef HAVE_OPENGL
199 if( rbackend != "SDL" ) {
200 m_gui_graphics = new OpenGLGuiGraphics(*m_imagepool);
201 }
202 #endif
203 if( rbackend == "SDL" ) {
204 m_gui_graphics = new SdlGuiGraphics(*m_imagepool);
205 }
206 FL_LOG(_log, "Constructing GUI manager");
207 m_guimanager = new GUIManager(m_eventmanager, *m_imagepool);
208 m_eventmanager->addSdlEventListener(m_guimanager);
209 // keep guimanager as the first key listener so that it can evaluate guichan hits
210 m_eventmanager->addKeyListener(m_guimanager);
211 // keep guimanager as the first mouse listener so that it can evaluate guichan hits
212 m_eventmanager->addMouseListener(m_guimanager);
213 FL_LOG(_log, "Events bind to GUI manager");
214
215 FL_LOG(_log, "Creating default font");
216 m_defaultfont = m_guimanager->setDefaultFont(
217 m_settings.getDefaultFontPath(),
218 m_settings.getDefaultFontSize(),
219 m_settings.getDefaultFontGlyphs());
220 FL_LOG(_log, "Initializing GUI manager");
221 m_guimanager->init(m_gui_graphics, m_settings.getScreenWidth(), m_settings.getScreenHeight());
222 FL_LOG(_log, "GUI manager initialized");
223 SDL_EnableUNICODE(1);
224
225 FL_LOG(_log, "Creating sound manager");
226 m_soundmanager = new SoundManager(m_soundclippool);
227 m_soundmanager->setVolume(static_cast<float>(m_settings.getInitialVolume()) / 10);
228
229 FL_LOG(_log, "Creating model");
230 m_model = new Model();
231 FL_LOG(_log, "Adding pathers to model");
232 m_model->adoptPather(new LinearPather());
233 m_model->adoptPather(new RoutePather());
234
235 FL_LOG(_log, "Creating view");
236 m_view = new View(m_renderbackend, m_imagepool, m_animpool);
237 FL_LOG(_log, "Creating renderers to view");
238 m_view->addRenderer(new CameraZoneRenderer(m_renderbackend, 0, m_imagepool));
239 m_view->addRenderer(new InstanceRenderer(m_renderbackend, 10, m_imagepool, m_animpool));
240 m_view->addRenderer(new GridRenderer(m_renderbackend, 20));
241 m_view->addRenderer(new CellSelectionRenderer(m_renderbackend, 30));
242 m_view->addRenderer(new BlockingInfoRenderer(m_renderbackend, 40));
243 m_view->addRenderer(new FloatingTextRenderer(m_renderbackend, 50, dynamic_cast<AbstractFont*>(m_defaultfont)));
244 m_view->addRenderer(new QuadTreeRenderer(m_renderbackend, 60));
245 m_view->addRenderer(new CoordinateRenderer(m_renderbackend, 70, dynamic_cast<AbstractFont*>(m_defaultfont)));
246 m_cursor = new Cursor(m_imagepool, m_animpool, m_renderbackend);
247 FL_LOG(_log, "Engine intialized");
248 }
249
250 Engine::~Engine() {
251 FL_LOG(_log, "Destructing engine");
252 delete m_cursor;
253 delete m_view;
254 delete m_model;
255 delete m_soundmanager;
256 delete m_guimanager;
257 delete m_gui_graphics;
258
259 m_renderbackend->deinit();
260 delete m_renderbackend;
261
262 // Note the dependancy between image and animation pools
263 // as animations reference images they have to be deleted
264 // before clearing the image pool.
265 delete m_animpool;
266 delete m_imagepool;
267 delete m_eventmanager;
268
269 delete m_vfs;
270
271 delete m_timemanager;
272
273 TTF_Quit();
274 SDL_Quit();
275 FL_LOG(_log, "================== Engine destructed ==================");
276 //delete m_logmanager;
277 }
278 void Engine::initializePumping() {
279 m_eventmanager->processEvents();
280 }
281
282 void Engine::pump() {
283 m_renderbackend->startFrame();
284 m_timemanager->update();
285 m_model->update();
286 m_view->update();
287 m_guimanager->turn();
288 m_cursor->draw();
289 m_renderbackend->endFrame();
290 SDL_Delay(1);
291 m_eventmanager->processEvents();
292 }
293
294 void Engine::finalizePumping() {
295 // nothing here at the moment..
296 }
297 }//FIFE
298
299 /* vim: set noexpandtab: set shiftwidth=2: set tabstop=2: */