source: trunk/PARPG/run.py @ 97

Revision 88, 5.0 KB checked in by bretzel_parpg, 10 years ago (diff)

Added settings-dist.xml so that if there is no settings.xml, it will
copy the settings-dist.xml to make a settings.xml file.

  • 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
19# add paths to the swig extensions
20utils.addPaths ('../../engine/swigwrappers/python', '../../engine/extensions')
21
22if not os.path.exists('settings.xml'):
23    shutil.copyfile('settings-dist.xml', 'settings.xml')
24
25import fife_compat
26import fife,fifelog
27from scripts import world
28from scripts import engine
29from scripts.common import eventlistenerbase
30from basicapplication import ApplicationBase
31from settings import Setting
32
33TDS = Setting()
34
35# This folder holds the main meta-data for PARPG. This file should be
36# minimal, since folding code into the controller with MVC is usually bad
37# All game and logic and data is held held and referenced in /scripts/engine.py
38# All fife stuff goes in /scripts/world.py
39
40class ApplicationListener(eventlistenerbase.EventListenerBase):
41    def __init__(self, engine, world):
42        super(ApplicationListener, self).__init__(engine,
43                                                  regKeys=True,regCmd=True,
44                                                  regMouse=False, 
45                                                  regConsole=True,
46                                                  regWidget=True)
47        self.engine = engine
48        self.world = world
49        engine.getEventManager().setNonConsumableKeys([fife.Key.ESCAPE,])
50        self.quit = False
51        self.aboutWindow = None
52
53    def quitGame(self):
54        """Forces a quit game on next cycle"""
55        self.quit = True
56
57    def onCommand(self, command):
58        """Enables the game to be closed via the 'X' button on the window frame"""
59        if(command.getCommandType() == fife.CMD_QUIT_GAME):
60            self.quit = True
61            command.consume()
62
63class PARPG(ApplicationBase):
64    """Main Application class
65       We use an MVC data model.
66       self.world is our view,self.engine is our model
67       This file is the minimal controller"""
68    def __init__(self):
69        super(PARPG,self).__init__()
70        self.world = world.World(self.engine)
71        self.model = engine.Engine(self.world)
72        self.world.data = self.model
73        self.listener = ApplicationListener(self.engine,self.world)
74        self.world.quitFunction = self.listener.quitGame
75        self.model.loadMap(str(TDS.readSetting("MapFile")))   
76
77    def loadSettings(self):
78        """Load the settings from a python file and load them into the engine.
79           Called in the ApplicationBase constructor."""
80        import settings
81        self.settings = settings
82        eSet=self.engine.getSettings()
83        eSet.setDefaultFontGlyphs(str(TDS.readSetting("FontGlyphs",
84                                                      strip=False)))
85        eSet.setDefaultFontPath(str(TDS.readSetting("Font")))
86        eSet.setBitsPerPixel(int(TDS.readSetting("BitsPerPixel")))
87        eSet.setInitialVolume(float(TDS.readSetting("InitialVolume")))
88        eSet.setSDLRemoveFakeAlpha(int(TDS.readSetting("SDLRemoveFakeAlpha")))
89        eSet.setScreenWidth(int(TDS.readSetting("ScreenWidth")))
90        eSet.setScreenHeight(int(TDS.readSetting("ScreenHeight")))
91        eSet.setRenderBackend(str(TDS.readSetting("RenderBackend")))
92        eSet.setFullScreen(int(TDS.readSetting("FullScreen")))
93        try:
94            eSet.setWindowTitle(str(TDS.readSetting("WindowTitle")))
95            eSet.setWindowIcon(str(TDS.readSetting("WindowIcon")))
96        except:
97            pass           
98        try:
99            eSet.setImageChunkingSize(int(TDS.readSetting("ImageChunkSize")))
100        except:
101            pass
102
103    def initLogging(self):
104        """Initialize the LogManager"""
105        LogModules = TDS.readSetting("LogModules",type='list')
106        self.log = fifelog.LogManager(self.engine,
107                                      int(TDS.readSetting("LogToPrompt")),
108                                      int(TDS.readSetting("LogToFile")))
109        if(LogModules):
110            self.log.setVisibleModules(*LogModules)
111
112    def createListener(self):
113        # already created in constructor
114        # but if we don't put here, Fife gets bitchy :-)
115        pass
116
117    def _pump(self):
118        """Main game loop
119           There are in fact 2 main loops, this one and the one in World"""
120        if self.listener.quit:
121            self.breakRequested = True
122        else:
123            self.world.pump()
124
125def main():
126    """Application code starts from here"""
127    app = PARPG()
128    app.run()
129
130if __name__ == '__main__':
131    main()
132
Note: See TracBrowser for help on using the repository browser.