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