error proning

This commit is contained in:
Eduardo Bart 2011-04-23 20:23:52 -03:00
parent 3960240b8e
commit c1b2b3ed3a
8 changed files with 39 additions and 27 deletions

View File

@ -3,10 +3,10 @@ function messageBox(title, text)
local messageBoxLabel = messageBoxWindow:getChildByID("messageBoxLabel") local messageBoxLabel = messageBoxWindow:getChildByID("messageBoxLabel")
local messageBoxOkButton = messageBoxWindow:getChildByID("messageBoxOkButton") local messageBoxOkButton = messageBoxWindow:getChildByID("messageBoxOkButton")
local uiRoot = messageBoxWindow:getParent() local uiRoot = messageBoxWindow:getParent()
uiRoot:lock(messageBox) uiRoot:lock(messageBoxWindow)
messageBoxWindow:setTitle(text) --messageBoxWindow:setTitle(text)
messageBoxLabel:setText(text) --messageBoxLabel:setText(text)
messageBoxWindow:setSize(messageBoxLabel:getSize() + Size{20, 20}) --messageBoxWindow:setSize(messageBoxLabel:getSize() + Size{20, 20})
messageBox:setOnDestroy(function() uiRoot:unlock() end) messageBoxWindow:setOnDestroy(function() uiRoot:unlock() end)
messageBoxOkButton:setOnClick(function() messageBoxWindow:destroy() end) messageBoxOkButton:setOnClick(function() messageBoxWindow:destroy() end)
end end

View File

@ -47,7 +47,8 @@ Image::Image(const std::string& texture, Rect textureCoords) :
void Image::draw(const Rect& screenCoords) 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);
} }

View File

@ -45,19 +45,24 @@ TexturePtr Textures::get(const std::string& textureFile)
// texture not found, load it // texture not found, load it
if(!texture) { if(!texture) {
// currently only png textures are supported // currently only png textures are supported
if(!boost::ends_with(textureFile, ".png")) if(!boost::ends_with(textureFile, ".png")) {
flogFatal("FATAL ERROR: Unable to load texture %s, file format no supported.", textureFile.c_str()); flogError("ERROR: Unable to load texture %s, file format no supported.", textureFile.c_str());
return texture;
}
// load texture file data // load texture file data
uint fileSize; uint fileSize;
uchar *textureFileData = g_resources.loadFile(textureFile, &fileSize); uchar *textureFileData = g_resources.loadFile(textureFile, &fileSize);
if(!textureFileData) if(!textureFileData) {
flogFatal("FATAL ERROR: Unable to load texture %s, file could not be read.", textureFile.c_str()); flogError("ERROR: Unable to load texture %s, file could not be read.", textureFile.c_str());
return texture;
}
// load the texture // load the texture
texture = TexturePtr(TextureLoader::loadPNG(textureFileData)); texture = TexturePtr(TextureLoader::loadPNG(textureFileData));
if(!texture) 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; delete[] textureFileData;
} }

View File

