summaryrefslogtreecommitdiff
path: root/src/guichan/sdl
diff options
context:
space:
mode:
Diffstat (limited to 'src/guichan/sdl')
-rw-r--r--src/guichan/sdl/sdlgraphics.cpp225
-rw-r--r--src/guichan/sdl/sdlimage.cpp24
-rw-r--r--src/guichan/sdl/sdlpixel.hpp213
3 files changed, 221 insertions, 241 deletions
diff --git a/src/guichan/sdl/sdlgraphics.cpp b/src/guichan/sdl/sdlgraphics.cpp
index c64018755..d80c1dd87 100644
--- a/src/guichan/sdl/sdlgraphics.cpp
+++ b/src/guichan/sdl/sdlgraphics.cpp
@@ -62,9 +62,10 @@
namespace gcn
{
- SDLGraphics::SDLGraphics()
+ SDLGraphics::SDLGraphics() :
+ mTarget(0),
+ mAlpha(false)
{
- mAlpha = false;
}
void SDLGraphics::_beginDraw()
@@ -108,9 +109,7 @@ namespace gcn
Graphics::popClipArea();
if (mClipStack.empty())
- {
return;
- }
const ClipRectangle& carea = mClipStack.top();
SDL_Rect rect;
@@ -137,7 +136,8 @@ namespace gcn
{
if (mClipStack.empty())
{
- throw GCN_EXCEPTION("Clip stack is empty, perhaps you called a draw funtion outside of _beginDraw() and _endDraw()?");
+ throw GCN_EXCEPTION("Clip stack is empty, perhaps you called a "
+ "draw funtion outside of _beginDraw() and _endDraw()?");
}
const ClipRectangle& top = mClipStack.top();
@@ -155,7 +155,8 @@ namespace gcn
if (srcImage == NULL)
{
- throw GCN_EXCEPTION("Trying to draw an image of unknown format, must be an SDLImage.");
+ throw GCN_EXCEPTION("Trying to draw an image of unknown format,"
+ " must be an SDLImage.");
}
SDL_BlitSurface(srcImage->getSurface(), &src, mTarget, &dst);
@@ -165,38 +166,36 @@ namespace gcn
{
if (mClipStack.empty())
{
- throw GCN_EXCEPTION("Clip stack is empty, perhaps you called a draw funtion outside of _beginDraw() and _endDraw()?");
+ throw GCN_EXCEPTION("Clip stack is empty, perhaps you called a "
+ "draw funtion outside of _beginDraw() and _endDraw()?");
}
const ClipRectangle& top = mClipStack.top();
-
+
Rectangle area = rectangle;
area.x += top.xOffset;
area.y += top.yOffset;
- if(!area.isIntersecting(top))
- {
+ if (!area.isIntersecting(top))
return;
- }
if (mAlpha)
{
int x1 = area.x > top.x ? area.x : top.x;
int y1 = area.y > top.y ? area.y : top.y;
- int x2 = area.x + area.width < top.x + top.width ? area.x + area.width : top.x + top.width;
- int y2 = area.y + area.height < top.y + top.height ? area.y + area.height : top.y + top.height;
+ int x2 = area.x + area.width < top.x + top.width ?
+ area.x + area.width : top.x + top.width;
+ int y2 = area.y + area.height < top.y + top.height ?
+ area.y + area.height : top.y + top.height;
int x, y;
SDL_LockSurface(mTarget);
for (y = y1; y < y2; y++)
{
for (x = x1; x < x2; x++)
- {
SDLputPixelAlpha(mTarget, x, y, mColor);
- }
}
SDL_UnlockSurface(mTarget);
-
}
else
{
@@ -219,32 +218,30 @@ namespace gcn
{
if (mClipStack.empty())
{
- throw GCN_EXCEPTION("Clip stack is empty, perhaps you called a draw funtion outside of _beginDraw() and _endDraw()?");
+ throw GCN_EXCEPTION("Clip stack is empty, perhaps you called a "
+ "draw funtion outside of _beginDraw() and _endDraw()?");
}
const ClipRectangle& top = mClipStack.top();
-
+
x += top.xOffset;
y += top.yOffset;
- if(!top.isPointInRect(x,y))
+ if (!top.isPointInRect(x, y))
return;
if (mAlpha)
- {
SDLputPixelAlpha(mTarget, x, y, mColor);
- }
else
- {
SDLputPixel(mTarget, x, y, mColor);
- }
}
void SDLGraphics::drawHLine(int x1, int y, int x2)
{
if (mClipStack.empty())
{
- throw GCN_EXCEPTION("Clip stack is empty, perhaps you called a draw funtion outside of _beginDraw() and _endDraw()?");
+ throw GCN_EXCEPTION("Clip stack is empty, perhaps you called a "
+ "draw funtion outside of _beginDraw() and _endDraw()?");
}
const ClipRectangle& top = mClipStack.top();
@@ -254,9 +251,7 @@ namespace gcn
x2 += top.xOffset;
if (y < top.y || y >= top.y + top.height)
- {
return;
- }
if (x1 > x2)
{
@@ -268,9 +263,7 @@ namespace gcn
if (top.x > x1)
{
if (top.x > x2)
- {
return;
- }
x1 = top.x;
}
@@ -278,10 +271,8 @@ namespace gcn
if (top.x + top.width <= x2)
{
if (top.x + top.width <= x1)
- {
return;
- }
-
+
x2 = top.x + top.width -1;
}
@@ -298,25 +289,22 @@ namespace gcn
switch(bpp)
{
case 1:
- for (;x1 <= x2; ++x1)
- {
+ for (; x1 <= x2; ++x1)
*(p++) = pixel;
- }
break;
-
+
case 2:
{
Uint16* q = (Uint16*)p;
- for (;x1 <= x2; ++x1)
- {
+ for (; x1 <= x2; ++x1)
*(q++) = pixel;
- }
break;
}
+
case 3:
- if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
+ if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
{
- for (;x1 <= x2; ++x1)
+ for (; x1 <= x2; ++x1)
{
p[0] = (pixel >> 16) & 0xff;
p[1] = (pixel >> 8) & 0xff;
@@ -326,7 +314,7 @@ namespace gcn
}
else
{
- for (;x1 <= x2; ++x1)
+ for (; x1 <= x2; ++x1)
{
p[0] = pixel & 0xff;
p[1] = (pixel >> 8) & 0xff;
@@ -336,14 +324,14 @@ namespace gcn
}
break;
- case 4:
- {
+ case 4:
+ {
Uint32* q = (Uint32*)p;
- for (;x1 <= x2; ++x1)
+ for (; x1 <= x2; ++x1)
{
if (mAlpha)
{
- *q = SDLAlpha32(pixel,*q,mColor.a);
+ *q = SDLAlpha32(pixel, *q, mColor.a);
q++;
}
else
@@ -355,7 +343,6 @@ namespace gcn
}
default:
break;
-
} // end switch
SDL_UnlockSurface(mTarget);
@@ -365,7 +352,8 @@ namespace gcn
{
if (mClipStack.empty())
{
- throw GCN_EXCEPTION("Clip stack is empty, perhaps you called a draw funtion outside of _beginDraw() and _endDraw()?");
+ throw GCN_EXCEPTION("Clip stack is empty, perhaps you called a "
+ "draw funtion outside of _beginDraw() and _endDraw()?");
}
const ClipRectangle& top = mClipStack.top();
@@ -375,10 +363,8 @@ namespace gcn
y2 += top.yOffset;
if (x < top.x || x >= top.x + top.width)
- {
return;
- }
-
+
if (y1 > y2)
{
y1 ^= y2;
@@ -389,9 +375,7 @@ namespace gcn
if (top.y > y1)
{
if (top.y > y2)
- {
return;
- }
y1 = top.y;
}
@@ -399,9 +383,7 @@ namespace gcn
if (top.y + top.height <= y2)
{
if (top.y + top.height <= y1)
- {
return;
- }
y2 = top.y + top.height - 1;
}
@@ -412,68 +394,65 @@ namespace gcn
Uint8 *p = (Uint8 *)mTarget->pixels + y1 * mTarget->pitch + x * bpp;
- Uint32 pixel = SDL_MapRGB(mTarget->format, mColor.r, mColor.g, mColor.b);
+ Uint32 pixel = SDL_MapRGB(mTarget->format, mColor.r,
+ mColor.g, mColor.b);
switch(bpp)
{
- case 1:
- for (;y1 <= y2; ++y1)
- {
- *p = pixel;
- p += mTarget->pitch;
- }
- break;
-
- case 2:
- for (;y1 <= y2; ++y1)
- {
- *(Uint16*)p = pixel;
- p += mTarget->pitch;
- }
- break;
-
- case 3:
- if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
- {
- for (;y1 <= y2; ++y1)
- {
- p[0] = (pixel >> 16) & 0xff;
- p[1] = (pixel >> 8) & 0xff;
- p[2] = pixel & 0xff;
- p += mTarget->pitch;
- }
- }
- else
- {
- for (;y1 <= y2; ++y1)
- {
- p[0] = pixel & 0xff;
- p[1] = (pixel >> 8) & 0xff;
- p[2] = (pixel >> 16) & 0xff;
- p += mTarget->pitch;
- }
- }
- break;
-
- case 4:
- for (;y1 <= y2; ++y1)
- {
- if (mAlpha)
- {
- *(Uint32*)p = SDLAlpha32(pixel,*(Uint32*)p,mColor.a);
- }
- else
- {
- *(Uint32*)p = pixel;
- }
- p += mTarget->pitch;
- }
- break;
-
- default:
- break;
+ case 1:
+ for (; y1 <= y2; ++y1)
+ {
+ *p = pixel;
+ p += mTarget->pitch;
+ }
+ break;
+
+ case 2:
+ for (; y1 <= y2; ++y1)
+ {
+ *(Uint16*)p = pixel;
+ p += mTarget->pitch;
+ }
+ break;
+
+ case 3:
+ if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
+ {
+ for (; y1 <= y2; ++y1)
+ {
+ p[0] = (pixel >> 16) & 0xff;
+ p[1] = (pixel >> 8) & 0xff;
+ p[2] = pixel & 0xff;
+ p += mTarget->pitch;
+ }
+ }
+ else
+ {
+ for (; y1 <= y2; ++y1)
+ {
+ p[0] = pixel & 0xff;
+ p[1] = (pixel >> 8) & 0xff;
+ p[2] = (pixel >> 16) & 0xff;
+ p += mTarget->pitch;
+ }
+ }
+ break;
+
+ case 4:
+ for (; y1 <= y2; ++y1)
+ {
+ if (mAlpha)
+ *(Uint32*)p = SDLAlpha32(pixel, *(Uint32*)p, mColor.a);
+ else
+ *(Uint32*)p = pixel;
+ p += mTarget->pitch;
+ }
+ break;
+
+ default:
+ break;
} // end switch
-
+
SDL_UnlockSurface(mTarget);
}
@@ -506,7 +485,8 @@ namespace gcn
if (mClipStack.empty())
{
- throw GCN_EXCEPTION("Clip stack is empty, perhaps you called a draw funtion outside of _beginDraw() and _endDraw()?");
+ throw GCN_EXCEPTION("Clip stack is empty, perhaps you called a "
+ "draw funtion outside of _beginDraw() and _endDraw()?");
}
const ClipRectangle& top = mClipStack.top();
@@ -546,13 +526,9 @@ namespace gcn
if (top.isPointInRect(x, y))
{
if (mAlpha)
- {
SDLputPixelAlpha(mTarget, x, y, mColor);
- }
else
- {
SDLputPixel(mTarget, x, y, mColor);
- }
}
p += dy;
@@ -574,13 +550,9 @@ namespace gcn
if (top.isPointInRect(x, y))
{
if (mAlpha)
- {
SDLputPixelAlpha(mTarget, x, y, mColor);
- }
else
- {
SDLputPixel(mTarget, x, y, mColor);
- }
}
p += dy;
@@ -618,13 +590,9 @@ namespace gcn
if (top.isPointInRect(x, y))
{
if (mAlpha)
- {
SDLputPixelAlpha(mTarget, x, y, mColor);
- }
else
- {
SDLputPixel(mTarget, x, y, mColor);
- }
}
p += dx;
@@ -646,13 +614,9 @@ namespace gcn
if (top.isPointInRect(x, y))
{
if (mAlpha)
- {
SDLputPixelAlpha(mTarget, x, y, mColor);
- }
else
- {
SDLputPixel(mTarget, x, y, mColor);
- }
}
p += dx;
@@ -685,7 +649,8 @@ namespace gcn
{
if (mClipStack.empty())
{
- throw GCN_EXCEPTION("Clip stack is empty, perhaps you called a draw funtion outside of _beginDraw() and _endDraw()?");
+ throw GCN_EXCEPTION("Clip stack is empty, perhaps you called a "
+ "draw funtion outside of _beginDraw() and _endDraw()?");
}
const ClipRectangle& top = mClipStack.top();
diff --git a/src/guichan/sdl/sdlimage.cpp b/src/guichan/sdl/sdlimage.cpp
index cab5effdb..12c5571cf 100644
--- a/src/guichan/sdl/sdlimage.cpp
+++ b/src/guichan/sdl/sdlimage.cpp
@@ -61,9 +61,7 @@ namespace gcn
SDLImage::~SDLImage()
{
if (mAutoFree)
- {
free();
- }
}
SDL_Surface* SDLImage::getSurface() const
@@ -75,7 +73,8 @@ namespace gcn
{
if (mSurface == NULL)
{
- throw GCN_EXCEPTION("Trying to get the width of a non loaded image.");
+ throw GCN_EXCEPTION("Trying to get the width of "
+ "a non loaded image.");
}
return mSurface->w;
@@ -85,7 +84,8 @@ namespace gcn
{
if (mSurface == NULL)
{
- throw GCN_EXCEPTION("Trying to get the height of a non loaded image.");
+ throw GCN_EXCEPTION("Trying to get the height of "
+ "a non loaded image.");
}
return mSurface->h;
@@ -95,7 +95,8 @@ namespace gcn
{
if (mSurface == NULL)
{
- throw GCN_EXCEPTION("Trying to get a pixel from a non loaded image.");
+ throw GCN_EXCEPTION("Trying to get a pixel from a "
+ "non loaded image.");
}
return SDLgetPixel(mSurface, x, y);
@@ -105,7 +106,8 @@ namespace gcn
{
if (mSurface == NULL)
{
- throw GCN_EXCEPTION("Trying to put a pixel in a non loaded image.");
+ throw GCN_EXCEPTION("Trying to put a pixel in a "
+ "non loaded image.");
}
SDLputPixel(mSurface, x, y, color);
@@ -115,7 +117,8 @@ namespace gcn
{
if (mSurface == NULL)
{
- throw GCN_EXCEPTION("Trying to convert a non loaded image to display format.");
+ throw GCN_EXCEPTION("Trying to convert a non loaded image "
+ "to display format.");
}
int i;
@@ -124,7 +127,8 @@ namespace gcn
for (i = 0; i < mSurface->w * mSurface->h; ++i)
{
- if (((unsigned int*)mSurface->pixels)[i] == SDL_MapRGB(mSurface->format,255,0,255))
+ if (((unsigned int*)mSurface->pixels)[i] == SDL_MapRGB(
+ mSurface->format, 255, 0, 255))
{
hasPink = true;
break;
@@ -161,14 +165,12 @@ namespace gcn
}
if (tmp == NULL)
- {
throw GCN_EXCEPTION("Unable to convert image to display format.");
- }
if (hasPink)
{
SDL_SetColorKey(tmp, SDL_SRCCOLORKEY,
- SDL_MapRGB(tmp->format,255,0,255));
+ SDL_MapRGB(tmp->format, 255, 0, 255));
}
if (hasAlpha)
{
diff --git a/src/guichan/sdl/sdlpixel.hpp b/src/guichan/sdl/sdlpixel.hpp
index a5d9ca5a1..675b10fcd 100644
--- a/src/guichan/sdl/sdlpixel.hpp
+++ b/src/guichan/sdl/sdlpixel.hpp
@@ -70,40 +70,36 @@ namespace gcn
switch(bpp)
{
- case 1:
- color = *p;
- break;
-
- case 2:
- color = *(Uint16 *)p;
- break;
-
- case 3:
- if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
- {
- color = p[0] << 16 | p[1] << 8 | p[2];
- }
- else
- {
- color = p[0] | p[1] << 8 | p[2] << 16;
- }
- break;
+ case 1:
+ color = *p;
+ break;
+
+ case 2:
+ color = *(Uint16 *)p;
+ break;
+
+ case 3:
+ if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
+ color = p[0] << 16 | p[1] << 8 | p[2];
+ else
+ color = p[0] | p[1] << 8 | p[2] << 16;
+ break;
case 4:
- color = *(Uint32 *)p;
- break;
+ color = *(Uint32 *)p;
+ break;
default:
- color = *p;
- break;
+ color = *p;
+ break;
}
- unsigned char r,g,b,a;
+ unsigned char r, g, b, a;
SDL_GetRGBA(color, surface->format, &r, &g, &b, &a);
SDL_UnlockSurface(surface);
- return Color(r,g,b,a);
+ return Color(r, g, b, a);
}
/**
@@ -113,7 +109,8 @@ namespace gcn
* @param y the y coordinate on the surface.
* @param color the color the pixel should be in.
*/
- inline void SDLputPixel(SDL_Surface* surface, int x, int y, const Color& color)
+ inline void SDLputPixel(SDL_Surface* surface, int x, int y,
+ const Color& color)
{
int bpp = surface->format->BytesPerPixel;
@@ -125,35 +122,35 @@ namespace gcn
switch(bpp)
{
- case 1:
- *p = pixel;
- break;
-
- case 2:
- *(Uint16 *)p = pixel;
- break;
-
- case 3:
- if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
- {
- p[0] = (pixel >> 16) & 0xff;
- p[1] = (pixel >> 8) & 0xff;
- p[2] = pixel & 0xff;
- }
- else
- {
- p[0] = pixel & 0xff;
- p[1] = (pixel >> 8) & 0xff;
- p[2] = (pixel >> 16) & 0xff;
- }
- break;
-
- case 4:
- *(Uint32 *)p = pixel;
- break;
-
- default:
- break;
+ case 1:
+ *p = pixel;
+ break;
+
+ case 2:
+ *(Uint16 *)p = pixel;
+ break;
+
+ case 3:
+ if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
+ {
+ p[0] = (pixel >> 16) & 0xff;
+ p[1] = (pixel >> 8) & 0xff;
+ p[2] = pixel & 0xff;
+ }
+ else
+ {
+ p[0] = pixel & 0xff;
+ p[1] = (pixel >> 8) & 0xff;
+ p[2] = (pixel >> 16) & 0xff;
+ }
+ break;
+
+ case 4:
+ *(Uint32 *)p = pixel;
+ break;
+
+ default:
+ break;
}
SDL_UnlockSurface(surface);
@@ -166,11 +163,14 @@ namespace gcn
* @param dst the destination color.
* @param a alpha.
*/
- inline unsigned int SDLAlpha32(unsigned int src, unsigned int dst, unsigned char a)
+ inline unsigned int SDLAlpha32(unsigned int src, unsigned int dst,
+ unsigned char a)
{
unsigned int b = ((src & 0xff) * a + (dst & 0xff) * (255 - a)) >> 8;
- unsigned int g = ((src & 0xff00) * a + (dst & 0xff00) * (255 - a)) >> 8;
- unsigned int r = ((src & 0xff0000) * a + (dst & 0xff0000) * (255 - a)) >> 8;
+ unsigned int g = ((src & 0xff00) * a + (dst & 0xff00)
+ * (255 - a)) >> 8;
+ unsigned int r = ((src & 0xff0000) * a + (dst & 0xff0000)
+ * (255 - a)) >> 8;
return (b & 0xff) | (g & 0xff00) | (r & 0xff0000);
}
@@ -182,13 +182,18 @@ namespace gcn
* @param dst the destination color.
* @param a alpha.
*/
- inline unsigned short SDLAlpha16(unsigned short src, unsigned short dst, unsigned char a, const SDL_PixelFormat *f)
+ inline unsigned short SDLAlpha16(unsigned short src, unsigned short dst,
+ unsigned char a, const SDL_PixelFormat *f)
{
- unsigned int b = ((src & f->Rmask) * a + (dst & f->Rmask) * (255 - a)) >> 8;
- unsigned int g = ((src & f->Gmask) * a + (dst & f->Gmask) * (255 - a)) >> 8;
- unsigned int r = ((src & f->Bmask) * a + (dst & f->Bmask) * (255 - a)) >> 8;
-
- return (unsigned short)((b & f->Rmask) | (g & f->Gmask) | (r & f->Bmask));
+ unsigned int b = ((src & f->Rmask) * a + (dst & f->Rmask)
+ * (255 - a)) >> 8;
+ unsigned int g = ((src & f->Gmask) * a + (dst & f->Gmask)
+ * (255 - a)) >> 8;
+ unsigned int r = ((src & f->Bmask) * a + (dst & f->Bmask)
+ * (255 - a)) >> 8;
+
+ return (unsigned short)((b & f->Rmask)
+ | (g & f->Gmask) | (r & f->Bmask));
}
/*
@@ -211,7 +216,8 @@ namespace gcn
* @param y the y coordinate on the surface.
* @param color the color the pixel should be in.
*/
- inline void SDLputPixelAlpha(SDL_Surface* surface, int x, int y, const Color& color)
+ inline void SDLputPixelAlpha(SDL_Surface* surface, int x, int y,
+ const Color& color)
{
int bpp = surface->format->BytesPerPixel;
@@ -223,42 +229,49 @@ namespace gcn
switch(bpp)
{
- case 1:
- *p = pixel;
- break;
-
- case 2:
- *(Uint16 *)p = SDLAlpha16(pixel, *(Uint32 *)p, color.a, surface->format);
- break;
-
- case 3:
- if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
- {
- unsigned int r = (p[0] * (255 - color.a) + color.r * color.a) >> 8;
- unsigned int g = (p[1] * (255 - color.a) + color.g * color.a) >> 8;
- unsigned int b = (p[2] * (255 - color.a) + color.b * color.a) >> 8;
-
- p[2] = b;
- p[1] = g;
- p[0] = r;
- }
- else
- {
- unsigned int r = (p[2] * (255 - color.a) + color.r * color.a) >> 8;
- unsigned int g = (p[1] * (255 - color.a) + color.g * color.a) >> 8;
- unsigned int b = (p[0] * (255 - color.a) + color.b * color.a) >> 8;
-
- p[0] = b;
- p[1] = g;
- p[2] = r;
- }
- break;
-
- case 4:
- *(Uint32 *)p = SDLAlpha32(pixel, *(Uint32 *)p, color.a);
- break;
- default:
- break;
+ case 1:
+ *p = pixel;
+ break;
+
+ case 2:
+ *(Uint16 *)p = SDLAlpha16(pixel, *(Uint32 *)p,
+ color.a, surface->format);
+ break;
+
+ case 3:
+ if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
+ {
+ unsigned int r = (p[0] * (255 - color.a)
+ + color.r * color.a) >> 8;
+ unsigned int g = (p[1] * (255 - color.a)
+ + color.g * color.a) >> 8;
+ unsigned int b = (p[2] * (255 - color.a)
+ + color.b * color.a) >> 8;
+
+ p[2] = b;
+ p[1] = g;
+ p[0] = r;
+ }
+ else
+ {
+ unsigned int r = (p[2] * (255 - color.a)
+ + color.r * color.a) >> 8;
+ unsigned int g = (p[1] * (255 - color.a)
+ + color.g * color.a) >> 8;
+ unsigned int b = (p[0] * (255 - color.a)
+ + color.b * color.a) >> 8;
+
+ p[0] = b;
+ p[1] = g;
+ p[2] = r;
+ }
+ break;
+
+ case 4:
+ *(Uint32 *)p = SDLAlpha32(pixel, *(Uint32 *)p, color.a);
+ break;
+ default:
+ break;
}
SDL_UnlockSurface(surface);