-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from remance/battle-loading-screen-feedback
Improve loading speed and output
- Loading branch information
Showing
6 changed files
with
187 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import pygame | ||
|
||
class PickleableSurface: | ||
|
||
def __init__(self, surface): | ||
self.surface = surface | ||
|
||
def __getstate__(self): | ||
return ( pygame.image.tobytes(self.surface,"RGBA"), self.surface.get_size() ) | ||
|
||
|
||
|
||
def __setstate__(self, state): | ||
self.surface = pygame.image.frombytes(state[0],state[1],"RGBA") | ||
|
||
|
||
|
||
|
||
def recursive_cast_surface_to_pickleable_surface(_dict): | ||
|
||
for k,v in _dict.items(): | ||
if type(v) == dict: | ||
recursive_cast_surface_to_pickleable_surface(v) | ||
elif type(v) == pygame.Surface: | ||
_dict[k] = PickleableSurface(v) | ||
elif type(v) == tuple: | ||
_dict[k] = tuple([ c if type(c) != pygame.Surface else PickleableSurface(c) for c in _dict[k]]) | ||
|
||
|
||
def recursive_cast_pickleable_surface_to_surface(_dict): | ||
|
||
for k,v in _dict.items(): | ||
if type(v) == dict: | ||
recursive_cast_pickleable_surface_to_surface(v) | ||
if type(v) == PickleableSurface: | ||
_dict[k] = v.surface | ||
elif type(v) == tuple: | ||
_dict[k] = tuple([ c if type(c) != PickleableSurface else c.surface for c in _dict[k]]) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pep8 --max-line-length=180 --first --exclude='env' $(git diff HEAD main --name-only) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
from unittest.mock import patch | ||
|
||
def test_pickleable_surface(): | ||
import pickle | ||
import pygame | ||
#from gamescript.common.game.create_troop_sprite_pool import PickleableSurface | ||
from gamescript.create_troop_sprite_pool_methods import PickleableSurface | ||
import io | ||
|
||
surface = pygame.Surface((64,)*2) | ||
pygame.draw.circle( surface, 'red', (32,32), 32 ) | ||
ps = PickleableSurface( surface ) | ||
|
||
handle = io.BytesIO() | ||
pickle.dump(ps,handle) | ||
|
||
|
||
handle.seek(0) | ||
|
||
ps2 = pickle.load(handle) | ||
|
||
s1 = surface | ||
s2 = ps2.surface | ||
|
||
for x in range(64): | ||
for y in range(64): | ||
assert s1.get_at( (x,y) ) == s2.get_at((x,y)) | ||
|
||
|
||
|
||
@patch("gamescript.create_troop_sprite_pool_methods.PickleableSurface.__eq__", | ||
lambda self,other : self.surface == other.surface) | ||
def test_recursive_surface_to_pickleable_surface(): | ||
import pygame | ||
from gamescript.create_troop_sprite_pool_methods import recursive_cast_surface_to_pickleable_surface | ||
from gamescript.create_troop_sprite_pool_methods import PickleableSurface | ||
|
||
surface = pygame.Surface((64,)*2) | ||
pygame.draw.circle( surface, 'red', (32,32), 32 ) | ||
|
||
|
||
|
||
_dict = { | ||
"a":{ | ||
"b": surface | ||
} | ||
} | ||
|
||
|
||
recursive_cast_surface_to_pickleable_surface(_dict) | ||
|
||
assert _dict == { | ||
"a": { | ||
"b": PickleableSurface(surface) | ||
} | ||
} | ||
|
||
|
||
|
||
|