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

PixelIterator.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 "PixelIterator.h"
00010 
00011 #include "SurfaceLock.h"
00012 #include "PixelTool.h"
00013 
00014 #include <assert.h>
00015 
00016 //-----------------------------------------------------------------
00017 /**
00018  * Creates new pixel iterator and locks the surface.
00019  */
00020 PixelIterator::PixelIterator(SDL_Surface *surface)
00021 {
00022     m_surface = surface;
00023     m_lock = new SurfaceLock(m_surface);
00024     m_p = static_cast<Uint8*>(surface->pixels);
00025     m_end = m_p + m_surface->h * m_surface->pitch;
00026     m_bpp = surface->format->BytesPerPixel;
00027 }
00028 //-----------------------------------------------------------------
00029 /**
00030  * Unlocks surface.
00031  */
00032 PixelIterator::~PixelIterator()
00033 {
00034     delete m_lock;
00035 }
00036 //-----------------------------------------------------------------
00037     void
00038 PixelIterator::setPos(const V2 &pos)
00039 {
00040     int x = pos.getX();
00041     int y = pos.getY();
00042     assert((0 <= x && x < m_surface->w) && (0 <= y && y < m_surface->h));
00043 
00044     m_p = static_cast<Uint8*>(m_surface->pixels) +
00045         y * m_surface->pitch +
00046         x * m_bpp;
00047 }
00048 //-----------------------------------------------------------------
00049 bool
00050 PixelIterator::isTransparent() const
00051 {
00052     return getPixel() == m_surface->format->colorkey;
00053 }
00054 //-----------------------------------------------------------------
00055 SDL_Color
00056 PixelIterator::getColor() const
00057 {
00058     SDL_Color color;
00059     SDL_GetRGBA(getPixel(), m_surface->format,
00060             &color.r, &color.g, &color.b, &color.unused);
00061     return color;
00062 }
00063 //-----------------------------------------------------------------
00064 Uint32
00065 PixelIterator::getPixel() const
00066 {
00067     return PixelTool::unpackPixel(m_bpp, m_p);
00068 }
00069 //-----------------------------------------------------------------
00070     void
00071 PixelIterator::putColor(const SDL_Color &color)
00072 {
00073     Uint32 pixel = SDL_MapRGBA(m_surface->format,
00074             color.r, color.g, color.b, color.unused);
00075     putPixel(pixel);
00076 }
00077 //-----------------------------------------------------------------
00078     void
00079 PixelIterator::putPixel(Uint32 pixel)
00080 {
00081     PixelTool::packPixel(m_bpp, m_p, pixel);
00082 }
00083 

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