diff --git a/include/ZE_ZError.h b/include/ZE_ZError.h deleted file mode 100755 index 1e8be59..0000000 --- a/include/ZE_ZError.h +++ /dev/null @@ -1,127 +0,0 @@ -/******************************************************************************* - This file is Part of the ZEngine Library for 2D game development. - Copyright (C) 2002, 2003 James Turk - - Licensed under a BSD-style license. - - The maintainer of this library is James Turk (james@conceptofzero.net) - and the home of this Library is http://www.zengine.sourceforge.net -*******************************************************************************/ - -/*! - \file ZE_ZError.h - \brief Definition file for ZError. - - Definition file for ZError, the Error logging class for ZEngine. - This class should never be used by the average user, it is used by ZEngine to store information on an error. -
$Id: ZE_ZError.h,v 1.14 2003/08/31 18:34:38 cozman Exp $
- \author James Turk -**/ - -#ifndef __ze_zerror_h__ -#define __ze_zerror_h__ - -#include "ZE_Utility.h" -#include - -namespace ZE -{ - -/*! - \brief Enumeration of ZEngine error codes. - - All the error codes currently possibly by ZEngine, note that ZERR_LAST is not used as an error code, but instead - as a range check on the others. -**/ -enum ZErrorCode -{ - ZERR_NONE, /*!< No error has occured. */ - ZERR_SDL_INTERNAL, /*!< Error internal to SDL has occured, usually more detail is given by SDL. */ - ZERR_SDL_INIT, /*!< Error Initializing SDL. */ - ZERR_MIX_INIT, /*!< Error Initializing SDL_mixer. */ - ZERR_TTF_INIT, /*!< Error Initializing SDL_ttf. */ - ZERR_VIDMODE, /*!< Error setting up the display. */ - ZERR_LOAD_IMAGE, /*!< Error loading an image. */ - ZERR_LOAD_SOUND, /*!< Error loading a sound sample. */ - ZERR_LOAD_MUSIC, /*!< Error loading music. */ - ZERR_LOAD_FONT, /*!< Error loading a font. */ - ZERR_NOIMAGE, /*!< Error trying to use a ZImage without properly loading an image. */ - ZERR_NOSOUND, /*!< Error trying to use a ZSound without properly loading a sound. */ - ZERR_NOMUSIC, /*!< Error trying to use a ZMusic without properly loading music. */ - ZERR_NOFONT, /*!< Error trying to use a ZFont without properly loading a font. */ - ZERR_LAST /*!< Value used as range index, not a valid error code. */ -}; - -/*! - \brief ZError class for describing errors. - - ZError class for storing and printing information on errors. Inherited from ZObject and tied closely to ZEngine. - \since 0.8.2 -**/ -class ZError -{ - protected: - //! Static Array of Error Identifiers - static std::string sErrorDesc[ZERR_LAST]; - //! Error ID. - ZErrorCode rCode; - //! Error Description. - std::string rDescription; - //! File which error occured in. - std::string rFilename; - //! Line which error occured on. - unsigned int rLine; - - public: - /*! - \brief Construct string table for error strings. - - Constructs a string table for errors, enabling ZEngine to properly delete the table on exit. - **/ - static void CreateStringTable(); - - /*! - \brief Default constructor for ZError. - - Make new ZError object, by default set rCode to ZERR_NONE with no description. - \param code ZErrorCode to set object to, defaults to ZERR_NONE. - \param desc Description to use for object, defaults to nothing. - \param file Optional argument specifying the file the error occured in. - \param line Optional argument specifying the line the error occured on. - **/ - ZError(ZErrorCode code=ZERR_NONE, std::string desc="", std::string file="", int line=0); - - /*! - \brief Set members of error object. - - Set new values in ZError object. - \param code ZErrorCode to set object to. - \param desc Description to use for object, defaults to nothing. - \param file Optional argument specifying the file the error occured in. - \param line Optional argument specifying the line the error occured on. - **/ - void Create(ZErrorCode code, std::string desc="", std::string file="", int line=0); - - ///////////// - //Accessors// - ///////////// - - /*! - \brief Get ZErrorCode of error. - - Access ZErrorCode of the ZError object. - \return The error ZErrorCode. - **/ - ZErrorCode Code() const; - - /*! - \brief Get formatted string for log file. - - Return the string to be written to the logfile. Called by ZEngine in LogError. - **/ - std::string LogString() const; -}; - -} - -#endif //__ze_zerror_h__ diff --git a/src/ZE_ZEngine.cpp b/src/ZE_ZEngine.cpp index 391d497..50cb926 100644 --- a/src/ZE_ZEngine.cpp +++ b/src/ZE_ZEngine.cpp @@ -13,7 +13,7 @@ \brief Central source file for ZEngine. Actual implementation of ZEngine singleton class, the core of ZEngine. -
$Id: ZE_ZEngine.cpp,v 1.64 2003/10/24 21:20:09 cozman Exp $
+
$Id: ZE_ZEngine.cpp,v 1.65 2003/11/24 02:21:20 cozman Exp $
\author James Turk **/ @@ -33,12 +33,26 @@ ZEngine::ZEngine() : mSecPerFrame(0.0), mNeedReload(false), mActive(false), mQuit(false), mKeyIsPressed(NULL), mMouseX(0), mMouseY(0), mMouseB(0), - mLogAllErrors(true), mErrlog(stderr), mEventFilter(NULL) + mErrlog(stderr), mEventFilter(NULL) { for(int k = 0; k < SDLK_LAST; ++k) mKeyPress[k] = false; - ZError::CreateStringTable(); + //create error strings + mErrorDesc[ZERR_NONE] = "No Error. [%s]"; + mErrorDesc[ZERR_SDL_INTERNAL] = "SDL Error. [%s]"; + mErrorDesc[ZERR_SDL_INIT] = "Error Initializing SDL: %s"; + mErrorDesc[ZERR_MIX_INIT] = "Error Initializing SDL_mixer: %s"; + mErrorDesc[ZERR_TTF_INIT] = "Error Initializing SDL_ttf: %s"; + mErrorDesc[ZERR_VIDMODE] = "Error Creating Display: %s"; + mErrorDesc[ZERR_LOAD_IMAGE] = "Failed to load Image: %s"; + mErrorDesc[ZERR_LOAD_SOUND] = "Failed to load Sound: %s"; + mErrorDesc[ZERR_LOAD_MUSIC] = "Failed to load Music: %s"; + mErrorDesc[ZERR_LOAD_FONT] = "Failed to load Font: %s"; + mErrorDesc[ZERR_NOIMAGE] = "Called ZImage::%s with no Image loaded."; + mErrorDesc[ZERR_NOSOUND] = "Called ZSound::%s with no Sound loaded."; + mErrorDesc[ZERR_NOMUSIC] = "Called ZMusic::%s with no Music loaded."; + mErrorDesc[ZERR_NOFONT] = "Called ZFont::%s with no Font loaded."; } ZEngine* ZEngine::GetInstance() @@ -102,7 +116,7 @@ bool ZEngine::CreateDisplay(int width, int height, int bpp, bool fullscreen, std if(bpp != -1 && bpp != 8 && bpp != 15 && bpp != 16 && bpp != 24 && bpp !=32) { - ReportError(ZERR_VIDMODE,FormatStr("%d is invalid BPP, must be 8,15,16,24 or 32, trying best BPP.",bpp)); + ReportError(ZERR_VIDMODE,"%d is invalid BPP, must be 8,15,16,24 or 32, trying best BPP.",bpp); bpp = -1; } else //this decides correcr BPP @@ -113,12 +127,12 @@ bool ZEngine::CreateDisplay(int width, int height, int bpp, bool fullscreen, std okBPP = SDL_VideoModeOK(width, height, bpp, vidFlags); if(!okBPP) { - ReportError(ZERR_VIDMODE,FormatStr("%dx%d not supported in any depth.",width,height)); + ReportError(ZERR_VIDMODE,"%dx%d not supported in any depth.",width,height); return false; //return now } else if(okBPP != bpp) { - ReportError(ZERR_VIDMODE,FormatStr("%dx%d not supported in %dBPP, trying %dBPP.",width,height,bpp,okBPP)); + ReportError(ZERR_VIDMODE,"%dx%d not supported in %dBPP, trying %dBPP.",width,height,bpp,okBPP); bpp = okBPP; } } @@ -178,7 +192,7 @@ bool ZEngine::CreateDisplay(int width, int height, int bpp, bool fullscreen, std if(!mScreen) { - ReportError(ZERR_VIDMODE,FormatStr("Unknown Error. %dx%d %dBPP (%s)", width, height, bpp, SDL_GetError())); + ReportError(ZERR_VIDMODE,"Unknown Error. %dx%d %dBPP (%s)", width, height, bpp, SDL_GetError()); #ifdef USE_SDL_MIXER Mix_CloseAudio(); @@ -228,7 +242,7 @@ void ZEngine::CloseDisplay() SDL_Quit(); - if(mErrlog != stderr && mErrlog != stdin) + if(mErrlog && mErrlog != stderr && mErrlog != stdin) fclose(mErrlog); mInitialized = false; @@ -534,12 +548,11 @@ void ZEngine::SetEventFilter(SDL_EventFilter filter) mEventFilter = filter; } -void ZEngine::SetErrorLog(bool logAll, std::string logFile) +void ZEngine::SetErrorLog(std::string logFile) { - mLogAllErrors = logAll; if(logFile.length()) { - //stderr & stdout are special cases, and should be directed to their appropriate streams + //stderr & stdout directed to their appropriate streams if(logFile == "stderr") mErrlog = stderr; else if(logFile == "stdout") @@ -549,42 +562,27 @@ void ZEngine::SetErrorLog(bool logAll, std::string logFile) } } -void ZEngine::ReportError(ZErrorCode code, std::string desc, std::string file, unsigned int line) +void ZEngine::DisableErrorLog() { - mCurError.Create(code,desc,file,line); - - if(mLogAllErrors) - LogError(mCurError); - else - mErrorQueue.push(mCurError); + mErrlog = NULL; } -ZErrorCode ZEngine::GetLastError() +void ZEngine::ReportError(ZErrorCode type, std::string desc, ...) { - ZErrorCode code = mCurError.Code(); - mCurError.Create(ZERR_NONE); - return code; -} + char buf[512]; + va_list args; + std::string msg; -void ZEngine::WriteLog(std::string str) -{ - std::fprintf(mErrlog,str.c_str()); - std::fprintf(mErrlog,"\n"); - std::fflush(mErrlog); -} + va_start(args,desc); + vsprintf(buf,desc.c_str(),args); + va_end(args); -void ZEngine::LogError(ZError error) -{ - std::fprintf(mErrlog,error.LogString().c_str()); - std::fflush(mErrlog); -} + msg = desc.length() ? FormatStr(mErrorDesc[type],buf) : mErrorDesc[type]; -void ZEngine::FlushErrors() -{ - while(!mErrorQueue.empty()) + if(mErrlog) { - LogError(mErrorQueue.front()); - mErrorQueue.pop(); + std::fprintf(mErrlog,msg.c_str()); + std::fflush(mErrlog); } } diff --git a/src/ZE_ZError.cpp b/src/ZE_ZError.cpp deleted file mode 100755 index 89c2cc6..0000000 --- a/src/ZE_ZError.cpp +++ /dev/null @@ -1,79 +0,0 @@ -/******************************************************************************* - This file is Part of the ZEngine Library for 2D game development. - Copyright (C) 2002, 2003 James Turk - - Licensed under a BSD-style license. - - The maintainer of this library is James Turk (james@conceptofzero.net) - and the home of this Library is http://www.zengine.sourceforge.net -*******************************************************************************/ - -/** - \file ZE_ZError.cpp - \brief Source file for ZError. - - Implementation of ZError, the ZEngine internal error information storage class. -
$Id: ZE_ZError.cpp,v 1.12 2003/08/31 18:34:38 cozman Exp $
- \author James Turk -**/ - -#include "ZE_ZError.h" - -namespace ZE -{ - -std::string ZError::sErrorDesc[ZERR_LAST]; - -void ZError::CreateStringTable() -{ - //create error strings - sErrorDesc[ZERR_NONE] = "No Error. [%s]"; - sErrorDesc[ZERR_SDL_INTERNAL] = "SDL Error. [%s]"; - sErrorDesc[ZERR_SDL_INIT] = "Error Initializing SDL: %s"; - sErrorDesc[ZERR_MIX_INIT] = "Error Initializing SDL_mixer: %s"; - sErrorDesc[ZERR_TTF_INIT] = "Error Initializing SDL_ttf: %s"; - sErrorDesc[ZERR_VIDMODE] = "Error Creating Display: %s"; - sErrorDesc[ZERR_LOAD_IMAGE] = "Failed to load Image: %s"; - sErrorDesc[ZERR_LOAD_SOUND] = "Failed to load Sound: %s"; - sErrorDesc[ZERR_LOAD_MUSIC] = "Failed to load Music: %s"; - sErrorDesc[ZERR_LOAD_FONT] = "Failed to load Font: %s"; - sErrorDesc[ZERR_NOIMAGE] = "Called ZImage::%s with no Image loaded."; - sErrorDesc[ZERR_NOSOUND] = "Called ZSound::%s with no Sound loaded."; - sErrorDesc[ZERR_NOMUSIC] = "Called ZMusic::%s with no Music loaded."; - sErrorDesc[ZERR_NOFONT] = "Called ZFont::%s with no Font loaded."; -} - -ZError::ZError(ZErrorCode code, std::string desc, std::string file, int line) : - rCode(code), rDescription(desc), rFilename(file), rLine(line) -{ -} - -void ZError::Create(ZErrorCode code, std::string desc, std::string file, int line) -{ - rCode = code; - rDescription = desc; - rFilename = file; - rLine = line; -} - -ZErrorCode ZError::Code() const -{ - return rCode; -} - -std::string ZError::LogString() const -{ - std::string msg; - - //if there is a description be sure to integrate it - msg = rDescription.length() ? FormatStr(sErrorDesc[rCode].c_str(),rDescription.c_str()) : sErrorDesc[rCode]; - - if(rLine != 0) //if there is a line (there is also a filename) - return FormatStr(" -%s(%d): %s\n",rFilename.c_str(),rLine,msg.c_str()); - else if(rFilename.length()) //no line, just filename - return FormatStr(" -%s: %s\n",rFilename.c_str(),msg.c_str()); - else //just the message - return FormatStr(" -%s\n",msg.c_str()); -} - -} diff --git a/src/ZE_ZFont.cpp b/src/ZE_ZFont.cpp index d3786c8..d61e7ee 100644 --- a/src/ZE_ZFont.cpp +++ b/src/ZE_ZFont.cpp @@ -1,244 +1,244 @@ -/******************************************************************************* - This file is Part of the ZEngine Library for 2D game development. - Copyright (C) 2002, 2003 James Turk - - Licensed under a BSD-style license. - - The maintainer of this library is James Turk (james@conceptofzero.net) - and the home of this Library is http://www.zengine.sourceforge.net -*******************************************************************************/ - -/** - \file ZE_ZFont.cpp - \brief Source file for ZFont. - - Implementation of ZFont, the basic Font class for ZEngine. -
$Id: ZE_ZFont.cpp,v 1.15 2003/10/12 04:09:46 cozman Exp $
- \author James Turk -**/ - -#include "ZE_ZFont.h" - -#ifdef USE_SDL_TTF - -namespace ZE -{ - -ZFont::ZFont() : - rEngine(ZEngine::GetInstance()), - rFont(NULL) -{ - rColor.r = rColor.g = rColor.b = rColor.unused = 255; - rBGColor.r = rBGColor.g = rBGColor.b = rBGColor.unused = 0; -} - -ZFont::ZFont(std::string filename, int size) : - rEngine(ZEngine::GetInstance()), - rFont(NULL) -{ - rColor.r = rColor.g = rColor.b = rColor.unused = 255; - rBGColor.r = rBGColor.g = rBGColor.b = rBGColor.unused = 0; - Open(filename,size); -} - -ZFont::~ZFont() -{ - Release(); -} - -void ZFont::Open(std::string filename, int size) -{ - Release(); +/******************************************************************************* + This file is Part of the ZEngine Library for 2D game development. + Copyright (C) 2002, 2003 James Turk + + Licensed under a BSD-style license. + + The maintainer of this library is James Turk (james@conceptofzero.net) + and the home of this Library is http://www.zengine.sourceforge.net +*******************************************************************************/ + +/** + \file ZE_ZFont.cpp + \brief Source file for ZFont. + + Implementation of ZFont, the basic Font class for ZEngine. +
$Id: ZE_ZFont.cpp,v 1.16 2003/11/24 02:21:20 cozman Exp $
+ \author James Turk +**/ + +#include "ZE_ZFont.h" + +#ifdef USE_SDL_TTF + +namespace ZE +{ + +ZFont::ZFont() : + rEngine(ZEngine::GetInstance()), + rFont(NULL) +{ + rColor.r = rColor.g = rColor.b = rColor.unused = 255; + rBGColor.r = rBGColor.g = rBGColor.b = rBGColor.unused = 0; +} + +ZFont::ZFont(std::string filename, int size) : + rEngine(ZEngine::GetInstance()), + rFont(NULL) +{ + rColor.r = rColor.g = rColor.b = rColor.unused = 255; + rBGColor.r = rBGColor.g = rBGColor.b = rBGColor.unused = 0; + Open(filename,size); +} + +ZFont::~ZFont() +{ + Release(); +} + +void ZFont::Open(std::string filename, int size) +{ + Release(); rFilename = filename; rFont = TTF_OpenFont(filename.c_str(),size); if(!rFont) - rEngine->ReportError(ZERR_LOAD_FONT,filename); -} - -void ZFont::OpenFromZip(std::string zipname, std::string filename, int size) -{ - SDL_RWops *rw; - - Release(); - rZipname = zipname; - rFilename = filename; - - rw = RWFromZip(zipname,filename); - - if(rw) + rEngine->ReportError(ZERR_LOAD_FONT,filename); +} + +void ZFont::OpenFromZip(std::string zipname, std::string filename, int size) +{ + SDL_RWops *rw; + + Release(); + rZipname = zipname; + rFilename = filename; + + rw = RWFromZip(zipname,filename); + + if(rw) { - rFont = TTF_OpenFontRW(rw,0,size); - //delete []rw->hidden.mem.base; //must free buffer - //SDL_FreeRW(rw); - } - + rFont = TTF_OpenFontRW(rw,0,size); + //delete []rw->hidden.mem.base; //must free buffer + //SDL_FreeRW(rw); + } + if(!rFont) - rEngine->ReportError(ZERR_LOAD_FONT,FormatStr("%s in %s archive",filename.c_str(),zipname.c_str())); -} - -void ZFont::Release() -{ - FreeFont(rFont); -} - -void ZFont::DrawText(std::string text, ZImage &image) const -{ - if(rFont) - { - if(text.length() == 0) - text = " "; - image.Attach(TTF_RenderText_Blended(rFont, text.c_str(), rColor)); - image.SetAlpha(rColor.unused); //the images alpha comes from the SetColor a parameter - } -} - -void ZFont::DrawShadedText(std::string text, ZImage &image) const -{ - if(rFont) - { - if(text.length() == 0) - text = " "; - image.Attach(TTF_RenderText_Shaded(rFont, text.c_str(), rColor, rBGColor)); - image.SetAlpha(rColor.unused); - } -} - -void ZFont::SetColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a) -{ - rColor.r = r; - rColor.g = g; - rColor.b = b; - rColor.unused = a; //used in DrawText and DrawBlendedText -} - -void ZFont::SetBGColor(Uint8 r, Uint8 g, Uint8 b) -{ - rBGColor.r = r; - rBGColor.g = g; - rBGColor.b = b; -} - -void ZFont::SetStyle(bool bold, bool italic, bool underline) -{ - int flags=0; - - if(bold) - flags |= TTF_STYLE_BOLD; - if(italic) - flags |= TTF_STYLE_ITALIC; - if(underline) - flags |= TTF_STYLE_UNDERLINE; - if(!flags) - flags = TTF_STYLE_NORMAL; - - if(rFont) - TTF_SetFontStyle(rFont,flags); - else - rEngine->ReportError(ZERR_NOFONT,"SetStyle"); -} - -void ZFont::Resize(int size) -{ - if(rZipname.length()) - OpenFromZip(rZipname,rFilename,size); - else - Open(rFilename,size); -} - -bool ZFont::IsLoaded() const -{ - return rFont != NULL; -} - -bool ZFont::IsBold() const -{ - if(rFont) - return (TTF_GetFontStyle(rFont) & TTF_STYLE_BOLD) > 0; - else - { - rEngine->ReportError(ZERR_NOFONT, "IsBold"); - return false; - } -} - -bool ZFont::IsItalic() const -{ - if(rFont) - return (TTF_GetFontStyle(rFont) & TTF_STYLE_ITALIC) > 0; - else - { - rEngine->ReportError(ZERR_NOFONT, "IsItalic"); - return false; - } -} - -bool ZFont::IsUnderlined() const -{ - if(rFont) - return (TTF_GetFontStyle(rFont) & TTF_STYLE_UNDERLINE) > 0; - else - { - rEngine->ReportError(ZERR_NOFONT, "IsUnderlined"); - return false; - } -} - -int ZFont::Height() const -{ - if(rFont) - return TTF_FontHeight(rFont); - else - { - rEngine->ReportError(ZERR_NOFONT, "GetHeight"); - return 0; - } -} - -int ZFont::LineSkip() const -{ - if(rFont) - return TTF_FontLineSkip(rFont); - else - { - rEngine->ReportError(ZERR_NOFONT, "GetLineSkip"); - return 0; - } -} - -int ZFont::StringWidth(std::string text) const -{ - int w,h; - - if(rFont) - { - TTF_SizeText(rFont,text.c_str(),&w,&h); - return w; - } - else - { - rEngine->ReportError(ZERR_NOFONT, "GetStringWidth"); - return 0; - } -} - -int ZFont::StringHeight(std::string text) const -{ - int w,h; - - if(rFont) - { - TTF_SizeText(rFont,text.c_str(),&w,&h); - return h; - } - else - { - rEngine->ReportError(ZERR_NOFONT, "GetStringHeight"); - return 0; - } -} - -} - -#endif //USE_SDL_TTF + rEngine->ReportError(ZERR_LOAD_FONT,"%s in %s archive",filename.c_str(),zipname.c_str()); +} + +void ZFont::Release() +{ + FreeFont(rFont); +} + +void ZFont::DrawText(std::string text, ZImage &image) const +{ + if(rFont) + { + if(text.length() == 0) + text = " "; + image.Attach(TTF_RenderText_Blended(rFont, text.c_str(), rColor)); + image.SetAlpha(rColor.unused); //the images alpha comes from the SetColor a parameter + } +} + +void ZFont::DrawShadedText(std::string text, ZImage &image) const +{ + if(rFont) + { + if(text.length() == 0) + text = " "; + image.Attach(TTF_RenderText_Shaded(rFont, text.c_str(), rColor, rBGColor)); + image.SetAlpha(rColor.unused); + } +} + +void ZFont::SetColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a) +{ + rColor.r = r; + rColor.g = g; + rColor.b = b; + rColor.unused = a; //used in DrawText and DrawBlendedText +} + +void ZFont::SetBGColor(Uint8 r, Uint8 g, Uint8 b) +{ + rBGColor.r = r; + rBGColor.g = g; + rBGColor.b = b; +} + +void ZFont::SetStyle(bool bold, bool italic, bool underline) +{ + int flags=0; + + if(bold) + flags |= TTF_STYLE_BOLD; + if(italic) + flags |= TTF_STYLE_ITALIC; + if(underline) + flags |= TTF_STYLE_UNDERLINE; + if(!flags) + flags = TTF_STYLE_NORMAL; + + if(rFont) + TTF_SetFontStyle(rFont,flags); + else + rEngine->ReportError(ZERR_NOFONT,"SetStyle"); +} + +void ZFont::Resize(int size) +{ + if(rZipname.length()) + OpenFromZip(rZipname,rFilename,size); + else + Open(rFilename,size); +} + +bool ZFont::IsLoaded() const +{ + return rFont != NULL; +} + +bool ZFont::IsBold() const +{ + if(rFont) + return (TTF_GetFontStyle(rFont) & TTF_STYLE_BOLD) > 0; + else + { + rEngine->ReportError(ZERR_NOFONT, "IsBold"); + return false; + } +} + +bool ZFont::IsItalic() const +{ + if(rFont) + return (TTF_GetFontStyle(rFont) & TTF_STYLE_ITALIC) > 0; + else + { + rEngine->ReportError(ZERR_NOFONT, "IsItalic"); + return false; + } +} + +bool ZFont::IsUnderlined() const +{ + if(rFont) + return (TTF_GetFontStyle(rFont) & TTF_STYLE_UNDERLINE) > 0; + else + { + rEngine->ReportError(ZERR_NOFONT, "IsUnderlined"); + return false; + } +} + +int ZFont::Height() const +{ + if(rFont) + return TTF_FontHeight(rFont); + else + { + rEngine->ReportError(ZERR_NOFONT, "GetHeight"); + return 0; + } +} + +int ZFont::LineSkip() const +{ + if(rFont) + return TTF_FontLineSkip(rFont); + else + { + rEngine->ReportError(ZERR_NOFONT, "GetLineSkip"); + return 0; + } +} + +int ZFont::StringWidth(std::string text) const +{ + int w,h; + + if(rFont) + { + TTF_SizeText(rFont,text.c_str(),&w,&h); + return w; + } + else + { + rEngine->ReportError(ZERR_NOFONT, "GetStringWidth"); + return 0; + } +} + +int ZFont::StringHeight(std::string text) const +{ + int w,h; + + if(rFont) + { + TTF_SizeText(rFont,text.c_str(),&w,&h); + return h; + } + else + { + rEngine->ReportError(ZERR_NOFONT, "GetStringHeight"); + return 0; + } +} + +} + +#endif //USE_SDL_TTF diff --git a/src/ZE_ZImage.cpp b/src/ZE_ZImage.cpp index 204425b..adab997 100644 --- a/src/ZE_ZImage.cpp +++ b/src/ZE_ZImage.cpp @@ -1,165 +1,165 @@ -/******************************************************************************* - This file is Part of the ZEngine Library for 2D game development. - Copyright (C) 2002, 2003 James Turk - - Licensed under a BSD-style license. - - The maintainer of this library is James Turk (james@conceptofzero.net) - and the home of this Library is http://www.zengine.sourceforge.net -*******************************************************************************/ - -/** - \file ZE_ZImage.cpp - \brief Source file for ZImage. - - Implementation of ZImage, the Image class for ZEngine. -
$Id: ZE_ZImage.cpp,v 1.51 2003/11/14 02:11:50 cozman Exp $
- \author James Turk -**/ - -#include "ZE_ZImage.h" - -namespace ZE -{ - -#if (GFX_BACKEND == ZE_OGL) - +/******************************************************************************* + This file is Part of the ZEngine Library for 2D game development. + Copyright (C) 2002, 2003 James Turk + + Licensed under a BSD-style license. + + The maintainer of this library is James Turk (james@conceptofzero.net) + and the home of this Library is http://www.zengine.sourceforge.net +*******************************************************************************/ + +/** + \file ZE_ZImage.cpp + \brief Source file for ZImage. + + Implementation of ZImage, the Image class for ZEngine. +
$Id: ZE_ZImage.cpp,v 1.52 2003/11/24 02:21:20 cozman Exp $
+ \author James Turk +**/ + +#include "ZE_ZImage.h" + +namespace ZE +{ + +#if (GFX_BACKEND == ZE_OGL) + //from SDL's testgl.c power_of_two int ZImage::PowerOfTwo(int num) -{ - int value = 1; - - while(value < num) //texture coord must be >= input - value <<= 1; //value <<= 1 is the same as value *= 2 - return value; +{ + int value = 1; + + while(value < num) //texture coord must be >= input + value <<= 1; //value <<= 1 is the same as value *= 2 + return value; } //from SDL's testgl.c SDL_GL_LoadTexture -GLuint ZImage::SurfaceToTexture(SDL_Surface *surface, GLfloat *texcoord) -{ - GLuint texture; - int w, h; - SDL_Surface *temp; - SDL_Rect area; - Uint32 saved_flags; - Uint8 saved_alpha; - - //expand width and height to nearest powers of 2 - w = PowerOfTwo(surface->w); - h = PowerOfTwo(surface->h); - texcoord[0] = 0.0f; //min X - texcoord[1] = 0.0f; //min Y - texcoord[2] = (GLfloat)surface->w / w; //max X - texcoord[3] = (GLfloat)surface->h / h; //max Y - - temp = SDL_CreateRGBSurface( - SDL_SWSURFACE, - w, h, - 32, -#if SDL_BYTEORDER == SDL_LIL_ENDIAN //endian specific color masks - 0x000000FF, - 0x0000FF00, - 0x00FF0000, - 0xFF000000 -#else - 0xFF000000, - 0x00FF0000, - 0x0000FF00, - 0x000000FF -#endif - ); - if(!temp) //failure in CreateRGBSurface - return 0; - - //save alpha - saved_flags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK); - saved_alpha = surface->format->alpha; - if((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) - SDL_SetAlpha(surface, 0, 0); - - //copy surface (do not alter passed surface to allow this function to be used in special situations) - area.x = 0; - area.y = 0; - area.w = static_cast(surface->w); - area.h = static_cast(surface->h); - SDL_BlitSurface(surface, &area, temp, &area); - - //restore saved alpha - if((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) - SDL_SetAlpha(surface, saved_flags, saved_alpha); - - //create the OpenGL texture - glGenTextures(1, &texture); - //setup texture parmaters - glBindTexture(GL_TEXTURE_2D, texture); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, temp->pixels); - SDL_FreeSurface(temp); //temp surface no longer needed - - return texture; -} -#endif //GFX_BACKEND == ZE_OGL - -ZImage::ZImage() : - rEngine(ZEngine::GetInstance()), - rImage(NULL), - rAlpha(255) -{ - Release(); -} - -ZImage::ZImage(const ZImage &rhs) : - rEngine(ZEngine::GetInstance()), - rImage(NULL), - rAlpha(rhs.Alpha()) -{ - OpenFromImage(rhs.Surface(),0,0,(Sint16)rhs.Surface()->w,(Sint16)rhs.Surface()->h); -#if (GFX_BACKEND == ZE_OGL) - rWidth = rhs.rWidth; - rHeight = rhs.rHeight; -#endif -} - -ZImage::ZImage(std::string filename) : - rEngine(ZEngine::GetInstance()), - rImage(NULL), - rAlpha(255) -{ - Open(filename); -} - -ZImage::ZImage(SDL_Surface *surface) : - rEngine(ZEngine::GetInstance()), - rImage(NULL), - rAlpha(255) -{ - Attach(surface); -} - -ZImage::ZImage(SDL_Surface *img, Sint16 x, Sint16 y, Sint16 w, Sint16 h) : - rEngine(ZEngine::GetInstance()), - rImage(NULL), - rAlpha(255) -{ - OpenFromImage(img,x,y,w,h); -} - -ZImage::ZImage(const ZImage &img, Sint16 x, Sint16 y, Sint16 w, Sint16 h) : - rEngine(ZEngine::GetInstance()), - rImage(NULL), - rAlpha(255) -{ - OpenFromImage(img.Surface(),x,y,w,h); //call SDL_Surface* version instead of taking the long way -} - -ZImage::~ZImage() -{ - Release(); -} - -void ZImage::Open(std::string filename) -{ +GLuint ZImage::SurfaceToTexture(SDL_Surface *surface, GLfloat *texcoord) +{ + GLuint texture; + int w, h; + SDL_Surface *temp; + SDL_Rect area; + Uint32 saved_flags; + Uint8 saved_alpha; + + //expand width and height to nearest powers of 2 + w = PowerOfTwo(surface->w); + h = PowerOfTwo(surface->h); + texcoord[0] = 0.0f; //min X + texcoord[1] = 0.0f; //min Y + texcoord[2] = (GLfloat)surface->w / w; //max X + texcoord[3] = (GLfloat)surface->h / h; //max Y + + temp = SDL_CreateRGBSurface( + SDL_SWSURFACE, + w, h, + 32, +#if SDL_BYTEORDER == SDL_LIL_ENDIAN //endian specific color masks + 0x000000FF, + 0x0000FF00, + 0x00FF0000, + 0xFF000000 +#else + 0xFF000000, + 0x00FF0000, + 0x0000FF00, + 0x000000FF +#endif + ); + if(!temp) //failure in CreateRGBSurface + return 0; + + //save alpha + saved_flags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK); + saved_alpha = surface->format->alpha; + if((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) + SDL_SetAlpha(surface, 0, 0); + + //copy surface (do not alter passed surface to allow this function to be used in special situations) + area.x = 0; + area.y = 0; + area.w = static_cast(surface->w); + area.h = static_cast(surface->h); + SDL_BlitSurface(surface, &area, temp, &area); + + //restore saved alpha + if((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA) + SDL_SetAlpha(surface, saved_flags, saved_alpha); + + //create the OpenGL texture + glGenTextures(1, &texture); + //setup texture parmaters + glBindTexture(GL_TEXTURE_2D, texture); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, temp->pixels); + SDL_FreeSurface(temp); //temp surface no longer needed + + return texture; +} +#endif //GFX_BACKEND == ZE_OGL + +ZImage::ZImage() : + rEngine(ZEngine::GetInstance()), + rImage(NULL), + rAlpha(255) +{ + Release(); +} + +ZImage::ZImage(const ZImage &rhs) : + rEngine(ZEngine::GetInstance()), + rImage(NULL), + rAlpha(rhs.Alpha()) +{ + OpenFromImage(rhs.Surface(),0,0,(Sint16)rhs.Surface()->w,(Sint16)rhs.Surface()->h); +#if (GFX_BACKEND == ZE_OGL) + rWidth = rhs.rWidth; + rHeight = rhs.rHeight; +#endif +} + +ZImage::ZImage(std::string filename) : + rEngine(ZEngine::GetInstance()), + rImage(NULL), + rAlpha(255) +{ + Open(filename); +} + +ZImage::ZImage(SDL_Surface *surface) : + rEngine(ZEngine::GetInstance()), + rImage(NULL), + rAlpha(255) +{ + Attach(surface); +} + +ZImage::ZImage(SDL_Surface *img, Sint16 x, Sint16 y, Sint16 w, Sint16 h) : + rEngine(ZEngine::GetInstance()), + rImage(NULL), + rAlpha(255) +{ + OpenFromImage(img,x,y,w,h); +} + +ZImage::ZImage(const ZImage &img, Sint16 x, Sint16 y, Sint16 w, Sint16 h) : + rEngine(ZEngine::GetInstance()), + rImage(NULL), + rAlpha(255) +{ + OpenFromImage(img.Surface(),x,y,w,h); //call SDL_Surface* version instead of taking the long way +} + +ZImage::~ZImage() +{ + Release(); +} + +void ZImage::Open(std::string filename) +{ SDL_Surface *image; #ifdef USE_SDL_IMAGE @@ -169,479 +169,478 @@ void ZImage::Open(std::string filename) #endif //USE_SDL_IMAGE if(!image) - rEngine->ReportError(ZERR_LOAD_IMAGE,filename); - else - Attach(image); -} - -void ZImage::OpenFromZip(std::string zipname, std::string filename) -{ - SDL_Surface *image=NULL; - SDL_RWops *rw; - - rw = RWFromZip(zipname,filename); - - if(rw) - { + rEngine->ReportError(ZERR_LOAD_IMAGE,filename); + else + Attach(image); +} + +void ZImage::OpenFromZip(std::string zipname, std::string filename) +{ + SDL_Surface *image=NULL; + SDL_RWops *rw; + + rw = RWFromZip(zipname,filename); + + if(rw) + { #ifdef USE_SDL_IMAGE image = IMG_Load_RW(rw,0); #else image = SDL_LoadBMP_RW(rw,0); -#endif //USE_SDL_IMAGE - delete []rw->hidden.mem.base; //must free buffer - SDL_FreeRW(rw); - } - +#endif //USE_SDL_IMAGE + delete []rw->hidden.mem.base; //must free buffer + SDL_FreeRW(rw); + } + if(!image) - rEngine->ReportError(ZERR_LOAD_IMAGE,FormatStr("%s in %s archive",filename.c_str(),zipname.c_str())); - else - Attach(image); -} - -void ZImage::OpenFromImage(SDL_Surface *image, Sint16 x, Sint16 y, Sint16 w, Sint16 h) -{ - SDL_Surface *screen = rEngine->Display(); - SDL_Surface *cutImg = NULL; - SDL_Rect rect; - SDL_VideoInfo *videoInfo; - - //either set hardware or software surface// - videoInfo = const_cast(SDL_GetVideoInfo()); - - rect.x = x; - rect.y = y; - rect.w = w; - rect.h = h; - - if(!image) - rEngine->ReportError(ZERR_NOIMAGE,"OpenFromImage"); - - cutImg = SDL_CreateRGBSurface(0, rect.w, rect.h, rEngine->DisplayDepth(), - screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, screen->format->Amask); - - if(!cutImg) - rEngine->ReportError(ZERR_SDL_INTERNAL,FormatStr("SDL_CreateRGBSurface failed in ZImage::OpenFromImage: %s.",SDL_GetError())); - - SDL_BlitSurface(image,&rect,cutImg,NULL); - Attach(cutImg); -} - -void ZImage::OpenFromImage(const ZImage &img, Sint16 x, Sint16 y, Sint16 w, Sint16 h) -{ - OpenFromImage(img.Surface(),x,y,w,h); -} - -#if (GFX_BACKEND == ZE_OGL) - -//attach is really the core of ZImage, everything calls it, it converts SDL_Surface->OpenGL Texture->ZImage -void ZImage::Attach(SDL_Surface *surface) -{ - GLfloat coord[4]; - - Release(); //avoid most user inflicted memory leaks associated with ZImage - - if(surface) - { - SDL_Surface *temp = SDL_DisplayFormatAlpha(surface); //TTF_RenderTextBlended relys on this - if(temp) //if conversion succeeds, free old surface - { - FreeImage(surface); - surface = temp; - } - else //can't convert, leave surface as is - { - rEngine->ReportError(ZERR_SDL_INTERNAL,FormatStr("SDL_DisplayFormatAlpha failed in ZImage::Attach: %s",SDL_GetError())); - } - - rWidth = static_cast(surface->w); - rHeight = static_cast(surface->h); - rTexID = SurfaceToTexture(surface,coord); - rTexMinX = coord[0]; - rTexMinY = coord[1]; - rTexMaxX = coord[2]; - rTexMaxY = coord[3]; - rImage = surface; - } - else - rEngine->ReportError(ZERR_NOIMAGE,"Attach"); -} - -void ZImage::Reload() -{ - //this little hack helps to reload images to OpenGL surfaces after loss - SDL_Surface *temp = rImage; - rImage = NULL; - Attach(temp); -} - -void ZImage::Release() -{ - //set everything back the way it came - if(glIsTexture(rTexID)) - glDeleteTextures(1,&rTexID); - rTexMinX = rTexMinY = rTexMaxX = rTexMaxY = 0.0f; - rTexID = 0; - rWidth = rHeight = 0; - FreeImage(rImage); -} - -void ZImage::SetAlpha(Uint8 alpha) -{ - rAlpha = alpha; -} - -void ZImage::SetColorKey(Uint8 red, Uint8 green, Uint8 blue) -{ - Uint32 color; - - if(rImage) - { - color = SDL_MapRGB(rImage->format,red,green,blue); - if(SDL_SetColorKey(rImage, SDL_SRCCOLORKEY, color) < 0) - rEngine->ReportError(ZERR_SDL_INTERNAL,FormatStr("SDL_SetColorKey failed in ZImage::SetColorKey: %s",SDL_GetError())); - else - Reload(); //do the reattach hack, this gets a new OpenGL surface for the same image - } - else - rEngine->ReportError(ZERR_NOIMAGE,"SetColorKey"); -} - -void ZImage::Draw(int x, int y) const -{ - Draw(static_cast(x),static_cast(y)); -} - -void ZImage::DrawClipped(int x, int y, ZRect clipRect) const -{ - DrawClipped(static_cast(x),static_cast(y),clipRect); -} - -void ZImage::Draw(float x, float y) const -{ - glColor4ub(255,255,255,rAlpha); //sets the color correctly - Bind(); - glBegin(GL_TRIANGLE_STRIP); //triangle strips, speedier? - glTexCoord2f(rTexMinX,rTexMinY); glVertex2f(x,y); - glTexCoord2f(rTexMaxX,rTexMinY); glVertex2f(x+rWidth,y); - glTexCoord2f(rTexMinX,rTexMaxY); glVertex2f(x,y+rHeight); - glTexCoord2f(rTexMaxX,rTexMaxY); glVertex2f(x+rWidth,y+rHeight); - glEnd(); - glColor4ub(255,255,255,255); //be responsible, return to standard color state -} - -void ZImage::Draw(float x, float y, Uint8 vc[]) const -{ - Bind(); - glBegin(GL_TRIANGLE_STRIP); //triangle strips, speedier? - glTexCoord2f(rTexMinX,rTexMinY); glColor4ub(vc[0],vc[1],vc[2],vc[3]); glVertex2f(x,y); - glTexCoord2f(rTexMaxX,rTexMinY); glColor4ub(vc[4],vc[5],vc[6],vc[7]); glVertex2f(x+rWidth,y); - glTexCoord2f(rTexMinX,rTexMaxY); glColor4ub(vc[12],vc[13],vc[14],vc[15]); glVertex2f(x,y+rHeight); //12-15 here to keep counterclockwise - glTexCoord2f(rTexMaxX,rTexMaxY); glColor4ub(vc[8],vc[9],vc[10],vc[11]); glVertex2f(x+rWidth,y+rHeight); - glEnd(); - glColor4ub(255,255,255,255); //be responsible, return to standard color state -} - -void ZImage::DrawRotated(int x, int y, float angle) const -{ - DrawRotated(static_cast(x),static_cast(y),angle); -} - -void ZImage::DrawRotated(float x, float y, float angle) const -{ - //center point - float cX,cY; - cX = rWidth/2.0f; - cY = rHeight/2.0f; - - glPushMatrix(); - glTranslatef(x+cX,y+cY,0); //translate to center - glRotatef(angle,0,0,1.0f); //rotate on z axis, to keep x&y parallel to 2D plane - glColor4ub(255,255,255,rAlpha); - Bind(); - //draw is modified to be based around center// - glBegin(GL_TRIANGLE_STRIP); - glTexCoord2f(rTexMinX,rTexMinY); glVertex2f(-cX,-cY); - glTexCoord2f(rTexMaxX,rTexMinY); glVertex2f(-cX+rWidth,-cY); - glTexCoord2f(rTexMinX,rTexMaxY); glVertex2f(-cX,-cY+rHeight); - glTexCoord2f(rTexMaxX,rTexMaxY); glVertex2f(-cX+rWidth,-cY+rHeight); - glEnd(); - glPopMatrix(); -} - -void ZImage::DrawRotated(float x, float y, float angle, Uint8 vc[]) const -{ - //center point - float cX,cY; - cX = rWidth/2.0f; - cY = rHeight/2.0f; - - glPushMatrix(); - glTranslatef(x+cX,y+cY,0); //translate to center - glRotatef(angle,0,0,1.0f); //rotate on z axis, to keep x&y parallel to 2D plane - Bind(); - //draw is modified to be based around center// - glBegin(GL_TRIANGLE_STRIP); - glTexCoord2f(rTexMinX,rTexMinY); glColor4ub(vc[0],vc[1],vc[2],vc[3]); glVertex2f(-cX,-cY); - glTexCoord2f(rTexMaxX,rTexMinY); glColor4ub(vc[4],vc[6],vc[6],vc[7]); glVertex2f(-cX+rWidth,-cY); - glTexCoord2f(rTexMinX,rTexMaxY); glColor4ub(vc[12],vc[13],vc[14],vc[15]); glVertex2f(-cX,-cY+rHeight); - glTexCoord2f(rTexMaxX,rTexMaxY); glColor4ub(vc[8],vc[9],vc[10],vc[11]); glVertex2f(-cX+rWidth,-cY+rHeight); - glEnd(); - glPopMatrix(); -} - -void ZImage::DrawClipped(float x, float y, ZRect clipRect) const -{ - ZRect imgRect(x,y,rWidth,rHeight); - - if(clipRect.Contains(imgRect)) - { - Draw(x,y); - } - else if(clipRect.Intersects(imgRect)) - { - //This is some pretty complex code, it is broken down in 4 steps. - - //Step 1: The intersection rectangle (inRect) is compared to the image rectangle and the overlapping area is found. - ZRect inRect = clipRect.Intersection(imgRect); - - //Step 2: The portion of the image that needs to be drawn is being mapped to triangle strips the same size as the intersection - // of the clipping and image rectangles and then transformed to texture coordinates via xScale and yScale. - // (double is used for needed precision when dealing with the scaling) - double xScale = (rTexMaxX - rTexMinX)*rWidth; //texCoordWidth/imgWidth - double yScale = (rTexMaxY - rTexMinY)*rHeight; //texCoordHeight/imgHeight - double nx = rTexMinX + (inRect.X()-x)/xScale; //cut off left side - double ny = rTexMinY + (inRect.Y()-y)/yScale; //cut off top - double nw = nx + inRect.Width()/xScale; //cut off right side - double nh = ny + inRect.Height()/yScale; //cut off bottom - - glColor4ub(255,255,255,rAlpha); - Bind(); - glBegin(GL_TRIANGLE_STRIP); - //Step 3: The texture coords are modified to only specify the portion of the texture which falls within the clipping rect. - //Step 4: The vertices are changed to the sides of the clipping rectangle in glVertex2f. - glTexCoord2d(nx,ny); glVertex2f(inRect.Left(),inRect.Top()); - glTexCoord2d(nw,ny); glVertex2f(inRect.Right(),inRect.Top()); - glTexCoord2d(nx,nh); glVertex2f(inRect.Left(),inRect.Bottom()); - glTexCoord2d(nw,nh); glVertex2f(inRect.Right(),inRect.Bottom()); - glEnd(); - glColor4ub(255,255,255,255); //be responsible, return to standard color state - } - //otherwise it doesn't contain nor intersect, so nothing should be drawn -} - -void ZImage::DrawClipped(float x, float y, ZRect clipRect, Uint8 vc[]) const -{ - ZRect imgRect(x,y,rWidth,rHeight); - - if(clipRect.Contains(imgRect)) - { - Draw(x,y); - } - else if(clipRect.Intersects(imgRect)) - { - //This is some pretty complex code, it is broken down in 4 steps. - - //Step 1: The intersection rectangle (inRect) is compared to the image rectangle and the overlapping area is found. - ZRect inRect = clipRect.Intersection(imgRect); - - //Step 2: The portion of the image that needs to be drawn is being mapped to triangle strips the same size as the intersection - // of the clipping and image rectangles and then transformed to texture coordinates via xScale and yScale. - // (double is used for needed precision when dealing with the scaling) - double xScale = (rTexMaxX - rTexMinX)*rWidth; //texCoordWidth/imgWidth - double yScale = (rTexMaxY - rTexMinY)*rHeight; //texCoordHeight/imgHeight - double nx = rTexMinX + (inRect.X()-x)/xScale; //cut off left side - double ny = rTexMinY + (inRect.Y()-y)/yScale; //cut off top - double nw = nx + inRect.Width()/xScale; //cut off right side - double nh = ny + inRect.Height()/yScale; //cut off bottom - - Bind(); - glBegin(GL_TRIANGLE_STRIP); - //Step 3: The texture coords are modified to only specify the portion of the texture which falls within the clipping rect. - //Step 4: The vertices are changed to the sides of the clipping rectangle in glVertex2f. - glTexCoord2d(nx,ny); glColor4ub(vc[0],vc[1],vc[2],vc[3]); glVertex2f(inRect.Left(),inRect.Top()); - glTexCoord2d(nw,ny); glColor4ub(vc[4],vc[5],vc[6],vc[7]); glVertex2f(inRect.Right(),inRect.Top()); - glTexCoord2d(nx,nh); glColor4ub(vc[12],vc[13],vc[14],vc[15]); glVertex2f(inRect.Left(),inRect.Bottom()); - glTexCoord2d(nw,nh); glColor4ub(vc[8],vc[9],vc[10],vc[11]); glVertex2f(inRect.Right(),inRect.Bottom()); - glEnd(); - glColor4ub(255,255,255,255); //be responsible, return to standard color state - } - else //doesn't contain nor intersect - { - //draw nothing - } -} - -void ZImage::Flip(bool horizontal, bool vertical) -{ - //all that a flip does is invert the Min/Max coordinates - if(horizontal) - std::swap(rTexMinX,rTexMaxX); - if(vertical) - std::swap(rTexMinY,rTexMaxY); -} - -//stretching and resizing is very inexpensive, done via variables -void ZImage::Stretch(float xFactor, float yFactor) -{ - rWidth = xFactor*rWidth; - rHeight = yFactor*rHeight; -} - -void ZImage::Resize(float width, float height) -{ - rWidth = width; - rHeight = height; -} - -//this is available for other uses of ZEngine -void ZImage::Bind() const -{ - if(rTexID) - glBindTexture(GL_TEXTURE_2D, rTexID); - else - rEngine->ReportError(ZERR_NOIMAGE,"Bind"); - -} - -bool ZImage::IsLoaded() const -{ - return glIsTexture(rTexID) == GL_TRUE; -} - -#elif (GFX_BACKEND == ZE_SDL) - -void ZImage::Attach(SDL_Surface *surface) -{ - Release(); - - if(surface) - { - SDL_Surface *temp = SDL_DisplayFormatAlpha(surface); //TTF_RenderTextBlended relys on this - if(temp) //if conversion succeeds, free old surface - { - FreeImage(surface); - surface = temp; - } - else //can't convert, leave surface as is - { - rEngine->ReportError(ZERR_SDL_INTERNAL,FormatStr("SDL_DisplayFormatAlpha failed in ZImage::Attach: %s",SDL_GetError())); - } - - rImage = surface; - } - else - rEngine->ReportError(ZERR_NOIMAGE,"Attach"); -} - -void ZImage::Reload() -{ - //currently a no-op -} - -void ZImage::Release() -{ - FreeImage(rImage); -} - -void ZImage::SetAlpha(Uint8 alpha) -{ - rAlpha = alpha; - if(rImage) - { - if(SDL_SetAlpha(rImage, SDL_SRCALPHA, alpha) < 0) - rEngine->ReportError(ZERR_SDL_INTERNAL,FormatStr("SDL_SetAlpha failed in ZImage::SetAlpha: %s",SDL_GetError())); - } - else - rEngine->ReportError(ZERR_NOIMAGE,"SetAlpha"); -} - -void ZImage::SetColorKey(Uint8 red, Uint8 green, Uint8 blue) -{ - Uint32 color; - - if(rImage) - { - color = SDL_MapRGBA(rImage->format,red,green,blue,255); - if(SDL_SetColorKey(rImage, SDL_RLEACCEL|SDL_SRCCOLORKEY, color) < 0) - rEngine->ReportError(ZERR_SDL_INTERNAL,FormatStr("SDL_SetColorKey failed in ZImage::SetColorKey: %s",SDL_GetError())); - //surface conversion// - SDL_Surface *temp = rImage; - rImage = SDL_DisplayFormatAlpha(temp); //TTF_RenderTextBlended relys on this - if(rImage) - { - FreeImage(temp); - } - else //can't convert - { - rEngine->ReportError(ZERR_SDL_INTERNAL,FormatStr("SDL_DisplayFormatAlpha failed in ZImage::SetColorKey: %s",SDL_GetError())); - rImage = temp; - } - } - else - rEngine->ReportError(ZERR_NOIMAGE,"SetColorKey"); -} - -void ZImage::Draw(int x, int y) const -{ - SDL_Rect rect; - rect.x = static_cast(x); - rect.y = static_cast(y); - SDL_BlitSurface(rImage, NULL, rEngine->Display(), &rect); -} - -void ZImage::DrawClipped(int x, int y, ZRect clipRect) const -{ - ZRect img(static_cast(x),static_cast(y),static_cast(rImage->w),static_cast(rImage->h)); - SDL_Rect inRect,imgRect; - - imgRect = inRect = clipRect.Intersection(img).SDLrect(); - inRect.x -= x; - inRect.y -= y; - - SDL_BlitSurface(rImage, &inRect, rEngine->Display(), &imgRect); -} - -bool ZImage::IsLoaded() const -{ - return rImage ? true : false; -} - -#endif //GFX_BACKEND - -SDL_Surface* ZImage::Surface() const -{ - return rImage; -} - -#if (GFX_BACKEND == ZE_OGL) - -float ZImage::Width() const -{ - return rWidth; -} - -float ZImage::Height() const -{ - return rHeight; -} - -#elif (GFX_BACKEND == ZE_SDL) - -int ZImage::Width() const -{ - return rImage->w; -} - -int ZImage::Height() const -{ - return rImage->h; -} - -#endif //GFX_BACKEND - -Uint8 ZImage::Alpha() const -{ - return rAlpha; -} - -} + rEngine->ReportError(ZERR_LOAD_IMAGE,"%s in %s archive",filename.c_str(),zipname.c_str()); + else + Attach(image); +} + +void ZImage::OpenFromImage(SDL_Surface *image, Sint16 x, Sint16 y, Sint16 w, Sint16 h) +{ + SDL_Surface *cutImg = NULL; + SDL_Rect rect; + Uint8 oldAlpha; + + rect.x = x; + rect.y = y; + rect.w = w; + rect.h = h; + + if(!image) + rEngine->ReportError(ZERR_NOIMAGE,"OpenFromImage"); + + cutImg = SDL_CreateRGBSurface(0, rect.w, rect.h, image->format->BitsPerPixel, + image->format->Rmask, image->format->Gmask, image->format->Bmask, image->format->Amask); + + if(!cutImg) + rEngine->ReportError(ZERR_SDL_INTERNAL,"SDL_CreateRGBSurface failed in ZImage::OpenFromImage: %s.",SDL_GetError()); + + oldAlpha = image->format->alpha; //store alpha + SDL_SetAlpha(image,0,SDL_ALPHA_OPAQUE); //turn off alpha for RGBA->RGBA copy + SDL_BlitSurface(image,&rect,cutImg,NULL); + SDL_SetAlpha(image,oldAlpha == SDL_ALPHA_OPAQUE ? 0 : SDL_SRCALPHA, oldAlpha); //turn alpha back on iff alpha was not opaque to start + Attach(cutImg); +} + +void ZImage::OpenFromImage(const ZImage &img, Sint16 x, Sint16 y, Sint16 w, Sint16 h) +{ + OpenFromImage(img.Surface(),x,y,w,h); +} + +#if (GFX_BACKEND == ZE_OGL) + +//attach is really the core of ZImage, everything calls it, it converts SDL_Surface->OpenGL Texture->ZImage +void ZImage::Attach(SDL_Surface *surface) +{ + GLfloat coord[4]; + + Release(); //avoid most user inflicted memory leaks associated with ZImage + + if(surface) + { + SDL_Surface *temp = SDL_DisplayFormatAlpha(surface); //TTF_RenderTextBlended relys on this + if(temp) //if conversion succeeds, free old surface + { + FreeImage(surface); + surface = temp; + } + else //can't convert, leave surface as is + { + rEngine->ReportError(ZERR_SDL_INTERNAL,"SDL_DisplayFormatAlpha failed in ZImage::Attach: %s",SDL_GetError()); + } + + rWidth = static_cast(surface->w); + rHeight = static_cast(surface->h); + rTexID = SurfaceToTexture(surface,coord); + rTexMinX = coord[0]; + rTexMinY = coord[1]; + rTexMaxX = coord[2]; + rTexMaxY = coord[3]; + rImage = surface; + } + else + rEngine->ReportError(ZERR_NOIMAGE,"Attach"); +} + +void ZImage::Reload() +{ + //this little hack helps to reload images to OpenGL surfaces after loss + SDL_Surface *temp = rImage; + rImage = NULL; + Attach(temp); +} + +void ZImage::Release() +{ + //set everything back the way it came + if(glIsTexture(rTexID)) + glDeleteTextures(1,&rTexID); + rTexMinX = rTexMinY = rTexMaxX = rTexMaxY = 0.0f; + rTexID = 0; + rWidth = rHeight = 0; + FreeImage(rImage); +} + +void ZImage::SetAlpha(Uint8 alpha) +{ + rAlpha = alpha; +} + +void ZImage::SetColorKey(Uint8 red, Uint8 green, Uint8 blue) +{ + Uint32 color; + + if(rImage) + { + color = SDL_MapRGB(rImage->format,red,green,blue); + if(SDL_SetColorKey(rImage, SDL_SRCCOLORKEY, color) < 0) + rEngine->ReportError(ZERR_SDL_INTERNAL,"SDL_SetColorKey failed in ZImage::SetColorKey: %s",SDL_GetError()); + else + Reload(); //do the reattach hack, this gets a new OpenGL surface for the same image + } + else + rEngine->ReportError(ZERR_NOIMAGE,"SetColorKey"); +} + +void ZImage::Draw(int x, int y) const +{ + Draw(static_cast(x),static_cast(y)); +} + +void ZImage::DrawClipped(int x, int y, ZRect clipRect) const +{ + DrawClipped(static_cast(x),static_cast(y),clipRect); +} + +void ZImage::Draw(float x, float y) const +{ + //glColor4ub(255,255,255,rAlpha); //sets the color correctly + Bind(); + glBegin(GL_TRIANGLE_STRIP); //triangle strips, speedier? + glTexCoord2f(rTexMinX,rTexMinY); glVertex2f(x,y); + glTexCoord2f(rTexMaxX,rTexMinY); glVertex2f(x+rWidth,y); + glTexCoord2f(rTexMinX,rTexMaxY); glVertex2f(x,y+rHeight); + glTexCoord2f(rTexMaxX,rTexMaxY); glVertex2f(x+rWidth,y+rHeight); + glEnd(); + glColor4ub(255,255,255,255); //be responsible, return to standard color state +} + +void ZImage::Draw(float x, float y, Uint8 vc[]) const +{ + Bind(); + glBegin(GL_TRIANGLE_STRIP); //triangle strips, speedier? + glTexCoord2f(rTexMinX,rTexMinY); glColor4ub(vc[0],vc[1],vc[2],vc[3]); glVertex2f(x,y); + glTexCoord2f(rTexMaxX,rTexMinY); glColor4ub(vc[4],vc[5],vc[6],vc[7]); glVertex2f(x+rWidth,y); + glTexCoord2f(rTexMinX,rTexMaxY); glColor4ub(vc[12],vc[13],vc[14],vc[15]); glVertex2f(x,y+rHeight); //12-15 here to keep counterclockwise + glTexCoord2f(rTexMaxX,rTexMaxY); glColor4ub(vc[8],vc[9],vc[10],vc[11]); glVertex2f(x+rWidth,y+rHeight); + glEnd(); + glColor4ub(255,255,255,255); //be responsible, return to standard color state +} + +void ZImage::DrawRotated(int x, int y, float angle) const +{ + DrawRotated(static_cast(x),static_cast(y),angle); +} + +void ZImage::DrawRotated(float x, float y, float angle) const +{ + //center point + float cX,cY; + cX = rWidth/2.0f; + cY = rHeight/2.0f; + + glPushMatrix(); + glTranslatef(x+cX,y+cY,0); //translate to center + glRotatef(angle,0,0,1.0f); //rotate on z axis, to keep x&y parallel to 2D plane + glColor4ub(255,255,255,rAlpha); + Bind(); + //draw is modified to be based around center// + glBegin(GL_TRIANGLE_STRIP); + glTexCoord2f(rTexMinX,rTexMinY); glVertex2f(-cX,-cY); + glTexCoord2f(rTexMaxX,rTexMinY); glVertex2f(-cX+rWidth,-cY); + glTexCoord2f(rTexMinX,rTexMaxY); glVertex2f(-cX,-cY+rHeight); + glTexCoord2f(rTexMaxX,rTexMaxY); glVertex2f(-cX+rWidth,-cY+rHeight); + glEnd(); + glPopMatrix(); +} + +void ZImage::DrawRotated(float x, float y, float angle, Uint8 vc[]) const +{ + //center point + float cX,cY; + cX = rWidth/2.0f; + cY = rHeight/2.0f; + + glPushMatrix(); + glTranslatef(x+cX,y+cY,0); //translate to center + glRotatef(angle,0,0,1.0f); //rotate on z axis, to keep x&y parallel to 2D plane + Bind(); + //draw is modified to be based around center// + glBegin(GL_TRIANGLE_STRIP); + glTexCoord2f(rTexMinX,rTexMinY); glColor4ub(vc[0],vc[1],vc[2],vc[3]); glVertex2f(-cX,-cY); + glTexCoord2f(rTexMaxX,rTexMinY); glColor4ub(vc[4],vc[6],vc[6],vc[7]); glVertex2f(-cX+rWidth,-cY); + glTexCoord2f(rTexMinX,rTexMaxY); glColor4ub(vc[12],vc[13],vc[14],vc[15]); glVertex2f(-cX,-cY+rHeight); + glTexCoord2f(rTexMaxX,rTexMaxY); glColor4ub(vc[8],vc[9],vc[10],vc[11]); glVertex2f(-cX+rWidth,-cY+rHeight); + glEnd(); + glPopMatrix(); +} + +void ZImage::DrawClipped(float x, float y, ZRect clipRect) const +{ + ZRect imgRect(x,y,rWidth,rHeight); + + if(clipRect.Contains(imgRect)) + { + Draw(x,y); + } + else if(clipRect.Intersects(imgRect)) + { + //This is some pretty complex code, it is broken down in 4 steps. + + //Step 1: The intersection rectangle (inRect) is compared to the image rectangle and the overlapping area is found. + ZRect inRect = clipRect.Intersection(imgRect); + + //Step 2: The portion of the image that needs to be drawn is being mapped to triangle strips the same size as the intersection + // of the clipping and image rectangles and then transformed to texture coordinates via xScale and yScale. + // (double is used for needed precision when dealing with the scaling) + double xScale = (rTexMaxX - rTexMinX)*rWidth; //texCoordWidth/imgWidth + double yScale = (rTexMaxY - rTexMinY)*rHeight; //texCoordHeight/imgHeight + double nx = rTexMinX + (inRect.X()-x)/xScale; //cut off left side + double ny = rTexMinY + (inRect.Y()-y)/yScale; //cut off top + double nw = nx + inRect.Width()/xScale; //cut off right side + double nh = ny + inRect.Height()/yScale; //cut off bottom + + glColor4ub(255,255,255,rAlpha); + Bind(); + glBegin(GL_TRIANGLE_STRIP); + //Step 3: The texture coords are modified to only specify the portion of the texture which falls within the clipping rect. + //Step 4: The vertices are changed to the sides of the clipping rectangle in glVertex2f. + glTexCoord2d(nx,ny); glVertex2f(inRect.Left(),inRect.Top()); + glTexCoord2d(nw,ny); glVertex2f(inRect.Right(),inRect.Top()); + glTexCoord2d(nx,nh); glVertex2f(inRect.Left(),inRect.Bottom()); + glTexCoord2d(nw,nh); glVertex2f(inRect.Right(),inRect.Bottom()); + glEnd(); + glColor4ub(255,255,255,255); //be responsible, return to standard color state + } + //otherwise it doesn't contain nor intersect, so nothing should be drawn +} + +void ZImage::DrawClipped(float x, float y, ZRect clipRect, Uint8 vc[]) const +{ + ZRect imgRect(x,y,rWidth,rHeight); + + if(clipRect.Contains(imgRect)) + { + Draw(x,y); + } + else if(clipRect.Intersects(imgRect)) + { + //This is some pretty complex code, it is broken down in 4 steps. + + //Step 1: The intersection rectangle (inRect) is compared to the image rectangle and the overlapping area is found. + ZRect inRect = clipRect.Intersection(imgRect); + + //Step 2: The portion of the image that needs to be drawn is being mapped to triangle strips the same size as the intersection + // of the clipping and image rectangles and then transformed to texture coordinates via xScale and yScale. + // (double is used for needed precision when dealing with the scaling) + double xScale = (rTexMaxX - rTexMinX)*rWidth; //texCoordWidth/imgWidth + double yScale = (rTexMaxY - rTexMinY)*rHeight; //texCoordHeight/imgHeight + double nx = rTexMinX + (inRect.X()-x)/xScale; //cut off left side + double ny = rTexMinY + (inRect.Y()-y)/yScale; //cut off top + double nw = nx + inRect.Width()/xScale; //cut off right side + double nh = ny + inRect.Height()/yScale; //cut off bottom + + Bind(); + glBegin(GL_TRIANGLE_STRIP); + //Step 3: The texture coords are modified to only specify the portion of the texture which falls within the clipping rect. + //Step 4: The vertices are changed to the sides of the clipping rectangle in glVertex2f. + glTexCoord2d(nx,ny); glColor4ub(vc[0],vc[1],vc[2],vc[3]); glVertex2f(inRect.Left(),inRect.Top()); + glTexCoord2d(nw,ny); glColor4ub(vc[4],vc[5],vc[6],vc[7]); glVertex2f(inRect.Right(),inRect.Top()); + glTexCoord2d(nx,nh); glColor4ub(vc[12],vc[13],vc[14],vc[15]); glVertex2f(inRect.Left(),inRect.Bottom()); + glTexCoord2d(nw,nh); glColor4ub(vc[8],vc[9],vc[10],vc[11]); glVertex2f(inRect.Right(),inRect.Bottom()); + glEnd(); + glColor4ub(255,255,255,255); //be responsible, return to standard color state + } + else //doesn't contain nor intersect + { + //draw nothing + } +} + +void ZImage::Flip(bool horizontal, bool vertical) +{ + //all that a flip does is invert the Min/Max coordinates + if(horizontal) + std::swap(rTexMinX,rTexMaxX); + if(vertical) + std::swap(rTexMinY,rTexMaxY); +} + +//stretching and resizing is very inexpensive, done via variables +void ZImage::Stretch(float xFactor, float yFactor) +{ + rWidth = xFactor*rWidth; + rHeight = yFactor*rHeight; +} + +void ZImage::Resize(float width, float height) +{ + rWidth = width; + rHeight = height; +} + +//this is available for other uses of ZEngine +void ZImage::Bind() const +{ + if(rTexID) + glBindTexture(GL_TEXTURE_2D, rTexID); + else + rEngine->ReportError(ZERR_NOIMAGE,"Bind"); + +} + +bool ZImage::IsLoaded() const +{ + return glIsTexture(rTexID) == GL_TRUE; +} + +#elif (GFX_BACKEND == ZE_SDL) + +void ZImage::Attach(SDL_Surface *surface) +{ + Release(); + + if(surface) + { + SDL_Surface *temp = SDL_DisplayFormatAlpha(surface); //TTF_RenderTextBlended relys on this + if(temp) //if conversion succeeds, free old surface + { + FreeImage(surface); + surface = temp; + } + else //can't convert, leave surface as is + { + rEngine->ReportError(ZERR_SDL_INTERNAL,"SDL_DisplayFormatAlpha failed in ZImage::Attach: %s",SDL_GetError())); + } + + rImage = surface; + } + else + rEngine->ReportError(ZERR_NOIMAGE,"Attach"); +} + +void ZImage::Reload() +{ + //currently a no-op +} + +void ZImage::Release() +{ + FreeImage(rImage); +} + +void ZImage::SetAlpha(Uint8 alpha) +{ + rAlpha = alpha; + if(rImage) + { + if(SDL_SetAlpha(rImage, rAlpha == SDL_ALPHA_OPAQUE ? 0 : SDL_SRCALPHA, alpha) < 0) + rEngine->ReportError(ZERR_SDL_INTERNAL,"SDL_SetAlpha failed in ZImage::SetAlpha: %s",SDL_GetError())); + } + else + rEngine->ReportError(ZERR_NOIMAGE,"SetAlpha"); +} + +void ZImage::SetColorKey(Uint8 red, Uint8 green, Uint8 blue) +{ + Uint32 color; + + if(rImage) + { + color = SDL_MapRGBA(rImage->format,red,green,blue,255); + if(SDL_SetColorKey(rImage, SDL_RLEACCEL|SDL_SRCCOLORKEY, color) < 0) + rEngine->ReportError(ZERR_SDL_INTERNAL,"SDL_SetColorKey failed in ZImage::SetColorKey: %s",SDL_GetError())); + //surface conversion// + SDL_Surface *temp = rImage; + rImage = SDL_DisplayFormatAlpha(temp); //TTF_RenderTextBlended relys on this + if(rImage) + { + FreeImage(temp); + } + else //can't convert + { + rEngine->ReportError(ZERR_SDL_INTERNAL,"SDL_DisplayFormatAlpha failed in ZImage::SetColorKey: %s",SDL_GetError())); + rImage = temp; + } + } + else + rEngine->ReportError(ZERR_NOIMAGE,"SetColorKey"); +} + +void ZImage::Draw(int x, int y) const +{ + SDL_Rect rect; + rect.x = static_cast(x); + rect.y = static_cast(y); + SDL_BlitSurface(rImage, NULL, rEngine->Display(), &rect); +} + +void ZImage::DrawClipped(int x, int y, ZRect clipRect) const +{ + ZRect img(static_cast(x),static_cast(y),static_cast(rImage->w),static_cast(rImage->h)); + SDL_Rect inRect,imgRect; + + imgRect = inRect = clipRect.Intersection(img).SDLrect(); + inRect.x -= x; + inRect.y -= y; + + SDL_BlitSurface(rImage, &inRect, rEngine->Display(), &imgRect); +} + +bool ZImage::IsLoaded() const +{ + return rImage ? true : false; +} + +#endif //GFX_BACKEND + +SDL_Surface* ZImage::Surface() const +{ + return rImage; +} + +#if (GFX_BACKEND == ZE_OGL) + +float ZImage::Width() const +{ + return rWidth; +} + +float ZImage::Height() const +{ + return rHeight; +} + +#elif (GFX_BACKEND == ZE_SDL) + +int ZImage::Width() const +{ + return rImage->w; +} + +int ZImage::Height() const +{ + return rImage->h; +} + +#endif //GFX_BACKEND + +Uint8 ZImage::Alpha() const +{ + return rAlpha; +} + +} diff --git a/src/ZE_ZMusic.cpp b/src/ZE_ZMusic.cpp index c7fbb6a..db5329a 100644 --- a/src/ZE_ZMusic.cpp +++ b/src/ZE_ZMusic.cpp @@ -1,165 +1,165 @@ -/******************************************************************************* - This file is Part of the ZEngine Library for 2D game development. - Copyright (C) 2002, 2003 James Turk - - Licensed under a BSD-style license. - - The maintainer of this library is James Turk (james@conceptofzero.net) - and the home of this Library is http://www.zengine.sourceforge.net -*******************************************************************************/ - -/** - \file ZE_ZMusic.cpp - \brief Source file for ZMusic. - - Implementation of ZMusic, the basic Music class for ZEngine. -
$Id: ZE_ZMusic.cpp,v 1.11 2003/09/24 02:03:18 cozman Exp $
- \author James Turk -**/ - -#include "ZE_ZMusic.h" - -#ifdef USE_SDL_MIXER - -namespace ZE -{ - -//ZMusic is a very simple class, each call basically wraps a self-explanatory function of SDL_Mixer -const int ZMusic::LoopInfinite = -1; //constant for infinite, as used by SDL_Mixer - -ZMusic::ZMusic() : - rEngine(ZEngine::GetInstance()), - rMusic(NULL) -{ -} - -ZMusic::ZMusic(std::string filename) : - rEngine(ZEngine::GetInstance()), - rMusic(NULL) -{ - Open(filename); -} - -ZMusic::~ZMusic() -{ - Release(); -} - -void ZMusic::Open(std::string filename) -{ +/******************************************************************************* + This file is Part of the ZEngine Library for 2D game development. + Copyright (C) 2002, 2003 James Turk + + Licensed under a BSD-style license. + + The maintainer of this library is James Turk (james@conceptofzero.net) + and the home of this Library is http://www.zengine.sourceforge.net +*******************************************************************************/ + +/** + \file ZE_ZMusic.cpp + \brief Source file for ZMusic. + + Implementation of ZMusic, the basic Music class for ZEngine. +
$Id: ZE_ZMusic.cpp,v 1.12 2003/11/24 02:21:20 cozman Exp $
+ \author James Turk +**/ + +#include "ZE_ZMusic.h" + +#ifdef USE_SDL_MIXER + +namespace ZE +{ + +//ZMusic is a very simple class, each call basically wraps a self-explanatory function of SDL_Mixer +const int ZMusic::LoopInfinite = -1; //constant for infinite, as used by SDL_Mixer + +ZMusic::ZMusic() : + rEngine(ZEngine::GetInstance()), + rMusic(NULL) +{ +} + +ZMusic::ZMusic(std::string filename) : + rEngine(ZEngine::GetInstance()), + rMusic(NULL) +{ + Open(filename); +} + +ZMusic::~ZMusic() +{ + Release(); +} + +void ZMusic::Open(std::string filename) +{ Release(); rMusic = Mix_LoadMUS(filename.c_str()); if(!rMusic) - rEngine->ReportError(ZERR_LOAD_MUSIC,filename); -} - -void ZMusic::Release() -{ - Mix_HaltMusic(); - FreeMusic(rMusic); -} - -void ZMusic::Play(int loopNum, int fadeTime) const -{ - if(Mix_PlayingMusic()) //stop currently playing music - Mix_HaltMusic(); - - if(rMusic) - { - if(fadeTime) - Mix_FadeInMusic(rMusic, loopNum, fadeTime); - else - Mix_PlayMusic(rMusic, loopNum); - } - else - rEngine->ReportError(ZERR_NOMUSIC, "Play"); -} - -void ZMusic::Pause() const -{ - if(rMusic) - Mix_PauseMusic(); - else - rEngine->ReportError(ZERR_NOMUSIC, "Pause"); -} - -void ZMusic::Unpause() const -{ - if(rMusic) - Mix_ResumeMusic(); - else - rEngine->ReportError(ZERR_NOMUSIC, "Unpause"); -} - -void ZMusic::Rewind() const -{ - if(rMusic) - Mix_RewindMusic(); - else - rEngine->ReportError(ZERR_NOMUSIC, "Rewind"); -} - -void ZMusic::Stop(int fadeTime) const -{ - if(rMusic) - { - if(fadeTime) - Mix_FadeOutMusic(fadeTime); - else - Mix_HaltMusic(); - } - else - rEngine->ReportError(ZERR_NOMUSIC, "Stop"); -} - -void ZMusic::SetVolume(int volume) -{ - if(rMusic) - Mix_VolumeMusic(volume); - else - rEngine->ReportError(ZERR_NOMUSIC, "SetVolume"); -} - -bool ZMusic::IsLoaded() const -{ - return rMusic != NULL; -} - -bool ZMusic::IsPlaying() const -{ - if(rMusic) - return Mix_PlayingMusic() > 0; - else - { - rEngine->ReportError(ZERR_NOMUSIC, "IsPlaying"); - return false; - } -} - -bool ZMusic::IsPaused() const -{ - if(rMusic) - return Mix_PausedMusic() > 0; - else - { - rEngine->ReportError(ZERR_NOMUSIC, "IsPaused"); - return false; - } -} - -int ZMusic::Volume() const -{ - if(rMusic) - return Mix_VolumeMusic(-1); - else - { - rEngine->ReportError(ZERR_NOMUSIC, "GetVolume"); - return false; - } -} - -} - -#endif + rEngine->ReportError(ZERR_LOAD_MUSIC,filename); +} + +void ZMusic::Release() +{ + Mix_HaltMusic(); + FreeMusic(rMusic); +} + +void ZMusic::Play(int loopNum, int fadeTime) const +{ + if(Mix_PlayingMusic()) //stop currently playing music + Mix_HaltMusic(); + + if(rMusic) + { + if(fadeTime) + Mix_FadeInMusic(rMusic, loopNum, fadeTime); + else + Mix_PlayMusic(rMusic, loopNum); + } + else + rEngine->ReportError(ZERR_NOMUSIC, "Play"); +} + +void ZMusic::Pause() const +{ + if(rMusic) + Mix_PauseMusic(); + else + rEngine->ReportError(ZERR_NOMUSIC, "Pause"); +} + +void ZMusic::Unpause() const +{ + if(rMusic) + Mix_ResumeMusic(); + else + rEngine->ReportError(ZERR_NOMUSIC, "Unpause"); +} + +void ZMusic::Rewind() const +{ + if(rMusic) + Mix_RewindMusic(); + else + rEngine->ReportError(ZERR_NOMUSIC, "Rewind"); +} + +void ZMusic::Stop(int fadeTime) const +{ + if(rMusic) + { + if(fadeTime) + Mix_FadeOutMusic(fadeTime); + else + Mix_HaltMusic(); + } + else + rEngine->ReportError(ZERR_NOMUSIC, "Stop"); +} + +void ZMusic::SetVolume(int volume) +{ + if(rMusic) + Mix_VolumeMusic(volume); + else + rEngine->ReportError(ZERR_NOMUSIC, "SetVolume"); +} + +bool ZMusic::IsLoaded() const +{ + return rMusic != NULL; +} + +bool ZMusic::IsPlaying() const +{ + if(rMusic) + return Mix_PlayingMusic() > 0; + else + { + rEngine->ReportError(ZERR_NOMUSIC, "IsPlaying"); + return false; + } +} + +bool ZMusic::IsPaused() const +{ + if(rMusic) + return Mix_PausedMusic() > 0; + else + { + rEngine->ReportError(ZERR_NOMUSIC, "IsPaused"); + return false; + } +} + +int ZMusic::Volume() const +{ + if(rMusic) + return Mix_VolumeMusic(-1); + else + { + rEngine->ReportError(ZERR_NOMUSIC, "GetVolume"); + return false; + } +} + +} + +#endif diff --git a/src/ZE_ZSound.cpp b/src/ZE_ZSound.cpp index 17a76fc..2aeddbd 100644 --- a/src/ZE_ZSound.cpp +++ b/src/ZE_ZSound.cpp @@ -13,7 +13,7 @@ \brief Source file for ZSound. Implementation of ZSound, the basic Sound class for ZEngine. -
$Id: ZE_ZSound.cpp,v 1.13 2003/11/20 02:23:14 cozman Exp $
+
$Id: ZE_ZSound.cpp,v 1.14 2003/11/24 02:21:20 cozman Exp $
\author James Turk **/ @@ -69,7 +69,7 @@ void ZSound::OpenFromZip(std::string zipname, std::string filename) } if(!rSound) - rEngine->ReportError(ZERR_LOAD_SOUND,FormatStr("%s in %s archive",filename.c_str(),zipname.c_str())); + rEngine->ReportError(ZERR_LOAD_SOUND,"%s in %s archive",filename.c_str(),zipname.c_str()); } void ZSound::Release() diff --git a/test/ZTimerTest.cpp b/test/ZTimerTest.cpp index abf5279..9f25dde 100644 --- a/test/ZTimerTest.cpp +++ b/test/ZTimerTest.cpp @@ -9,7 +9,7 @@ This example file is in the public domain, it may be used with no restrictions. and the home of this Library is http://www.zengine.sourceforge.net *******************************************************************************/ -/*$Id: ZTimerTest.cpp,v 1.18 2003/10/21 01:17:35 cozman Exp $*/ +/*$Id: ZTimerTest.cpp,v 1.19 2003/11/24 02:17:32 cozman Exp $*/ #include #include @@ -115,10 +115,10 @@ void Test() } while(!engine->QuitRequested()); //quit only when engine has encountered a quit request } -int main(int argc, char *argv[]) -{ - if(Initialize()) - Test(); - ZEngine::ReleaseInstance(); - return 0; +int main(int argc, char *argv[]) +{ + if(Initialize()) + Test(); + ZEngine::ReleaseInstance(); + return 0; }