FIFE  2008.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
image_loader.cpp
1 /***************************************************************************
2  * Copyright (C) 2005-2008 by the FIFE team *
3  * http://www.fifengine.de *
4  * This file is part of FIFE. *
5  * *
6  * FIFE is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU Lesser General Public *
8  * License as published by the Free Software Foundation; either *
9  * version 2.1 of the License, or (at your option) any later version. *
10  * *
11  * This library is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
14  * Lesser General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU Lesser General Public *
17  * License along with this library; if not, write to the *
18  * Free Software Foundation, Inc., *
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
20  ***************************************************************************/
21 
22 // Standard C++ library includes
23 
24 // 3rd party library includes
25 #include <boost/scoped_array.hpp>
26 #include <boost/scoped_ptr.hpp>
27 #include <SDL.h>
28 #include <SDL_image.h>
29 
30 // FIFE includes
31 // These includes are split up in two parts, separated by one empty line
32 // First block: files included from the FIFE root src directory
33 // Second block: files included from the same folder
34 #include "util/base/exception.h"
35 #include "util/resource/resource_location.h"
36 #include "util/resource/resource.h"
37 #include "vfs/raw/rawdata.h"
38 #include "vfs/vfs.h"
39 #include "video/renderbackend.h"
40 #include "video/image_location.h"
41 
42 #include "image_loader.h"
43 
44 namespace FIFE {
45  IResource* ImageLoader::loadResource(const ResourceLocation& location) {
46  const ImageLocation* loc = dynamic_cast<const ImageLocation*>(&location);
47 
48  const std::string& filename = location.getFilename();
49  boost::scoped_ptr<RawData> data (m_vfs->open(filename));
50  size_t datalen = data->getDataLength();
51  boost::scoped_array<uint8_t> darray(new uint8_t[datalen]);
52  data->readInto(darray.get(), datalen);
53  SDL_RWops* rwops = SDL_RWFromConstMem(darray.get(), datalen);
54  SDL_Surface* surface = IMG_Load_RW(rwops, false);
55  SDL_FreeRW(rwops);
56  if( !surface ) {
57  return 0;
58  }
59 
60  Image* res = RenderBackend::instance()->createImage(surface);
61  res->setResourceLocation(location);
62  if (loc) {
63  res->setXShift(loc->getXShift());
64  res->setYShift(loc->getYShift());
65  }
66  res->setAlphaOptimizerEnabled(true);
67  return res;
68  };
69 }