merge total remake

This commit is contained in:
Eduardo Bart
2011-08-13 23:09:11 -03:00
parent 0af7856475
commit 55862b07ad
253 changed files with 6777 additions and 8463 deletions

View File

@@ -1,30 +1,9 @@
/* The MIT License
*
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "animatedtexture.h"
#include "graphics.h"
#include <core/platform.h>
#include <core/eventdispatcher.h>
#include <graphics/animatedtexture.h>
#include <core/engine.h>
#include <core/dispatcher.h>
#include <GL/gl.h>
AnimatedTexture::AnimatedTexture(int width, int height, int channels, int numFrames, uchar *framesPixels, int *framesDelay) :
Texture(),
@@ -44,11 +23,13 @@ AnimatedTexture::AnimatedTexture(int width, int height, int channels, int numFra
}
m_currentFrame = -1;
g_dispatcher.scheduleTask(std::bind(&AnimatedTexture::processAnimation, this), 0);
g_dispatcher.scheduleEvent(std::bind(&AnimatedTexture::processAnimation, this), 0);
}
AnimatedTexture::~AnimatedTexture()
{
g_graphics.disableDrawing();
glDeleteTextures(m_numFrames, m_framesTextureId);
delete[] m_framesTextureId;
delete[] m_framesDelay;
@@ -57,6 +38,8 @@ AnimatedTexture::~AnimatedTexture()
void AnimatedTexture::enableBilinearFilter()
{
g_graphics.disableDrawing();
for(int i=0;i<m_numFrames;++i) {
glBindTexture(GL_TEXTURE_2D, m_framesTextureId[i]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
@@ -72,5 +55,5 @@ void AnimatedTexture::processAnimation()
m_textureId = m_framesTextureId[m_currentFrame];
AnimatedTexturePtr me = std::static_pointer_cast<AnimatedTexture>(shared_from_this());
if(me.use_count() > 1)
g_dispatcher.scheduleTask(std::bind(&AnimatedTexture::processAnimation, me), m_framesDelay[m_currentFrame]);
g_dispatcher.addEvent(std::bind(&AnimatedTexture::processAnimation, me), m_framesDelay[m_currentFrame]);
}

View File

@@ -1,32 +1,7 @@
/* The MIT License
*
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef ANIMATEDTEXTURE_H
#define ANIMATEDTEXTURE_H
#include <global.h>
#include <graphics/graphics.h>
#include "texture.h"
class AnimatedTexture : public Texture
{
@@ -48,4 +23,4 @@ private:
typedef std::shared_ptr<AnimatedTexture> AnimatedTexturePtr;
typedef std::weak_ptr<AnimatedTexture> AnimatedTextureWeakPtr;
#endif // ANIMATEDTEXTURE_H
#endif

View File

@@ -1,73 +0,0 @@
/* The MIT License
*
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef BORDEREDIMAGE_H
#define BORDEREDIMAGE_H
#include <global.h>
#include <graphics/image.h>
#include <graphics/texture.h>
#include <otml/otmlnode.h>
class BorderedImage;
typedef std::shared_ptr<BorderedImage> BorderedImagePtr;
class BorderedImage : public Image
{
public:
BorderedImage(TexturePtr texture,
const Rect& left,
const Rect& right,
const Rect& top,
const Rect& bottom,
const Rect& topLeft,
const Rect& topRight,
const Rect& bottomLeft,
const Rect& bottomRight,
const Rect& center);
void draw(const Rect& screenCoords);
Size getDefaultSize() const { return m_defaultSize; }
static BorderedImagePtr loadFromOTMLNode(OTMLNode *node, TexturePtr defaultTexture = TexturePtr());
private:
Rect m_leftBorderTexCoords;
Rect m_rightBorderTexCoords;
Rect m_topBorderTexCoords;
Rect m_bottomBorderTexCoords;
Rect m_topLeftCornerTexCoords;
Rect m_topRightCornerTexCoords;
Rect m_bottomLeftCornerTexCoords;
Rect m_bottomRightCornerTexCoords;
Rect m_centerTexCoords;
Size m_bordersSize;
Size m_defaultSize;
};
#endif // BORDEREDIMAGE_H

View File

@@ -1,33 +1,11 @@
/* The MIT License
*
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "borderimage.h"
#include "graphics.h"
#include "texture.h"
#include "texturemanager.h"
#include <otml/otml.h>
#include <global.h>
#include <graphics/borderedimage.h>
#include <graphics/graphics.h>
#include <graphics/textures.h>
BorderedImage::BorderedImage(TexturePtr texture,
BorderImage::BorderImage(TexturePtr texture,
const Rect& left,
const Rect& right,
const Rect& top,
@@ -59,7 +37,7 @@ BorderedImage::BorderedImage(TexturePtr texture,
center.height());
}
BorderedImagePtr BorderedImage::loadFromOTMLNode(OTMLNode* node, TexturePtr defaultTexture)
BorderImagePtr BorderImage::loadFromOTML(const OTMLNodePtr& borderImageNode)
{
Rect leftBorder;
Rect rightBorder;
@@ -75,26 +53,27 @@ BorderedImagePtr BorderedImage::loadFromOTMLNode(OTMLNode* node, TexturePtr defa
Size size;
Point offset;
TexturePtr texture;
std::string textureSource = node->readAt("source", std::string());
if(!textureSource.empty())
texture = g_textures.get(textureSource);
else
texture = defaultTexture;
// load texture
std::string source = borderImageNode->at("source")->value();
TexturePtr texture = g_textures.getTexture(source);
if(!texture)
return BorderedImagePtr();
throw OTMLException(borderImageNode, "could not load border-image texture");
// load basic border confs
size = texture->getSize();
size = node->readAt("size", size);
offset = node->readAt("offset", offset);
size = borderImageNode->readAt("size", size);
offset = borderImageNode->readAt("offset", offset);
border = borderImageNode->readAt("border", 0);
subRect = Rect(offset, size);
border = node->readAt("border", 0);
// load border margins
top = bottom = left = right = border;
top = node->readAt("top", top);
bottom = node->readAt("bottom", bottom);
left = node->readAt("left", left);
right = node->readAt("right", right);
top = borderImageNode->readAt("border.top", top);
bottom = borderImageNode->readAt("border.bottom", bottom);
left = borderImageNode->readAt("border.left", left);
right = borderImageNode->readAt("border.right", right);
// calculates border coords
leftBorder = Rect(subRect.left(), subRect.top() + top, left, subRect.height() - top - bottom);
rightBorder = Rect(subRect.right() - right + 1, subRect.top() + top, right, subRect.height() - top - bottom);
topBorder = Rect(subRect.left() + left, subRect.top(), subRect.width() - right - left, top);
@@ -104,17 +83,21 @@ BorderedImagePtr BorderedImage::loadFromOTMLNode(OTMLNode* node, TexturePtr defa
bottomLeftCorner = Rect(subRect.left(), subRect.bottom() - bottom, left, bottom);
bottomRightCorner = Rect(subRect.right() - right + 1, subRect.bottom() - bottom + 1, right, bottom);
center = Rect(subRect.left() + left, subRect.top() + top, subRect.width() - right - left, subRect.height() - top - bottom);
leftBorder = node->readAt("left border", leftBorder);
rightBorder = node->readAt("right border", rightBorder);
topBorder = node->readAt("top border", topBorder);
bottomBorder = node->readAt("bottom border", bottomBorder);
topLeftCorner = node->readAt("top left corner", topLeftCorner);
topRightCorner = node->readAt("top right corner", topRightCorner);
bottomLeftCorner = node->readAt("bottom left corner", bottomLeftCorner);
bottomRightCorner = node->readAt("bottom right corner", bottomRightCorner);
center = node->readAt("center", center);
return BorderedImagePtr(new BorderedImage(texture,
// load individual border conf if supplied
/*
leftBorder = borderImageNode->readAt("left border", leftBorder);
rightBorder = borderImageNode->readAt("right border", rightBorder);
topBorder = borderImageNode->readAt("top border", topBorder);
bottomBorder = borderImageNode->readAt("bottom border", bottomBorder);
topLeftCorner = borderImageNode->readAt("top left corner", topLeftCorner);
topRightCorner = borderImageNode->readAt("top right corner", topRightCorner);
bottomLeftCorner = borderImageNode->readAt("bottom left corner", bottomLeftCorner);
bottomRightCorner = borderImageNode->readAt("bottom right corner", bottomRightCorner);
center = borderImageNode->readAt("center", center);
*/
return BorderImagePtr(new BorderImage(texture,
leftBorder,
rightBorder,
topBorder,
@@ -126,8 +109,10 @@ BorderedImagePtr BorderedImage::loadFromOTMLNode(OTMLNode* node, TexturePtr defa
center));
}
void BorderedImage::draw(const Rect& screenCoords)
void BorderImage::draw(const Rect& screenCoords)
{
//TODO: borderimage drawing could be optimized by caching the render into a texture
Rect rectCoords;
Size centerSize = screenCoords.size() - m_bordersSize;
@@ -190,5 +175,4 @@ void BorderedImage::draw(const Rect& screenCoords)
screenCoords.top() + m_topRightCornerTexCoords.height() + centerSize.height(),
m_bottomRightCornerTexCoords.size());
g_graphics.drawTexturedRect(rectCoords, m_texture, m_bottomRightCornerTexCoords);
}
}

