Skip to content

Commit fea504c

Browse files
Fix for unordinary behavior of size() in setup() for skia (#420)
* Fix for unordinary behavior of size() in setup() for skia * Taking snapshot of last surface and rendering on new * Retaining style settings after setup call * Using deepcopy() instead of copy() * Added verbose comments for the changes
1 parent 8f4777b commit fea504c

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

p5/sketch/Skia2DRenderer/base.py

+19-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from OpenGL import GL
66
from time import time
77

8+
import copy
89
from ..events import handler_names
910
from .handlers import *
1011
from .util import *
@@ -205,19 +206,33 @@ def frame_buffer_resize_callback_handler(self, window, width, height):
205206
Values of width and height may not be equal to the actual window's width and height
206207
in Retina Display
207208
"""
208-
209209
# Callback handler for frame buffer resize events
210+
211+
self.resized = False
210212
builtins.pixel_x_density = width / self.size[0]
211213
builtins.pixel_y_density = height / self.size[1]
212214
self.pixel_density = width * height // (self.size[0] * self.size[1])
213215

216+
# Creates an Image of current surface and a copy of current style configurations
217+
# For the purpose of handling setup_method() re-call
218+
# Ref: Issue #419
219+
old_image = self.surface.makeImageSnapshot()
220+
old_image = old_image.resize(old_image.width(), old_image.height())
221+
old_style = copy.deepcopy(p5.renderer.style)
222+
214223
GL.glViewport(0, 0, width, height)
215224
self.create_surface(size=(width, height))
216-
# with self.surface as self.canvas:
217-
# # redraw on the canvas/ ( new frame buffer ) after resizing
218-
# # and do not rewind/clear the path
225+
self.setup_method()
226+
227+
# Redraws Image on the canvas/ new frame buffer
228+
# Previously stored style configurations are restored for
229+
# discarding setup_method() style changes
230+
p5.renderer.style = old_style
231+
with self.surface as self.canvas:
232+
self.canvas.drawImage(old_image, 0, 0)
219233

220234
# Tell the program, we have resized the frame buffer
235+
# and do not rewind/clear the path
221236
# Restart the rendering again
222237
self.resized = True
223238

p5/sketch/userspace.py

+7
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,13 @@ def size(width, height):
254254
:type height: int
255255
256256
"""
257+
258+
# return with no change in size when called from frame buffer resize callback
259+
# this is to handle setup function re-call during resize
260+
# Ref: Issue #419
261+
if(builtins.current_renderer == "skia" and not p5.sketch.resized):
262+
return
263+
257264
builtins.width = int(width)
258265
builtins.height = int(height)
259266
p5.sketch.size = (builtins.width, builtins.height)

0 commit comments

Comments
 (0)