diff --git a/changelog.txt b/changelog.txt index 954dea9..59811e6 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,5 +1,5 @@ ZEngine Version Log for Version 0.8.5 -$Id: changelog.txt,v 1.61 2003/12/31 12:59:36 cozman Exp $ +$Id: changelog.txt,v 1.62 2003/12/31 21:17:11 cozman Exp $ Changes are marked with symbols that describe them: ! is code that breaks backwards compatibility (used after 0.8.0-rc1, previous versions broke compatibility) @@ -18,6 +18,7 @@ Changes are marked with symbols that describe them: ! Removed Doxygen documentation comments. ! Changed several member functions with obsolete names. + Introduced severity option to error reporting for better error logs. + + A lot more error logging internally. + Added HTML output option to error system. + Added XML resource file support via TinyXML (ZEngine::SetResourceFile, ZEngine::GetStringResource, etc) + Added ZAnimation class. diff --git a/include/ZE_ZEngine.h b/include/ZE_ZEngine.h index 67fca88..dec1910 100644 --- a/include/ZE_ZEngine.h +++ b/include/ZE_ZEngine.h @@ -130,7 +130,7 @@ class ZEngine void SetEventFilter(SDL_EventFilter filter); void SetErrorLog(ZErrorLogStyle logStyle, std::string logFile); - void ReportError(ZErrorSeverity type, std::string desc="", ...); + void ReportError(ZErrorSeverity type, std::string desc, ...); void WriteLog(std::string str, ...); void SeedRandGen(unsigned long seed); diff --git a/src/ZE_Utility.cpp b/src/ZE_Utility.cpp index 58ecc41..7689ba6 100755 --- a/src/ZE_Utility.cpp +++ b/src/ZE_Utility.cpp @@ -9,6 +9,7 @@ *******************************************************************************/ #include "ZE_Utility.h" +#include "ZE_ZEngine.h" //needed for error log, can't be in ZE_Utility.h (circular dependency) namespace ZE { @@ -33,7 +34,7 @@ SDL_RWops* RWFromZip(std::string zipname, std::string filename) if(!zip) //failed to open zip { - //log error + ZEngine::GetInstance()->ReportError(ZERR_WARNING,"Could not open zipfile %s",zipname.c_str()); return NULL; } @@ -41,7 +42,7 @@ SDL_RWops* RWFromZip(std::string zipname, std::string filename) unzLocateFile(zip,filename.c_str(),0); if(unzOpenCurrentFile(zip) != UNZ_OK) //failed to open file within zip { - return NULL; + return NULL; //error should reported in calling function } //find current file info (we are looking for uncompressed file size) @@ -53,7 +54,7 @@ SDL_RWops* RWFromZip(std::string zipname, std::string filename) { unzCloseCurrentFile(zip); unzClose(zip); - //log error (failed to allocate memory?!) + ZEngine::GetInstance()->ReportError(ZERR_ERROR,"RWFromZip failed to allocate enough memory for buffer while loading %s from %s.",filename.c_str(),zipname.c_str()); return NULL; } diff --git a/src/ZE_ZAnimation.cpp b/src/ZE_ZAnimation.cpp index dbf6443..d04aee4 100644 --- a/src/ZE_ZAnimation.cpp +++ b/src/ZE_ZAnimation.cpp @@ -34,6 +34,8 @@ ZAnimation::~ZAnimation() void ZAnimation::Create(ZImage *images, int numFrames, Uint32 frameDelay, ZAnimType type, bool backwards) { + if(!images) + rEngine->ReportError(ZERR_WARNING,"Called ZAnimation::Create with NULL images parameter."); rAnimImages = images; rNumFrames = numFrames; rFrameDelay = frameDelay; @@ -44,6 +46,8 @@ void ZAnimation::Create(ZImage *images, int numFrames, Uint32 frameDelay, ZAnimT void ZAnimation::SetAnimImages(ZImage *images, int numFrames) { + if(!images) + rEngine->ReportError(ZERR_WARNING,"Called ZAnimation::SetAnimImages with NULL images parameter."); rAnimImages = images; rNumFrames = numFrames; } @@ -67,11 +71,18 @@ void ZAnimation::Reset() void ZAnimation::Start() { - if(rAnimType != ZANIM_NONE) - rFrameStep = rBackwards ? -1 : 1; + if(rAnimImages) + { + if(rAnimType != ZANIM_NONE) + rFrameStep = rBackwards ? -1 : 1; + else + rFrameStep = 0; + rNextFrameTime = rEngine->GetTime()+rFrameDelay; + } else - rFrameStep = 0; - rNextFrameTime = rEngine->GetTime()+rFrameDelay; + { + rEngine->ReportError(ZERR_WARNING,"Called ZAnimation::Start with no images loaded."); + } } void ZAnimation::Pause() @@ -87,7 +98,7 @@ void ZAnimation::SetFrame(int frame) SetFrame(rNumFrames+frame); else { - //invalid frame + rEngine->ReportError(ZERR_WARNING,"Attempt to set frame to %d in ZAnimation::SetFrame, valid range is %d to %d.",frame,-rNumFrames,rNumFrames-1); } } @@ -117,7 +128,7 @@ void ZAnimation::Update() case ZANIM_NONE: default: Reset(); - //error? + rEngine->ReportError(ZERR_ERROR,"Unknown error: Invalid Enum [%d] in ZAnimation::Update",static_cast(rAnimType)); break; } } @@ -129,8 +140,8 @@ void ZAnimation::Draw(float x, float y) const { if(rAnimImages) rAnimImages[rCurFrame].Draw(x,y); - //else - //error: images not loaded + else + rEngine->ReportError(ZERR_WARNING,"Called ZAnimation::Draw with no images loaded."); } bool ZAnimation::IsRunning() const diff --git a/src/ZE_ZEngine.cpp b/src/ZE_ZEngine.cpp index b0e1d14..02cc6c7 100644 --- a/src/ZE_ZEngine.cpp +++ b/src/ZE_ZEngine.cpp @@ -20,9 +20,6 @@ ZEngine *ZEngine::sInstance=NULL; ZEngine::ZEngine() : mScreen(NULL), mFullscreen(true), mInitialized(false), mPaused(false), mUnpauseOnActive(false), -#ifdef DEPRECIATED - mDesiredFramerate(0), -#endif DEPRECIATED mNextUpdate(0), mLastPause(0), mPausedTime(0), mLastTime(0), mSecPerFrame(0.0), mNeedReload(false), mActive(false), mQuit(false), mKeyIsPressed(NULL), @@ -31,32 +28,42 @@ ZEngine::ZEngine() : { for(int k = 0; k < SDLK_LAST; ++k) mKeyPress[k] = false; + atexit(ZEngine::ReleaseInstance); } TiXmlElement* ZEngine::FindElement(std::string type, std::string id) { + TiXmlElement *elem=NULL; + if(rZRF.RootElement()) { - TiXmlElement *elem = rZRF.RootElement()->FirstChildElement(); + elem = rZRF.RootElement()->FirstChildElement(); //while element exists while(elem) { if(strcmpi(elem->Value(),type.c_str()) == 0 && strcmpi(elem->Attribute("id"),id.c_str()) == 0) - return elem; + break; //found our guy else elem = elem->NextSiblingElement(); } + + //if it gets here, element not found + ReportError(ZERR_WARNING,"No '%s' resource found with id '%s'",type.c_str(),id.c_str()); + elem = NULL; + } + else + { + ReportError(ZERR_WARNING,"No root element in ZRF file."); } - return NULL; //if it gets this far, problem + return elem; } ZEngine* ZEngine::GetInstance() { if(!sInstance) //first time through, gets new instance, each time after returns same one sInstance = new ZEngine; - atexit(ZEngine::ReleaseInstance); return sInstance; } @@ -257,7 +264,7 @@ void ZEngine::ToggleFullscreen() #ifdef linux //SDL_WM_TF only works on Linux SDL_WM_ToggleFullScreen(mScreen); #else - CreateDisplay(mScreen->w,mScreen->h,mScreen->format->BitsPerPixel,!mFullscreen); + CreateDisplay(mScreen->w,mScreen->h,mScreen->format->BitsPerPixel,!mFullscreen); //replacement for WM_TF on Windows #endif SetReloadNeed(true); //images need to be reloaded on fullscreen swap } @@ -277,17 +284,7 @@ void ZEngine::Update() //keeps track of spf// mSecPerFrame = (GetTime()-mLastTime)/1000.0; - mLastTime = GetTime(); - -#ifdef DEPRECIATED - //framerate limiting// - if(mDesiredFramerate) - { - if(mLastTime < mNextUpdate) - SDL_Delay(mNextUpdate-mLastTime); - mNextUpdate = GetTime()+(1000/mDesiredFramerate); - } -#endif //DEPRECIATED + mLastTime = GetTime(); } #if (GFX_BACKEND == ZE_OGL) @@ -477,7 +474,9 @@ void ZEngine::CheckEvents() while(SDL_PollEvent(&event)) { - if(!mEventFilter || mEventFilter(&event)) //if the filter returns 0 it is removing the event, it will not be processed + //only process event if filter doesn't exist or if filter returns a nonzero value + //the SDL spec says that filters return 0 when they wish to remove an item from the event queue + if(!mEventFilter || mEventFilter(&event)) { switch(event.type) //only certain events are handled, mEventFilter can handle user requests { @@ -666,8 +665,8 @@ double ZEngine::RandDouble() void ZEngine::SetResourceFile(std::string filename) { rZRF.LoadFile(filename); - //if(rZRF.Error()) - //log an error + if(rZRF.Error()) + ReportError(ZERR_ERROR,"Error loading resource file %s.",filename.c_str()); } std::string ZEngine::GetStringResource(std::string type, std::string id, std::string element) @@ -677,7 +676,7 @@ std::string ZEngine::GetStringResource(std::string type, std::string id, std::st return elem->Attribute(element.c_str()); else { - //error + ReportError(ZERR_WARNING,"GetStringResource(%s,%s,%s) failed.",type.c_str(),id.c_str(),element.c_str()); return ""; //empty string } } @@ -685,34 +684,35 @@ std::string ZEngine::GetStringResource(std::string type, std::string id, std::st int ZEngine::GetIntResource(std::string type, std::string id, std::string element) { TiXmlElement *elem = FindElement(type,id); - int ret; + int ret=0; - if(elem && (elem->QueryIntAttribute(element.c_str(),&ret) == TIXML_SUCCESS)) - return ret; - else + if(!elem || (elem->QueryIntAttribute(element.c_str(),&ret) != TIXML_SUCCESS)) { if(!elem) - WriteLog("no elem"); + ReportError(ZERR_WARNING,"GetIntResource(%s,%s,%s) failed, no tags of that type.",type.c_str(),id.c_str(),element.c_str()); else if(elem->QueryIntAttribute(element.c_str(),&ret) == TIXML_NO_ATTRIBUTE) - WriteLog("no attribute"); + ReportError(ZERR_WARNING,"GetIntResource(%s,%s,%s) failed, no tag with that ID.",type.c_str(),id.c_str(),element.c_str()); else if(elem->QueryIntAttribute(element.c_str(),&ret) == TIXML_WRONG_TYPE) - WriteLog("wrong type"); - return 0; + ReportError(ZERR_WARNING,"GetIntResource(%s,%s,%s) failed, not an integer.",type.c_str(),id.c_str(),element.c_str()); } + return ret; } double ZEngine::GetDoubleResource(std::string type, std::string id, std::string element) { TiXmlElement *elem = FindElement(type,id); - double ret; + double ret=0; - if(elem && elem->QueryDoubleAttribute(element.c_str(),&ret) == TIXML_SUCCESS) - return ret; - else + if(!elem || (elem->QueryDoubleAttribute(element.c_str(),&ret) != TIXML_SUCCESS)) { - //error - return 0; + if(!elem) + ReportError(ZERR_WARNING,"GetDoubleResource(%s,%s,%s) failed, no tags of that type.",type.c_str(),id.c_str(),element.c_str()); + else if(elem->QueryDoubleAttribute(element.c_str(),&ret) == TIXML_NO_ATTRIBUTE) + ReportError(ZERR_WARNING,"GetDoubleResource(%s,%s,%s) failed, no tag with that ID.",type.c_str(),id.c_str(),element.c_str()); + else if(elem->QueryDoubleAttribute(element.c_str(),&ret) == TIXML_WRONG_TYPE) + ReportError(ZERR_WARNING,"GetDoubleResource(%s,%s,%s) failed, not a double.",type.c_str(),id.c_str(),element.c_str()); } + return ret; } bool ZEngine::DisplayCreated() diff --git a/src/ZE_ZFont.cpp b/src/ZE_ZFont.cpp index 1ed2ed9..239a682 100644 --- a/src/ZE_ZFont.cpp +++ b/src/ZE_ZFont.cpp @@ -78,7 +78,7 @@ void ZFont::OpenFromZRF(std::string resourceId) if(filename.length() && size) Open(filename,size); else - ;//error + rEngine->ReportError(ZERR_WARNING,"Failed to load font resource '%s'",resourceId.c_str()); } void ZFont::Release() @@ -95,6 +95,8 @@ void ZFont::DrawText(std::string text, ZImage &image) const image.Attach(TTF_RenderText_Blended(rFont, text.c_str(), rColor)); image.SetAlpha(rColor.unused); //the images alpha comes from the SetColor a parameter } + else + rEngine->ReportError(ZERR_VERBOSE,"Called ZFont::DrawText with no font loaded."); } void ZFont::DrawShadedText(std::string text, ZImage &image) const @@ -106,6 +108,8 @@ void ZFont::DrawShadedText(std::string text, ZImage &image) const image.Attach(TTF_RenderText_Shaded(rFont, text.c_str(), rColor, rBGColor)); image.SetAlpha(rColor.unused); } + else + rEngine->ReportError(ZERR_VERBOSE,"Called ZFont::DrawShadedText with no font loaded."); } void ZFont::SetColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a) @@ -144,10 +148,15 @@ void ZFont::SetStyle(bool bold, bool italic, bool underline) void ZFont::Resize(int size) { - if(rZipname.length()) - OpenFromZip(rZipname,rFilename,size); + if(rFilename.length()) + { + if(rZipname.length()) + OpenFromZip(rZipname,rFilename,size); + else + Open(rFilename,size); + } else - Open(rFilename,size); + rEngine->ReportError(ZERR_VERBOSE,"Called ZFont::Resize with no font loaded."); } bool ZFont::IsLoaded() const diff --git a/src/ZE_ZImage.cpp b/src/ZE_ZImage.cpp index 813ab3d..db39606 100644 --- a/src/ZE_ZImage.cpp +++ b/src/ZE_ZImage.cpp @@ -117,8 +117,8 @@ void ZImage::OpenFromZRF(std::string resourceId) std::string filename = rEngine->GetStringResource("image",resourceId,"filename"); if(filename.length()) Open(filename); - //else - //error + else + rEngine->ReportError(ZERR_WARNING,"Failed to load image resource '%s'",resourceId.c_str()); } void ZImage::OpenFromImage(SDL_Surface *image, Sint16 x, Sint16 y, Sint16 w, Sint16 h) diff --git a/src/ZE_ZMusic.cpp b/src/ZE_ZMusic.cpp index e2e8ee7..10d6008 100644 --- a/src/ZE_ZMusic.cpp +++ b/src/ZE_ZMusic.cpp @@ -51,8 +51,8 @@ void ZMusic::OpenFromZRF(std::string resourceId) std::string filename = rEngine->GetStringResource("music",resourceId,"filename"); if(filename.length()) Open(filename); - //else - //error + else + rEngine->ReportError(ZERR_WARNING,"Failed to load music resource '%s'",resourceId.c_str()); } void ZMusic::Release() diff --git a/src/ZE_ZSound.cpp b/src/ZE_ZSound.cpp index aca86de..34fe9e8 100644 --- a/src/ZE_ZSound.cpp +++ b/src/ZE_ZSound.cpp @@ -68,8 +68,8 @@ void ZSound::OpenFromZRF(std::string resourceId) std::string filename = rEngine->GetStringResource("sound",resourceId,"filename"); if(filename.length()) Open(filename); - //else - //error + else + rEngine->ReportError(ZERR_WARNING,"Failed to load sound resource '%s'",resourceId.c_str()); } void ZSound::Release()