Main Page | Class Hierarchy | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

Outline.cpp

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004 Ivo Danihelka (ivo@danihelka.net)
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
00008  */
00009 #include "Outline.h"
00010 
00011 #include "Log.h"
00012 #include "PixelTool.h"
00013 #include "SurfaceTool.h"
00014 #include "SurfaceLock.h"
00015 
00016 //-----------------------------------------------------------------
00017 Outline::Outline(const SDL_Color &color, int width)
00018     : m_color(color)
00019 {
00020     m_width = width;
00021     m_pixel = 0;
00022 }
00023 //-----------------------------------------------------------------
00024 /**
00025  * Draw outline, use surface colorkey as bg color.
00026  */
00027 void
00028 Outline::drawOnColorKey(SDL_Surface *surface)
00029 {
00030     Uint32 bgKey = surface->format->colorkey;
00031     drawOn(surface, bgKey);
00032 }
00033 //-----------------------------------------------------------------
00034 /**
00035  * Draw outline on bg color.
00036  * @param surface picture with shape to outline
00037  * @param bgKey color used for background
00038  */
00039 void
00040 Outline::drawOn(SDL_Surface *surface, Uint32 bgKey)
00041 {
00042     SurfaceLock lock1(surface);
00043 
00044     precomputePixel(surface->format);
00045     for (int i = 0; i < m_width; ++i) {
00046         drawOneLayer(surface, bgKey);
00047     }
00048 }
00049 //-----------------------------------------------------------------
00050 void
00051 Outline::precomputePixel(SDL_PixelFormat *format)
00052 {
00053     m_pixel = SDL_MapRGB(format, m_color.r, m_color.g, m_color.b);
00054 }
00055 //-----------------------------------------------------------------
00056 /**
00057  * Draw outline with width=1.
00058  */
00059 void
00060 Outline::drawOneLayer(SDL_Surface *surface, Uint32 bgKey)
00061 {
00062     SDL_Surface *copy = SurfaceTool::createClone(surface);
00063     drawAlongCopy(surface, bgKey, copy);
00064     SDL_FreeSurface(copy);
00065 }
00066 //-----------------------------------------------------------------
00067 /**
00068  * Draw outline with width=1.
00069  * Cloned surface will be used as a model. It will not be modified.
00070  * Destination surface must be locked.
00071  */
00072 void
00073 Outline::drawAlongCopy(SDL_Surface *surface, Uint32 bgKey, SDL_Surface *copy)
00074 {
00075     SurfaceLock lock1(copy);
00076 
00077     for (int py = 0; py < surface->h; ++py) {
00078         for (int px = 0; px < surface->w; ++px) {
00079             if (PixelTool::getPixel(copy, px, py) != bgKey) {
00080                 fillNeighbourhood(surface, bgKey, px, py);
00081             }
00082         }
00083     }
00084 }
00085 //-----------------------------------------------------------------
00086 /**
00087  * Fill neighboors around this point.
00088  * Neighboors with bgKey will be filled.
00089  * Surface must be locked.
00090  * m_pixel must be precomputed.
00091  */
00092 void
00093 Outline::fillNeighbourhood(SDL_Surface *surface, Uint32 bgKey, int x, int y)
00094 {
00095     //TODO: optimize for speed
00096     if (x > 0 && PixelTool::getPixel(surface, x - 1, y) == bgKey) {
00097         PixelTool::putPixel(surface, x - 1, y, m_pixel);
00098     }
00099     if (y > 0 && PixelTool::getPixel(surface, x, y - 1) == bgKey) {
00100         PixelTool::putPixel(surface, x, y - 1, m_pixel);
00101     }
00102 
00103     if (x + 1 < surface->w &&
00104             PixelTool::getPixel(surface, x + 1, y) == bgKey)
00105     {
00106         PixelTool::putPixel(surface, x + 1, y, m_pixel);
00107     }
00108     if (y + 1 < surface->h &&
00109             PixelTool::getPixel(surface, x, y + 1) == bgKey)
00110     {
00111         PixelTool::putPixel(surface, x, y + 1, m_pixel);
00112     }
00113 }
00114 
00115 

Generated on Wed Jun 1 09:54:31 2005 for Fish Fillets - Next Generation by  doxygen 1.4.2