Mercurial > fife-parpg
comparison engine/python/fife/extensions/basicapplication.py @ 378:64738befdf3b
bringing in the changes from the build_system_rework branch in preparation for the 0.3.0 release. This commit will require the Jan2010 devkit. Clients will also need to be modified to the new way to import fife.
author | vtchill@33b003aa-7bff-0310-803a-e67f0ece8222 |
---|---|
date | Mon, 11 Jan 2010 23:34:52 +0000 |
parents | |
children | ad7969d9460b |
comparison
equal
deleted
inserted
replaced
377:fe6fb0e0ed23 | 378:64738befdf3b |
---|---|
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.serializers.xmlanimation import XMLAnimationLoader | |
33 | |
34 class ExitEventListener(fife.IKeyListener): | |
35 """ | |
36 Default, rudimentary event listener. | |
37 | |
38 Will cause the application to quit on pressing ESC. | |
39 """ | |
40 def __init__(self, app): | |
41 self.app = app | |
42 self.engine = app.engine | |
43 eventmanager = self.engine.getEventManager() | |
44 #eventmanager.setNonConsumableKeys([fife.Key.ESCAPE]) | |
45 fife.IKeyListener.__init__(self) | |
46 eventmanager.addKeyListener(self) | |
47 self.quitRequested = False | |
48 | |
49 def keyPressed(self, evt): | |
50 keyval = evt.getKey().getValue() | |
51 if keyval == fife.Key.ESCAPE: | |
52 self.app.quit() | |
53 elif keyval == fife.Key.F10: | |
54 self.engine.getGuiManager().getConsole().toggleShowHide() | |
55 evt.consume() | |
56 | |
57 def keyReleased(self, evt): | |
58 pass | |
59 | |
60 class ApplicationBase(object): | |
61 """ | |
62 ApplicationBase is an extendable class that provides a basic environment for a FIFE-based client. | |
63 | |
64 The unextended application reads in and initializes engine settings, sets up a simple event | |
65 listener, and pumps the engine while listening for a quit message. Specialized applications can | |
66 modify settings.py to change initial engine settings. They can provide their own event listener | |
67 by overriding L{createListener}. And they can override the L{_pump} method | |
68 to define runtime behavior of the application. | |
69 | |
70 """ | |
71 def __init__(self): | |
72 self.engine = fife.Engine() | |
73 | |
74 self.loadSettings() | |
75 self.initLogging() | |
76 | |
77 self.engine.init() | |
78 | |
79 self._animationloader = XMLAnimationLoader(self.engine.getImagePool(), self.engine.getVFS()) | |
80 self.engine.getAnimationPool().addResourceLoader(self._animationloader) | |
81 | |
82 self.quitRequested = False | |
83 self.breakRequested = False | |
84 self.returnValues = [] | |
85 | |
86 def loadSettings(self): | |
87 """ | |
88 Load the settings from a python file and load them into the engine. | |
89 Called in the ApplicationBase constructor. | |
90 """ | |
91 import settings | |
92 self.settings = settings | |
93 | |
94 engineSetting = self.engine.getSettings() | |
95 engineSetting.setDefaultFontGlyphs(settings.FontGlyphs) | |
96 engineSetting.setDefaultFontPath(settings.Font) | |
97 engineSetting.setDefaultFontSize(12) | |
98 engineSetting.setBitsPerPixel(settings.BitsPerPixel) | |
99 engineSetting.setFullScreen(settings.FullScreen) | |
100 engineSetting.setInitialVolume(settings.InitialVolume) | |
101 engineSetting.setRenderBackend(settings.RenderBackend) | |
102 engineSetting.setSDLRemoveFakeAlpha(settings.SDLRemoveFakeAlpha) | |
103 engineSetting.setScreenWidth(settings.ScreenWidth) | |
104 engineSetting.setScreenHeight(settings.ScreenHeight) | |
105 | |
106 | |
107 try: | |
108 engineSetting.setWindowTitle(settings.WindowTitle) | |
109 engineSetting.setWindowIcon(settings.WindowIcon) | |
110 except: | |
111 pass | |
112 try: | |
113 engineSetting.setImageChunkingSize(settings.ImageChunkSize) | |
114 except: | |
115 pass | |
116 | |
117 def initLogging(self): | |
118 """ | |
119 Initialize the LogManager. | |
120 """ | |
121 self.log = fifelog.LogManager(self.engine, self.settings.LogToPrompt, self.settings.LogToFile) | |
122 if self.settings.LogModules: | |
123 self.log.setVisibleModules(*self.settings.LogModules) | |
124 | |
125 def createListener(self): | |
126 """ | |
127 This creates a default event listener, which will just close the program | |
128 after pressing ESC. | |
129 | |
130 You should override this method to provide your own event handling. | |
131 """ | |
132 return ExitEventListener(self) | |
133 | |
134 def run(self): | |
135 """ | |
136 Initialize the event listener and event loop - and start it. | |
137 """ | |
138 eventlistener = self.createListener() | |
139 self.engine.initializePumping() | |
140 retval = self.mainLoop() | |
141 self.engine.finalizePumping() | |
142 self.engine.destroy() | |
143 return retval | |
144 | |
145 def mainLoop(self): | |
146 """ | |
147 The programs main loop. | |
148 | |
149 Do not override this, instead provide your own L{_pump} method. | |
150 You can call this recursively, e.g. to provide synchronous | |
151 Dialogs :-) and break out of the current mainLoop by calling | |
152 L{breakFromMainLoop}. It will return the argument passed | |
153 to L{breakFromMainLoop}. | |
154 """ | |
155 self.returnValues.append(None) | |
156 while not self.quitRequested: | |
157 try: | |
158 self.engine.pump() | |
159 except RuntimeError, e: | |
160 print str(e) | |
161 | |
162 self._pump() | |
163 | |
164 if self.breakRequested: | |
165 self.breakRequested = False | |
166 break | |
167 | |
168 return self.returnValues.pop() | |
169 | |
170 def breakFromMainLoop(self,returnValue): | |
171 """ | |
172 Break from the currently running L{mainLoop}. | |
173 | |
174 The passed argument will be returned by the mainLoop. | |
175 """ | |
176 self.returnValues[-1] = returnValue | |
177 self.breakRequested = True | |
178 | |
179 | |
180 def _pump(self): | |
181 """ | |
182 Application pump. | |
183 | |
184 Derived classes can specialize this for unique behavior. | |
185 This is called every frame. | |
186 """ | |
187 | |
188 def quit(self): | |
189 """ | |
190 Quit the application. Really! | |
191 """ | |
192 self.quitRequested = True |