Skip to content

Commit

Permalink
very dirty bugfix (JsonBasedSprSheet cls) but hey, its a prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
wkta committed Apr 22, 2024
1 parent a1ced46 commit 0007f87
Showing 1 changed file with 40 additions and 13 deletions.
53 changes: 40 additions & 13 deletions src/pyved_engine/compo/gfx.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,43 +10,70 @@

class JsonBasedSprSheet:
def __init__(self, filename_noext_nopath, pathinfo=None, ck=None):
"""
warning:
the current source-code looks stupid, but it has written that way in order to
avoid several bugs that exist in the pyVM component.
(scaling images, and use .subsurface on these images for cutting sub images)
As long as the pyVM has not bee toroughly debugged and tested,
I recommend not modifying the following code /!\ unless you want to take risks
"""

print('create SpriteSheet based on json:', filename_noext_nopath)
p = pathinfo if pathinfo else ''
self.sheet_surf = _hub.pygame.image.load(f'{p}{filename_noext_nopath}.png')
json_def_file = open(f'{p}{filename_noext_nopath}.json', 'r')
jsondata = json.load(json_def_file)
chosen_scale = "1.0"

chosen_scale = "1"
meta = jsondata.get("meta", "")
if meta:
chosen_scale = meta.get("scale", chosen_scale)

print('[JsonBasedSprSheet] image scale after reading meta field:', chosen_scale)
else:
print('[JsonBasedSprSheet] no meta field has been found in the json file')

try:
chosen_scale_f = float(chosen_scale)
except:
except ValueError:
e_msg = f"[JsonBasedSprSheet:] WARNING! Cannot convert scale '{chosen_scale}' to float, using default val."
print(e_msg)
chosen_scale_f = 1.0
print(f"Error in JsonBasedSprSheet: can't convert scale '{chosen_scale}' to float. Using scale of 1.")

if chosen_scale_f != 1.0:
homo = _hub.pygame.transform.scale
w, h = self.sheet_surf.get_size()
self.sheet_surf = homo(self.sheet_surf, (chosen_scale_f * w, chosen_scale_f * h))

#if chosen_scale_f != 1.0:
# w, h = self.sheet_surf.get_size()
# self.sheet_surf = _hub.pygame.transform.scale(
# self.sheet_surf, (chosen_scale_f * w, chosen_scale_f * h)
# )
if ck:
self.sheet_surf.set_colorkey(ck)

assoc_tmp = dict()
self.all_names = set()
y = chosen_scale_f
chosen_scale_f = 1.0
if isinstance(jsondata['frames'], list): # we support 2 formats of json desc
for infos in jsondata['frames']:
gname = infos['filename']
self.all_names.add(gname)
args = (infos['frame']['x'] * chosen_scale_f, infos['frame']['y'] * chosen_scale_f, infos['frame']['w'] * chosen_scale_f, infos['frame']['h'] * chosen_scale_f)
assoc_tmp[gname] = self.sheet_surf.subsurface(_hub.pygame.Rect(*args)).copy()
args = (infos['frame']['x'] * chosen_scale_f, infos['frame']['y'] * chosen_scale_f,
infos['frame']['w'] * chosen_scale_f, infos['frame']['h'] * chosen_scale_f)
tempp = self.sheet_surf.subsurface(_hub.pygame.Rect(*args)).copy()
lw, lh = tempp.get_size()
assoc_tmp[gname] = _hub.pygame.transform.scale(
tempp, (y*lw, y*lh)
)
else:
for sprname, infos in jsondata['frames'].items():
self.all_names.add(sprname)
args = (infos['frame']['x'] * chosen_scale_f, infos['frame']['y'] * chosen_scale_f, infos['frame']['w'] * chosen_scale_f, infos['frame']['h'] * chosen_scale_f)
assoc_tmp[sprname] = self.sheet_surf.subsurface(_hub.pygame.Rect(*args)).copy()
args = (infos['frame']['x'] * chosen_scale_f, infos['frame']['y'] * chosen_scale_f,
infos['frame']['w'] * chosen_scale_f, infos['frame']['h'] * chosen_scale_f)
tempp = self.sheet_surf.subsurface(_hub.pygame.Rect(*args)).copy()
lw, lh = tempp.get_size()
assoc_tmp[sprname] = _hub.pygame.transform.scale(
tempp, (y*lw, y*lh)
)

self.assoc_name_spr = assoc_tmp

def __getitem__(self, item):
Expand Down

0 comments on commit 0007f87

Please sign in to comment.