Bastien Sevajol преди 6 години
родител
ревизия
76ce137aa0
променени са 1 файла, в които са добавени 23 реда и са изтрити 1 реда
  1. 23 1
      synergine2_cocos2d/actor.py

+ 23 - 1
synergine2_cocos2d/actor.py Целия файл

@@ -1,5 +1,8 @@
1 1
 # coding: utf-8
2
+import io
3
+import os
2 4
 import typing
5
+import ntpath
3 6
 
4 7
 import pyglet
5 8
 
@@ -32,8 +35,13 @@ class Actor(AnimatedInterface, cocos.sprite.Sprite):
32 35
     ):
33 36
         # Note: Parameter required, but we want to modify little as possible parent init
34 37
         assert config, "Config is a required parameter"
38
+        self.config = config
35 39
         self.path_manager = PathManager(config.resolve('global.include_path.graphics'))
36
-        image = pyglet.resource.image(self.path_manager.path(image_path))
40
+        default_image_path = self.build_default_image(
41
+            subject.id,
42
+            self.path_manager.path(image_path),
43
+        )
44
+        image = pyglet.image.load(os.path.abspath(default_image_path))
37 45
         self.animation_images = {}  # type: typing.Dict[str, typing.List[pyglet.image.TextureRegion]]  # nopep8
38 46
         super().__init__(
39 47
             image,
@@ -54,6 +62,20 @@ class Actor(AnimatedInterface, cocos.sprite.Sprite):
54 62
         self.properties = properties or {}
55 63
         self._freeze = False
56 64
 
65
+    def build_default_image(self, subject_id: int, base_image_path: str) -> str:
66
+        cache_dir = self.config.resolve('global.cache_dir_path')
67
+        with open(base_image_path, 'rb') as base_image_file:
68
+
69
+            final_name = '_'.join([
70
+                str(subject_id),
71
+                ntpath.basename(base_image_path),
72
+            ])
73
+            final_path = os.path.join(cache_dir, final_name)
74
+            with open(final_path, 'wb+') as built_image_file:
75
+                built_image_file.write(base_image_file.read())
76
+
77
+        return final_path
78
+
57 79
     def freeze(self) -> None:
58 80
         """
59 81
         Set object to freeze mode: No visual modification can be done anymore