Skip to content

Commit

Permalink
fix: resize fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ShashkovS committed Nov 12, 2023
1 parent 35b02e9 commit a3b1565
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/drawzero/utils/draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,12 @@ def quit():
renderer.draw_quit()


def run():
while True:
renderer.draw_tick(1)
_update_events_coordinates()


def fps(fontsize=24, *, prev=[time()]):
cur = time()
diff = cur - prev[0]
Expand Down
18 changes: 14 additions & 4 deletions src/drawzero/utils/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,18 @@ def _create_surface():
We create surface only on first drawing call
So without any drawing command new windows is not created nor opened
"""
global _surface
global _surface, _saved_surface
if not _surface:
_surface = pygame.display.set_mode((surface_size, surface_size), pygame.locals.RESIZABLE)
_surface.fill((0, 0, 0))
pygame.display.set_caption('Draw Zero')
_saved_surface = _surface.copy()


def draw_resize(width: int, height: int):
global _surface
global _surface, _saved_surface
_surface = pygame.display.set_mode([width, height])
_saved_surface = _surface.copy()


def draw_line(color: Clr, start: Pt, end: Pt, alpha: int = 255, line_width: int = None):
Expand Down Expand Up @@ -241,27 +243,32 @@ def draw_image(path: str, pos: Pt, width: int = None, alpha: int = 255):


def _resize(nw: int, nh: int):
global _surface, surface_size
global _surface, _saved_surface, surface_size
surface_size = min(nw, nh)
set_real_size(surface_size, surface_size)
scaled = pygame.transform.smoothscale(_surface, (surface_size, surface_size))
scaled = pygame.transform.smoothscale(_saved_surface, (surface_size, surface_size))
_surface = pygame.display.set_mode((surface_size, surface_size), pygame.locals.RESIZABLE)
_surface.blit(scaled, (0, 0))
_saved_surface = _surface.copy()


def _display_update():
global _saved_surface
try:
pygame.display.update()
_saved_surface.blit(_surface, (0, 0))
except pygame.error:
pygame.quit()
sys.stderr = None
sys.exit()


def _display_update_if_no_animation():
global _saved_surface
if _animation_not_detected:
try:
pygame.display.update()
_saved_surface.blit(_surface, (0, 0))
except pygame.error:
pygame.quit()
sys.stderr = None
Expand Down Expand Up @@ -299,6 +306,7 @@ def draw_tick(r=1, *, display_update=True):
sys.stderr = None
sys.exit()
elif event.type == pygame.VIDEORESIZE:
print('resize')
_resize(event.w, event.h)
elif event.type == pygame.KEYDOWN:
keysdown.append(event)
Expand Down Expand Up @@ -372,6 +380,8 @@ def _init():


_surface: Optional[pygame.Surface] = None
_saved_surface: Optional[pygame.Surface] = None

surface_size = 0
keysdown = []
keysup = []
Expand Down

0 comments on commit a3b1565

Please sign in to comment.