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 #ifdef _WIN32
00060 #define WIN32_LEAN_AND_MEAN
00061 #include <windows.h>
00062 #endif
00063
00064 #ifdef __amigaos4__
00065 #include <mgl/gl.h>
00066 #else
00067 #include <GL/gl.h>
00068 #endif
00069
00070 #include "guichan/opengl/openglimageloader.hpp"
00071 #include "guichan/exception.hpp"
00072
00073 namespace gcn
00074 {
00075
00076 OpenGLImageLoader::OpenGLImageLoader()
00077 {
00078 mImageLoader = 0;
00079 }
00080
00081 OpenGLImageLoader::OpenGLImageLoader(ImageLoader* imageLoader)
00082 {
00083 mImageLoader = imageLoader;
00084 }
00085
00086 void OpenGLImageLoader::setHostImageLoader(ImageLoader* imageLoader)
00087 {
00088 mImageLoader = imageLoader;
00089 }
00090
00091 void OpenGLImageLoader::prepare(const std::string& filename)
00092 {
00093 if(mImageLoader == NULL)
00094 {
00095 throw GCN_EXCEPTION("No host ImageLoader present.");
00096 }
00097
00098 mImageLoader->prepare(filename);
00099 }
00100
00101 void* OpenGLImageLoader::getRawData()
00102 {
00103 if(mImageLoader == NULL)
00104 {
00105 throw GCN_EXCEPTION("No host ImageLoader present.");
00106 }
00107
00108 return mImageLoader->getRawData();
00109 }
00110
00111 void* OpenGLImageLoader::finalize()
00112 {
00113 if(mImageLoader == NULL)
00114 {
00115 throw GCN_EXCEPTION("No host ImageLoader present.");
00116 }
00117
00118 unsigned int *rawData = (unsigned int *)mImageLoader->getRawData();
00119 int width = mImageLoader->getWidth();
00120 int height = mImageLoader->getHeight();
00121 int realWidth = 1, realHeight = 1;
00122
00123 while(realWidth < width)
00124 {
00125 realWidth *= 2;
00126 }
00127
00128 while(realHeight < height)
00129 {
00130 realHeight *= 2;
00131 }
00132
00133 unsigned int *realData = new unsigned int[realWidth*realHeight];
00134 int x, y;
00135
00136 for (y = 0; y < realHeight; y++)
00137 {
00138 for (x = 0; x < realWidth; x++)
00139 {
00140 if (x < width && y < height)
00141 {
00142 if (rawData[x+y*width] == 0xffff00ff)
00143 {
00144 realData[x+y*realWidth] = 0x00000000;
00145 }
00146 else
00147 {
00148 realData[x+y*realWidth] = rawData[x+y*width];
00149 }
00150 }
00151 else
00152 {
00153 realData[x+y*realWidth] = 0;
00154 }
00155 }
00156 }
00157
00158 GLuint *texture = new GLuint[1];
00159 glGenTextures(1, texture);
00160 glBindTexture(GL_TEXTURE_2D, *texture);
00161
00162 glTexImage2D(GL_TEXTURE_2D,
00163 0,
00164 4,
00165 realWidth,
00166 realHeight,
00167 0,
00168 GL_RGBA,
00169 GL_UNSIGNED_BYTE,
00170 realData);
00171
00172 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
00173 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
00174
00175 delete[] realData;
00176 mImageLoader->discard();
00177
00178 GLenum error = glGetError();
00179 if (error)
00180 {
00181 std::string errmsg;
00182 switch (error)
00183 {
00184 case GL_INVALID_ENUM:
00185 errmsg = "GL_INVALID_ENUM";
00186 break;
00187
00188 case GL_INVALID_VALUE:
00189 errmsg = "GL_INVALID_VALUE";
00190 break;
00191
00192 case GL_INVALID_OPERATION:
00193 errmsg = "GL_INVALID_OPERATION";
00194 break;
00195
00196 case GL_STACK_OVERFLOW:
00197 errmsg = "GL_STACK_OVERFLOW";
00198 break;
00199
00200 case GL_STACK_UNDERFLOW:
00201 errmsg = "GL_STACK_UNDERFLOW";
00202 break;
00203
00204 case GL_OUT_OF_MEMORY:
00205 errmsg = "GL_OUT_OF_MEMORY";
00206 break;
00207 }
00208
00209 throw GCN_EXCEPTION(std::string("glGetError said: ") + errmsg);
00210 }
00211
00212 return (void *)texture;
00213 }
00214
00215 void OpenGLImageLoader::discard()
00216 {
00217 if(mImageLoader == NULL)
00218 {
00219 throw GCN_EXCEPTION("No host ImageLoader present.");
00220 }
00221
00222 mImageLoader->discard();
00223 }
00224
00225 void OpenGLImageLoader::free(Image* image)
00226 {
00227 glDeleteTextures(1, (GLuint *)image->_getData());
00228
00229 delete[] (GLuint *)(image->_getData());
00230 }
00231
00232 int OpenGLImageLoader::getWidth() const
00233 {
00234 if(mImageLoader == NULL)
00235 {
00236 throw GCN_EXCEPTION("No host ImageLoader present.");
00237 }
00238
00239 return mImageLoader->getWidth();
00240 }
00241
00242 int OpenGLImageLoader::getHeight() const
00243 {
00244 if(mImageLoader == NULL)
00245 {
00246 throw GCN_EXCEPTION("No host ImageLoader present.");
00247 }
00248
00249 return mImageLoader->getHeight();
00250 }
00251
00252 Color OpenGLImageLoader::getPixel(int x, int y)
00253 {
00254 if(mImageLoader == NULL)
00255 {
00256 throw GCN_EXCEPTION("No host ImageLoader present.");
00257 }
00258
00259 return mImageLoader->getPixel(x, y);
00260 }
00261
00262 void OpenGLImageLoader::putPixel(int x, int y, const Color& color)
00263 {
00264 if(mImageLoader == NULL)
00265 {
00266 throw GCN_EXCEPTION("No host ImageLoader present.");
00267 }
00268
00269 mImageLoader->putPixel(x, y, color);
00270 }
00271 }