From 15b3c680da7cef0490f6b737d70eebb737bf6619 Mon Sep 17 00:00:00 2001 From: Andre Heider Date: Tue, 20 Jan 2009 15:24:47 +0000 Subject: added ImageMan to vkeybd svn-id: r35952 --- backends/module.mk | 1 + backends/vkeybd/imageman.cpp | 131 +++++++++++++++++++++++++++++++++++++++++++ backends/vkeybd/imageman.h | 111 ++++++++++++++++++++++++++++++++++++ 3 files changed, 243 insertions(+) create mode 100644 backends/vkeybd/imageman.cpp create mode 100644 backends/vkeybd/imageman.h diff --git a/backends/module.mk b/backends/module.mk index e6e0ba1a2f..8a6ee70c0f 100644 --- a/backends/module.mk +++ b/backends/module.mk @@ -37,6 +37,7 @@ MODULE_OBJS := \ saves/posix/posix-saves.o \ saves/psp/psp-saves.o \ timer/default/default-timer.o \ + vkeybd/imageman.o \ vkeybd/image-map.o \ vkeybd/polygon.o \ vkeybd/virtual-keyboard.o \ diff --git a/backends/vkeybd/imageman.cpp b/backends/vkeybd/imageman.cpp new file mode 100644 index 0000000000..5b9c011875 --- /dev/null +++ b/backends/vkeybd/imageman.cpp @@ -0,0 +1,131 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * $URL$ + * $Id$ + */ + +#include "backends/vkeybd/imageman.h" +#include "graphics/imagedec.h" +#include "graphics/surface.h" + +#include "common/unzip.h" + +DECLARE_SINGLETON(Graphics::ImageManager); + +namespace Graphics { + +ImageManager::ImageManager() { +} + +ImageManager::~ImageManager() { + for (Iterator pos = _surfaces.begin(); pos != _surfaces.end(); ++pos) { + (*pos)->surface->free(); + delete (*pos)->surface; + delete *pos; + *pos = 0; + } + _surfaces.clear(); +} + +bool ImageManager::addArchive(const Common::String &name) { +#ifdef USE_ZLIB + Common::ZipArchive *arch = new Common::ZipArchive(name); + if (!arch || !arch->isOpen()) + return false; + _archives.add(name, arch); +#endif + return true; +} + +void ImageManager::removeArchive(const Common::String &name) { +#ifdef USE_ZLIB + _archives.remove(name); +#endif +} + +bool ImageManager::registerSurface(const Common::String &name, Surface *surf) { + if (getSurface(name)) { + return false; + } + + Entry *newHandle = new Entry; + if (!newHandle) + return false; + + if (!surf) + surf = ImageDecoder::loadFile(name); + +#ifdef USE_ZLIB + if (!surf) { + Common::SeekableReadStream *stream = _archives.openFile(name); + if (stream) { + surf = ImageDecoder::loadFile(*stream); + delete stream; + } + } +#endif + + if (!surf) + return false; + + newHandle->surface = surf; + newHandle->name = name; + _surfaces.push_back(newHandle); + + return true; +} + +bool ImageManager::unregisterSurface(const Common::String &name) { + Iterator pos = searchHandle(name); + if (pos == _surfaces.end()) { + // no surface handle it as success + return true; + } + + (*pos)->surface->free(); + delete (*pos)->surface; + delete *pos; + *pos = 0; + + _surfaces.erase(pos); + + return true; +} + +Surface * ImageManager::getSurface(const Common::String &name) { + Iterator pos = searchHandle(name); + if (pos == _surfaces.end()) { + // no surface handle it as success + return 0; + } + return (*pos)->surface; +} + +ImageManager::Iterator ImageManager::searchHandle(const Common::String &name) { + Iterator pos = _surfaces.begin(); + while (pos != _surfaces.end()) { + if ((*pos)->name == name) + break; + ++pos; + } + return pos; +} +} // end of namespace Graphics diff --git a/backends/vkeybd/imageman.h b/backends/vkeybd/imageman.h new file mode 100644 index 0000000000..f555b4c844 --- /dev/null +++ b/backends/vkeybd/imageman.h @@ -0,0 +1,111 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * file distributed with this source distribution. + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * $URL$ + * $Id$ + */ + +#ifndef GRAPHICS_IMAGEMAN_H +#define GRAPHICS_IMAGEMAN_H + +#include "common/scummsys.h" + +#include "common/archive.h" +#include "common/singleton.h" +#include "common/str.h" +#include "common/list.h" + +namespace Graphics { + +struct Surface; + +class ImageManager : public Common::Singleton { +public: + ~ImageManager(); + + /** + * adds an .zip archive to the pool where the ImageManager searches + * for image files + * + * @param name the name of the archive + * @return true on success and false on failure + */ + bool addArchive(const Common::String &name); + + /** + * deletes an .zip archive from the pool where the Image Manager searches + * for image files + * + * @param name the name of the archive + */ + void removeArchive(const Common::String &name); + + /** + * registers a surface to the ImageManager. + * surf->free(), also delete surf, will be called when the ImageManager will + * be destroyed or if ImageManager::unregisterSurface is called. + * if the parameter 'surf' is 0 the Manger will try to load an image with + * the filename 'name' + * + * @param name the name of the new handle + * @param surf the surface which should be associated to the given name + * @return returns true on success and false on failure + */ + bool registerSurface(const Common::String &name, Surface *surf); + + /** + * unregisters a surface, after this the returned surface from + * getSurface should NOT be used anymore + * + * @param name the handle + * @return true on success, false on failure + */ + bool unregisterSurface(const Common::String &name); + + /** + * gets a surface registered to a handle + * + * @param name the name of the handle + * @return returns an pointer to an Surface object or 0 on failure + */ + Surface *getSurface(const Common::String &name); +private: + friend class Common::Singleton; + ImageManager(); + + struct Entry { + Common::String name; + Surface *surface; + }; + typedef Common::List::iterator Iterator; + + Iterator searchHandle(const Common::String &name); + + Common::List _surfaces; + Common::SearchSet _archives; +}; + +} // end of namespace Graphics + +/** Shortcut for accessing the image manager. */ +#define ImageMan (Graphics::ImageManager::instance()) + +#endif + -- cgit v1.2.3