source: trunk/demo/run.py @ 32

Revision 32, 4.6 KB checked in by tie_parpg, 11 years ago (diff)
  • Enabled game closing via the window decoration button
  • Moved the 'magic path code' atrocity to scripts/common/utils.py
  • Property svn:executable set to *
Line 
1#!/usr/bin/python
2
3#   This program is free software: you can redistribute it and/or modify
4#   it under the terms of the GNU General Public License as published by
5#   the Free Software Foundation, either version 3 of the License, or
6#   (at your option) any later version.
7
8#   This program is distributed in the hope that it will be useful,
9#   but WITHOUT ANY WARRANTY; without even the implied warranty of
10#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11#   GNU General Public License for more details.
12
13#   You should have received a copy of the GNU General Public License
14#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
16import sys, os, shutil
17
18from scripts.common import utils
19utils.add_paths ('../../engine/swigwrappers/python', '../../engine/extensions')
20 #magic code to make swig work ;-)
21#def _jp(path):
22    #return os.path.sep.join(path.split('/'))
23
24#_paths = ('../../engine/swigwrappers/python', '../../engine/extensions')
25#for p in _paths:
26    #if p not in sys.path:
27        #sys.path.append(_jp(p))
28
29import fife_compat
30import fife, fifelog
31from scripts import world
32from scripts.common import eventlistenerbase
33from basicapplication import ApplicationBase
34from settings import Setting
35
36TDS = Setting()
37
38class ApplicationListener(eventlistenerbase.EventListenerBase):
39    def __init__(self, engine, world):
40        super(ApplicationListener, self).__init__(engine,
41                                                  regKeys=True,regCmd=True, regMouse=False, 
42                                                  regConsole=True, regWidget=True)
43        self.engine = engine
44        self.world = world
45        engine.getEventManager().setNonConsumableKeys([fife.Key.ESCAPE,])
46        self.quit = False
47        self.aboutWindow = None
48
49    def keyPressed(self, evt):
50        """Function to deal with keypress events
51           @param evt: The event that was captured"""
52        keyval = evt.getKey().getValue()
53        if keyval == fife.Key.ESCAPE:
54            self.quit = True
55        evt.consume()
56         
57    def onCommand(self, command):
58        """Enables the game to be closed via the 'X' button on the window frame"""
59        self.quit = (command.getCommandType() == fife.CMD_QUIT_GAME)
60        if self.quit:
61            command.consume()
62
63class PARPG(ApplicationBase):
64    """Main Application class"""
65    def __init__(self):
66        super(PARPG,self).__init__()
67        self.world = world.World(self.engine)
68        self.listener = ApplicationListener(self.engine, self.world)
69        self.world.load(str(TDS.readSetting("MapFile")))
70
71    def loadSettings(self):
72        """Load the settings from a python file and load them into the engine.
73           Called in the ApplicationBase constructor."""
74        import settings
75        self.settings = settings
76
77        eSet = self.engine.getSettings()
78        eSet.setDefaultFontGlyphs(str(TDS.readSetting("FontGlyphs",strip=False)))
79        eSet.setDefaultFontPath(str(TDS.readSetting("Font")))
80        eSet.setBitsPerPixel(int(TDS.readSetting("BitsPerPixel")))
81        eSet.setInitialVolume(float(TDS.readSetting("InitialVolume")))
82        eSet.setSDLRemoveFakeAlpha(int(TDS.readSetting("SDLRemoveFakeAlpha")))
83        eSet.setScreenWidth(int(TDS.readSetting("ScreenWidth")))
84        eSet.setScreenHeight(int(TDS.readSetting("ScreenHeight")))
85        eSet.setRenderBackend(str(TDS.readSetting("RenderBackend")))
86        eSet.setFullScreen(int(TDS.readSetting("FullScreen")))
87        try:
88            eSet.setWindowTitle(str(TDS.readSetting("WindowTitle")))
89            eSet.setWindowIcon(str(TDS.readSetting("WindowIcon")))
90        except:
91            pass           
92        try:
93            eSet.setImageChunkingSize(int(TDS.readSetting("ImageChunkSize")))
94        except:
95            pass
96
97    def initLogging(self):
98        """Initialize the LogManager"""
99        LogModules = TDS.readSetting("LogModules",type='list')
100        self.log = fifelog.LogManager(self.engine,
101                                      int(TDS.readSetting("LogToPrompt")),
102                                      int(TDS.readSetting("LogToFile")))
103        if LogModules:
104            self.log.setVisibleModules(*LogModules)
105
106    def createListener(self):
107        # already created in constructor
108        pass
109
110    def _pump(self):
111        """Main game loop
112           There are in fact 2 main loops, this one and the one in World"""
113        if self.listener.quit:
114            self.breakRequested = True
115        else:
116            self.world.pump()
117
118def main():
119    """Application code starts from here"""
120    app = PARPG()
121    app.run()
122
123if __name__ == '__main__':
124    main()
125
Note: See TracBrowser for help on using the repository browser.