00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "gui.h"
00012
00013
00014
00015 GuiImage::GuiImage() : displayWidth(0), displayHeight(0)
00016 {
00017 image = NULL;
00018 width = 0;
00019 height = 0;
00020 imageangle = 0;
00021 tile = -1;
00022 stripe = 0;
00023 imgType = IMAGE_DATA;
00024 }
00025
00026 GuiImage::GuiImage(GuiImageData * img) : displayWidth(0), displayHeight(0)
00027 {
00028 image = NULL;
00029 width = 0;
00030 height = 0;
00031 if(img)
00032 {
00033 image = img->GetImage();
00034 width = img->GetWidth();
00035 height = img->GetHeight();
00036 }
00037 imageangle = 0;
00038 tile = -1;
00039 stripe = 0;
00040 imgType = IMAGE_DATA;
00041 }
00042
00043 GuiImage::GuiImage(u8 * img, int w, int h) : displayWidth(0), displayHeight(0)
00044 {
00045 image = img;
00046 width = w;
00047 height = h;
00048 imageangle = 0;
00049 tile = -1;
00050 stripe = 0;
00051 imgType = IMAGE_TEXTURE;
00052 }
00053
00054 GuiImage::GuiImage(int w, int h, GXColor c) : displayWidth(0), displayHeight(0)
00055 {
00056 image = (u8 *)memalign (32, w * h * 4);
00057 width = w;
00058 height = h;
00059 imageangle = 0;
00060 tile = -1;
00061 stripe = 0;
00062 imgType = IMAGE_COLOR;
00063
00064 if(!image)
00065 return;
00066
00067 int x, y;
00068
00069 for(y=0; y < h; y++)
00070 {
00071 for(x=0; x < w; x++)
00072 {
00073 this->SetPixel(x, y, c);
00074 }
00075 }
00076 int len = w*h*4;
00077 if(len%32) len += (32-len%32);
00078 DCFlushRange(image, len);
00079 }
00080
00081
00082
00083
00084 GuiImage::~GuiImage()
00085 {
00086 if(imgType == IMAGE_COLOR && image)
00087 free(image);
00088 }
00089
00090 u8 * GuiImage::GetImage()
00091 {
00092 return image;
00093 }
00094
00095 void GuiImage::SetImage(GuiImageData * img)
00096 {
00097 image = NULL;
00098 width = 0;
00099 height = 0;
00100 if(img)
00101 {
00102 image = img->GetImage();
00103 width = img->GetWidth();
00104 height = img->GetHeight();
00105 }
00106 imgType = IMAGE_DATA;
00107 }
00108
00109 void GuiImage::SetImage(u8 * img, int w, int h)
00110 {
00111 image = img;
00112 width = w;
00113 height = h;
00114 imgType = IMAGE_TEXTURE;
00115 }
00116
00117 void GuiImage::SetAngle(float a)
00118 {
00119 imageangle = a;
00120 }
00121
00122 void GuiImage::SetTile(int t)
00123 {
00124 tile = t;
00125 }
00126
00127 GXColor GuiImage::GetPixel(int x, int y)
00128 {
00129 if(!image || this->GetWidth() <= 0 || x < 0 || y < 0)
00130 return (GXColor){0, 0, 0, 0};
00131
00132 u32 offset = (((y >> 2)<<4)*this->GetWidth()) + ((x >> 2)<<6) + (((y%4 << 2) + x%4 ) << 1);
00133 GXColor color;
00134 color.a = *(image+offset);
00135 color.r = *(image+offset+1);
00136 color.g = *(image+offset+32);
00137 color.b = *(image+offset+33);
00138 return color;
00139 }
00140
00141 void GuiImage::SetPixel(int x, int y, GXColor color)
00142 {
00143 if(!image || this->GetWidth() <= 0 || x < 0 || y < 0)
00144 return;
00145
00146 u32 offset = (((y >> 2)<<4)*this->GetWidth()) + ((x >> 2)<<6) + (((y%4 << 2) + x%4 ) << 1);
00147 *(image+offset) = color.a;
00148 *(image+offset+1) = color.r;
00149 *(image+offset+32) = color.g;
00150 *(image+offset+33) = color.b;
00151 }
00152
00153 void GuiImage::SetColor(GXColor c)
00154 {
00155 int x, y;
00156
00157 for(y=0; y < height; y++)
00158 {
00159 for(x=0; x < width; x++)
00160 {
00161 this->SetPixel(x, y, c);
00162 }
00163 }
00164 }
00165
00166 void GuiImage::SetStripe(int s)
00167 {
00168 stripe = s;
00169 }
00170
00171 void GuiImage::ColorStripe(int shift)
00172 {
00173 int x, y;
00174 GXColor color;
00175 int alt = 0;
00176
00177 for(y=0; y < this->GetHeight(); y++)
00178 {
00179 if(y % 3 == 0)
00180 alt ^= 1;
00181
00182 for(x=0; x < this->GetWidth(); x++)
00183 {
00184 color = GetPixel(x, y);
00185
00186 if(alt)
00187 {
00188 if(color.r < 255-shift)
00189 color.r += shift;
00190 else
00191 color.r = 255;
00192 if(color.g < 255-shift)
00193 color.g += shift;
00194 else
00195 color.g = 255;
00196 if(color.b < 255-shift)
00197 color.b += shift;
00198 else
00199 color.b = 255;
00200
00201 color.a = 255;
00202 }
00203 else
00204 {
00205 if(color.r > shift)
00206 color.r -= shift;
00207 else
00208 color.r = 0;
00209 if(color.g > shift)
00210 color.g -= shift;
00211 else
00212 color.g = 0;
00213 if(color.b > shift)
00214 color.b -= shift;
00215 else
00216 color.b = 0;
00217
00218 color.a = 255;
00219 }
00220 SetPixel(x, y, color);
00221 }
00222 }
00223 int len = width*height*4;
00224 if(len%32) len += (32-len%32);
00225 DCFlushRange(image, len);
00226 }
00227
00228 void GuiImage::Grayscale()
00229 {
00230 GXColor color;
00231 u32 offset, gray;
00232
00233 for (int x = 0; x < width; x++)
00234 {
00235 for (int y = 0; y < height; y++)
00236 {
00237 offset = (((y >> 2) << 4) * this->GetWidth()) + ((x >> 2) << 6)
00238 + (((y % 4 << 2) + x % 4) << 1);
00239 color.r = *(image + offset + 1);
00240 color.g = *(image + offset + 32);
00241 color.b = *(image + offset + 33);
00242
00243 gray = (77 * color.r + 150 * color.g + 28 * color.b) / 255;
00244
00245 *(image + offset + 1) = gray;
00246 *(image + offset + 32) = gray;
00247 *(image + offset + 33) = gray;
00248 }
00249 }
00250 int len = width*height*4;
00251 if(len%32) len += (32-len%32);
00252 DCFlushRange(image, len);
00253 }
00254
00255
00256
00257
00258 void GuiImage::Draw()
00259 {
00260 if(!image || !this->IsVisible() || tile == 0)
00261 return;
00262
00263 float currScale = this->GetScale();
00264 int currLeft = this->GetLeft();
00265
00266 if(tile > 0)
00267 {
00268 for(int i=0; i<tile; i++)
00269 Menu_DrawImg(currLeft+width*i, this->GetTop(), width, height, image,
00270 imageangle, currScale, currScale, this->GetAlpha(),
00271 displayWidth, displayHeight);
00272 }
00273 else
00274 {
00275
00276 if(scale != 1)
00277 currLeft = currLeft - width/2 + (width*scale)/2;
00278
00279 Menu_DrawImg(currLeft, this->GetTop(), width, height, image, imageangle,
00280 currScale, currScale, this->GetAlpha(),
00281 displayWidth, displayHeight);
00282 }
00283
00284 if(stripe > 0)
00285 for(int y=0; y < this->GetHeight(); y+=6)
00286 Menu_DrawRectangle(currLeft,this->GetTop()+y,this->GetWidth(),3,(GXColor){0, 0, 0, stripe},1);
00287
00288 this->UpdateEffects();
00289 }