Created Image
This commit is contained in:
@@ -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);
|
||||
}
|
||||
@@ -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
|
||||
+2
-2
@@ -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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
+7
-8
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user