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); } }
void AllegroGlyphKeeperFont::load(const std::string& filename, int w, int h) { mKeeper = gk_create_keeper(0,0); if (mKeeper == NULL) { throw GCN_EXCEPTION("Can't create keeper."); } mFace = gk_load_face_from_file(filename.c_str(), 0); if (mFace == NULL) { throw GCN_EXCEPTION("Can't load font from file."); } mRend = gk_create_renderer(mFace,mKeeper); if (mRend == NULL) { throw GCN_EXCEPTION("Can't create renderer."); } gk_rend_set_hinting_off(mRend); gk_rend_set_size_pixels(mRend, w, h); gk_rend_set_text_color_rgb(mRend, 0, 0, 0); }
virtual Image* load(const std::string& filename, bool convertToDisplayFormat = true) { SDL_Surface *loadedSurface = loadSDLSurface(filename); if (loadedSurface == NULL) { throw GCN_EXCEPTION( std::string("Unable to load image file: ") + filename); } SDL_Surface *surface = convertToStandardFormat(loadedSurface); SDL_FreeSurface(loadedSurface); if (surface == NULL) { throw GCN_EXCEPTION( std::string("Not enough memory to load: ") + filename); } OpenGLImage *image = new OpenGLImage((unsigned int*)surface->pixels, surface->w, surface->h, convertToDisplayFormat); SDL_FreeSurface(surface); return image; }
void DirectX3DGraphics::drawImage(const Image* image, int srcX, int srcY, int dstX, int dstY, int width, int height) { const DirectX3DImage* srcImage = dynamic_cast<const DirectX3DImage*>(image); if (srcImage == NULL) { throw GCN_EXCEPTION("Trying to draw an image of unknown format, must be a DirectXImage."); } if (mClipStack.empty()) { throw GCN_EXCEPTION("Clip stack is empty, perhaps you called a draw funtion outside of _beginDraw() and _endDraw()?"); } const ClipRectangle& top = mClipStack.top(); dstX += top.xOffset; dstY += top.yOffset; // Find DirectX texture coordinates float texX1 = srcX / (float)srcImage->getTextureWidth(); float texY1 = srcY / (float)srcImage->getTextureHeight(); float texX2 = (srcX+width) / (float)srcImage->getTextureWidth(); float texY2 = (srcY+height) / (float)srcImage->getTextureHeight(); // Check if blending already is enabled if (!mAlpha) { mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); } DWORD color = 0xFFFFFFFF; VertexWithTexture vertices[]= { {(float)dstX, (float)dstY + height, 0.0f, 1.0f, color, texX1, texY2}, {(float)dstX, (float)dstY, 0.0f, 1.0f, color, texX1, texY1}, {(float)dstX + width, (float)dstY + height, 0.0f, 1.0f, color, texX2, texY2}, {(float)dstX + width, (float)dstY, 0.0f, 1.0f, color, texX2, texY1}, }; mDevice->SetFVF(D3DFVF_XYZRHW|D3DFVF_DIFFUSE|D3DFVF_TEX1); mDevice->SetTexture(0, srcImage->getTexture()); mDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, vertices, sizeof(VertexWithTexture)); mDevice->SetTexture(0, 0); if (!mAlpha) { mDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); } }
Color CairoImage::getPixel(int x, int y) { if (!mCairoSurface) { throw GCN_EXCEPTION("Trying to get a pixel from a non loaded image."); } int stride=cairo_image_surface_get_stride(mCairoSurface); int yindex=y*stride; unsigned char *imagePixels=cairo_image_surface_get_data(mCairoSurface); if (!imagePixels) { throw GCN_EXCEPTION("Surface data are not available (surface is not of type cairo_image_surface or has been finished)"); } // deal differently with each surface format switch(cairo_image_surface_get_format(mCairoSurface)) { case CAIRO_FORMAT_ARGB32: return GetColorFromARGB(*((unsigned long*)(&imagePixels[x*4 + yindex]))); break; case CAIRO_FORMAT_RGB24: return GetColorFromRGB(*((unsigned long*)(&imagePixels[x*4 + yindex]))); break; case CAIRO_FORMAT_A8: return Color(0,0,0,imagePixels[x + yindex]); break; default : return Color(0,0,0); //do nothing break; } }
void CairoImage::putPixel(int x, int y, const Color& color) { if (!mCairoSurface) { throw GCN_EXCEPTION("Trying to write a pixel on a non loaded image."); } int stride=cairo_image_surface_get_stride(mCairoSurface); unsigned char *imagePixels=cairo_image_surface_get_data(mCairoSurface); if (!imagePixels) { throw GCN_EXCEPTION("Surface data are not available (surface is not of type Image or has been finished)"); } // deal differently with each surface format switch(cairo_image_surface_get_format(mCairoSurface)) { case CAIRO_FORMAT_ARGB32: *((unsigned long*)(&imagePixels[x*4 + y*stride]))=PrecomputeAlpha(color); break; case CAIRO_FORMAT_RGB24: *((unsigned long*)(&imagePixels[x*4 + y*stride]))=GetRGB(color); break; case CAIRO_FORMAT_A8: imagePixels[x + y*stride]=(unsigned char)color.a; break; default : //do nothing break; } }
void COCOS2DXGraphics::drawImage(const Image* image, int srcX, int srcY, int dstX, int dstY, int width, int height) { if (mClipStack.empty()) { throw GCN_EXCEPTION("Clip stack is empty, perhaps you called a draw funtion outside of _beginDraw() and _endDraw()?"); } const ClipRectangle& top = mClipStack.top(); SDLMOD_Rect src; SDLMOD_Rect dst; src.x = srcX; src.y = srcY; src.w = width; src.h = height; dst.x = dstX + top.xOffset; dst.y = dstY + top.yOffset; const COCOS2DXImage* srcImage = dynamic_cast<const COCOS2DXImage*>(image); if (srcImage == NULL) { throw GCN_EXCEPTION("Trying to draw an image of unknown format, must be an SDLImage."); } SDLMOD_BlitSurface(srcImage->getSurface(), &src, mTarget, &dst); }
gcn::Image* InfraellyImageLoader::load(SDL_RWops *source_Rwop, bool freeRWOP, bool convertToDisplayFormat){ //load the rWop into a SDL Surface SDL_Surface *loadedSurface = IMG_Load_RW(source_Rwop, freeRWOP); if (loadedSurface == NULL) { throw GCN_EXCEPTION( std::string("Unable to load image file: ") ); } SDL_Surface *surface = convertToStandardFormat(loadedSurface); SDL_FreeSurface(loadedSurface); if (surface == NULL) { throw GCN_EXCEPTION( std::string("Not enough memory to load: ") ); } gcn::Image *image = new gcn::SDLImage(surface, true); if (convertToDisplayFormat) { image->convertToDisplayFormat(); } return image; }
gcn::Image* InfraellyImageLoader::load(unsigned char *buffer, long bufferLength, bool convertToDisplayFormat){ //make rWop out of character buffer SDL_RWops *source_Rwop = SDL_RWFromMem(buffer, bufferLength); //load the rWop into a SDL Surface SDL_Surface *loadedSurface = IMG_Load_RW(source_Rwop, 1); if (loadedSurface == NULL) { throw GCN_EXCEPTION( std::string("Unable to load image file: ") ); } SDL_Surface *surface = convertToStandardFormat(loadedSurface); SDL_FreeSurface(loadedSurface); if (surface == NULL) { throw GCN_EXCEPTION( std::string("Not enough memory to load: ") ); } gcn::Image *image = new gcn::SDLImage(surface, true); if (convertToDisplayFormat) { image->convertToDisplayFormat(); } return image; }
Color OpenGLImage::getPixel(int x, int y) { 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"); } 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); }
Image* GLUTImageLoader::load(const std::string& filename, bool convertToDisplayFormat) { SDLMOD_Surface *loadedSurface = loadSDLSurface(filename); if (loadedSurface == NULL) { throw GCN_EXCEPTION( std::string("Unable to load image file: ") + filename); } #if 0 SDLMOD_Surface *surface = convertToStandardFormat(loadedSurface); SDLMOD_FreeSurface(loadedSurface); if (surface == NULL) { throw GCN_EXCEPTION( std::string("Not enough memory to load: ") + filename); } #else SDLMOD_Surface *surface = loadedSurface; #endif Image *image = new GLUTImage(surface, true); if (convertToDisplayFormat) { image->convertToDisplayFormat(); } return image; }
void FocusHandler::applyChanges() { if (mToBeFocused != NULL) { unsigned int i = 0; int toBeFocusedIndex = -1; for (i = 0; i < mWidgets.size(); ++i) { if (mWidgets[i] == mToBeFocused) { toBeFocusedIndex = i; break; } } if (toBeFocusedIndex < 0) { throw GCN_EXCEPTION("Trying to focus a none existing widget."); } Widget *oldFocused = mFocusedWidget; if (oldFocused != mToBeFocused) { mFocusedWidget = mWidgets.at(toBeFocusedIndex); if (oldFocused != NULL) { oldFocused->lostFocus(); } mWidgets.at(toBeFocusedIndex)->gotFocus(); } mToBeFocused = NULL; } if (mToBeDragged != NULL) { unsigned int i = 0; int toBeDraggedIndex = -1; for (i = 0; i < mWidgets.size(); ++i) { if (mWidgets[i] == mToBeDragged) { toBeDraggedIndex = i; break; } } if (toBeDraggedIndex < 0) { throw GCN_EXCEPTION("Trying to give drag to a none existing widget"); } mDraggedWidget = mWidgets.at(toBeDraggedIndex); mToBeDragged = NULL; } }
void ImageFont::addGlyph(unsigned char c, int &x, int &y, const Color& separator) { ImageLoader* il = Image::_getImageLoader(); Color color; do { ++x; if (x >= il->getWidth()) { y += mHeight + 1; x = 0; if (y >= il->getHeight()) { std::string str; std::ostringstream os(str); os << "Image "; os << mFilename; os << " with font is corrupt near character '"; os << c; os << "'"; throw GCN_EXCEPTION(os.str()); } } color = il->getPixel(x, y); } while (color == separator); int w = 0; do { ++w; if (x+w >= il->getWidth()) { std::string str; std::ostringstream os(str); os << "Image "; os << mFilename; os << " with font is corrupt near character '"; os << c; os << "'"; throw GCN_EXCEPTION(os.str()); } color = il->getPixel(x + w, y); } while (color != separator); mGlyph[c] = Rectangle(x, y, w, mHeight); x += w; }
Rectangle ImageFont::scanForGlyph(unsigned char glyph, int x, int y, const Color& separator) { Color color; do { ++x; if (x >= mImage->getWidth()) { y += mHeight + 1; x = 0; if (y >= mImage->getHeight()) { std::string str; std::ostringstream os(str); os << "Image "; os << mFilename; os << " with font is corrupt near character '"; os << glyph; os << "'"; throw GCN_EXCEPTION(os.str()); } } color = mImage->getPixel(x, y); } while (color == separator); int width = 0; do { ++width; if (x + width >= mImage->getWidth()) { std::string str; std::ostringstream os(str); os << "Image "; os << mFilename; os << " with font is corrupt near character '"; os << glyph; os << "'"; throw GCN_EXCEPTION(os.str()); } color = mImage->getPixel(x + width, y); } while (color != separator); return Rectangle(x, y, width, mHeight); }
ImageFont::ImageFont(Image* image, const std::string& glyphs) { mFilename = "Image*"; if (image == NULL) { GCN_EXCEPTION("Font image is NULL"); } mImage = image; Color separator = mImage->getPixel(0, 0); int i = 0; for (i = 0; i < mImage->getWidth() && separator == mImage->getPixel(i, 0); ++i) { } if (i >= mImage->getWidth()) { throw GCN_EXCEPTION("Corrupt image."); } int j = 0; for (j = 0; j < mImage->getHeight(); ++j) { if (separator == mImage->getPixel(i, j)) { break; } } mHeight = j; int x = 0, y = 0; unsigned char k; for (i = 0; i < (int)glyphs.size(); ++i) { k = glyphs.at(i); mGlyph[k] = scanForGlyph(k, x, y, separator); // Update x och y with new coordinates. x = mGlyph[k].x + mGlyph[k].width; y = mGlyph[k].y; } int w = mImage->getWidth(); int h = mImage->getHeight(); mImage->convertToDisplayFormat(); mRowSpacing = 0; mGlyphSpacing = 0; }
void SDLImageLoader::putPixel(int x, int y, const Color& color) { if (mCurrentImage == NULL) { throw GCN_EXCEPTION("No image prepared."); } if (x < 0 || y < 0 || x >= mCurrentImage->w || y >= mCurrentImage->h) { throw GCN_EXCEPTION("x and y out of image bound."); } SDLputPixel(mCurrentImage, x, y, color); }
Color SDLImageLoader::getPixel(int x, int y) { if (mCurrentImage == NULL) { throw GCN_EXCEPTION("No image prepared."); } if (x < 0 || y < 0 || x >= mCurrentImage->w || y >= mCurrentImage->h) { throw GCN_EXCEPTION("x and y out of image bound."); } return SDLgetPixel(mCurrentImage, x, y); }
void gcn::Panel::add(gcn::Widget *widget) { // width of current widget exceeds remaining space, go down 1 row int dimx = _nextx + _spacingX + widget->getDimension().width; if(dimx > getChildrenArea().width || (_slotsx > 0 && _usedx >= _slotsx)) { _usedx = 0; _nextx = _spacingX; _usedy++; _nexty += (_maxheight + _spacingY); _maxheight = 0; } // height of current widget exceeds remaining space, whoops int dimy = _nexty + _spacingY + widget->getDimension().height; if(dimy > getChildrenArea().height && _slotsy >= 0 && _usedy >= _slotsy) { throw GCN_EXCEPTION("height exceeded, can't add more widgets"); } widget->setPosition(_nextx, _nexty); Container::add(widget); _maxheight = std::max(_maxheight, widget->getDimension().height); _nextx += (_spacingX + widget->getDimension().width); _usedx++; }
void Text::setRow(unsigned int row, const std::string& content) { if (row >= mRows.size()) throw GCN_EXCEPTION("Row out of bounds!"); mRows[row] = content; }
void HGEImage::convertToDisplayFormat() { DWORD *pLockPtr = mHGE->Texture_Lock(mTexture); if (pLockPtr == NULL) { throw GCN_EXCEPTION("Locking of the texture failed. HGE only support locking of 32bit textures."); } int i; int end = mHGE->Texture_GetWidth(mTexture, true) * mHGE->Texture_GetHeight(mTexture, true); for (i = 0; i < end; i++) { DWORD color = pLockPtr[i]; if (GETR(color) == 0xFF && GETG(color) == 0x00 && GETB(color) == 0xFF) { pLockPtr[i] = ARGB(0x00, 0x00, 0x00, 0x00); } } mHGE->Texture_Unlock(mTexture); mHGESprite = new hgeSprite(mTexture, 0, 0, mHGE->Texture_GetWidth(mTexture, true), mHGE->Texture_GetHeight(mTexture, true)); }
std::string Text::getRow(unsigned int row) const { if (row >= mRows.size()) throw GCN_EXCEPTION("Row out of bounds!"); return mRows[row]; }
void OpenGLGraphics::drawLine(int x1, int y1, int x2, int y2) { if (mClipStack.empty()) { throw GCN_EXCEPTION("Clip stack is empty, perhaps you called a draw funtion outside of _beginDraw() and _endDraw()?"); } const ClipRectangle& top = mClipStack.top(); x1 += top.xOffset; y1 += top.yOffset; x2 += top.xOffset; y2 += top.yOffset; glBegin(GL_LINES); glVertex2f(x1 + 0.375f, y1 + 0.375f); glVertex2f(x2 + 1.0f - 0.375f, y2 + 1.0f - 0.375f); glEnd(); glBegin(GL_POINTS); glVertex2f(x2 + 1.0f - 0.375f, y2 + 1.0f - 0.375f); glEnd(); glBegin(GL_POINTS); glVertex2f(x1 + 0.375f, y1 + 0.375f); glEnd(); }
void Gui::handleMouseInput() { while (!mInput->isMouseQueueEmpty()) { MouseInput mouseInput = mInput->dequeueMouseInput(); // Save the current mouse state. It will be needed if modal focus // changes or modal mouse input focus changes. mLastMouseX = mouseInput.getX(); mLastMouseY = mouseInput.getY(); switch (mouseInput.getType()) { case MouseInput::PRESSED: handleMousePressed(mouseInput); break; case MouseInput::RELEASED: handleMouseReleased(mouseInput); break; case MouseInput::MOVED: handleMouseMoved(mouseInput); break; case MouseInput::WHEEL_MOVED_DOWN: handleMouseWheelMovedDown(mouseInput); break; case MouseInput::WHEEL_MOVED_UP: handleMouseWheelMovedUp(mouseInput); break; default: throw GCN_EXCEPTION("Unknown mouse input type."); break; } } }
int SDLInput::convertMouseButton(int button) { switch (button) { case SDL_BUTTON_LEFT: return MouseInput::LEFT; break; case SDL_BUTTON_RIGHT: return MouseInput::RIGHT; break; case SDL_BUTTON_MIDDLE: return MouseInput::MIDDLE; break; case SDL_BUTTON_WHEELUP: return MouseInput::WHEEL_UP; break; case SDL_BUTTON_WHEELDOWN: return MouseInput::WHEEL_DOWN; break; } throw GCN_EXCEPTION("Unknown SDL mouse type."); return 0; }
void OGLFTFont::drawString(gcn::Graphics* graphics, const std::string& text, int x, int y) { if (text == "") { return; } gcn::OpenGLGraphics* glGraphics = dynamic_cast<gcn::OpenGLGraphics *>(graphics); if(glGraphics == NULL) { throw GCN_EXCEPTION("Graphics object not an OpenGL graphics object!"); } const gcn::ClipRectangle& top = glGraphics->getCurrentClipArea(); mFont->setForegroundColor(mFontColor.r/255, mFontColor.g/255, mFontColor.b/255); glPushMatrix(); glEnable(GL_TEXTURE_2D); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glTranslated(x + top.xOffset, y + top.yOffset + (mSize/2)+5, 0.); glRotatef(180., 1., 0., 0.); mFont->draw(text.c_str()); glDisable(GL_BLEND); glDisable(GL_TEXTURE_2D); glPopMatrix(); }
const ClipRectangle& Graphics::getCurrentClipArea() { if (mClipStack.empty()) throw GCN_EXCEPTION("The clip area stack is empty."); return mClipStack.top(); }
void DirectX3DGraphics::fillRectangle(const Rectangle& rectangle) { if (mClipStack.empty()) { throw GCN_EXCEPTION("Clip stack is empty, perhaps you called a draw funtion outside of _beginDraw() and _endDraw()?"); } const ClipRectangle& top = mClipStack.top(); DWORD color = D3DCOLOR_RGBA(mColor.r, mColor.g, mColor.b, mColor.a); Vertex vertices[]= { {(float)rectangle.x + top.xOffset, (float)rectangle.y + rectangle.height + top.yOffset, 0.0f, 1.0f, color}, {(float)rectangle.x + top.xOffset, (float)rectangle.y + top.yOffset, 0.0f, 1.0f, color}, {(float)rectangle.x + rectangle.width + top.xOffset, (float)rectangle.y + rectangle.height + top.yOffset, 0.0f, 1.0f, color}, {(float)rectangle.x + rectangle.width + top.xOffset, (float)rectangle.y + top.yOffset, 0.0f, 1.0f, color} }; mDevice->SetFVF(D3DFVF_XYZRHW|D3DFVF_DIFFUSE); mDevice->DrawPrimitiveUP(D3DPT_TRIANGLESTRIP, 2, vertices, sizeof(Vertex)); }
OpenLayerTTFont::OpenLayerTTFont(const std::string& filename, int width, int height) : mTextRenderer(filename.c_str(), width, height) { if (!mTextRenderer.IsValid()) { throw GCN_EXCEPTION("Unable to load font."); } }
void Graphics::popClipArea() { if (mClipStack.empty()) throw GCN_EXCEPTION("Tried to pop clip area from empty stack."); mClipStack.pop(); }
void Widget::requestModalMouseInputFocus() { if (mFocusHandler == NULL) { throw GCN_EXCEPTION("No focushandler set (did you add the widget to the gui?)."); } mFocusHandler->requestModalMouseInputFocus(this); }