ya2 · news · projects · code · about

on aspect ratio changed
[pmachines.git] / pmachines / pmachines.py
CommitLineData
8ee66edd 1import argparse
4894bb48
FC
2import simplepbr
3import gltf
8ee66edd
FC
4from sys import platform, argv
5from logging import info
6from os.path import exists
7from os import makedirs
1be87278
FC
8from panda3d.core import Filename, load_prc_file_data, AntialiasAttrib
9from panda3d.bullet import BulletWorld, BulletDebugNode
8ee66edd 10from direct.showbase.ShowBase import ShowBase
1be87278 11from pmachines.scene import Scene
54a1397e 12from pmachines.music import MusicMgr
9ba5488b
FC
13from lib.dictfile import DctFile
14from lib.lib.p3d.p3d import LibP3d
8ee66edd
FC
15
16
17class Pmachines:
18
19 def __init__(self):
20 self._configure()
21 self.base = ShowBase()
22 info('platform: %s' % platform)
23 info('exists main.py: %s' % exists('main.py'))
8ee66edd 24 args = self._parse_args()
9ba5488b 25 self._prepare_window(args)
8ee66edd
FC
26 self.updating = args.update
27 self.version = args.version
28 if args.update:
29 return
9ba5488b 30 MusicMgr(self._options['settings']['volume'])
1be87278 31 self._set_physics()
651713a9 32 self._scene = Scene(self.world)
8ee66edd
FC
33
34 def _configure(self):
35 load_prc_file_data('', 'window-title pmachines')
4894bb48
FC
36 load_prc_file_data('', 'framebuffer-multisample 1')
37 load_prc_file_data('', 'multisamples 4')
38 load_prc_file_data('', 'framebuffer-srgb true')
8ee66edd
FC
39
40 def _parse_args(self):
41 parser = argparse.ArgumentParser()
42 parser.add_argument('--update', action='store_true')
43 parser.add_argument('--version', action='store_true')
9ba5488b 44 parser.add_argument('--optfile')
8ee66edd
FC
45 cmd_line = [arg for arg in iter(argv[1:]) if not arg.startswith('-psn_')]
46 args = parser.parse_args(cmd_line)
47 return args
48
9ba5488b 49 def _prepare_window(self, args):
8ee66edd
FC
50 data_path = ''
51 if (platform.startswith('win') or platform.startswith('linux')) and (
52 not exists('main.py') or __file__.startswith('/app/bin/')):
53 # it is the deployed version for windows
54 data_path = str(Filename.get_user_appdata_directory()) + '/pmachines'
55 home = '/home/flavio' # we must force this for wine
56 if data_path.startswith('/c/users/') and exists(home + '/.wine/'):
57 data_path = home + '/.wine/drive_' + data_path[1:]
58 info('creating dirs: %s' % data_path)
59 makedirs(data_path, exist_ok=True)
9ba5488b
FC
60 optfile = args.optfile if args.optfile else 'options.ini'
61 info('data path: %s' % data_path)
62 info('option file: %s' % optfile)
63 info('fixed path: %s' % LibP3d.fixpath(data_path + '/' + optfile))
64 default_opt = {
65 'settings': {
66 'volume': 1},
67 'development': {
68 'verbose_log': 0,
69 'physics_debug': 0}}
70 opt_path = LibP3d.fixpath(data_path + '/' + optfile) if data_path else optfile
71 opt_exists = exists(opt_path)
72 self._options = DctFile(
73 LibP3d.fixpath(data_path + '/' + optfile) if data_path else optfile,
74 default_opt)
75 if not opt_exists:
76 self._options.store()
4894bb48
FC
77 gltf.patch_loader(base.loader)
78 pipeline = simplepbr.init(
79 use_normal_maps=True,
80 use_emission_maps=False,
81 use_occlusion_maps=True,
82 msaa_samples=4)
83 render.setAntialias(AntialiasAttrib.MAuto)
1be87278
FC
84 self.base.set_background_color(0, 0, 0, 1)
85 self.base.disable_mouse()
651713a9
FC
86 #self.base.accept('window-event', self._on_win_evt)
87 self.base.accept('aspectRatioChanged', self._on_aspect_ratio_changed)
1be87278
FC
88
89 def _set_physics(self):
9ba5488b
FC
90 if self._options['development']['physics_debug']:
91 debug_node = BulletDebugNode('Debug')
92 debug_node.show_wireframe(True)
93 debug_node.show_constraints(True)
94 debug_node.show_bounding_boxes(True)
95 debug_node.show_normals(True)
96 debug_np = render.attach_new_node(debug_node)
97 debug_np.show()
1be87278
FC
98 self.world = BulletWorld()
99 self.world.set_gravity((0, 0, -9.81))
9ba5488b
FC
100 if self._options['development']['physics_debug']:
101 self.world.set_debug_node(debug_np.node())
1be87278
FC
102 def update(task):
103 dt = globalClock.get_dt()
104 self.world.do_physics(dt)
105 return task.cont
106 taskMgr.add(update, 'update')
651713a9
FC
107
108 def _on_aspect_ratio_changed(self):
109 self._scene.on_aspect_ratio_changed()