Switched to a newer grapgics system

Not using Codebase as a submodule, since this project is so small anyway.
This commit is contained in:
Kayne Ruse
2013-01-21 03:06:01 +11:00
parent a0a376b5bc
commit c407add405
4 changed files with 103 additions and 287 deletions

View File

@@ -1,7 +1,7 @@
/* File Name: image.cpp /* File Name: image.cpp
* Author: Kayne Ruse * Author: Kayne Ruse
* Date (dd/mm/yyyy): 20/06/2011 * Date (dd/mm/yyyy): 14/12/2012
* Copyright: (c) Kayne Ruse 2011, 2012 * Copyright: (c) Kayne Ruse 2012
* *
* This software is provided 'as-is', without any express or implied * This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages * warranty. In no event will the authors be held liable for any damages
@@ -23,152 +23,93 @@
* distribution. * distribution.
* *
* Description: * Description:
* The Image class, a wrapper for SDL_Surface and other drawing functionality. * ...
* Part of the KAGE Game Engine.
*/ */
#include <iostream>
#include "image.h" #include "image.h"
using namespace std;
using namespace KAGE; #include <exception>
//------------------------- //-------------------------
//Public access members //Public access members
//------------------------- //-------------------------
Image::Image(const char* fileName, int x, int y, int w, int h): Image::Image() {
localSurface(true) m_clip.x = m_clip.y = m_clip.w = m_clip.h = 0;
{ m_pSurface = NULL;
//Create a local surface from a file m_bLocal = false;
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() { Image::~Image() {
if (localSurface) UnloadSurface();
SDL_FreeSurface(surface);
} }
//------------------------- //-------------------------
//Graphical members //Surface handlers
//------------------------- //-------------------------
void Image::Draw(SDL_Surface* dest, int camX, int camY) { void Image::LoadSurface(const char* fname) {
SDL_Rect tmp = dclip; SDL_Surface* pSurface;
tmp.x += camX;
tmp.y += camY; if ( (pSurface = SDL_LoadBMP(fname)) == NULL)
SDL_BlitSurface(surface,&sclip,dest,&tmp); 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) { void Image::UnloadSurface() {
if (localSurface) if (m_bLocal) {
return SDL_SetColorKey(surface,SDL_SRCCOLORKEY,SDL_MapRGB(surface->format,red,green,blue)); SDL_FreeSurface(m_pSurface);
else m_bLocal = false;
return -2; }
m_pSurface = NULL;
} }
int Image::ClearColorKey() { SDL_Surface* Image::SetSurface(SDL_Surface* pSurface) {
if (localSurface) if (m_bLocal)
return SDL_SetColorKey(surface,0,0); throw(std::exception("Failed to set surface, Image has a local surface"));
else
return -2;
}
//------------------------- if (!pSurface)
//Accessors and mutators throw(std::exception("Failed to set surface, given surface is NULL"));
//-------------------------
void Image::SetPos(int x, int y) { m_pSurface = pSurface;
dclip.x = x;
dclip.y = y;
}
int Image::SetX(int x) { m_clip.x = 0;
return dclip.x = x; m_clip.y = 0;
} m_clip.w = m_pSurface->w;
m_clip.h = m_pSurface->h;
int Image::SetY(int y) { m_bLocal = false;
return dclip.y = y;
}
int Image::SetClipX(int x) { return m_pSurface;
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;
} }
SDL_Surface* Image::GetSurface() { SDL_Surface* Image::GetSurface() {
return surface; return m_pSurface;
} }
bool Image::LocalSurface() { void Image::SetColorKey(Uint8 r, Uint8 g, Uint8 b) {
return localSurface; 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);
} }

View File

@@ -1,7 +1,7 @@
/* File Name: image.h /* File Name: image.h
* Author: Kayne Ruse * Author: Kayne Ruse
* Date (dd/mm/yyyy): 20/06/2011 * Date (dd/mm/yyyy): 14/12/2012
* Copyright: (c) Kayne Ruse 2011, 2012 * Copyright: (c) Kayne Ruse 2012
* *
* This software is provided 'as-is', without any express or implied * This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages * warranty. In no event will the authors be held liable for any damages
@@ -23,57 +23,53 @@
* distribution. * distribution.
* *
* Description: * Description:
* The Image class, a wrapper for SDL_Surface and other drawing functionality. * ...
* Part of the KAGE Game Engine.
*/ */
#ifndef KR_KAGE_IMAGE_H_ #ifndef KR_IMAGE_H_
#define KR_KAGE_IMAGE_H_ #define KR_IMAGE_H_ 2012121401
#include "SDL.h" #include "SDL.h"
namespace KAGE { class Image {
class Image { public:
public: /* Public access members */
/* Public access members */ Image();
Image(const char* fileName, int x = 0, int y = 0, int w = 0, int h = 0); virtual ~Image();
Image(SDL_Surface* surface, int x = 0, int y = 0, int w = 0, int h = 0);
~Image();
/* Graphical members */ /* Surface handlers */
virtual void Draw(SDL_Surface* dest, int camX = 0, int camY = 0); void LoadSurface(const char* fname);
void UnloadSurface();
int SetColorKey(int red, int green, int blue); SDL_Surface* SetSurface(SDL_Surface*);
int ClearColorKey(); SDL_Surface* GetSurface();
/* Accessors and mutators */ void SetColorKey(Uint8 r, Uint8 g, Uint8 b);
virtual void SetPos(int x, int y); void ClearColorKey();
virtual int SetX(int);
virtual int SetY(int);
virtual int SetClipX(int);
virtual int SetClipY(int);
virtual int SetWidth(int);
virtual int SetHeight(int);
virtual int GetX(); /* Rendering */
virtual int GetY(); virtual void DrawTo(SDL_Surface* const, Sint16 x, Sint16 y);
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);
SDL_Surface* SetSurface (SDL_Surface* surf); /* Clip handlers */
SDL_Surface* GetSurface (); SDL_Rect SetClip(SDL_Rect r) { return m_clip = r; }
bool LocalSurface (); SDL_Rect GetClip() { return m_clip; }
private:
/* Private access members */ Sint16 SetClipX(Sint16 x) { return m_clip.x = x; }
SDL_Surface* surface; Sint16 SetClipY(Sint16 y) { return m_clip.y = y; }
SDL_Rect sclip; Uint16 SetClipW(Uint16 w) { return m_clip.w = w; }
SDL_Rect dclip; Uint16 SetClipH(Uint16 h) { return m_clip.h = h; }
const bool localSurface;
}; 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 #endif

View File

@@ -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 <iostream>
#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);
}

View File

@@ -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<Image> {
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