FIFE  2008.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Pages
subimage_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 #include <iostream>
24 
25 // 3rd party library includes
26 #include <SDL.h>
27 #include <boost/lexical_cast.hpp>
28 
29 // FIFE includes
30 // These includes are split up in two parts, separated by one empty line
31 // First block: files included from the FIFE root src directory
32 // Second block: files included from the same folder
33 #include "video/image.h"
34 #include "video/renderbackend.h"
35 #include "video/image_location.h"
36 #include "util/base/fife_stdint.h"
37 #include "util/resource/resource.h"
38 #include "util/base/exception.h"
39 #include "util/log/logger.h"
40 
41 #include "subimage_loader.h"
42 
43 namespace FIFE {
44  static Logger _log(LM_NATIVE_LOADERS);
45 
46  IResource* SubImageLoader::loadResource(const ResourceLocation& location) {
47  const ImageLocation* loc = dynamic_cast<const ImageLocation*>(&location);
48  if (!loc) {
49  // Wrong location type given for subimage provider
50  return NULL;
51  }
52  Image* r = loc->getParentSource();
53  if (!r) {
54  // No parent source assigned, cannot provide subimage
55  return NULL;
56  }
57  SDL_Surface* src = r->getSurface();
58  if (!src) {
59  return NULL;
60  }
61  SDL_Rect src_rect;
62  src_rect.x = loc->getXShift();
63  src_rect.y = loc->getYShift();
64  src_rect.w = loc->getWidth();
65  src_rect.h = loc->getHeight();
66 
67  FL_DBG(_log, LMsg("subimage_loader")
68  << " rect:" << Rect(src_rect.x,src_rect.y,src_rect.w,src_rect.h));
69 
70  uint32_t Amask = src->format->Amask ? AMASK : 0;
71  SDL_Surface* result = SDL_CreateRGBSurface(SDL_SWSURFACE, src_rect.w, src_rect.h, 32,
72  RMASK, GMASK, BMASK, Amask);
73  SDL_FillRect(result, NULL, 0);
74  SDL_SetAlpha(src,0,SDL_ALPHA_OPAQUE);
75  SDL_BlitSurface(src,&src_rect,result,0);
76 
77  Image* image = RenderBackend::instance()->createImage(result);
78  image->setResourceLocation(location);
79  return image;
80  };
81 
82 }