@ -29,6 +29,7 @@
void UIContainer::internalOnDestroy() void UIContainer::internalOnDestroy()
{ {
setFocusedElement(UIElementPtr());
// destroy children // destroy children
for(auto it = m_children.begin(); it != m_children.end(); ++it) { for(auto it = m_children.begin(); it != m_children.end(); ++it) {
(*it)->setParent(UIContainerPtr()); (*it)->setParent(UIContainerPtr());

View File

@ -56,6 +56,7 @@ void UIElement::internalOnDestroy()
{ {
setVisible(false); setVisible(false);
setEnabled(false); setEnabled(false);
// check for leaks, the number of references must be always 2 here // check for leaks, the number of references must be always 2 here
assert(asUIElement().use_count() == 2); assert(asUIElement().use_count() == 2);
} }

View File

@ -118,8 +118,10 @@ void UILoader::populateContainer(const UIContainerPtr& parent, const YAML::Node&
// check if it's and element id // check if it's and element id
if(id.find("#") != std::string::npos) { if(id.find("#") != std::string::npos) {
UIElementPtr element = createElementFromId(id); UIElementPtr element = createElementFromId(id);
if(!element) if(!element) {
throw YAML::Exception(it.first().GetMark(), "invalid element type"); logError(YAML::Exception(it.first().GetMark(), "invalid element type").what());
continue;
}
parent->addChild(element); parent->addChild(element);
// also populate this element if it's a parent // 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.pushFunction(funcRef);
g_lua.lua_UIElement_setOnLoad(); g_lua.lua_UIElement_setOnLoad();
} else } 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")) { if(node.FindValue("onDestroy")) {
@ -229,7 +231,7 @@ void UILoader::loadElement(const UIElementPtr& element, const YAML::Node& node)
g_lua.pushFunction(funcRef); g_lua.pushFunction(funcRef);
g_lua.lua_UIElement_setOnDestroy(); g_lua.lua_UIElement_setOnDestroy();
} else } 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 // load specific element type
@ -260,8 +262,10 @@ void UILoader::loadElementAnchor(const UIElementPtr& element, EAnchorType type,
std::vector<std::string> split; std::vector<std::string> split;
boost::split(split, anchorDescription, boost::is_any_of(std::string("."))); boost::split(split, anchorDescription, boost::is_any_of(std::string(".")));
if(split.size() != 2) if(split.size() != 2) {
throw YAML::Exception(node.GetMark(), "invalid anchors description"); logError(YAML::Exception(node.GetMark(), "invalid anchor").what());
return;
}
std::string relativeElementId = split[0]; std::string relativeElementId = split[0];
std::string relativeAnchorTypeId = split[1]; std::string relativeAnchorTypeId = split[1];
@ -279,8 +283,10 @@ void UILoader::loadElementAnchor(const UIElementPtr& element, EAnchorType type,
relativeAnchorType = ANCHOR_HORIZONTAL_CENTER; relativeAnchorType = ANCHOR_HORIZONTAL_CENTER;
else if(relativeAnchorTypeId == "verticalCenter") else if(relativeAnchorTypeId == "verticalCenter")
relativeAnchorType = ANCHOR_VERTICAL_CENTER; relativeAnchorType = ANCHOR_VERTICAL_CENTER;
else else {
throw YAML::Exception(node.GetMark(), "invalid anchors description"); logError(YAML::Exception(node.GetMark(), "invalid anchor type").what());
return;
}
UILayoutPtr relativeElement; UILayoutPtr relativeElement;
if(relativeElementId == "parent" && element->getParent()) { if(relativeElementId == "parent" && element->getParent()) {
@ -295,9 +301,9 @@ void UILoader::loadElementAnchor(const UIElementPtr& element, EAnchorType type,
if(relativeElement) { if(relativeElement) {
if(!element->addAnchor(type, AnchorLine(relativeElement, relativeAnchorType))) 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 { } 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.pushFunction(funcRef);
g_lua.lua_UIButton_setOnClick(); g_lua.lua_UIButton_setOnClick();
} else { } 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());
} }
} }
} }

View File

@ -84,7 +84,7 @@ int main(int argc, const char *argv[])
// init platform stuff // init platform stuff
Platform::init("OTClient"); Platform::init("OTClient");
// init random numbers // seed random
srand(time(NULL)); srand(time(NULL));
// init resources // 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"); logFatal("FATAL ERROR: no ui loaded at all, no reason to continue running");
Platform::showWindow(); Platform::showWindow();
//Platform::hideMouseCursor();
// main loop, run everything // main loop, run everything
g_engine.run(); g_engine.run();
@ -143,7 +142,6 @@ int main(int argc, const char *argv[])
// save configurations before exiting // save configurations before exiting
saveConfigs(); saveConfigs();
//Platform::showMouseCursor();
Platform::terminate(); Platform::terminate();
// unload resources // unload resources