00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "LayeredPicture.h"
00010
00011 #include "Path.h"
00012 #include "ResImagePack.h"
00013 #include "ResourceException.h"
00014 #include "SurfaceLock.h"
00015 #include "PixelTool.h"
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 LayeredPicture::LayeredPicture(const Path &bg_file, const V2 &loc,
00026 const Path &lowerLayer, const Path &colorMask)
00027 : Picture(bg_file, loc)
00028 {
00029 m_lowerLayer = ResImagePack::loadImage(lowerLayer);
00030 m_colorMask = ResImagePack::loadImage(colorMask);
00031 if (m_lowerLayer->w != m_colorMask->w
00032 || m_lowerLayer->h != m_colorMask->h) {
00033 SDL_FreeSurface(m_lowerLayer);
00034 SDL_FreeSurface(m_colorMask);
00035 SDL_FreeSurface(m_surface);
00036
00037 throw ResourceException(ExInfo(
00038 "lowerLayer and colorMask have different proportions")
00039 .addInfo("lowerLayer", lowerLayer.getNative())
00040 .addInfo("colorMask", colorMask.getNative()));
00041 }
00042
00043 setNoActive();
00044 }
00045
00046 LayeredPicture::~LayeredPicture()
00047 {
00048 SDL_FreeSurface(m_lowerLayer);
00049 SDL_FreeSurface(m_colorMask);
00050 }
00051
00052
00053
00054
00055
00056 Uint32
00057 LayeredPicture::getMaskAtWorld(const V2 &worldLoc)
00058 {
00059 V2 localLoc = worldLoc.minus(m_loc);
00060 return getMaskAt(localLoc);
00061 }
00062
00063
00064
00065
00066 Uint32
00067 LayeredPicture::getMaskAt(const V2 &loc)
00068 {
00069 Uint32 result = MASK_NO;
00070
00071 if ((0 <= loc.getX() && loc.getX() < m_colorMask->w)
00072 && (0 <= loc.getY() && loc.getY() < m_colorMask->h))
00073 {
00074 SurfaceLock lock1(m_colorMask);
00075 result = PixelTool::getPixel(m_colorMask,
00076 loc.getX(), loc.getY());
00077 }
00078 return result;
00079 }
00080
00081 void
00082 LayeredPicture::drawOn(SDL_Surface *screen)
00083 {
00084 Picture::drawOn(screen);
00085 if (m_activeColor == MASK_NO) {
00086 return;
00087 }
00088
00089 SurfaceLock lock1(screen);
00090 SurfaceLock lock2(m_lowerLayer);
00091 SurfaceLock lock3(m_colorMask);
00092
00093
00094 for (int py = 0; py < m_colorMask->h; ++py) {
00095 int world_y = m_loc.getY() + py;
00096 for (int px = 0; px < m_colorMask->w; ++px) {
00097 Uint32 sample = PixelTool::getPixel(m_colorMask, px, py);
00098
00099 if (sample == m_activeColor) {
00100 SDL_Color lower = PixelTool::getColor(m_lowerLayer, px, py);
00101 if (lower.unused == 255) {
00102 PixelTool::putColor(screen,
00103 m_loc.getX() + px, world_y, lower);
00104 }
00105 }
00106 }
00107 }
00108 }
00109