| Home | Trees | Indices | Help |
|---|
|
|
1 # -*- coding: utf-8 -*-
2
3 # ####################################################################
4 # Copyright (C) 2005-2009 by the FIFE team
5 # http://www.fifengine.de
6 # This file is part of FIFE.
7 #
8 # FIFE is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU Lesser General Public
10 # License as published by the Free Software Foundation; either
11 # version 2.1 of the License, or (at your option) any later version.
12 #
13 # This library is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # Lesser General Public License for more details.
17 #
18 # You should have received a copy of the GNU Lesser General Public
19 # License along with this library; if not, write to the
20 # Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 # ####################################################################
23
24 """
25 The basic application and main loop.
26
27 See the L{ApplicationBase} documentation.
28 """
29
30 from fife import fife
31 from fife.extensions import fifelog
32 from fife.extensions import pychan
33 from fife.extensions.fife_settings import Setting
34
36 """
37 Default, rudimentary event listener.
38
39 Will cause the application to quit on pressing ESC.
40 """
42 self.app = app
43 self.engine = app.engine
44 eventmanager = self.engine.getEventManager()
45 #eventmanager.setNonConsumableKeys([fife.Key.ESCAPE])
46 fife.IKeyListener.__init__(self)
47 eventmanager.addKeyListener(self)
48 self.quitRequested = False
49
51 keyval = evt.getKey().getValue()
52 if keyval == fife.Key.ESCAPE:
53 self.app.quit()
54 elif keyval == fife.Key.F10:
55 pychan.manager.hook.guimanager.getConsole().toggleShowHide()
56 evt.consume()
57
60
62 """
63 ApplicationBase is an extendable class that provides a basic environment for a FIFE-based client.
64
65 The unextended application reads in and initializes engine settings, sets up a simple event
66 listener, and pumps the engine while listening for a quit message. Specialized applications can
67 modify settings.py to change initial engine settings. They can provide their own event listener
68 by overriding L{createListener}. And they can override the L{_pump} method
69 to define runtime behavior of the application.
70
71 """
73 if setting:
74 self._setting = setting
75 else:
76 self._setting = Setting(app_name="", settings_file="./settings.xml", settings_gui_xml="")
77
78 self.engine = fife.Engine()
79
80 self.initLogging()
81 self.loadSettings()
82
83 self.engine.init()
84
85 """
86 we are giving users a valid screen resolution option that is supported
87 """
88 screen_modes = self.engine.getDeviceCaps().getSupportedScreenModes()
89 resolutions = list(set([(mode.getWidth(), mode.getHeight())
90 for mode in screen_modes]))
91
92 resolutions = ["{0}x{1}".format(item[0], item[1]) for item in sorted(resolutions)[1:]]
93 self._setting.setValidResolutions(resolutions)
94
95 pychan.init(self.engine, debug = self._finalSetting['PychanDebug'])
96 pychan.setupModalExecution(self.mainLoop,self.breakFromMainLoop)
97
98 self.quitRequested = False
99 self.breakRequested = False
100 self.returnValues = []
101
103 """
104 Load the settings from a python file and load them into the engine.
105 Called in the ApplicationBase constructor.
106 """
107
108
109 # get finalSetting (from the xml file, or if absent the default value)
110 self._finalSetting = self._setting.getSettingsFromFile("FIFE", self._log)
111
112 engineSetting = self.engine.getSettings()
113
114 engineSetting.setDefaultFontGlyphs(self._finalSetting['FontGlyphs'])
115 engineSetting.setDefaultFontPath(self._finalSetting['Font'])
116 engineSetting.setDefaultFontSize(self._finalSetting['DefaultFontSize'])
117 engineSetting.setBitsPerPixel(self._finalSetting['BitsPerPixel'])
118 engineSetting.setInitialVolume(self._finalSetting['InitialVolume'])
119 engineSetting.setSDLRemoveFakeAlpha(self._finalSetting['SDLRemoveFakeAlpha'])
120 engineSetting.setGLCompressImages(self._finalSetting['GLCompressImages'])
121 engineSetting.setGLUseFramebuffer(self._finalSetting['GLUseFramebuffer'])
122 engineSetting.setGLUseNPOT(self._finalSetting['GLUseNPOT'])
123 (width, height) = self._finalSetting['ScreenResolution'].split('x')
124 engineSetting.setScreenWidth(int(width))
125 engineSetting.setScreenHeight(int(height))
126 engineSetting.setRenderBackend(self._finalSetting['RenderBackend'])
127 engineSetting.setFullScreen(self._finalSetting['FullScreen'])
128 engineSetting.setVideoDriver(self._finalSetting['VideoDriver'])
129 engineSetting.setLightingModel(self._finalSetting['Lighting'])
130
131 try:
132 engineSetting.setColorKeyEnabled(self._finalSetting['ColorKeyEnabled'])
133 except:
134 pass
135
136 try:
137 engineSetting.setColorKey(self._finalSetting['ColorKey'][0],self._finalSetting['ColorKey'][1],self._finalSetting['ColorKey'][2])
138 except:
139 pass
140
141 try:
142 engineSetting.setWindowTitle(self._finalSetting['WindowTitle'])
143 engineSetting.setWindowIcon(self._finalSetting['WindowIcon'])
144 except:
145 pass
146
147 try:
148 engineSetting.setFrameLimitEnabled(self._finalSetting['FrameLimitEnabled'])
149 engineSetting.setFrameLimit(self._finalSetting['FrameLimit'])
150 except:
151 pass
152
153 try:
154 engineSetting.setMouseSensitivity(self._finalSetting['MouseSensitivity'])
155 except:
156 pass
157
158 try:
159 engineSetting.setMouseAcceleration(self._finalSetting['MouseAcceleration'])
160 except:
161 pass
162
163
165 """
166 Initialize the LogManager.
167 """
168
169 engineSetting = self.engine.getSettings()
170 logmodules = self._setting.get("FIFE", "LogModules", ["controller"])
171
172 #log to both the console and log file
173 self._log = fifelog.LogManager(self.engine,
174 self._setting.get("FIFE", "LogToPrompt", "0"),
175 self._setting.get("FIFE", "LogToFile", "0"))
176
177 self._log.setLevelFilter(self._setting.get("FIFE", "LogLevelFilter", fife.LogManager.LEVEL_DEBUG))
178
179 if logmodules:
180 self._log.setVisibleModules(*logmodules)
181
183 """
184 This creates a default event listener, which will just close the program
185 after pressing ESC.
186
187 You should override this method to provide your own event handling.
188 """
189 return ExitEventListener(self)
190
192 """
193 Initialize the event listener and event loop - and start it.
194 """
195 eventlistener = self.createListener()
196 self.engine.initializePumping()
197 retval = self.mainLoop()
198 self.engine.finalizePumping()
199 self.engine.destroy()
200 return retval
201
203 """
204 The programs main loop.
205
206 Do not override this, instead provide your own L{_pump} method.
207 You can call this recursively, e.g. to provide synchronous
208 Dialogs :-) and break out of the current mainLoop by calling
209 L{breakFromMainLoop}. It will return the argument passed
210 to L{breakFromMainLoop}.
211 """
212 self.returnValues.append(None)
213 while not self.quitRequested:
214 try:
215 self.engine.pump()
216 except RuntimeError, e:
217 print str(e)
218
219 self._pump()
220
221 if self.breakRequested:
222 self.breakRequested = False
223 break
224
225 return self.returnValues.pop()
226
228 """
229 Break from the currently running L{mainLoop}.
230
231 The passed argument will be returned by the mainLoop.
232 """
233 self.returnValues[-1] = returnValue
234 self.breakRequested = True
235
236
238 """
239 Application pump.
240
241 Derived classes can specialize this for unique behavior.
242 This is called every frame.
243 """
244
250
| Home | Trees | Indices | Help |
|---|
| Generated by Epydoc 3.0.1 on Tue May 22 00:00:35 2012 | http://epydoc.sourceforge.net |