Created SurfaceManager

This commit is contained in:
Kayne Ruse
2013-04-28 23:04:17 +10:00
parent 79304f24b8
commit 1226fa08ca
8 changed files with 189 additions and 5 deletions
+1
View File
@@ -8,6 +8,7 @@
#Directories
Release/
Debug/
client/rsc/
#Project generated files
*.db
+6
View File
@@ -6,6 +6,9 @@
using namespace std;
int main(int, char**) {
#ifdef DEBUG
cout << "Beginning program" << endl;
#endif
SceneManager app;
try {
app.Init();
@@ -16,5 +19,8 @@ int main(int, char**) {
cerr << "Fatal error: " << e.what() << endl;
return 1;
}
#ifdef DEBUG
cout << "Clean exit" << endl;
#endif
return 0;
}
+4 -5
View File
@@ -1,8 +1,7 @@
CXXFLAGS+=-std=c++11
DFLAGS=-DDEBUG
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
SRC=test_systems.cpp surface_manager.cpp
all: debug
@@ -10,10 +9,10 @@ release: $(OBJ)
$(CXX) $(CXXFLAGS) $(SRC) $(OBJ) $(LIB)
debug: $(OBJ)
$(CXX) $(CXXFLAGS) $(DFLAGS) $(SRC) $(OBJ) $(LIB)
$(CXX) $(CXXFLAGS) $(SRC) $(OBJ) $(LIB)
clean:
-$(RM) *.o *.a *.exe
unit:
$(CXX) $(CXXFLAGS) unit.cpp
$(CXX) $(CXXFLAGS) unit.cpp surface_manager.cpp -lmingw32 -lSDLmain -lSDL
+62
View File
@@ -0,0 +1,62 @@
#include "surface_manager.h"
#include <stdexcept>
SurfaceManager::SurfaceManager() {
//
}
SurfaceManager::~SurfaceManager() {
for (auto it : surfaceMap) {
SDL_FreeSurface(it.second);
}
surfaceMap.clear();
}
SDL_Surface* SurfaceManager::Load(std::string key, std::string fname) {
mapType::iterator it = surfaceMap.find(key);
if (it != surfaceMap.end()) {
throw(std::runtime_error(std::string("Surface already loaded: ") + key + std::string(", ") + fname));
}
return LoadSurface(key, fname);
}
SDL_Surface* SurfaceManager::Reload(std::string key, std::string fname) {
mapType::iterator it = surfaceMap.find(key);
if (it != surfaceMap.end()) {
SDL_FreeSurface(it->second);
surfaceMap.erase(it);
}
return LoadSurface(key, fname);
}
SDL_Surface* SurfaceManager::Get(std::string key) {
mapType::iterator it = surfaceMap.find(key);
if (it == surfaceMap.end()) {
throw(std::runtime_error(std::string("Could not find key: ") + key));
}
return it->second;
}
SDL_Surface* SurfaceManager::Set(std::string key, SDL_Surface* ptr) {
mapType::iterator it = surfaceMap.find(key);
if (it != surfaceMap.end()) {
throw(std::runtime_error(std::string("Key already exists: ") + key));
}
}
void SurfaceManager::Free(std::string key) {
mapType::iterator it = surfaceMap.find(key);
if (it != surfaceMap.end()) {
SDL_FreeSurface(it->second);
surfaceMap.erase(it);
}
}
SDL_Surface* SurfaceManager::LoadSurface(std::string key, std::string fname) {
SDL_Surface* ptr = SDL_LoadBMP(fname.c_str());
if (ptr == nullptr) {
throw(std::runtime_error(std::string("Failed to load file: ") + fname));
}
return surfaceMap[key] = ptr;
}
+25
View File
@@ -0,0 +1,25 @@
#ifndef SURFACEMANAGER_H_
#define SURFACEMANAGER_H_
#include "SDL/SDL.h"
#include <map>
#include <string>
class SurfaceManager {
public:
SurfaceManager();
~SurfaceManager();
SDL_Surface* Load(std::string key, std::string fname);
SDL_Surface* Reload(std::string key, std::string fname);
SDL_Surface* Get(std::string key);
SDL_Surface* Set(std::string key, SDL_Surface* ptr);
void Free(std::string key);
private:
SDL_Surface* LoadSurface(std::string key, std::string fname);
typedef std::map<std::string, SDL_Surface*> mapType;
mapType surfaceMap;
};
#endif
+64
View File
@@ -0,0 +1,64 @@
#include "surface_manager.h"
#include "SDL/SDL.h"
#include <exception>
#include <iostream>
using namespace std;
int go(int, char**) {
SDL_Init(SDL_INIT_VIDEO);
SDL_Surface* screen = SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);
SurfaceManager sMgr;
sMgr.Load("player", "rsc/graphics/sprites/elliot2.bmp");
sMgr.Load("tileset", "rsc/graphics/tilesets/MishMash.bmp");
bool running = true;
while (running) {
SDL_Event event;
while(SDL_PollEvent(&event)) {
switch(event.type) {
case SDL_QUIT:
running = false;
break;
case SDL_KEYDOWN:
switch(event.key.keysym.sym) {
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);
SDL_Flip(screen);
}
SDL_Quit();
return 0;
}
int SDL_main(int argc, char* argv[]) {
try {
go(argc, argv);
}
catch(exception& e) {
cerr << "Error: " << e.what() << endl;
return 1;
}
return 0;
}
+27
View File
@@ -22,3 +22,30 @@ Player:
Animations:
multiple cells in one animation (x-axis)
multiple animations in one sheet (y-axis)
Image:
SetSurface(surface)
GetSurface()
SetClip
GetClip
//clips
-------------------------
Rememer: Top down programming/K.I.S.S.
KeyDown:
up:
PlayerManager.ShiftMotion(playerIndex, up)
down:
PlayerManager.ShiftMotion(playerIndex, down)
...
end
Receive:
switch(message->type):
player update:
PlayerManager.Update(message)
end
Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 MiB