comparison demos/rio_de_hola/run.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 70697641fca3
comparison
equal deleted inserted replaced
377:fe6fb0e0ed23 378:64738befdf3b
1 #!/usr/bin/env python
2 # coding: utf-8
3 # This is the rio de hola client for FIFE.
4
5 import sys, os, re, math, random, shutil
6
7 from fife import fife
8 from fife.extensions import *
9 from scripts import world
10 from scripts.common import eventlistenerbase
11 from fife.extensions.basicapplication import ApplicationBase
12 from fife.extensions import pychan
13 from fife.extensions.pychan import widgets
14 from settings import Setting
15 from fife.extensions.fife_utils import getUserDataDirectory
16
17 TDS = Setting()
18
19 class ApplicationListener(eventlistenerbase.EventListenerBase):
20 def __init__(self, engine, world):
21 super(ApplicationListener, self).__init__(engine,regKeys=True,regCmd=True, regMouse=False, regConsole=True, regWidget=True)
22 self.engine = engine
23 self.world = world
24 engine.getEventManager().setNonConsumableKeys([
25 fife.Key.ESCAPE,])
26
27 self.quit = False
28 self.aboutWindow = None
29
30 self.rootpanel = pychan.loadXML('gui/rootpanel.xml')
31 self.rootpanel.mapEvents({
32 'quitButton' : self.onQuitButtonPress,
33 'aboutButton' : self.onAboutButtonPress,
34 'optionsButton' : TDS.onOptionsPress
35 })
36 self.rootpanel.show()
37
38 def keyPressed(self, evt):
39 print evt
40 keyval = evt.getKey().getValue()
41 keystr = evt.getKey().getAsString().lower()
42 consumed = False
43 if keyval == fife.Key.ESCAPE:
44 self.quit = True
45 evt.consume()
46 elif keyval == fife.Key.F10:
47 self.engine.getGuiManager().getConsole().toggleShowHide()
48 evt.consume()
49 elif keystr == 'p':
50 self.engine.getRenderBackend().captureScreen('screenshot.png')
51 evt.consume()
52
53 def onCommand(self, command):
54 self.quit = (command.getCommandType() == fife.CMD_QUIT_GAME)
55 if self.quit:
56 command.consume()
57
58 def onConsoleCommand(self, command):
59 result = ''
60 if command.lower() in ('quit', 'exit'):
61 self.quit = True
62 result = 'quitting'
63 elif command.lower() in ( 'help', 'help()' ):
64 self.engine.getGuiManager().getConsole().println( open( 'misc/infotext.txt', 'r' ).read() )
65 result = "-- End of help --"
66 else:
67 result = self.world.onConsoleCommand(command)
68 if not result:
69 try:
70 result = str(eval(command))
71 except:
72 pass
73 if not result:
74 result = 'no result'
75 return result
76
77 def onQuitButtonPress(self):
78 cmd = fife.Command()
79 cmd.setSource(None)
80 cmd.setCommandType(fife.CMD_QUIT_GAME)
81 self.engine.getEventManager().dispatchCommand(cmd)
82
83 def onAboutButtonPress(self):
84 if not self.aboutWindow:
85 self.aboutWindow = pychan.loadXML('gui/help.xml')
86 self.aboutWindow.mapEvents({ 'closeButton' : self.aboutWindow.hide })
87 self.aboutWindow.distributeData({ 'helpText' : open("misc/infotext.txt").read() })
88 self.aboutWindow.show()
89
90 class IslandDemo(ApplicationBase):
91 def __init__(self):
92 super(IslandDemo,self).__init__()
93 pychan.init(self.engine, debug=TDS.readSetting("PychanDebug", type='bool'))
94 self.world = world.World(self.engine)
95 self.listener = ApplicationListener(self.engine, self.world)
96 self.world.load(str(TDS.readSetting("MapFile")))
97
98 self.soundmanager = self.engine.getSoundManager()
99 self.soundmanager.init()
100
101 if int(TDS.readSetting("PlaySounds")):
102 # play track as background music
103 emitter = self.soundmanager.createEmitter()
104 id = self.engine.getSoundClipPool().addResourceFromFile('music/rio_de_hola.ogg')
105 emitter.setSoundClip(id)
106 emitter.setLooping(True)
107 emitter.play()
108
109 def loadSettings(self):
110 """
111 Load the settings from a python file and load them into the engine.
112 Called in the ApplicationBase constructor.
113 """
114 import settings
115 self.settings = settings
116
117 engineSetting = self.engine.getSettings()
118 engineSetting.setDefaultFontGlyphs(str(TDS.readSetting("FontGlyphs", strip=False)))
119 engineSetting.setDefaultFontPath(str(TDS.readSetting("Font")))
120 engineSetting.setDefaultFontSize(12)
121 engineSetting.setBitsPerPixel(int(TDS.readSetting("BitsPerPixel")))
122 engineSetting.setInitialVolume(float(TDS.readSetting("InitialVolume")))
123 engineSetting.setSDLRemoveFakeAlpha(int(TDS.readSetting("SDLRemoveFakeAlpha")))
124 engineSetting.setScreenWidth(int(TDS.readSetting("ScreenWidth")))
125 engineSetting.setScreenHeight(int(TDS.readSetting("ScreenHeight")))
126 engineSetting.setRenderBackend(str(TDS.readSetting("RenderBackend")))
127 engineSetting.setFullScreen(int(TDS.readSetting("FullScreen")))
128
129 try:
130 engineSetting.setWindowTitle(str(TDS.readSetting("WindowTitle")))
131 engineSetting.setWindowIcon(str(TDS.readSetting("WindowIcon")))
132 except:
133 pass
134 try:
135 engineSetting.setImageChunkingSize(int(TDS.readSetting("ImageChunkSize")))
136 except:
137 pass
138
139 def initLogging(self):
140 """
141 Initialize the LogManager.
142 """
143 LogModules = TDS.readSetting("LogModules", type='list')
144 self.log = fifelog.LogManager(self.engine, int(TDS.readSetting("LogToPrompt")), int(TDS.readSetting("LogToFile")))
145 if LogModules:
146 self.log.setVisibleModules(*LogModules)
147
148 def createListener(self):
149 pass # already created in constructor
150
151 def _pump(self):
152 if self.listener.quit:
153 self.breakRequested = True
154
155 # get the correct directory to save the map file to
156 mapSaveDir = getUserDataDirectory("fife", "rio_de_hola") + "/maps"
157
158 # create the directory structure if it does not exist
159 if not os.path.isdir(mapSaveDir):
160 os.makedirs(mapSaveDir)
161
162 # save map file to directory
163 self.world.save(mapSaveDir + "/savefile.xml")
164 else:
165 self.world.pump()
166
167 def main():
168 app = IslandDemo()
169 app.run()
170
171
172 if __name__ == '__main__':
173 if TDS.readSetting("ProfilingOn", type='bool'):
174 import hotshot, hotshot.stats
175 print "Starting profiler"
176 prof = hotshot.Profile("fife.prof")
177 prof.runcall(main)
178 prof.close()
179 print "analysing profiling results"
180 stats = hotshot.stats.load("fife.prof")
181 stats.strip_dirs()
182 stats.sort_stats('time', 'calls')
183 stats.print_stats(20)
184 else:
185 if TDS.readSetting("UsePsyco", type='bool'):
186 # Import Psyco if available
187 try:
188 import psyco
189 psyco.full()
190 print "Psyco acceleration in use"
191 except ImportError:
192 print "Psyco acceleration not used"
193 else:
194 print "Psyco acceleration not used"
195 main()