From c1b2b3ed3a2420c881618c8b0eae06e5e13d31f9 Mon Sep 17 00:00:00 2001 From: Eduardo Bart Date: Sat, 23 Apr 2011 20:23:52 -0300 Subject: [PATCH] error proning --- data/modules/messagebox/messagebox.lua | 12 +++++------ data/modules/messagebox/messagebox.yml | 2 +- src/framework/graphics/image.cpp | 3 ++- src/framework/graphics/textures.cpp | 15 +++++++++----- src/framework/ui/uicontainer.cpp | 1 + src/framework/ui/uielement.cpp | 1 + src/framework/ui/uiloader.cpp | 28 ++++++++++++++++---------- src/main.cpp | 4 +--- 8 files changed, 39 insertions(+), 27 deletions(-) diff --git a/data/modules/messagebox/messagebox.lua b/data/modules/messagebox/messagebox.lua index a8e48ce1..d353fcdc 100644 --- a/data/modules/messagebox/messagebox.lua +++ b/data/modules/messagebox/messagebox.lua @@ -3,10 +3,10 @@ function messageBox(title, text) local messageBoxLabel = messageBoxWindow:getChildByID("messageBoxLabel") local messageBoxOkButton = messageBoxWindow:getChildByID("messageBoxOkButton") local uiRoot = messageBoxWindow:getParent() - uiRoot:lock(messageBox) - messageBoxWindow:setTitle(text) - messageBoxLabel:setText(text) - messageBoxWindow:setSize(messageBoxLabel:getSize() + Size{20, 20}) - messageBox:setOnDestroy(function() uiRoot:unlock() end) + uiRoot:lock(messageBoxWindow) + --messageBoxWindow:setTitle(text) + --messageBoxLabel:setText(text) + --messageBoxWindow:setSize(messageBoxLabel:getSize() + Size{20, 20}) + messageBoxWindow:setOnDestroy(function() uiRoot:unlock() end) messageBoxOkButton:setOnClick(function() messageBoxWindow:destroy() end) -end \ No newline at end of file +end diff --git a/data/modules/messagebox/messagebox.yml b/data/modules/messagebox/messagebox.yml index 072a402f..aeb67827 100644 --- a/data/modules/messagebox/messagebox.yml +++ b/data/modules/messagebox/messagebox.yml @@ -15,4 +15,4 @@ window#messageBoxWindow: anchors.right: parent.right anchors.bottom: parent.bottom margin.bottom: 10 - margin.right: 10 \ No newline at end of file + margin.right: 10 diff --git a/src/framework/graphics/image.cpp b/src/framework/graphics/image.cpp index c0cfa7f1..0e377b2d 100644 --- a/src/framework/graphics/image.cpp +++ b/src/framework/graphics/image.cpp @@ -47,7 +47,8 @@ Image::Image(const std::string& texture, Rect textureCoords) : void Image::draw(const Rect& screenCoords) { - g_graphics.drawTexturedRect(screenCoords, m_texture, m_textureCoords); + if(m_texture) + g_graphics.drawTexturedRect(screenCoords, m_texture, m_textureCoords); } diff --git a/src/framework/graphics/textures.cpp b/src/framework/graphics/textures.cpp index 4b1b81d4..ec1b5457 100644 --- a/src/framework/graphics/textures.cpp +++ b/src/framework/graphics/textures.cpp @@ -45,19 +45,24 @@ TexturePtr Textures::get(const std::string& textureFile) // texture not found, load it if(!texture) { // currently only png textures are supported - if(!boost::ends_with(textureFile, ".png")) - flogFatal("FATAL ERROR: Unable to load texture %s, file format no supported.", textureFile.c_str()); + if(!boost::ends_with(textureFile, ".png")) { + flogError("ERROR: Unable to load texture %s, file format no supported.", textureFile.c_str()); + return texture; + } // load texture file data uint fileSize; uchar *textureFileData = g_resources.loadFile(textureFile, &fileSize); - if(!textureFileData) - flogFatal("FATAL ERROR: Unable to load texture %s, file could not be read.", textureFile.c_str()); + if(!textureFileData) { + flogError("ERROR: Unable to load texture %s, file could not be read.", textureFile.c_str()); + return texture; + } + // load the texture texture = TexturePtr(TextureLoader::loadPNG(textureFileData)); if(!texture) - flogFatal("FATAL ERROR: Unable to load texture %s", textureFile.c_str()); + flogError("ERROR: Unable to load texture %s", textureFile.c_str()); delete[] textureFileData; } diff --git a/src/framework/ui/uicontainer.cpp b/src/framework/ui/uicontainer.cpp index 95e7646b..032d6390 100644 --- a/src/framework/ui/uicontainer.cpp +++ b/src/framework/ui/uicontainer.cpp @@ -29,6 +29,7 @@ void UIContainer::internalOnDestroy() { + setFocusedElement(UIElementPtr()); // destroy children for(auto it = m_children.begin(); it != m_children.end(); ++it) { (*it)->setParent(UIContainerPtr()); diff --git a/src/framework/ui/uielement.cpp b/src/framework/ui/uielement.cpp index 0bba5ca8..39a04bd6 100644 --- a/src/framework/ui/uielement.cpp +++ b/src/framework/ui/uielement.cpp @@ -56,6 +56,7 @@ void UIElement::internalOnDestroy() { setVisible(false); setEnabled(false); + // check for leaks, the number of references must be always 2 here assert(asUIElement().use_count() == 2); } diff --git a/src/framework/ui/uiloader.cpp b/src/framework/ui/uiloader.cpp index 667ef7b7..10972fa6 100644 --- a/src/framework/ui/uiloader.cpp +++ b/src/framework/ui/uiloader.cpp @@ -118,8 +118,10 @@ void UILoader::populateContainer(const UIContainerPtr& parent, const YAML::Node& // check if it's and element id if(id.find("#") != std::string::npos) { UIElementPtr element = createElementFromId(id); - if(!element) - throw YAML::Exception(it.first().GetMark(), "invalid element type"); + if(!element) { + logError(YAML::Exception(it.first().GetMark(), "invalid element type").what()); + continue; + } parent->addChild(element); // also populate this element if it's a parent @@ -218,7 +220,7 @@ void UILoader::loadElement(const UIElementPtr& element, const YAML::Node& node) g_lua.pushFunction(funcRef); g_lua.lua_UIElement_setOnLoad(); } else - throw YAML::Exception(cnode.GetMark(), "failed to parse lua script"); + logError(YAML::Exception(cnode.GetMark(), "failed to parse inline lua script").what()); } if(node.FindValue("onDestroy")) { @@ -229,7 +231,7 @@ void UILoader::loadElement(const UIElementPtr& element, const YAML::Node& node) g_lua.pushFunction(funcRef); g_lua.lua_UIElement_setOnDestroy(); } else - throw YAML::Exception(cnode.GetMark(), "failed to parse lua script"); + logError(YAML::Exception(cnode.GetMark(), "failed to parse inline lua script").what()); } // load specific element type @@ -260,8 +262,10 @@ void UILoader::loadElementAnchor(const UIElementPtr& element, EAnchorType type, std::vector split; boost::split(split, anchorDescription, boost::is_any_of(std::string("."))); - if(split.size() != 2) - throw YAML::Exception(node.GetMark(), "invalid anchors description"); + if(split.size() != 2) { + logError(YAML::Exception(node.GetMark(), "invalid anchor").what()); + return; + } std::string relativeElementId = split[0]; std::string relativeAnchorTypeId = split[1]; @@ -279,8 +283,10 @@ void UILoader::loadElementAnchor(const UIElementPtr& element, EAnchorType type, relativeAnchorType = ANCHOR_HORIZONTAL_CENTER; else if(relativeAnchorTypeId == "verticalCenter") relativeAnchorType = ANCHOR_VERTICAL_CENTER; - else - throw YAML::Exception(node.GetMark(), "invalid anchors description"); + else { + logError(YAML::Exception(node.GetMark(), "invalid anchor type").what()); + return; + } UILayoutPtr relativeElement; if(relativeElementId == "parent" && element->getParent()) { @@ -295,9 +301,9 @@ void UILoader::loadElementAnchor(const UIElementPtr& element, EAnchorType type, if(relativeElement) { if(!element->addAnchor(type, AnchorLine(relativeElement, relativeAnchorType))) - throw YAML::Exception(node.GetMark(), "error while processing anchors"); + logError(YAML::Exception(node.GetMark(), "anchoring failed").what()); } else { - throw YAML::Exception(node.GetMark(), "anchoring failed, does the relative element really exists?"); + logError(YAML::Exception(node.GetMark(), "anchoring failed, does the relative element really exists?").what()); } } @@ -313,7 +319,7 @@ void UILoader::loadButton(const UIButtonPtr& button, const YAML::Node& node) g_lua.pushFunction(funcRef); g_lua.lua_UIButton_setOnClick(); } else { - throw YAML::Exception(node["onClick"].GetMark(), "failed to parse lua script"); + logError(YAML::Exception(node["onClick"].GetMark(), "failed to parse inline lua script").what()); } } } diff --git a/src/main.cpp b/src/main.cpp index 9f607ec2..97b0a882 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -84,7 +84,7 @@ int main(int argc, const char *argv[]) // init platform stuff Platform::init("OTClient"); - // init random numbers + // seed random srand(time(NULL)); // init resources @@ -124,7 +124,6 @@ int main(int argc, const char *argv[]) logFatal("FATAL ERROR: no ui loaded at all, no reason to continue running"); Platform::showWindow(); - //Platform::hideMouseCursor(); // main loop, run everything g_engine.run(); @@ -143,7 +142,6 @@ int main(int argc, const char *argv[]) // save configurations before exiting saveConfigs(); - //Platform::showMouseCursor(); Platform::terminate(); // unload resources