diff --git a/Hearts/image.cpp b/Hearts/image.cpp index d6820e8..a81d70e 100644 --- a/Hearts/image.cpp +++ b/Hearts/image.cpp @@ -1,7 +1,7 @@ /* File Name: image.cpp * Author: Kayne Ruse - * Date (dd/mm/yyyy): 20/06/2011 - * Copyright: (c) Kayne Ruse 2011, 2012 + * Date (dd/mm/yyyy): 14/12/2012 + * Copyright: (c) Kayne Ruse 2012 * * This software is provided 'as-is', without any express or implied * warranty. In no event will the authors be held liable for any damages @@ -23,152 +23,93 @@ * distribution. * * Description: - * The Image class, a wrapper for SDL_Surface and other drawing functionality. - * Part of the KAGE Game Engine. + * ... */ -#include #include "image.h" -using namespace std; -using namespace KAGE; + +#include //------------------------- //Public access members //------------------------- -Image::Image(const char* fileName, int x, int y, int w, int h): - localSurface(true) -{ - //Create a local surface from a file - surface = SDL_LoadBMP(fileName); - - if (surface == NULL) { - cout << "Error in Image::Image()" << endl; - cout << "SDL_LoadBMP() returned NULL" << endl; - cout << "fileName: " << fileName << endl; - return; - } - - SetColorKey(255,0,255); - Init(x,y,0,0,w,h); -} - -Image::Image(SDL_Surface* surf, int x, int y, int w, int h): - localSurface(false) -{ - //Use a surface passed by the calling function - surface = surf; - Init(x,y,0,0,w,h); -} - -void Image::Init(int x, int y, int sx, int sy, int sw, int sh) { - //Set up the variables - dclip.x = x; - dclip.y = y; - sclip.x = sx; - sclip.y = sy; - sclip.w = (sw == 0) ? surface->w : sw; - sclip.h = (sh == 0) ? surface->h : sh; +Image::Image() { + m_clip.x = m_clip.y = m_clip.w = m_clip.h = 0; + m_pSurface = NULL; + m_bLocal = false; } Image::~Image() { - if (localSurface) - SDL_FreeSurface(surface); + UnloadSurface(); } //------------------------- -//Graphical members +//Surface handlers //------------------------- -void Image::Draw(SDL_Surface* dest, int camX, int camY) { - SDL_Rect tmp = dclip; - tmp.x += camX; - tmp.y += camY; - SDL_BlitSurface(surface,&sclip,dest,&tmp); +void Image::LoadSurface(const char* fname) { + SDL_Surface* pSurface; + + if ( (pSurface = SDL_LoadBMP(fname)) == NULL) + throw(std::exception("Failed to load surface")); + + UnloadSurface(); + + SetSurface(pSurface); + + m_bLocal = true; + + SetColorKey(255, 0, 255); } -int Image::SetColorKey(int red, int green, int blue) { - if (localSurface) - return SDL_SetColorKey(surface,SDL_SRCCOLORKEY,SDL_MapRGB(surface->format,red,green,blue)); - else - return -2; +void Image::UnloadSurface() { + if (m_bLocal) { + SDL_FreeSurface(m_pSurface); + m_bLocal = false; + } + + m_pSurface = NULL; } -int Image::ClearColorKey() { - if (localSurface) - return SDL_SetColorKey(surface,0,0); - else - return -2; -} +SDL_Surface* Image::SetSurface(SDL_Surface* pSurface) { + if (m_bLocal) + throw(std::exception("Failed to set surface, Image has a local surface")); -//------------------------- -//Accessors and mutators -//------------------------- + if (!pSurface) + throw(std::exception("Failed to set surface, given surface is NULL")); -void Image::SetPos(int x, int y) { - dclip.x = x; - dclip.y = y; -} + m_pSurface = pSurface; -int Image::SetX(int x) { - return dclip.x = x; -} + m_clip.x = 0; + m_clip.y = 0; + m_clip.w = m_pSurface->w; + m_clip.h = m_pSurface->h; -int Image::SetY(int y) { - return dclip.y = y; -} + m_bLocal = false; -int Image::SetClipX(int x) { - return sclip.x = x; -} - -int Image::SetClipY(int y) { - return sclip.y = y; -} - -int Image::SetWidth(int w) { - return sclip.w = w; -} - -int Image::SetHeight(int h) { - return sclip.h = h; -} - -int Image::GetX() { - return dclip.x; -} - -int Image::GetY() { - return dclip.y; -} - -int Image::GetClipX() { - return sclip.x; -} - -int Image::GetClipY() { - return sclip.y; -} - -int Image::GetWidth() { - return sclip.w; -} - -int Image::GetHeight() { - return sclip.h; -} - -//------------------------- -//Protected access members -//------------------------- - -SDL_Surface* Image::SetSurface(SDL_Surface* surf) { - return surface = surf; + return m_pSurface; } SDL_Surface* Image::GetSurface() { - return surface; + return m_pSurface; } -bool Image::LocalSurface() { - return localSurface; +void Image::SetColorKey(Uint8 r, Uint8 g, Uint8 b) { + if (m_pSurface != NULL) + SDL_SetColorKey(m_pSurface, SDL_SRCCOLORKEY, SDL_MapRGB(m_pSurface->format, r, g, b)); +} + +void Image::ClearColorKey() { + if (m_pSurface != NULL) + SDL_SetColorKey(m_pSurface, 0, 0); +} + +//------------------------- +//Rendering +//------------------------- + +void Image::DrawTo(SDL_Surface* const pDest, Sint16 x, Sint16 y) { + SDL_Rect sclip = m_clip, dclip = {x,y}; + + SDL_BlitSurface(m_pSurface, &sclip, pDest, &dclip); } diff --git a/Hearts/image.h b/Hearts/image.h index a7cf1da..5cc00cc 100644 --- a/Hearts/image.h +++ b/Hearts/image.h @@ -1,7 +1,7 @@ /* File Name: image.h * Author: Kayne Ruse - * Date (dd/mm/yyyy): 20/06/2011 - * Copyright: (c) Kayne Ruse 2011, 2012 + * Date (dd/mm/yyyy): 14/12/2012 + * Copyright: (c) Kayne Ruse 2012 * * This software is provided 'as-is', without any express or implied * warranty. In no event will the authors be held liable for any damages @@ -23,57 +23,53 @@ * distribution. * * Description: - * The Image class, a wrapper for SDL_Surface and other drawing functionality. - * Part of the KAGE Game Engine. + * ... */ -#ifndef KR_KAGE_IMAGE_H_ -#define KR_KAGE_IMAGE_H_ +#ifndef KR_IMAGE_H_ +#define KR_IMAGE_H_ 2012121401 #include "SDL.h" -namespace KAGE { - class Image { - public: - /* Public access members */ - Image(const char* fileName, int x = 0, int y = 0, int w = 0, int h = 0); - Image(SDL_Surface* surface, int x = 0, int y = 0, int w = 0, int h = 0); - ~Image(); +class Image { +public: + /* Public access members */ + Image(); + virtual ~Image(); - /* Graphical members */ - virtual void Draw(SDL_Surface* dest, int camX = 0, int camY = 0); + /* Surface handlers */ + void LoadSurface(const char* fname); + void UnloadSurface(); - int SetColorKey(int red, int green, int blue); - int ClearColorKey(); + SDL_Surface* SetSurface(SDL_Surface*); + SDL_Surface* GetSurface(); - /* Accessors and mutators */ - virtual void SetPos(int x, int y); - virtual int SetX(int); - virtual int SetY(int); - virtual int SetClipX(int); - virtual int SetClipY(int); - virtual int SetWidth(int); - virtual int SetHeight(int); + void SetColorKey(Uint8 r, Uint8 g, Uint8 b); + void ClearColorKey(); - virtual int GetX(); - virtual int GetY(); - virtual int GetClipX(); - virtual int GetClipY(); - virtual int GetWidth(); - virtual int GetHeight(); - protected: - /* Protected access members */ - void Init(int x, int y, int sx, int sy, int sw, int sh); + /* Rendering */ + virtual void DrawTo(SDL_Surface* const, Sint16 x, Sint16 y); - SDL_Surface* SetSurface (SDL_Surface* surf); - SDL_Surface* GetSurface (); - bool LocalSurface (); - private: - /* Private access members */ - SDL_Surface* surface; - SDL_Rect sclip; - SDL_Rect dclip; - const bool localSurface; - }; -} + /* Clip handlers */ + SDL_Rect SetClip(SDL_Rect r) { return m_clip = r; } + SDL_Rect GetClip() { return m_clip; } + + Sint16 SetClipX(Sint16 x) { return m_clip.x = x; } + Sint16 SetClipY(Sint16 y) { return m_clip.y = y; } + Uint16 SetClipW(Uint16 w) { return m_clip.w = w; } + Uint16 SetClipH(Uint16 h) { return m_clip.h = h; } + + Sint16 GetClipX() { return m_clip.x; } + Sint16 GetClipY() { return m_clip.y; } + Uint16 GetClipW() { return m_clip.w; } + Uint16 GetClipH() { return m_clip.h; } + + bool IsLoaded() { return m_pSurface != NULL; } + bool IsLocal() { return m_bLocal; } + +private: + SDL_Rect m_clip; + SDL_Surface* m_pSurface; + bool m_bLocal; +}; #endif diff --git a/Hearts/image_manager.cpp b/Hearts/image_manager.cpp deleted file mode 100644 index 8e76749..0000000 --- a/Hearts/image_manager.cpp +++ /dev/null @@ -1,74 +0,0 @@ -/* File Name: image_manager.cpp - * Author: Kayne Ruse - * Date (dd/mm/yyyy): 20/06/2011 - * Copyright: (c) Kayne Ruse 2011, 2012 - * - * This software is provided 'as-is', without any express or implied - * warranty. In no event will the authors be held liable for any damages - * arising from the use of this software. - * - * Permission is granted to anyone to use this software for any purpose, - * including commercial applications, and to alter it and redistribute it - * freely, subject to the following restrictions: - * - * 1. The origin of this software must not be misrepresented; you must not - * claim that you wrote the original software. If you use this software - * in a product, an acknowledgment in the product documentation would be - * appreciated but is not required. - * - * 2. Altered source versions must be plainly marked as such, and must not be - * misrepresented as being the original software. - * - * 3. This notice may not be removed or altered from any source - * distribution. - * - * Description: - * The Image Manager creates, organises and cleans up the Images in the game. - * Part of the KAGE Game Engine. -*/ -#include -#include "image_manager.h" -using namespace std; -using namespace KAGE; - -//------------------------- -//Public access members -//------------------------- - -Image* ImageManager::Add(const char* name,const char* fName, int x, int y, int w, int h) { - if (Exists(name)) { - cout << "Error in ImageManager::Add(name,fName)" << endl; - cout << "An Image of that name already exists" << endl; - cout << "name: " << name << endl; - return NULL; - } - - return NamedManager::Add(name,new Image(fName,x,y,w,h)); -} - -Image* ImageManager::Add(const char* name,SDL_Surface* surface, int x, int y, int w, int h) { - if (Exists(name)) { - cout << "Error in ImageManager::Add(name,surface)" << endl; - cout << "An Image of that name already exists" << endl; - cout << "name: " << name << endl; - return NULL; - } - - return NamedManager::Add(name,new Image(surface,x,y,w,h)); -} - -Image* ImageManager::Add(const char *name,Image* image) { - if (Exists(name)) { - cout << "Error in ImageManager::Add(name,image)" << endl; - cout << "An Image of that name already exists" << endl; - cout << "name: " << name << endl; - return NULL; - } - - return NamedManager::Add(name,image); -} - -void ImageManager::DrawAll(SDL_Surface* dest, int camX, int camY) { - for (MapType::iterator it = mapList.begin(); it != mapList.end(); it++) - it->second->Draw(dest,camX,camY); -} diff --git a/Hearts/image_manager.h b/Hearts/image_manager.h deleted file mode 100644 index bb0210b..0000000 --- a/Hearts/image_manager.h +++ /dev/null @@ -1,47 +0,0 @@ -/* File Name: image_manager.h - * Author: Kayne Ruse - * Date (dd/mm/yyyy): 20/06/2011 - * Copyright: (c) Kayne Ruse 2011, 2012 - * - * This software is provided 'as-is', without any express or implied - * warranty. In no event will the authors be held liable for any damages - * arising from the use of this software. - * - * Permission is granted to anyone to use this software for any purpose, - * including commercial applications, and to alter it and redistribute it - * freely, subject to the following restrictions: - * - * 1. The origin of this software must not be misrepresented; you must not - * claim that you wrote the original software. If you use this software - * in a product, an acknowledgment in the product documentation would be - * appreciated but is not required. - * - * 2. Altered source versions must be plainly marked as such, and must not be - * misrepresented as being the original software. - * - * 3. This notice may not be removed or altered from any source - * distribution. - * - * Description: - * The Image Manager creates, organises and cleans up the Images in the game. - * Part of the KAGE Game Engine. -*/ -#ifndef KR_KAGE_IMAGEMANAGER_H_ -#define KR_KAGE_IMAGEMANAGER_H_ - -#include "named_manager.h" -#include "image.h" - -namespace KAGE { - class ImageManager : public NamedManager { - public: - /* Public access members */ - Image* Add(const char* name, const char* fileName, int x = 0, int y = 0, int w = 0, int h = 0); - Image* Add(const char* name, SDL_Surface* surface, int x = 0, int y = 0, int w = 0, int h = 0); - Image* Add(const char* name, Image* image); - - void DrawAll(SDL_Surface* dest, int camX = 0, int camY = 0); - }; -} - -#endif