From d72a3f4fb556c351b526b818d9c36f5b62d50231 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Mon, 29 Apr 2013 02:50:58 +1000 Subject: [PATCH] Created Image --- client/image.cpp | 31 +++++++++++++++++++++++++++++++ client/image.h | 36 ++++++++++++++++++++++++++++++++++++ client/makefile | 4 ++-- client/surface_manager.cpp | 3 ++- client/unit.cpp | 15 +++++++-------- 5 files changed, 78 insertions(+), 11 deletions(-) create mode 100644 client/image.cpp create mode 100644 client/image.h diff --git a/client/image.cpp b/client/image.cpp new file mode 100644 index 0000000..eca7239 --- /dev/null +++ b/client/image.cpp @@ -0,0 +1,31 @@ +#include "image.h" + +Image::Image(SDL_Surface* p) { + SetSurface(p); +} + +Image::Image(SDL_Surface* p, SDL_Rect r) { + SetSurface(p, r); +} + +SDL_Surface* Image::SetSurface(SDL_Surface* p) { + surface = p; + clip = {0, 0, (Uint16)surface->w, (Uint16)surface->h}; + return surface; +} + +SDL_Surface* Image::SetSurface(SDL_Surface* p, SDL_Rect r) { + surface = p; + clip = r; + return surface; +} + +SDL_Surface* Image::GetSurface() const { + return surface; +} + +void Image::DrawTo(SDL_Surface* dest, Sint16 x, Sint16 y) { + SDL_Rect sclip = clip, dclip = {x,y}; + + SDL_BlitSurface(surface, &sclip, dest, &dclip); +} diff --git a/client/image.h b/client/image.h new file mode 100644 index 0000000..fddf2b8 --- /dev/null +++ b/client/image.h @@ -0,0 +1,36 @@ +#ifndef IMAGE_H_ +#define IMAGE_H_ + +#include "SDL/SDL.h" + +class Image { +public: + Image(SDL_Surface*); + Image(SDL_Surface*, SDL_Rect); + virtual ~Image() {} + + SDL_Surface* SetSurface(SDL_Surface*); + SDL_Surface* SetSurface(SDL_Surface*, SDL_Rect); + SDL_Surface* GetSurface() const; + + void DrawTo(SDL_Surface*, Sint16 x, Sint16 y); + + //Clip handlers + SDL_Rect SetClip(SDL_Rect r) { return clip = r; } + SDL_Rect GetClip() const { return clip; } + + Sint16 SetClipX(Sint16 x) { return clip.x = x; } + Sint16 SetClipY(Sint16 y) { return clip.y = y; } + Uint16 SetClipW(Uint16 w) { return clip.w = w; } + Uint16 SetClipH(Uint16 h) { return clip.h = h; } + + Sint16 GetClipX() const { return clip.x; } + Sint16 GetClipY() const { return clip.y; } + Uint16 GetClipW() const { return clip.w; } + Uint16 GetClipH() const { return clip.h; } +private: + SDL_Surface* surface; + SDL_Rect clip; +}; + +#endif diff --git a/client/makefile b/client/makefile index 6b22238..16b713a 100644 --- a/client/makefile +++ b/client/makefile @@ -1,7 +1,7 @@ CXXFLAGS+=-std=c++11 -DDEBUG LIB=-lmingw32 -lSDL_net -lSDLmain -lSDL -lwsock32 -liphlpapi OBJ=base_scene.o scene_manager.o main.o -SRC=test_systems.cpp surface_manager.cpp +SRC=test_systems.cpp surface_manager.cpp image.cpp all: debug @@ -15,4 +15,4 @@ clean: -$(RM) *.o *.a *.exe unit: - $(CXX) $(CXXFLAGS) unit.cpp surface_manager.cpp -lmingw32 -lSDLmain -lSDL + $(CXX) $(CXXFLAGS) unit.cpp surface_manager.cpp image.cpp -lmingw32 -lSDLmain -lSDL diff --git a/client/surface_manager.cpp b/client/surface_manager.cpp index 5730b02..b8ea466 100644 --- a/client/surface_manager.cpp +++ b/client/surface_manager.cpp @@ -58,5 +58,6 @@ SDL_Surface* SurfaceManager::LoadSurface(std::string key, std::string fname) { if (ptr == nullptr) { throw(std::runtime_error(std::string("Failed to load file: ") + fname)); } + SDL_SetColorKey(ptr, SDL_SRCCOLORKEY, SDL_MapRGB(ptr->format, 255, 0, 255)); //default return surfaceMap[key] = ptr; -} \ No newline at end of file +} diff --git a/client/unit.cpp b/client/unit.cpp index 15c7946..171a65f 100644 --- a/client/unit.cpp +++ b/client/unit.cpp @@ -1,3 +1,4 @@ +#include "image.h" #include "surface_manager.h" #include "SDL/SDL.h" @@ -14,9 +15,13 @@ int go(int, char**) { SurfaceManager sMgr; + //load all resources sMgr.Load("player", "rsc/graphics/sprites/elliot2.bmp"); sMgr.Load("tileset", "rsc/graphics/tilesets/MishMash.bmp"); + Image player(sMgr.Get("player")); + Image tiles(sMgr.Get("tileset")); + bool running = true; while (running) { SDL_Event event; @@ -30,20 +35,14 @@ int go(int, char**) { case SDLK_ESCAPE: running = false; break; - case SDLK_1: - sMgr.Reload("tileset","rsc/graphics/tilesets/terrain.bmp"); - break; - case SDLK_2: - sMgr.Reload("tileset","rsc/graphics/tilesets/MishMash.bmp"); - break; } } } SDL_FillRect(screen, 0, 0); - SDL_BlitSurface(sMgr.Get("tileset"), NULL, screen, NULL); - SDL_BlitSurface(sMgr.Get("player"), NULL, screen, NULL); + tiles.DrawTo(screen, 0, 0); + player.DrawTo(screen, 0, 0); SDL_Flip(screen); }