00001
00002
00003
00004
00005
00006
00007
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
00026
00027 void
00028 Outline::drawOnColorKey(SDL_Surface *surface)
00029 {
00030 Uint32 bgKey = surface->format->colorkey;
00031 drawOn(surface, bgKey);
00032 }
00033
00034
00035
00036
00037
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
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
00069
00070
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
00088
00089
00090
00091
00092 void
00093 Outline::fillNeighbourhood(SDL_Surface *surface, Uint32 bgKey, int x, int y)
00094 {
00095
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