Mercurial > fife-parpg
comparison engine/extensions/basicapplication.py @ 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 | 860d81602a2a |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4a0efb7baf70 |
---|---|
1 # coding: utf-8 | |
2 | |
3 """ | |
4 The basic application and main loop. | |
5 | |
6 See the L{ApplicationBase} documentation. | |
7 """ | |
8 | |
9 import fife | |
10 import fifelog | |
11 from serializers.xmlanimation import XMLAnimationLoader | |
12 | |
13 class ExitEventListener(fife.IKeyListener): | |
14 """ | |
15 Default, rudimentary event listener. | |
16 | |
17 Will cause the application to quit on pressing ESC. | |
18 """ | |
19 def __init__(self, app): | |
20 self.app = app | |
21 self.engine = app.engine | |
22 eventmanager = self.engine.getEventManager() | |
23 eventmanager.setNonConsumableKeys([fife.Key.ESCAPE]) | |
24 fife.IKeyListener.__init__(self) | |
25 eventmanager.addKeyListener(self) | |
26 self.quitRequested = False | |
27 | |
28 def keyPressed(self, evt): | |
29 keyval = evt.getKey().getValue() | |
30 if keyval == fife.Key.ESCAPE: | |
31 self.app.quit() | |
32 | |
33 def keyReleased(self, evt): | |
34 pass | |
35 | |
36 class ApplicationBase(object): | |
37 """ | |
38 ApplicationBase is an extendable class that provides a basic environment for a FIFE-based client. | |
39 | |
40 The unextended application reads in and initializes engine settings, sets up a simple event | |
41 listener, and pumps the engine while listening for a quit message. Specialized applications can | |
42 modify settings.py to change initial engine settings. They can provide their own event listener | |
43 by overriding L{createListener}. And they can override the L{_pump} method | |
44 to define runtime behavior of the application. | |
45 | |
46 """ | |
47 def __init__(self): | |
48 self.engine = fife.Engine() | |
49 | |
50 self.loadSettings() | |
51 self.initLogging() | |
52 | |
53 self.engine.init() | |
54 | |
55 self._animationloader = XMLAnimationLoader(self.engine.getImagePool(), self.engine.getVFS()) | |
56 self.engine.getAnimationPool().addResourceLoader(self._animationloader) | |
57 | |
58 self.quitRequested = False | |
59 self.breakRequested = False | |
60 self.returnValues = [] | |
61 | |
62 def loadSettings(self): | |
63 """ | |
64 Load the settings from a python file and load them into the engine. | |
65 Called in the ApplicationBase constructor. | |
66 """ | |
67 import settings | |
68 self.settings = settings | |
69 | |
70 engineSetting = self.engine.getSettings() | |
71 engineSetting.setDefaultFontGlyphs(settings.FontGlyphs) | |
72 engineSetting.setDefaultFontPath(settings.Font) | |
73 engineSetting.setDefaultFontSize(12) | |
74 engineSetting.setBitsPerPixel(settings.BitsPerPixel) | |
75 engineSetting.setFullScreen(settings.FullScreen) | |
76 engineSetting.setInitialVolume(settings.InitialVolume) | |
77 engineSetting.setRenderBackend(settings.RenderBackend) | |
78 engineSetting.setSDLRemoveFakeAlpha(settings.SDLRemoveFakeAlpha) | |
79 engineSetting.setScreenWidth(settings.ScreenWidth) | |
80 engineSetting.setScreenHeight(settings.ScreenHeight) | |
81 try: | |
82 engineSetting.setImageChunkingSize(settings.ImageChunkSize) | |
83 except: | |
84 pass | |
85 | |
86 def initLogging(self): | |
87 """ | |
88 Initialize the LogManager. | |
89 """ | |
90 self.log = fifelog.LogManager(self.engine, self.settings.LogToPrompt, self.settings.LogToFile) | |
91 if self.settings.LogModules: | |
92 self.log.setVisibleModules(*self.settings.LogModules) | |
93 | |
94 def createListener(self): | |
95 """ | |
96 This creates a default event listener, which will just close the program | |
97 after pressing ESC. | |
98 | |
99 You should override this method to provide your own event handling. | |
100 """ | |
101 return ExitEventListener(self) | |
102 | |
103 def run(self): | |
104 """ | |
105 Initialize the event listener and event loop - and start it. | |
106 """ | |
107 eventlistener = self.createListener() | |
108 self.engine.initializePumping() | |
109 retval = self.mainLoop() | |
110 self.engine.finalizePumping() | |
111 return retval | |
112 | |
113 def mainLoop(self): | |
114 """ | |
115 The programs main loop. | |
116 | |
117 Do not override this, instead provide your own L{_pump} method. | |
118 You can call this recursively, e.g. to provide synchronous | |
119 Dialogs :-) and break out of the current mainLoop by calling | |
120 L{breakFromMainLoop}. It will return the argument passed | |
121 to L{breakFromMainLoop}. | |
122 """ | |
123 self.returnValues.append(None) | |
124 while not self.quitRequested: | |
125 try: | |
126 self.engine.pump() | |
127 except RuntimeError, e: | |
128 print str(e) | |
129 | |
130 self._pump() | |
131 | |
132 if self.breakRequested: | |
133 self.breakRequested = False | |
134 break | |
135 | |
136 return self.returnValues.pop() | |
137 | |
138 def breakFromMainLoop(self,returnValue): | |
139 """ | |
140 Break from the currently running L{mainLoop}. | |
141 | |
142 The passed argument will be returned by the mainLoop. | |
143 """ | |
144 self.returnValues[-1] = returnValue | |
145 self.breakRequested = True | |
146 | |
147 | |
148 def _pump(self): | |
149 """ | |
150 Application pump. | |
151 | |
152 Derived classes can specialize this for unique behavior. | |
153 This is called every frame. | |
154 """ | |
155 | |
156 def quit(self): | |
157 """ | |
158 Quit the application. Really! | |
159 """ | |
160 self.quitRequested = True |