Commit | Line | Data |
---|---|---|
8ee66edd | 1 | import argparse |
4894bb48 FC |
2 | import simplepbr |
3 | import gltf | |
8ee66edd | 4 | from sys import platform, argv |
94a18c21 | 5 | from logging import info, debug |
8ee66edd FC |
6 | from os.path import exists |
7 | from os import makedirs | |
6fff1464 FC |
8 | from panda3d.core import Filename, load_prc_file_data, AntialiasAttrib, \ |
9 | Texture, WindowProperties, LVector2i | |
1be87278 | 10 | from panda3d.bullet import BulletWorld, BulletDebugNode |
8ee66edd | 11 | from direct.showbase.ShowBase import ShowBase |
5964572b | 12 | from direct.fsm.FSM import FSM |
1be87278 | 13 | from pmachines.scene import Scene |
54a1397e | 14 | from pmachines.music import MusicMgr |
5964572b | 15 | from pmachines.items.background import Background |
4071c6d8 | 16 | from pmachines.menu import Menu |
9ba5488b FC |
17 | from lib.dictfile import DctFile |
18 | from lib.lib.p3d.p3d import LibP3d | |
2aaa10d3 | 19 | from lib.engine.lang import LangMgr |
8ee66edd FC |
20 | |
21 | ||
5964572b FC |
22 | class MainFsm(FSM): |
23 | ||
24 | def __init__(self, pmachines): | |
25 | super().__init__('Main FSM') | |
26 | self._pmachines = pmachines | |
27 | ||
28 | def enterMenu(self): | |
29 | self._pmachines.on_menu_enter() | |
30 | ||
31 | def exitMenu(self): | |
32 | self._pmachines.on_menu_exit() | |
33 | ||
34 | def enterScene(self): | |
35 | self._pmachines.on_scene_enter() | |
36 | ||
37 | def exitScene(self): | |
38 | self._pmachines.on_scene_exit() | |
39 | ||
40 | ||
c8035584 | 41 | class PmachinesApp: |
8ee66edd FC |
42 | |
43 | def __init__(self): | |
44 | self._configure() | |
45 | self.base = ShowBase() | |
46 | info('platform: %s' % platform) | |
47 | info('exists main.py: %s' % exists('main.py')) | |
8ee66edd | 48 | args = self._parse_args() |
9ba5488b | 49 | self._prepare_window(args) |
8ee66edd FC |
50 | self.updating = args.update |
51 | self.version = args.version | |
52 | if args.update: | |
53 | return | |
e1e44d5c | 54 | self._music = MusicMgr(self._options['settings']['volume']) |
a0b33e12 | 55 | self.lang_mgr = LangMgr(self._options['settings']['language'], |
2aaa10d3 FC |
56 | 'pmachines', |
57 | 'assets/locale/') | |
5964572b | 58 | self._fsm = MainFsm(self) |
e669403e FC |
59 | tgt_state = 'Scene' if self._options['development']['auto_start'] else 'Menu' |
60 | self._fsm.demand(tgt_state) | |
5964572b FC |
61 | |
62 | def on_menu_enter(self): | |
63 | self._menu_bg = Background() | |
a9aba267 FC |
64 | self._menu = Menu( |
65 | self._fsm, self.lang_mgr, self._options, self._music, | |
66 | self._pipeline) | |
5964572b FC |
67 | |
68 | def on_home(self): | |
69 | self._fsm.demand('Menu') | |
70 | ||
71 | def on_menu_exit(self): | |
72 | self._menu_bg.destroy() | |
4071c6d8 | 73 | self._menu.destroy() |
5964572b FC |
74 | |
75 | def on_scene_enter(self): | |
1be87278 | 76 | self._set_physics() |
e669403e FC |
77 | self._scene = Scene( |
78 | self.world, self.on_home, | |
31237524 FC |
79 | self._options['development']['auto_close_instructions'], |
80 | self._options['development']['debug_items']) | |
5964572b FC |
81 | |
82 | def on_scene_exit(self): | |
83 | self._unset_physics() | |
84 | self._scene.destroy() | |
8ee66edd FC |
85 | |
86 | def _configure(self): | |
87 | load_prc_file_data('', 'window-title pmachines') | |
4894bb48 | 88 | load_prc_file_data('', 'framebuffer-srgb true') |
8ee66edd FC |
89 | |
90 | def _parse_args(self): | |
91 | parser = argparse.ArgumentParser() | |
92 | parser.add_argument('--update', action='store_true') | |
93 | parser.add_argument('--version', action='store_true') | |
9ba5488b | 94 | parser.add_argument('--optfile') |
8ee66edd FC |
95 | cmd_line = [arg for arg in iter(argv[1:]) if not arg.startswith('-psn_')] |
96 | args = parser.parse_args(cmd_line) | |
97 | return args | |
98 | ||
9ba5488b | 99 | def _prepare_window(self, args): |
8ee66edd FC |
100 | data_path = '' |
101 | if (platform.startswith('win') or platform.startswith('linux')) and ( | |
102 | not exists('main.py') or __file__.startswith('/app/bin/')): | |
103 | # it is the deployed version for windows | |
104 | data_path = str(Filename.get_user_appdata_directory()) + '/pmachines' | |
105 | home = '/home/flavio' # we must force this for wine | |
106 | if data_path.startswith('/c/users/') and exists(home + '/.wine/'): | |
107 | data_path = home + '/.wine/drive_' + data_path[1:] | |
108 | info('creating dirs: %s' % data_path) | |
109 | makedirs(data_path, exist_ok=True) | |
9ba5488b FC |
110 | optfile = args.optfile if args.optfile else 'options.ini' |
111 | info('data path: %s' % data_path) | |
112 | info('option file: %s' % optfile) | |
113 | info('fixed path: %s' % LibP3d.fixpath(data_path + '/' + optfile)) | |
114 | default_opt = { | |
115 | 'settings': { | |
a0b33e12 | 116 | 'volume': 1, |
6fff1464 FC |
117 | 'language': 'en', |
118 | 'fullscreen': 1, | |
a9aba267 | 119 | 'resolution': '', |
5fdf77d0 FC |
120 | 'antialiasing': 1, |
121 | 'shadows': 1}, | |
9ba5488b | 122 | 'development': { |
a5dc83f4 | 123 | 'simplepbr': 1, |
9ba5488b | 124 | 'verbose_log': 0, |
e669403e FC |
125 | 'physics_debug': 0, |
126 | 'auto_start': 0, | |
127 | 'auto_close_instructions': 0, | |
31237524 | 128 | 'show_buffers': 0, |
b41381b2 FC |
129 | 'debug_items': 0, |
130 | 'fps': 0}} | |
9ba5488b FC |
131 | opt_path = LibP3d.fixpath(data_path + '/' + optfile) if data_path else optfile |
132 | opt_exists = exists(opt_path) | |
133 | self._options = DctFile( | |
134 | LibP3d.fixpath(data_path + '/' + optfile) if data_path else optfile, | |
135 | default_opt) | |
136 | if not opt_exists: | |
137 | self._options.store() | |
6fff1464 FC |
138 | res = self._options['settings']['resolution'] |
139 | if res: | |
140 | res = LVector2i(*[int(_res) for _res in res.split('x')]) | |
141 | else: | |
142 | d_i = base.pipe.get_display_information() | |
143 | def _res(idx): | |
144 | return d_i.get_display_mode_width(idx), \ | |
145 | d_i.get_display_mode_height(idx) | |
146 | resolutions = [ | |
147 | _res(idx) for idx in range(d_i.get_total_display_modes())] | |
148 | res = sorted(resolutions)[-1] | |
149 | props = WindowProperties() | |
150 | props.set_size(res) | |
151 | props.set_fullscreen(self._options['settings']['fullscreen']) | |
94a18c21 | 152 | props.set_icon_filename('assets/icon/pmachines.ico') |
6fff1464 | 153 | base.win.request_properties(props) |
4894bb48 | 154 | gltf.patch_loader(base.loader) |
a5dc83f4 | 155 | if self._options['development']['simplepbr']: |
a9aba267 | 156 | self._pipeline = simplepbr.init( |
a5dc83f4 FC |
157 | use_normal_maps=True, |
158 | use_emission_maps=False, | |
a9aba267 | 159 | use_occlusion_maps=True, |
5fdf77d0 FC |
160 | msaa_samples=4 if self._options['settings']['antialiasing'] else 1, |
161 | enable_shadows=int(self._options['settings']['shadows'])) | |
94a18c21 FC |
162 | debug(f'msaa: {self._pipeline.msaa_samples}') |
163 | debug(f'shadows: {self._pipeline.enable_shadows}') | |
4894bb48 | 164 | render.setAntialias(AntialiasAttrib.MAuto) |
1be87278 FC |
165 | self.base.set_background_color(0, 0, 0, 1) |
166 | self.base.disable_mouse() | |
e669403e FC |
167 | if self._options['development']['show_buffers']: |
168 | base.bufferViewer.toggleEnable() | |
b41381b2 FC |
169 | if self._options['development']['fps']: |
170 | base.set_frame_rate_meter(True) | |
651713a9 FC |
171 | #self.base.accept('window-event', self._on_win_evt) |
172 | self.base.accept('aspectRatioChanged', self._on_aspect_ratio_changed) | |
1be87278 FC |
173 | |
174 | def _set_physics(self): | |
9ba5488b FC |
175 | if self._options['development']['physics_debug']: |
176 | debug_node = BulletDebugNode('Debug') | |
177 | debug_node.show_wireframe(True) | |
178 | debug_node.show_constraints(True) | |
179 | debug_node.show_bounding_boxes(True) | |
180 | debug_node.show_normals(True) | |
5964572b FC |
181 | self._debug_np = render.attach_new_node(debug_node) |
182 | self._debug_np.show() | |
1be87278 FC |
183 | self.world = BulletWorld() |
184 | self.world.set_gravity((0, 0, -9.81)) | |
9ba5488b | 185 | if self._options['development']['physics_debug']: |
5964572b | 186 | self.world.set_debug_node(self._debug_np.node()) |
1be87278 FC |
187 | def update(task): |
188 | dt = globalClock.get_dt() | |
189 | self.world.do_physics(dt) | |
190 | return task.cont | |
5964572b FC |
191 | self._phys_tsk = taskMgr.add(update, 'update') |
192 | ||
193 | def _unset_physics(self): | |
194 | if self._options['development']['physics_debug']: | |
195 | self._debug_np.remove_node() | |
196 | self.world = None | |
197 | taskMgr.remove(self._phys_tsk) | |
651713a9 FC |
198 | |
199 | def _on_aspect_ratio_changed(self): | |
5964572b FC |
200 | if self._fsm.state == 'Scene': |
201 | self._scene.on_aspect_ratio_changed() |