[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Minimizing hardware-accelerated application



Heikki Salo wrote:
Here is where my problem occurs. When iconizing the application (in HW-mode)
the pygame crashes and says that "pygame.error: Surface was lost". I believe
that this is the normal behaviour and when losing/gaining focus the program
should reaquire the old mode somehow. I have managed to use ACTIVEEVENT and
"pygame.display.toggle_fullscreen()" so that it is ok to iconize the
application, but when toggling back to fullscreen the program crashes with
that surface lost-error. What is the "normal" thing to do in order to
prevent those crashes? Any example would be nice.
the "lost surface" is the achilles heel of directx. directx will destroy your surfaces automatically and not tell you about it until later you try to actually use the surface (Thanks MS!)

what your app will need is a way to restore images. for simple programs this might not be too hard, but becomes a challenge once you start having programmtically created surfaces and more.

what you likely need is a small class to hold a surface and some functions for rebuilding the surface. this will require building a bit more framework for your game.

class ImageSurface:
def __init__(self, filename):
self.filename = filename
self.s = None
self.restore()
def restore(self):
self.s = pygame.image.load(self.filename).convert()

you could make these classes for different types of images, like alpha or colorkeyed surfaces also. now when drawing, if the surface is lost you are still ok.

def drawsurface(dst, src, pos):
try:
dst.blit(src.s, pos)
except pygame.error: #lost surface!
src.restore()
dst.blit(src.s, pos)


as for the "toggle full screen", the function really only works on linux. you can always call pygame.display.set_mode() a second time and it will close the original and open a new window.