/* _______ __ __ __ ______ __ __ _______ __ __
* / _____/\ / /\ / /\ / /\ / ____/\ / /\ / /\ / ___ /\ / |\/ /\
* / /\____\// / // / // / // /\___\// /_// / // /\_/ / // , |/ / /
* / / /__ / / // / // / // / / / ___ / // ___ / // /| ' / /
* / /_// /\ / /_// / // / // /_/_ / / // / // /\_/ / // / | / /
* /______/ //______/ //_/ //_____/\ /_/ //_/ //_/ //_/ //_/ /|_/ /
* \______\/ \______\/ \_\/ \_____\/ \_\/ \_\/ \_\/ \_\/ \_\/ \_\/
*
* Copyright (c) 2004, 2005, 2006, 2007 Olof Naessén and Per Larsson
*
* Js_./
* Per Larsson a.k.a finalman _RqZ{a<^_aa
* Olof Naessén a.k.a jansem/yakslem _asww7!uY`> )\a//
* _Qhm`] _f "'c 1!5m
* Visit: http://guichan.darkbits.org )Qk
ws?a-?' ._/L #'
* binary forms, with or without )4d[#7r, . ' )d`)[
* modification, are permitted provided _Q-5'5W..j/?' -?!\)cam'
* that the following conditions are met: j<. a J@\
* this list of conditions and the j(]1u= mWidth || y < 0 || y >= mHeight)
{
throw GCN_EXCEPTION("Coordinates outside of the image");
}
unsigned int c = mPixels[x + y * mTextureWidth];
#ifdef __BIG_ENDIAN__
unsigned char r = (c >> 24) & 0xff;
unsigned char g = (c >> 16) & 0xff;
unsigned char b = (c >> 8) & 0xff;
unsigned char a = c & 0xff;
#else
unsigned char a = (c >> 24) & 0xff;
unsigned char b = (c >> 16) & 0xff;
unsigned char g = (c >> 8) & 0xff;
unsigned char r = c & 0xff;
#endif
return Color(r, g, b, a);
}
void OpenGLImage::putPixel(int x, int y, const Color& color)
{
if (mPixels == NULL)
{
throw GCN_EXCEPTION("Image has been converted to display format");
}
if (x < 0 || x >= mWidth || y < 0 || y >= mHeight)
{
throw GCN_EXCEPTION("Coordinates outside of the image");
}
#ifdef __BIG_ENDIAN__
unsigned int c = color.a | color.b << 8 | color.g << 16 | color.r << 24;
#else
unsigned int c = color.r | color.g << 8 | color.b << 16 | color.a << 24;
#endif
mPixels[x + y * mTextureWidth] = c;
}
void OpenGLImage::convertToDisplayFormat()
{
if (mPixels == NULL)
{
throw GCN_EXCEPTION("Image has already been converted to display format");
}
glGenTextures(1, &mTextureHandle);
glBindTexture(GL_TEXTURE_2D, mTextureHandle);
glTexImage2D(GL_TEXTURE_2D,
0,
4,
mTextureWidth,
mTextureHeight,
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
mPixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
delete[] mPixels;
mPixels = NULL;
GLenum error = glGetError();
if (error)
{
std::string errmsg;
switch (error)
{
case GL_INVALID_ENUM:
errmsg = "GL_INVALID_ENUM";
break;
case GL_INVALID_VALUE:
errmsg = "GL_INVALID_VALUE";
break;
case GL_INVALID_OPERATION:
errmsg = "GL_INVALID_OPERATION";
break;
case GL_STACK_OVERFLOW:
errmsg = "GL_STACK_OVERFLOW";
break;
case GL_STACK_UNDERFLOW:
errmsg = "GL_STACK_UNDERFLOW";
break;
case GL_OUT_OF_MEMORY:
errmsg = "GL_OUT_OF_MEMORY";
break;
}
throw GCN_EXCEPTION(std::string("Unable to convert to OpenGL display format, glGetError said: ") + errmsg);
}
}
}