snes9xgx/source/gui/gui_image.cpp

259 lines
4.6 KiB
C++
Raw Normal View History

2009-03-11 18:28:37 +01:00
/****************************************************************************
2009-04-10 10:12:37 +02:00
* libwiigui
2009-03-11 18:28:37 +01:00
*
2009-04-10 10:12:37 +02:00
* Tantric 2009
2009-03-11 18:28:37 +01:00
*
* gui_image.cpp
*
* GUI class definitions
***************************************************************************/
#include "gui.h"
/**
* Constructor for the GuiImage class.
*/
GuiImage::GuiImage()
{
image = NULL;
width = 0;
height = 0;
imageangle = 0;
tile = -1;
stripe = 0;
imgType = IMAGE_DATA;
}
2009-03-11 18:28:37 +01:00
GuiImage::GuiImage(GuiImageData * img)
{
2009-06-07 20:00:57 +02:00
image = NULL;
width = 0;
height = 0;
if(img)
{
image = img->GetImage();
width = img->GetWidth();
height = img->GetHeight();
}
2009-03-11 18:28:37 +01:00
imageangle = 0;
2009-03-16 06:16:20 +01:00
tile = -1;
2009-03-11 18:28:37 +01:00
stripe = 0;
imgType = IMAGE_DATA;
}
GuiImage::GuiImage(u8 * img, int w, int h)
{
image = img;
width = w;
height = h;
imageangle = 0;
2009-03-16 06:16:20 +01:00
tile = -1;
2009-03-11 18:28:37 +01:00
stripe = 0;
imgType = IMAGE_TEXTURE;
}
GuiImage::GuiImage(int w, int h, GXColor c)
{
2010-01-25 08:34:45 +01:00
image = (u8 *)memalign (32, w * h << 2);
2009-03-11 18:28:37 +01:00
width = w;
height = h;
imageangle = 0;
2009-03-16 06:16:20 +01:00
tile = -1;
2009-03-11 18:28:37 +01:00
stripe = 0;
imgType = IMAGE_COLOR;
if(!image)
return;
int x, y;
2010-01-25 08:34:45 +01:00
for(y=0; y < h; ++y)
2009-03-11 18:28:37 +01:00
{
2010-01-25 08:34:45 +01:00
for(x=0; x < w; ++x)
2009-03-11 18:28:37 +01:00
{
this->SetPixel(x, y, c);
}
}
2010-01-25 08:34:45 +01:00
int len = w * h << 2;
2009-03-21 22:19:09 +01:00
if(len%32) len += (32-len%32);
DCFlushRange(image, len);
2009-03-11 18:28:37 +01:00
}
/**
* Destructor for the GuiImage class.
*/
GuiImage::~GuiImage()
{
if(imgType == IMAGE_COLOR && image)
free(image);
}
u8 * GuiImage::GetImage()
{
return image;
}
void GuiImage::SetImage(GuiImageData * img)
{
2009-06-07 20:00:57 +02:00
image = NULL;
width = 0;
height = 0;
if(img)
{
image = img->GetImage();
width = img->GetWidth();
height = img->GetHeight();
}
2009-03-11 18:28:37 +01:00
imgType = IMAGE_DATA;
}
void GuiImage::SetImage(u8 * img, int w, int h)
{
image = img;
width = w;
height = h;
imgType = IMAGE_TEXTURE;
}
void GuiImage::SetAngle(float a)
{
imageangle = a;
}
void GuiImage::SetTile(int t)
{
tile = t;
}
GXColor GuiImage::GetPixel(int x, int y)
{
if(!image || this->GetWidth() <= 0 || x < 0 || y < 0)
return (GXColor){0, 0, 0, 0};
u32 offset = (((y >> 2)<<4)*this->GetWidth()) + ((x >> 2)<<6) + (((y%4 << 2) + x%4 ) << 1);
GXColor color;
color.a = *(image+offset);
color.r = *(image+offset+1);
color.g = *(image+offset+32);
color.b = *(image+offset+33);
return color;
}
void GuiImage::SetPixel(int x, int y, GXColor color)
{
if(!image || this->GetWidth() <= 0 || x < 0 || y < 0)
return;
u32 offset = (((y >> 2)<<4)*this->GetWidth()) + ((x >> 2)<<6) + (((y%4 << 2) + x%4 ) << 1);
*(image+offset) = color.a;
*(image+offset+1) = color.r;
*(image+offset+32) = color.g;
*(image+offset+33) = color.b;
}
void GuiImage::SetStripe(int s)
{
stripe = s;
}
void GuiImage::ColorStripe(int shift)
{
GXColor color;
2010-01-25 08:34:45 +01:00
int x, y=0;
2009-03-11 18:28:37 +01:00
int alt = 0;
2010-01-25 08:34:45 +01:00
int thisHeight = this->GetHeight();
int thisWidth = this->GetWidth();
2009-03-11 18:28:37 +01:00
2010-01-25 08:34:45 +01:00
for(; y < thisHeight; ++y)
2009-03-11 18:28:37 +01:00
{
if(y % 3 == 0)
alt ^= 1;
2010-01-25 08:34:45 +01:00
if(alt)
2009-03-11 18:28:37 +01:00
{
2010-01-25 08:34:45 +01:00
for(x=0; x < thisWidth; ++x)
2009-03-11 18:28:37 +01:00
{
2010-01-25 08:34:45 +01:00
color = GetPixel(x, y);
2009-03-11 18:28:37 +01:00
if(color.r < 255-shift)
color.r += shift;
else
color.r = 255;
if(color.g < 255-shift)
color.g += shift;
else
color.g = 255;
if(color.b < 255-shift)
color.b += shift;
else
color.b = 255;
color.a = 255;
2010-01-25 08:34:45 +01:00
SetPixel(x, y, color);
2009-03-11 18:28:37 +01:00
}
2010-01-25 08:34:45 +01:00
}
else
{
for(x=0; x < thisWidth; ++x)
2009-03-11 18:28:37 +01:00
{
2010-01-25 08:34:45 +01:00
color = GetPixel(x, y);
2009-03-11 18:28:37 +01:00
if(color.r > shift)
color.r -= shift;
else
color.r = 0;
if(color.g > shift)
color.g -= shift;
else
color.g = 0;
if(color.b > shift)
color.b -= shift;
else
color.b = 0;
color.a = 255;
2010-01-25 08:34:45 +01:00
SetPixel(x, y, color);
2009-03-11 18:28:37 +01:00
}
}
}
}
/**
* Draw the button on screen
*/
void GuiImage::Draw()
{
2009-03-16 06:16:20 +01:00
if(!image || !this->IsVisible() || tile == 0)
2009-03-11 18:28:37 +01:00
return;
float currScale = this->GetScale();
int currLeft = this->GetLeft();
2010-01-25 08:34:45 +01:00
int thisTop = this->GetTop();
2009-03-11 18:28:37 +01:00
if(tile > 0)
{
2010-01-25 08:34:45 +01:00
int alpha = this->GetAlpha();
for(int i=0; i<tile; ++i)
{
Menu_DrawImg(currLeft+width*i, thisTop, width, height, image, imageangle, currScale, currScale, alpha);
}
2009-03-11 18:28:37 +01:00
}
else
{
// temporary (maybe), used to correct offset for scaled images
if(scale != 1)
2010-01-25 08:34:45 +01:00
currLeft += (width*(scale - 1))/2;
2009-03-11 18:28:37 +01:00
2010-01-25 08:34:45 +01:00
Menu_DrawImg(currLeft, thisTop, width, height, image, imageangle, currScale, currScale, this->GetAlpha());
2009-03-11 18:28:37 +01:00
}
if(stripe > 0)
2010-01-25 08:34:45 +01:00
{
int thisHeight = this->GetHeight();
int thisWidth = this->GetWidth();
for(int y=0; y < thisHeight; y+=6)
Menu_DrawRectangle(currLeft,thisTop+y,thisWidth,3,(GXColor){0, 0, 0, stripe},1);
}
2009-03-11 18:28:37 +01:00
this->UpdateEffects();
}