00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059 #include <SDL/SDL_image.h>
00060
00061 #include "guichan/sdl/sdlimageloader.hpp"
00062 #include "guichan/exception.hpp"
00063 #include "guichan/sdl/sdlpixel.hpp"
00064
00065 namespace gcn
00066 {
00067
00068 SDLImageLoader::SDLImageLoader()
00069 {
00070 mCurrentImage = NULL;
00071 }
00072
00073 void SDLImageLoader::prepare(const std::string& filename)
00074 {
00075 if (mCurrentImage != NULL)
00076 {
00077 throw GCN_EXCEPTION("Function called before finalizing or discarding last loaded image.");
00078 }
00079
00080 SDL_Surface* tmp = IMG_Load(filename.c_str());
00081
00082 if (tmp == NULL)
00083 {
00084 throw GCN_EXCEPTION(std::string("Unable to load image file: ")+filename);
00085 }
00086
00087 Uint32 rmask, gmask, bmask, amask;
00088 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
00089 rmask = 0xff000000;
00090 gmask = 0x00ff0000;
00091 bmask = 0x0000ff00;
00092 amask = 0x000000ff;
00093 #else
00094 rmask = 0x000000ff;
00095 gmask = 0x0000ff00;
00096 bmask = 0x00ff0000;
00097 amask = 0xff000000;
00098 #endif
00099
00100 mCurrentImage = SDL_CreateRGBSurface(SDL_SWSURFACE, 0, 0, 32,
00101 rmask, gmask, bmask, amask);
00102
00103 if (mCurrentImage == NULL)
00104 {
00105 throw GCN_EXCEPTION(std::string("Not enough memory to load: ")+filename);
00106 }
00107
00108 SDL_Surface* tmp2 = SDL_ConvertSurface(tmp, mCurrentImage->format, SDL_SWSURFACE);
00109 SDL_FreeSurface(tmp);
00110 SDL_FreeSurface(mCurrentImage);
00111
00112 mCurrentImage = tmp2;
00113 }
00114
00115 void* SDLImageLoader::getRawData()
00116 {
00117 return mCurrentImage->pixels;
00118 }
00119
00120 void* SDLImageLoader::finalize()
00121 {
00122 if (mCurrentImage == NULL)
00123 {
00124 throw GCN_EXCEPTION("No image prepared.");
00125 }
00126
00127 int i;
00128 bool hasPink = false;
00129 bool hasAlpha = false;
00130
00131 for (i = 0; i < mCurrentImage->w * mCurrentImage->h; ++i)
00132 {
00133 if (((unsigned int*)mCurrentImage->pixels)[i] == SDL_MapRGB(mCurrentImage->format,255,0,255))
00134 {
00135 hasPink = true;
00136 break;
00137 }
00138 }
00139
00140 for (i = 0; i < mCurrentImage->w * mCurrentImage->h; ++i)
00141 {
00142 Uint8 r, g, b, a;
00143
00144 SDL_GetRGBA(((unsigned int*)mCurrentImage->pixels)[i], mCurrentImage->format,
00145 &r, &g, &b, &a);
00146
00147 if (a != 255)
00148 {
00149 hasAlpha = true;
00150 break;
00151 }
00152 }
00153
00154
00155
00156 SDL_Surface *temp;
00157 if (hasAlpha)
00158 {
00159 temp = mCurrentImage;
00160 mCurrentImage = NULL;
00161 }
00162 else
00163 {
00164 temp = SDL_DisplayFormat(mCurrentImage);
00165 SDL_FreeSurface(mCurrentImage);
00166 mCurrentImage = NULL;
00167 }
00168
00169 if (hasPink)
00170 {
00171 SDL_SetColorKey(temp, SDL_SRCCOLORKEY,
00172 SDL_MapRGB(temp->format,255,0,255));
00173 }
00174 if (hasAlpha)
00175 {
00176 SDL_SetAlpha(temp, SDL_SRCALPHA, 255);
00177 }
00178
00179 return temp;
00180 }
00181
00182 void SDLImageLoader::discard()
00183 {
00184 if (mCurrentImage == NULL)
00185 {
00186 throw GCN_EXCEPTION("No image prepared.");
00187 }
00188
00189 SDL_FreeSurface(mCurrentImage);
00190
00191 mCurrentImage = NULL;
00192 }
00193
00194 void SDLImageLoader::free(Image* image)
00195 {
00196 if (image->_getData() == NULL)
00197 {
00198 throw GCN_EXCEPTION("Image data points to null.");
00199 }
00200
00201 SDL_FreeSurface((SDL_Surface*)image->_getData());
00202 }
00203
00204 int SDLImageLoader::getWidth() const
00205 {
00206 if (mCurrentImage == NULL)
00207 {
00208 throw GCN_EXCEPTION("No image prepared.");
00209 }
00210
00211 return mCurrentImage->w;
00212 }
00213
00214 int SDLImageLoader::getHeight() const
00215 {
00216 if (mCurrentImage == NULL)
00217 {
00218 throw GCN_EXCEPTION("No image prepared.");
00219 }
00220
00221 return mCurrentImage->h;
00222 }
00223
00224 Color SDLImageLoader::getPixel(int x, int y)
00225 {
00226 if (mCurrentImage == NULL)
00227 {
00228 throw GCN_EXCEPTION("No image prepared.");
00229 }
00230
00231 if (x < 0 || y < 0 || x >= mCurrentImage->w || y >= mCurrentImage->h)
00232 {
00233 throw GCN_EXCEPTION("x and y out of image bound.");
00234 }
00235
00236 return SDLgetPixel(mCurrentImage, x, y);
00237 }
00238
00239 void SDLImageLoader::putPixel(int x, int y, const Color& color)
00240 {
00241 if (mCurrentImage == NULL)
00242 {
00243 throw GCN_EXCEPTION("No image prepared.");
00244 }
00245
00246 if (x < 0 || y < 0 || x >= mCurrentImage->w || y >= mCurrentImage->h)
00247 {
00248 throw GCN_EXCEPTION("x and y out of image bound.");
00249 }
00250
00251 SDLputPixel(mCurrentImage, x, y, color);
00252 }
00253 }