subimage_loader.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <iostream>
00024
00025
00026 #include <SDL.h>
00027 #include <boost/lexical_cast.hpp>
00028
00029
00030
00031
00032
00033 #include "video/image.h"
00034 #include "video/renderbackend.h"
00035 #include "video/image_location.h"
00036 #include "util/base/fife_stdint.h"
00037 #include "util/resource/resource.h"
00038 #include "util/base/exception.h"
00039 #include "util/log/logger.h"
00040
00041 #include "subimage_loader.h"
00042
00043 namespace FIFE {
00044 static Logger _log(LM_NATIVE_LOADERS);
00045
00046 IResource* SubImageLoader::loadResource(const ResourceLocation& location) {
00047 const ImageLocation* loc = dynamic_cast<const ImageLocation*>(&location);
00048 if (!loc) {
00049
00050 return NULL;
00051 }
00052 Image* r = loc->getParentSource();
00053 if (!r) {
00054
00055 return NULL;
00056 }
00057 SDL_Surface* src = r->getSurface();
00058 if (!src) {
00059 return NULL;
00060 }
00061 SDL_Rect src_rect;
00062 src_rect.x = loc->getXShift();
00063 src_rect.y = loc->getYShift();
00064 src_rect.w = loc->getWidth();
00065 src_rect.h = loc->getHeight();
00066
00067 FL_DBG(_log, LMsg("subimage_loader")
00068 << " rect:" << Rect(src_rect.x,src_rect.y,src_rect.w,src_rect.h));
00069
00070 uint32_t Amask = src->format->Amask ? AMASK : 0;
00071 SDL_Surface* result = SDL_CreateRGBSurface(SDL_SWSURFACE, src_rect.w, src_rect.h, 32,
00072 RMASK, GMASK, BMASK, Amask);
00073 SDL_FillRect(result, NULL, 0);
00074 SDL_SetAlpha(src,0,SDL_ALPHA_OPAQUE);
00075 SDL_BlitSurface(src,&src_rect,result,0);
00076
00077 Image* image = RenderBackend::instance()->createImage(result);
00078 image->setResourceLocation(location);
00079 return image;
00080 };
00081
00082 }