kepkin
kepkin

Reputation: 1145

In pygame blit of subsurface cause error that the subsurface is locked

Here is a minimal script to reproduce that

#!/usr/bin/env python
import pygame

screen = pygame.display.set_mode((640, 480))
screen.fill((255, 255, 255))
screen_half = screen.subsurface((0,0, 640/2.0, 480))

print screen.get_locks()
print screen_half.get_locks()
screen_half.blit(screen_half, (0, 0))

the output is

()
()
Traceback (most recent call last):
  File "./blit_test.py", line 10, in <module>
    screen_half.blit(screen_half, (0, 0))
pygame.error: Surfaces must not be locked during blit

As you can see tuples with locks for screen and screen_half are empty. There is no error if I use screen instead of screen_half.

Upvotes: 5

Views: 3551

Answers (2)

Guso
Guso

Reputation: 31

I had similar problem and pmoreli is right. I just copied subsurface creating new surface and than blit it on display:

screen_half = screen_half.copy()
screen_half.blit(screen_half, (0, 0))

Upvotes: 3

pmoleri
pmoleri

Reputation: 4451

Probably the lock occurs during the blit. You're blitting a surface into itself, that's why you get the error.

If you want to copy half of the screen into the other half, you can ".copy" the subsurface and then blit it.

Upvotes: 1

Related Questions