pyved-engine icon indicating copy to clipboard operation
pyved-engine copied to clipboard

The pyVM encounters problems when working with upscaled JSON based Spritesheets

Open wkta opened this issue 10 months ago • 1 comments

Basically, in the curent state-of-affairs, you have to use a stupid work-around in order to have upscaled spritesheets working properly in the browser:

To be more specific:

  • version 1 is the source-code one writes without too much thinking, it is the "natural way" to solve the problem. But this creates a fatal error (pyVM crash) with the current version, that is: v24.4a2 of the Kata.Games system
  • version 2 works fine. What changed is the fact that instead upscaling then cutting in the spritesheet, we do it the other way around. As you can see this critical bug is related to details about how upscaling works within pyVM. The explanation is that how pyVM handles surface manipulation (in particular: calls to surface.sub_subsurface(...) ) is still a bit clunky.

Version 1

class JsonBasedSprSheet:
    def __init__(self, filename_noext_nopath, pathinfo=None, ck=None):
        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"
        meta = jsondata.get("meta", "")
        if meta:
            chosen_scale = meta.get("scale", chosen_scale)
        
        try:
            chosen_scale_f = float(chosen_scale)
        except:
            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 ck:
            self.sheet_surf.set_colorkey(ck)

        assoc_tmp = dict()
        self.all_names = set()
        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()
        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()
        self.assoc_name_spr = assoc_tmp

    def __getitem__(self, item):
        return self.assoc_name_spr[item]

Version 2

class JsonBasedSprSheet:
    def __init__(self, filename_noext_nopath, pathinfo=None, ck=None):
        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"
        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 ValueError:
            e_msg = f"[JsonBasedSprSheet:] WARNING! Cannot convert scale '{chosen_scale}' to float, using default val."
            print(e_msg)
            chosen_scale_f = 1.0

        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)
                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)
                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):
        return self.assoc_name_spr[item]

wkta avatar Apr 22 '24 18:04 wkta

To get more knowledge about this issue, check the pull request #24 and also: the file gfx.py as versionned at that moment.

wkta avatar Apr 22 '24 18:04 wkta