Source code for CoolWorld.images
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Image bank management module.
Implements:
class WorldImageManager -- generic image bank manager.
"""
"""
The MIT License (MIT)
Copyright (c) 2015 Raphaël SEBAN
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use, copy,
modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
try:
from . import world
except:
import world
# end try
[docs]class WorldImageManager (world.WorldItem):
"""Generic image bank manager.
As a WorldItem subclass, this can be accessed through e.g.
self.world.get("worldimagemanager") or any other arbitrary name
while registering.
Feel free to subclass this in order to best meet your own specific
needs.
"""
[docs] def do_step (self, *args, **kw):
"""Do a game frame (step) - hook method.
Called at each step of world game's main loop.
Current implementation does nothing.
Override this in subclasses whenever you need some action at
each step of game loop.
"""
pass
# end def
[docs] def finalize (self):
"""Finalize world item - hook method.
Called after exiting world game's main loop.
Current implementation merely clears up image bank.
Override this in subclasses whenever you need some deletion or
garbage collection at the end of the game (or at the end of
world item's lifecycle).
"""
self.images.clear()
# end def
[docs] def get_image (self, filepath):
"""Return an image object.
Parameter:
filepath -- path to image resource file.
Returns a cached image object if already asked at least once,
creates a new image object and returns it, otherwise.
Creates an image object through image_factory() hook method to
let each external lib provide an image object on its own.
"""
_image = self.images.get(filepath)
if not _image:
_image = self.image_factory(filepath)
self.images[filepath] = _image
# end if
return _image
# end def
[docs] def image_factory (self, filepath):
"""Image object provider - hook method.
Parameter:
filepath -- path to image resource file.
This hook method MUST be overridden in subclasses or it will
raise a NotImplementedError.
Should return an image object created along with external lib
specific implementation.
"""
raise NotImplementedError
# end def
[docs] def initialize (self):
"""Initialize world item - hook method.
Called just before entering world game's main loop.
Current implementation initializes an image bank dictionary.
Override this in subclasses whenever you need some
initializations at the beginning of the game (or at the
beginning of world item's lifecycle).
"""
self.images = dict()
# end def
# end class