ya2 · news · projects · code · about

2cfbd61108bb3f225200f82696feee6b7a9fbe50
[pmachines.git] / pmachines / pmachines.py
1 import argparse
2 import simplepbr
3 import gltf
4 from sys import platform, argv
5 from logging import info
6 from os.path import exists
7 from os import makedirs
8 from panda3d.core import Filename, load_prc_file_data, AntialiasAttrib
9 from panda3d.bullet import BulletWorld, BulletDebugNode
10 from direct.showbase.ShowBase import ShowBase
11 from pmachines.scene import Scene
12 from pmachines.music import MusicMgr
13
14
15 class Pmachines:
16
17 def __init__(self):
18 self._configure()
19 self.base = ShowBase()
20 info('platform: %s' % platform)
21 info('exists main.py: %s' % exists('main.py'))
22 self._prepare_window()
23 args = self._parse_args()
24 self.updating = args.update
25 self.version = args.version
26 if args.update:
27 return
28 MusicMgr()
29 self._set_physics()
30 Scene(self.world)
31
32 def _configure(self):
33 load_prc_file_data('', 'window-title pmachines')
34 load_prc_file_data('', 'framebuffer-multisample 1')
35 load_prc_file_data('', 'multisamples 4')
36 load_prc_file_data('', 'framebuffer-srgb true')
37
38 def _parse_args(self):
39 parser = argparse.ArgumentParser()
40 parser.add_argument('--update', action='store_true')
41 parser.add_argument('--version', action='store_true')
42 cmd_line = [arg for arg in iter(argv[1:]) if not arg.startswith('-psn_')]
43 args = parser.parse_args(cmd_line)
44 return args
45
46 def _prepare_window(self):
47 data_path = ''
48 if (platform.startswith('win') or platform.startswith('linux')) and (
49 not exists('main.py') or __file__.startswith('/app/bin/')):
50 # it is the deployed version for windows
51 data_path = str(Filename.get_user_appdata_directory()) + '/pmachines'
52 home = '/home/flavio' # we must force this for wine
53 if data_path.startswith('/c/users/') and exists(home + '/.wine/'):
54 data_path = home + '/.wine/drive_' + data_path[1:]
55 info('creating dirs: %s' % data_path)
56 makedirs(data_path, exist_ok=True)
57 gltf.patch_loader(base.loader)
58 pipeline = simplepbr.init(
59 use_normal_maps=True,
60 use_emission_maps=False,
61 use_occlusion_maps=True,
62 msaa_samples=4)
63 render.setAntialias(AntialiasAttrib.MAuto)
64 self.base.set_background_color(0, 0, 0, 1)
65 self.base.disable_mouse()
66
67 def _set_physics(self):
68 debug_node = BulletDebugNode('Debug')
69 debug_node.show_wireframe(True)
70 debug_node.show_constraints(True)
71 debug_node.show_bounding_boxes(True)
72 debug_node.show_normals(True)
73 debug_np = render.attach_new_node(debug_node)
74 debug_np.show()
75 self.world = BulletWorld()
76 self.world.set_gravity((0, 0, -9.81))
77 self.world.set_debug_node(debug_np.node())
78 def update(task):
79 dt = globalClock.get_dt()
80 self.world.do_physics(dt)
81 return task.cont
82 taskMgr.add(update, 'update')