View File

@@ -0,0 +1,43 @@
#ifndef BORDERIMAGE_H
#define BORDERIMAGE_H
#include "image.h"
class BorderImage : public Image
{
public:
BorderImage(TexturePtr texture,
const Rect& left,
const Rect& right,
const Rect& top,
const Rect& bottom,
const Rect& topLeft,
const Rect& topRight,
const Rect& bottomLeft,
const Rect& bottomRight,
const Rect& center);
static BorderImagePtr loadFromOTML(const OTMLNodePtr& borderImageNode);
void draw(const Rect& screenCoords);
Size getDefaultSize() const { return m_defaultSize; }
private:
Rect m_leftBorderTexCoords;
Rect m_rightBorderTexCoords;
Rect m_topBorderTexCoords;
Rect m_bottomBorderTexCoords;
Rect m_topLeftCornerTexCoords;
Rect m_topRightCornerTexCoords;
Rect m_bottomLeftCornerTexCoords;
Rect m_bottomRightCornerTexCoords;
Rect m_centerTexCoords;
Size m_bordersSize;
Size m_defaultSize;
};
#endif

View File

@@ -1,104 +1,40 @@
#include <global.h>
#include <core/resources.h>
#include <graphics/font.h>
#include <graphics/textures.h>
#include <graphics/graphics.h>
#include "font.h"
#include "texturemanager.h"
#include "graphics.h"
#include <otml/otml.h>
void Font::calculateGlyphsWidthsAutomatically(const Size& glyphSize)
void Font::load(const OTMLNodePtr& fontNode)
{
std::string textureName = fontNode->readAt<std::string>("texture");
Size glyphSize = fontNode->readAt<Size>("glyph size");
m_glyphHeight = fontNode->readAt<int>("height");
m_topMargin = fontNode->readAt("top margin", 0);
m_firstGlyph = fontNode->readAt("first glyph", 32);
m_glyphSpacing = fontNode->readAt("spacing", Size(0,0));
// load font texture
m_texture = g_textures.getTexture(textureName);
if(!m_texture)
throw OTMLException(fontNode, "failed to load texture for font");
// auto calculate widths
calculateGlyphsWidthsAutomatically(glyphSize);
// read custom widths
if(OTMLNodePtr node = fontNode->get("glyph widths")) {
for(const OTMLNodePtr& child : node->childNodes())
m_glyphsSize[aux::safe_cast<int>(child->tag())].setWidth(child->read<int>());
}
// calculate glyphs texture coords
int numHorizontalGlyphs = m_texture->getSize().width() / glyphSize.width();
uchar *texturePixels = m_texture->getPixels();
// small AI to auto calculate pixels widths
for(int glyph = m_firstGlyph; glyph< 256; ++glyph) {
Rect glyphCoords(((glyph - m_firstGlyph) % numHorizontalGlyphs) * glyphSize.width(),
((glyph - m_firstGlyph) / numHorizontalGlyphs) * glyphSize.height(),
glyphSize.width(),
m_glyphHeight);
int width = glyphSize.width();
int lastColumnFilledPixels = 0;
for(int x = glyphCoords.left() + 1; x <= glyphCoords.right(); ++x) {
int columnFilledPixels = 0;
// check if all vertical pixels are alpha
for(int y = glyphCoords.top(); y <= glyphCoords.bottom(); ++y) {
if(texturePixels[(y * m_texture->getSize().width() * 4) + (x*4) + 3] != 0)
columnFilledPixels++;
}
// if all pixels were alpha we found the width
if(columnFilledPixels == 0) {
width = x - glyphCoords.left();
width += m_glyphSpacing.width();
if(m_glyphHeight >= 16 && lastColumnFilledPixels >= m_glyphHeight/3)
width += 1;
break;
}
lastColumnFilledPixels = columnFilledPixels;
}
// store glyph size
m_glyphsSize[glyph].setSize(width, m_glyphHeight);
for(int glyph = m_firstGlyph; glyph < 256; ++glyph) {
m_glyphsTextureCoords[glyph].setRect(((glyph - m_firstGlyph) % numHorizontalGlyphs) * glyphSize.width(),
((glyph - m_firstGlyph) / numHorizontalGlyphs) * glyphSize.height(),
m_glyphsSize[glyph].width(),
m_glyphHeight);
}
delete[] texturePixels;
}
bool Font::load(const std::string& file)
{
std::stringstream fin;
if(!g_resources.loadFile(file, fin)) {
logError("ERROR: Coult not load font file '",file,"'");
return false;
}
std::string textureName;
Size glyphSize;
try {
OTMLParser parser(fin, file);
OTMLNode* doc = parser.getDocument();
// required values
textureName = doc->valueAt("image");
glyphSize = doc->readAt("image-glyph-size", Size(16, 16));
m_glyphHeight = doc->readAt("glyph-height", 11);
m_firstGlyph = doc->readAt("first-glyph", 32);
m_topMargin = doc->readAt("top-margin", 0);
m_glyphSpacing = doc->readAt("glyph-spacing", Size(0,0));
// load texture
m_texture = g_textures.get(textureName);
if(!m_texture) {
logError("ERROR: Failed to load image for font file '",file,"'");
return false;
}
// auto calculate widths
calculateGlyphsWidthsAutomatically(glyphSize);
// read custom widths
if(doc->hasChild("glyph-widths")) {
std::map<int, int> glyphWidths;
doc->readAt("glyph-widths", &glyphWidths);
foreach(const auto& pair, glyphWidths)
m_glyphsSize[pair.first].setWidth(pair.second);
}
// calculate glyphs texture coords
int numHorizontalGlyphs = m_texture->getSize().width() / glyphSize.width();
for(int glyph = m_firstGlyph; glyph < 256; ++glyph) {
m_glyphsTextureCoords[glyph].setRect(((glyph - m_firstGlyph) % numHorizontalGlyphs) * glyphSize.width(),
((glyph - m_firstGlyph) / numHorizontalGlyphs) * glyphSize.height(),
m_glyphsSize[glyph].width(),
m_glyphHeight);
}
} catch(OTMLException e) {
logError("ERROR: Malformed font file \"", file.c_str(), "\":\n ", e.what());
return false;
}
return true;
}
void Font::renderText(const std::string& text,
@@ -112,9 +48,9 @@ void Font::renderText(const std::string& text,
void Font::renderText(const std::string& text,
const Rect& screenCoords,
AlignmentFlag align,
const Color& color)
const Rect& screenCoords,
AlignmentFlag align,
const Color& color)
{
// prevent glitches from invalid rects
if(!screenCoords.isValid())
@@ -190,7 +126,9 @@ void Font::renderText(const std::string& text,
}
}
const std::vector<Point>& Font::calculateGlyphsPositions(const std::string& text, AlignmentFlag align, Size *textBoxSize) const
const std::vector<Point>& Font::calculateGlyphsPositions(const std::string& text,
AlignmentFlag align,
Size *textBoxSize) const
{
// for performance reasons we use statics vectors that are allocated on demand
static std::vector<Point> glyphsPositions(1);
@@ -277,3 +215,41 @@ Size Font::calculateTextRectSize(const std::string& text)
return size;
}
void Font::calculateGlyphsWidthsAutomatically(const Size& glyphSize)
{
int numHorizontalGlyphs = m_texture->getSize().width() / glyphSize.width();
uchar *texturePixels = m_texture->getPixels();
// small AI to auto calculate pixels widths
for(int glyph = m_firstGlyph; glyph< 256; ++glyph) {
Rect glyphCoords(((glyph - m_firstGlyph) % numHorizontalGlyphs) * glyphSize.width(),
((glyph - m_firstGlyph) / numHorizontalGlyphs) * glyphSize.height(),
glyphSize.width(),
m_glyphHeight);
int width = glyphSize.width();
int lastColumnFilledPixels = 0;
for(int x = glyphCoords.left() + 1; x <= glyphCoords.right(); ++x) {
int columnFilledPixels = 0;
// check if all vertical pixels are alpha
for(int y = glyphCoords.top(); y <= glyphCoords.bottom(); ++y) {
if(texturePixels[(y * m_texture->getSize().width() * 4) + (x*4) + 3] != 0)
columnFilledPixels++;
}
// if all pixels were alpha we found the width
if(columnFilledPixels == 0) {
width = x - glyphCoords.left();
width += m_glyphSpacing.width();
if(m_glyphHeight >= 16 && lastColumnFilledPixels >= m_glyphHeight/3)
width += 1;
break;
}
lastColumnFilledPixels = columnFilledPixels;
}
// store glyph size
m_glyphsSize[glyph].setSize(width, m_glyphHeight);
}
delete[] texturePixels;
}

View File

@@ -1,68 +1,46 @@
/* The MIT License
*
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef FONT_H
#define FONT_H
#include <global.h>
#include <graphics/graphics.h>
#include "graphicsdeclarations.h"
#include <otml/otmldeclarations.h>
class Font
{
public:
Font(const std::string& name) :
m_name(name) { }
Font(const std::string& name) : m_name(name) { }
/// Load font from file
bool load(const std::string &file);
/// Load font from otml node
void load(const OTMLNodePtr& fontNode);
/// Simple text render starting at startPos
void renderText(const std::string& text,
const Point& startPos,
const Color& color = Color::white);
/// Advanced text render
/// Advanced text render delimited by a screen region and alignment
void renderText(const std::string& text,
const Rect& screenCoords,
AlignmentFlag align = AlignTopLeft,
const Color& color = Color::white);
/// Calculate glyphs positions to use on render, also calculates textBoxSize if wanted
const std::vector<Point>& calculateGlyphsPositions(const std::string& text, AlignmentFlag align = AlignTopLeft, Size *textBoxSize = NULL) const;
const std::vector<Point>& calculateGlyphsPositions(const std::string& text,
AlignmentFlag align = AlignTopLeft,
Size* textBoxSize = NULL) const;
/// Simulate render and calculate text size
Size calculateTextRectSize(const std::string& text);
std::string getName() const { return m_name; }
int getGlyphHeight() const { return m_glyphHeight; }
const Rect *getGlyphsTextureCoords() const { return m_glyphsTextureCoords; }
const Size *getGlyphsSize() const { return m_glyphsSize; }
const Rect* getGlyphsTextureCoords() const { return m_glyphsTextureCoords; }
const Size* getGlyphsSize() const { return m_glyphsSize; }
const TexturePtr& getTexture() const { return m_texture; }
int getTopMargin() const { return m_topMargin; }
Size getGlyphSpacing() const { return m_glyphSpacing; }
private:
/// Calculates each font character by inspecting font bitmap
void calculateGlyphsWidthsAutomatically(const Size& glyphSize);
std::string m_name;
@@ -75,6 +53,6 @@ private:
Size m_glyphsSize[256];
};
typedef std::shared_ptr<Font> FontPtr;
#endif // FONT_H
#endif

View File

@@ -0,0 +1,69 @@
#include "fontmanager.h"
#include <core/resourcemanager.h>
#include <otml/otml.h>
FontManager g_fonts;
void FontManager::releaseFonts()
{
m_defaultFont.reset();
m_fonts.clear();
}
bool FontManager::importFont(std::string fontFile)
{
try {
if(!boost::ends_with(fontFile, ".otfont"))
fontFile += ".otfont";
OTMLDocumentPtr doc = OTMLDocument::parse(fontFile);
OTMLNodePtr fontNode = doc->at("Font");
std::string name = fontNode->readAt<std::string>("name");
if(fontExists(name))
throw OTMLException(fontNode, "a font with the same name is already imported, did you duplicate font names?");
FontPtr font(new Font(name));
font->load(fontNode);
m_fonts.push_back(font);
// set as default if needed
if(!m_defaultFont)
m_defaultFont = font;
return true;
} catch(std::exception& e) {
logError("ERROR: could not load font '", fontFile, "': ", e.what());
return false;
}
}
bool FontManager::fontExists(const std::string& fontName)
{
for(const FontPtr& font : m_fonts) {
if(font->getName() == fontName)
return true;
}
return false;
}
FontPtr FontManager::getFont(const std::string& fontName)
{
// find font by name
for(const FontPtr& font : m_fonts) {
if(font->getName() == fontName)
return font;
}
// when not found, fallback to default font
return getDefaultFont();
}
FontPtr FontManager::getDefaultFont()
{
// default font should always exists, otherwise the app may crash
if(!m_defaultFont)
throw std::runtime_error("no default font to display, cannot continue to run");
return m_defaultFont;
}

View File

@@ -0,0 +1,27 @@
#ifndef FONTMANAGER_H
#define FONTMANAGER_H
#include "font.h"
class FontManager
{
public:
// Release fonts references, thus making possible to destruct them
void releaseFonts();
bool importFont(std::string fontFile);
bool fontExists(const std::string& fontName);
FontPtr getFont(const std::string& fontName);
FontPtr getDefaultFont();
void setDefaultFont(const std::string& fontName) { m_defaultFont = getFont(fontName); }
private:
std::vector<FontPtr> m_fonts;
FontPtr m_defaultFont;
};
extern FontManager g_fonts;
#endif

View File

@@ -1,38 +0,0 @@
#include <global.h>
#include <core/resources.h>
#include <graphics/fonts.h>
Fonts g_fonts;
void Fonts::init()
{
g_resources.pushCurrentPath("fonts");
// load all fonts
std::list<std::string> files = g_resources.listDirectoryFiles();
foreach(const std::string& file, files) {
if(boost::ends_with(file, ".otml")) {
std::string name = file;
boost::erase_first(name, ".otml");
FontPtr font(new Font(name));
if(font->load(file))
m_fonts.push_back(font);
}
}
g_resources.popCurrentPath();
}
FontPtr Fonts::get(const std::string& fontName)
{
// find font by name
foreach(const FontPtr& font, m_fonts) {
if(font->getName() == fontName)
return font;
}
logFatal("ERROR: Font '",fontName,"' not found");
return FontPtr();
}

View File

@@ -1,51 +0,0 @@
/* The MIT License
*
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef FONTS_H
#define FONTS_H
#include <global.h>
#include <graphics/font.h>
class Fonts
{
public:
Fonts() { }
/// Initialize all fonts
void init();
/// Terminate all fonts
void terminate() { }
/// Get a font by name
FontPtr get(const std::string& fontName);
private:
std::vector<FontPtr> m_fonts;
};
extern Fonts g_fonts;
#endif // FONTS_H

View File

@@ -1,30 +1,9 @@
/* The MIT License
*
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <global.h>
#include "framebuffer.h"
#include "graphics.h"
#include <core/platform.h>
#include <graphics/framebuffer.h>
#include <graphics/graphics.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glext.h>
PFNGLGENFRAMEBUFFERSPROC oglGenFramebuffers = 0;
PFNGLBINDFRAMEBUFFERPROC oglBindFramebuffer = 0;
@@ -55,11 +34,11 @@ FrameBuffer::FrameBuffer(int width, int height)
if(g_graphics.isExtensionSupported("GL_ARB_framebuffer_object")) {
m_fallbackOldImp = false;
if(!oglGenFramebuffers) {
oglGenFramebuffers = (PFNGLGENFRAMEBUFFERSPROC)Platform::getExtensionProcAddress("glGenFramebuffers");
oglBindFramebuffer = (PFNGLBINDFRAMEBUFFERPROC)Platform::getExtensionProcAddress("glBindFramebuffer");
oglFramebufferTexture2D = (PFNGLFRAMEBUFFERTEXTURE2DPROC)Platform::getExtensionProcAddress("glFramebufferTexture2D");
oglDeleteFramebuffers = (PFNGLDELETEFRAMEBUFFERSPROC)Platform::getExtensionProcAddress("glDeleteFramebuffers");
oglCheckFramebufferStatus = (PFNGLCHECKFRAMEBUFFERSTATUSPROC)Platform::getExtensionProcAddress("glCheckFramebufferStatus");
oglGenFramebuffers = (PFNGLGENFRAMEBUFFERSPROC)g_platform.getExtensionProcAddress("glGenFramebuffers");
oglBindFramebuffer = (PFNGLBINDFRAMEBUFFERPROC)g_platform.getExtensionProcAddress("glBindFramebuffer");
oglFramebufferTexture2D = (PFNGLFRAMEBUFFERTEXTURE2DPROC)g_platform.getExtensionProcAddress("glFramebufferTexture2D");
oglDeleteFramebuffers = (PFNGLDELETEFRAMEBUFFERSPROC)g_platform.getExtensionProcAddress("glDeleteFramebuffers");
oglCheckFramebufferStatus = (PFNGLCHECKFRAMEBUFFERSTATUSPROC)g_platform.getExtensionProcAddress("glCheckFramebufferStatus");
}
// generate FBO

View File

@@ -1,34 +1,7 @@
/* The MIT License
*
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef FRAMEBUFFER_H
#define FRAMEBUFFER_H
#include <global.h>
class FrameBuffer;
typedef std::shared_ptr<FrameBuffer> FrameBufferPtr;
#include "graphicsdeclarations.h"
class FrameBuffer
{
@@ -53,4 +26,4 @@ private:
int m_height;
};
#endif // FRAMEBUFFER_H
#endif

View File

@@ -1,38 +1,22 @@
/* The MIT License
*
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <global.h>
#include <graphics/graphics.h>
#include <graphics/texture.h>
#include "fontmanager.h"
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glext.h>
Graphics g_graphics;
void Graphics::init()
{
m_drawMode = DRAW_NONE;
// setup opengl
glEnable(GL_ALPHA_TEST); // enable alpha by default
glAlphaFunc(GL_GREATER, 0.0f); // default alpha mode
glDisable(GL_DEPTH_TEST); // we are rendering 2D only, we don't need it
glAlphaFunc(GL_GREATER, 0.0f); // default alpha func
glDisable(GL_DEPTH_TEST); // we are rendering 2D only, we don't need depth buffer
//glEnable(GL_TEXTURE_2D); // enable textures by default
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glShadeModel(GL_SMOOTH);
glEnable(GL_BLEND);
@@ -45,7 +29,7 @@ void Graphics::init()
void Graphics::terminate()
{
m_bindedTexture.reset();
g_fonts.releaseFonts();
}
bool Graphics::isExtensionSupported(const char *extension)
@@ -114,11 +98,18 @@ void Graphics::beginRender()
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
// bind white color by default
glColor4ubv(Color::white.rgbaPtr());
m_boundColor = Color::white;
}
void Graphics::endRender()
{
disableDrawing();
// clear any bound texture
m_boundTexture.reset();
}
void Graphics::disableDrawing()
@@ -127,12 +118,15 @@ void Graphics::disableDrawing()
glEnd();
m_drawMode = DRAW_NONE;
m_bindedTexture.reset();
m_boundTexture.reset();
glColor4ubv(Color::white.rgbaPtr());
}
}
void Graphics::drawTexturedRect(const Rect& screenCoords, const TexturePtr& texture, const Rect& textureCoords, const Color& color)
void Graphics::drawTexturedRect(const Rect& screenCoords,
const TexturePtr& texture,
const Rect& textureCoords,
const Color& color)
{
if(screenCoords.isEmpty() || textureCoords.isEmpty())
return;
@@ -163,7 +157,10 @@ void Graphics::drawTexturedRect(const Rect& screenCoords, const TexturePtr& text
glTexCoord2f(textureRight, textureTop); glVertex2i(right, top);
}
void Graphics::drawRepeatedTexturedRect(const Rect& screenCoords, const TexturePtr& texture, const Rect& textureCoords, const Color& color)
void Graphics::drawRepeatedTexturedRect(const Rect& screenCoords,
const TexturePtr& texture,
const Rect& textureCoords,
const Color& color)
{
if(screenCoords.isEmpty() || textureCoords.isEmpty())
return;
@@ -177,11 +174,13 @@ void Graphics::drawRepeatedTexturedRect(const Rect& screenCoords, const TextureP
// partialCoords to screenCoords bottomRight
if(partialCoords.bottom() > virtualScreenCoords.bottom()) {
partialTextureCoords.setBottom(partialTextureCoords.bottom() + (virtualScreenCoords.bottom() - partialCoords.bottom()));
partialTextureCoords.setBottom(partialTextureCoords.bottom() +
(virtualScreenCoords.bottom() - partialCoords.bottom()));
partialCoords.setBottom(virtualScreenCoords.bottom());
}
if(partialCoords.right() > virtualScreenCoords.right()) {
partialTextureCoords.setRight(partialTextureCoords.right() + (virtualScreenCoords.right() - partialCoords.right()));
partialTextureCoords.setRight(partialTextureCoords.right() +
(virtualScreenCoords.right() - partialCoords.right()));
partialCoords.setRight(virtualScreenCoords.right());
}
@@ -191,7 +190,8 @@ void Graphics::drawRepeatedTexturedRect(const Rect& screenCoords, const TextureP
}
}
void Graphics::drawFilledRect(const Rect& screenCoords, const Color& color)
void Graphics::drawFilledRect(const Rect& screenCoords,
const Color& color)
{
if(screenCoords.isEmpty())
return;
@@ -210,7 +210,9 @@ void Graphics::drawFilledRect(const Rect& screenCoords, const Color& color)
}
void Graphics::drawBoundingRect(const Rect& screenCoords, const Color& color, int innerLineWidth)
void Graphics::drawBoundingRect(const Rect& screenCoords,
const Color& color,
int innerLineWidth)
{
if(2 * innerLineWidth > screenCoords.height() || screenCoords.isEmpty())
return;
@@ -251,13 +253,13 @@ void Graphics::drawBoundingRect(const Rect& screenCoords, const Color& color, in
void Graphics::bindColor(const Color& color)
{
// switch drawing to colored quads
if(m_drawMode != DRAW_COLOR_QUADS || m_bindedColor != color) {
if(m_drawMode != DRAW_COLOR_QUADS || m_boundColor != color) {
if(m_drawMode != DRAW_NONE)
glEnd();
glDisable(GL_TEXTURE_2D);
if(m_bindedColor != color) {
if(m_boundColor != color) {
glColor4ubv(color.rgbaPtr());
m_bindedColor = color;
m_boundColor = color;
}
glBegin(GL_QUADS);
m_drawMode = DRAW_COLOR_QUADS;
@@ -267,17 +269,17 @@ void Graphics::bindColor(const Color& color)
void Graphics::bindTexture(const TexturePtr& texture, const Color& color)
{
// switch drawing to textured quads
if(m_drawMode != DRAW_TEXTURE_QUADS || m_bindedTexture != texture || m_bindedColor != color) {
if(m_drawMode != DRAW_TEXTURE_QUADS || m_boundTexture != texture || m_boundColor != color) {
if(m_drawMode != DRAW_NONE)
glEnd();
glEnable(GL_TEXTURE_2D);
if(m_bindedTexture != texture) {
if(m_boundTexture != texture) {
glBindTexture(GL_TEXTURE_2D, texture->getTextureId());
m_bindedTexture = texture;
m_boundTexture = texture;
}
if(m_bindedColor != color) {
if(m_boundColor != color) {
glColor4ubv(color.rgbaPtr());
m_bindedColor = color;
m_boundColor = color;
}
glBegin(GL_QUADS);
m_drawMode = DRAW_TEXTURE_QUADS;

View File

@@ -1,36 +1,7 @@
/* The MIT License
*
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef GRAPHICS_H
#define GRAPHICS_H
#include <global.h>
#include <graphics/textures.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glext.h>
#include "graphicsdeclarations.h"
class Graphics
{
@@ -44,9 +15,7 @@ class Graphics
};
public:
Graphics() : m_drawMode(DRAW_NONE) { }
/// Initialize graphics
/// Initialize default OpenGL states
void init();
/// Termiante graphics
@@ -55,7 +24,7 @@ public:
/// Check if a GL extension is supported
bool isExtensionSupported(const char *extension);
/// Called after every window resize
/// Resizes OpenGL viewport
void resize(const Size& size);
/// Restore original viewport
@@ -66,27 +35,38 @@ public:
/// Called after every render
void endRender();
void disableDrawing();
// drawing API
void drawTexturedRect(const Rect& screenCoords,
const TexturePtr& texture,
const Rect& textureCoords = Rect(),
const Color& color = Color::white);
void drawRepeatedTexturedRect(const Rect& screenCoords,
const TexturePtr& texture,
const Rect& textureCoords,
const Color& color = Color::white);
void drawFilledRect(const Rect& screenCoords,
const Color& color);
void drawBoundingRect(const Rect& screenCoords,
const Color& color = Color::green,
int innerLineWidth = 1);
const Size& getScreenSize() const { return m_screenSize; }
void disableDrawing();
void enableDrawing();
void drawTexturedRect(const Rect& screenCoords, const TexturePtr& texture, const Rect& textureCoords = Rect(), const Color& color = Color::white);
void drawRepeatedTexturedRect(const Rect& screenCoords, const TexturePtr& texture, const Rect& textureCoords, const Color& color = Color::white);
void drawFilledRect(const Rect& screenCoords, const Color& color);
void drawBoundingRect(const Rect& screenCoords, const Color& color = Color::green, int innerLineWidth = 1);
private:
void bindTexture(const TexturePtr& texture, const Color& color = Color::white);
void bindColor(const Color& color);
TexturePtr m_bindedTexture;
Color m_bindedColor;
TexturePtr m_boundTexture;
Color m_boundColor;
Size m_screenSize;
DrawMode m_drawMode;
};
extern Graphics g_graphics;
#endif // GRAPHICS_H
#endif

View File

@@ -0,0 +1,22 @@
#ifndef GRAPHICSDECLARATIONS_H
#define GRAPHICSDECLARATIONS_H
#include <global.h>
class Texture;
class Font;
class Image;
class BorderImage;
class TextArea;
class FrameBuffer;
typedef std::weak_ptr<Texture> TextureWeakPtr;
typedef std::shared_ptr<Texture> TexturePtr;
typedef std::shared_ptr<Font> FontPtr;
typedef std::shared_ptr<Image> ImagePtr;
typedef std::shared_ptr<BorderImage> BorderImagePtr;
typedef std::shared_ptr<TextArea> TextAreaPtr;
typedef std::shared_ptr<FrameBuffer> FrameBufferPtr;
#endif

View File

@@ -1,48 +1,37 @@
/* The MIT License
*
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "image.h"
#include "texture.h"
#include "graphics.h"
#include "texturemanager.h"
#include <otml/otml.h>
#include <global.h>
#include <graphics/image.h>
#include <graphics/graphics.h>
#include <graphics/textures.h>
Image::Image(TexturePtr texture)
: m_texture(texture)
Image::Image(TexturePtr texture, Rect textureCoords)
{
m_textureCoords = Rect(0, 0, m_texture->getSize());
m_texture = texture;
if(!textureCoords.isValid())
m_textureCoords = Rect(0, 0, m_texture->getSize());
else
m_textureCoords = textureCoords;
}
Image::Image(const std::string& texture)
ImagePtr Image::loadFromOTML(const OTMLNodePtr& imageNode)
{
m_texture = g_textures.get(texture);
m_textureCoords = Rect(0, 0, m_texture->getSize());
}
// load configs from otml node
std::string source = imageNode->hasValue() ? imageNode->read<std::string>() : imageNode->readAt<std::string>("source");
bool smooth = imageNode->readAt("smooth", false);
Rect textureCoords = imageNode->readAt("coords", Rect());
Image::Image(const std::string& texture, Rect textureCoords) :
m_textureCoords(textureCoords)
{
m_texture = g_textures.get(texture);
// load texture
TexturePtr texture = g_textures.getTexture(source);
if(!texture)
throw OTMLException(imageNode, "could not load image texture");
// enable texture bilinear filter
if(smooth)
texture->enableBilinearFilter();
// create image
return ImagePtr(new Image(texture, textureCoords));
}
void Image::draw(const Rect& screenCoords)
@@ -50,5 +39,3 @@ void Image::draw(const Rect& screenCoords)
if(m_texture)
g_graphics.drawTexturedRect(screenCoords, m_texture, m_textureCoords);
}

View File

@@ -1,42 +1,16 @@
/* The MIT License
*
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef IMAGE_H
#define IMAGE_H
#include <global.h>
#include <graphics/texture.h>
#include "graphicsdeclarations.h"
#include <otml/otmldeclarations.h>
class Image
{
public:
Image(TexturePtr texture);
Image(TexturePtr texture, Rect textureCoords) : m_texture(texture), m_textureCoords(textureCoords) { }
Image(const std::string& texture);
Image(const std::string& texture, Rect textureCoords);
Image(TexturePtr texture, Rect textureCoords = Rect());
static ImagePtr loadFromOTML(const OTMLNodePtr& imageNode);
/// Draw image on screen
virtual void draw(const Rect& screenCoords);
protected:
@@ -44,6 +18,4 @@ protected:
Rect m_textureCoords;
};
typedef std::shared_ptr<Image> ImagePtr;
#endif // IMAGE_H
#endif

View File

@@ -1,31 +1,8 @@
/* The MIT License
*
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "textarea.h"
#include "font.h"
#include "graphics.h"
#include <global.h>
#include <core/engine.h>
#include <graphics/textarea.h>
#include <graphics/graphics.h>
#include <core/platform.h>
TextArea::TextArea() :
m_align(AlignLeftCenter),
@@ -55,6 +32,8 @@ TextArea::TextArea(FontPtr font,
void TextArea::draw()
{
//TODO: text rendering could be much optimized by using vertex buffer or caching the render into a texture
int textLength = m_text.length();
const TexturePtr& texture = m_font->getTexture();
for(int i=0;i<textLength;++i) {
@@ -65,7 +44,7 @@ void TextArea::draw()
if(m_cursorVisible && m_cursorPos >= 0) {
assert(m_cursorPos <= textLength);
const int delay = 500;
int ticks = g_engine.getCurrentFrameTicks();
int ticks = g_platform.getTicks();
// draw every 500ms
if(ticks - m_cursorTicks <= delay) {
Rect cursorRect;
@@ -76,7 +55,7 @@ void TextArea::draw()
cursorRect = Rect(m_glyphsCoords[m_cursorPos-1].right(), m_glyphsCoords[m_cursorPos-1].top(), 1, m_font->getGlyphHeight());
g_graphics.drawFilledRect(cursorRect, m_color);
} else if(ticks - m_cursorTicks >= 2*delay) {
m_cursorTicks = g_engine.getCurrentFrameTicks();
m_cursorTicks = ticks;
}
}
}
@@ -113,12 +92,12 @@ void TextArea::recalculate()
} else if(m_cursorPos > m_startRenderPos || // cursor is after the previuos first rendered glyph
(m_cursorPos == m_startRenderPos && textLength == m_cursorPos)) // cursor is at the previuos rendered element, and is the last text element
{
Rect virtualRect(m_startInternalPos, m_screenCoords.size()); // previus rendered virtual rect
Rect virtualRect(m_startInternalPos, m_screenCoords.size()); // previous rendered virtual rect
int pos = m_cursorPos - 1; // element before cursor
glyph = (uchar)m_text[pos]; // glyph of the element before cursor
Rect glyphRect(glyphsPositions[pos], glyphsSize[glyph]);
// if the cursor is not on the previus rendered virtual rect we need to update it
// if the cursor is not on the previous rendered virtual rect we need to update it
if(!virtualRect.contains(glyphRect.topLeft()) || !virtualRect.contains(glyphRect.bottomRight())) {
// calculate where is the first glyph visible
Point startGlyphPos;
@@ -246,7 +225,7 @@ void TextArea::setText(const std::string& text)
m_text = text;
if(m_cursorPos >= 0) {
m_cursorPos = 0;
m_cursorTicks = g_engine.getCurrentFrameTicks();
m_cursorTicks = g_platform.getTicks();
}
recalculate();
}
@@ -285,7 +264,7 @@ void TextArea::enableCursor(bool enable)
{
if(enable) {
m_cursorPos = 0;
m_cursorTicks = g_engine.getCurrentFrameTicks();
m_cursorTicks = g_platform.getTicks();
} else
m_cursorPos = -1;
recalculate();
@@ -298,7 +277,7 @@ void TextArea::appendCharacter(char c)
tmp = c;
m_text.insert(m_cursorPos, tmp);
m_cursorPos++;
m_cursorTicks = g_engine.getCurrentFrameTicks();
m_cursorTicks = g_platform.getTicks();
recalculate();
}
}
@@ -310,7 +289,7 @@ void TextArea::removeCharacter(bool right)
m_text.erase(m_text.begin() + m_cursorPos);
else if((uint)m_cursorPos == m_text.length()) {
m_text.erase(m_text.begin() + (--m_cursorPos));
m_cursorTicks = g_engine.getCurrentFrameTicks();
m_cursorTicks = g_platform.getTicks();
}
recalculate();
}
@@ -321,12 +300,12 @@ void TextArea::moveCursor(bool right)
if(right) {
if((uint)m_cursorPos+1 <= m_text.length()) {
m_cursorPos++;
m_cursorTicks = g_engine.getCurrentFrameTicks();
m_cursorTicks = g_platform.getTicks();
}
} else {
if(m_cursorPos-1 >= 0) {
m_cursorPos--;
m_cursorTicks = g_engine.getCurrentFrameTicks();
m_cursorTicks = g_platform.getTicks();
}
}
recalculate();

View File

@@ -1,32 +1,7 @@
/* The MIT License
*
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef TEXTAREA_H
#define TEXTAREA_H
#include <global.h>
#include "font.h"
#include "graphicsdeclarations.h"
class TextArea
{
@@ -77,4 +52,4 @@ private:
std::vector<Rect> m_glyphsTexCoords;
};
#endif // TEXTAREA_H
#endif

View File

@@ -1,34 +1,11 @@
/* The MIT License
*
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <global.h>
#include <graphics/texture.h>
#include "texture.h"
#include "graphics.h"
#include <GL/gl.h>
Texture::Texture(int width, int height, int channels, uchar *pixels)
{
// generate opengl texture
// generate opengl texture
m_textureId = internalLoadGLTexture(pixels, channels, width, height);
}
@@ -36,10 +13,16 @@ uint Texture::internalLoadGLTexture(uchar *pixels, int channels, int width, int
{
g_graphics.disableDrawing();
GLint texSize = 0;
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &texSize);
if(width > texSize || height > texSize) {
logError("loading texture with size ",width,"x",height," failed, the maximum size is ",texSize,"x",texSize);
// get smax texture size supported by the driver
static GLint maxTexSize = -1;
if(maxTexSize == -1)
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTexSize);
// checks texture max size
if(width > maxTexSize || height > maxTexSize) {
logError("ERROR: loading texture with size ", width, "x", height, " failed, "
"the maximum size allowed by the graphics card is ", maxTexSize, "x", maxTexSize, ",",
"to prevent crashes the texture will be displayed as a blank texture");
return 0;
}
@@ -50,6 +33,7 @@ uint Texture::internalLoadGLTexture(uchar *pixels, int channels, int width, int
m_size.setSize(width, height);
bool mustFree = false;
// old opengl drivers only accept power of two dimensions
if(!g_graphics.isExtensionSupported("GL_ARB_texture_non_power_of_two") && pixels) {
int glWidth = 1;
while(glWidth < width)
@@ -63,9 +47,9 @@ uint Texture::internalLoadGLTexture(uchar *pixels, int channels, int width, int
uchar *tmp = new uchar[glHeight*glWidth*channels];
memset(tmp, 0, glHeight*glWidth*channels);
if(pixels)
for(int y=0;y<height;++y)
for(int x=0;x<width;++x)
for(int i=0;i<channels;++i)
for(int y=0; y<height; ++y)
for(int x=0; x<width; ++x)
for(int i=0; i<channels; ++i)
tmp[y*glWidth*channels+x*channels+i] = pixels[y*width*channels+x*channels+i];
pixels = tmp;
mustFree = true;
@@ -99,11 +83,10 @@ uint Texture::internalLoadGLTexture(uchar *pixels, int channels, int width, int
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
// nearest filtering
// nearest filtering (non smooth)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// free if needed
if(mustFree)
delete[] pixels;
@@ -112,30 +95,37 @@ uint Texture::internalLoadGLTexture(uchar *pixels, int channels, int width, int
Texture::~Texture()
{
g_graphics.disableDrawing();
// free texture from gl memory
if(m_textureId)
glDeleteTextures(1, &m_textureId);
}
void Texture::enableBilinearFilter()
{
g_graphics.disableDrawing();
// enable smooth texture
glBindTexture(GL_TEXTURE_2D, m_textureId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
uchar *Texture::getPixels()
uchar* Texture::getPixels()
{
g_graphics.disableDrawing();
// copy pixels from opengl memory
uchar *pixels = new uchar[m_glSize.area()*4];
uchar* pixels = new uchar[m_glSize.area()*4];
glBindTexture(GL_TEXTURE_2D, m_textureId);
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
// convert pixels to the real texture size
if(m_size != m_glSize)
for(int y=0;y<m_size.height();++y)
for(int x=0;x<m_size.width();++x)
for(int i=0;i<4;++i)
for(int y=0; y<m_size.height(); ++y)
for(int x=0; x<m_size.width(); ++x)
for(int i=0; i<4; ++i)
pixels[y*m_size.width()*4+x*4+i] = pixels[y*m_glSize.width()*4+x*4+i];
return pixels;
}

View File

@@ -1,37 +1,13 @@
/* The MIT License
*
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef TEXTURE_H
#define TEXTURE_H
#include <global.h>
#include "graphicsdeclarations.h"
class Texture : public std::enable_shared_from_this<Texture>
{
public:
/// Create a texture, width and height must be a multiple of 2
Texture(int width, int height, int channels, uchar *pixels = NULL);
Texture(int width, int height, int channels, uchar* pixels = NULL);
virtual ~Texture();
/// Enable texture bilinear filter (smooth scaled textures)
@@ -41,21 +17,18 @@ public:
virtual uint getTextureId() const { return m_textureId; }
/// Copy pixels from OpenGL texture
uchar *getPixels();
uchar* getPixels();
const Size& getSize() const { return m_size; }
const Size& getGlSize() const { return m_glSize; }
protected:
Texture() { }
uint internalLoadGLTexture(uchar *pixels, int channels, int w, int h);
uint internalLoadGLTexture(uchar* pixels, int channels, int w, int h);
uint m_textureId;
Size m_size;
Size m_glSize;
};
typedef std::shared_ptr<Texture> TexturePtr;
typedef std::weak_ptr<Texture> TextureWeakPtr;
#endif // TEXTURE_H
#endif

View File

@@ -1,46 +0,0 @@
/* The MIT License
*
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <global.h>
#include <graphics/textureloader.h>
#include <graphics/texture.h>
#include <util/apngloader.h>
#include "animatedtexture.h"
TexturePtr TextureLoader::loadPNG(std::stringstream& file)
{
TexturePtr texture;
apng_data apng;
if(load_apng(file, &apng) == 0) {
if(apng.num_frames > 1) { // animated texture
uchar *framesdata = apng.pdata + (apng.first_frame * apng.width * apng.height * apng.bpp);
texture = TexturePtr(new AnimatedTexture(apng.width, apng.height, apng.bpp, apng.num_frames, framesdata, (int*)apng.frames_delay));
} else
texture = TexturePtr(new Texture(apng.width, apng.height, apng.bpp, apng.pdata));
free_apng(&apng);
}
return texture;
}

View File

@@ -1,38 +0,0 @@
/* The MIT License
*
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef TEXTURELOADER_H
#define TEXTURELOADER_H
#include <global.h>
#include <graphics/texture.h>
class TextureLoader
{
public:
/// Load a png textures
static TexturePtr loadPNG(std::stringstream& file);
};
#endif // TEXTURELOADER_H

View File

@@ -0,0 +1,55 @@
#include "texturemanager.h"
#include "animatedtexture.h"
#include <core/resourcemanager.h>
#include <thirdparty/apngloader.h>
TextureManager g_textures;
TexturePtr TextureManager::getTexture(const std::string& textureFile)
{
TexturePtr texture;
// check if the texture is already loaded
auto it = m_textures.find(textureFile);
if(it != m_textures.end()) {
if(it->second.expired())
m_textures.erase(it);
else
texture = it->second.lock();
}
// texture not found, load it
if(!texture) {
try {
// currently only png textures are supported
if(!boost::ends_with(textureFile, ".png"))
throw std::logic_error("texture file format no supported");
// load texture file data
std::stringstream fin;
g_resources.loadFile(textureFile, fin);
texture = loadPNG(fin);
} catch(std::exception& e) {
logError("ERROR: unable to load texture '",textureFile,"': ", e.what());
}
}
return texture;
}
TexturePtr TextureManager::loadPNG(std::stringstream& file)
{
TexturePtr texture;
apng_data apng;
if(load_apng(file, &apng) == 0) {
if(apng.num_frames > 1) { // animated texture
uchar *framesdata = apng.pdata + (apng.first_frame * apng.width * apng.height * apng.bpp);
texture = TexturePtr(new AnimatedTexture(apng.width, apng.height, apng.bpp, apng.num_frames, framesdata, (int*)apng.frames_delay));
} else
texture = TexturePtr(new Texture(apng.width, apng.height, apng.bpp, apng.pdata));
free_apng(&apng);
}
return texture;
}

View File

@@ -0,0 +1,21 @@
#ifndef TEXTUREMANAGER_H
#define TEXTUREMANAGER_H
#include "texture.h"
class TextureManager
{
public:
/// Load a texture from file, if is already loaded, it will be retrieved from cache
TexturePtr getTexture(const std::string& textureFile);
/// Load a png textures
static TexturePtr loadPNG(std::stringstream& file);
private:
std::map<std::string, TextureWeakPtr> m_textures;
};
extern TextureManager g_textures;
#endif

View File

@@ -1,68 +0,0 @@
/* The MIT License
*
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <global.h>
#include <core/resources.h>
#include <graphics/textures.h>
#include <graphics/textureloader.h>
#include <core/dispatcher.h>
Textures g_textures;
TexturePtr Textures::get(const std::string& textureFile)
{
TexturePtr texture;
// check if the texture is already loaded
auto it = m_textures.find(textureFile);
if(it != m_textures.end()) {
if(it->second.expired())
m_textures.erase(it);
else
texture = it->second.lock();
}
// texture not found, load it
if(!texture) {
// currently only png textures are supported
if(!boost::ends_with(textureFile, ".png")) {
logError("ERROR: Unable to load texture '",textureFile,"', file format no supported.");
return texture;
}
// load texture file data
std::stringstream fin;
if(!g_resources.loadFile(textureFile, fin)) {
logError("ERROR: Unable to load texture '",textureFile,"', file could not be read.");
return texture;
}
// load the texture
texture = TexturePtr(TextureLoader::loadPNG(fin));
if(!texture)
logError("ERROR: Unable to load texture '",textureFile,"'");
}
return texture;
}

View File

@@ -1,45 +0,0 @@
/* The MIT License
*
* Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef TEXTURES_H
#define TEXTURES_H
#include <global.h>
#include <graphics/texture.h>
class Textures
{
public:
Textures() { }
/// Load a texture from file, if it was already loaded it will be retrieved from cache
TexturePtr get(const std::string& textureFile);
private:
std::map<std::string, TextureWeakPtr> m_textures;
};
extern Textures g_textures;
#endif // TEXTURES_H