diff --git a/.gitignore b/.gitignore index 0926749..bf5a098 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ #Directories Release/ Debug/ +client/rsc/ #Project generated files *.db diff --git a/client/main.cpp b/client/main.cpp index a3e9c86..27e84c0 100644 --- a/client/main.cpp +++ b/client/main.cpp @@ -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; } diff --git a/client/makefile b/client/makefile index 3e2110f..6b22238 100644 --- a/client/makefile +++ b/client/makefile @@ -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 diff --git a/client/surface_manager.cpp b/client/surface_manager.cpp new file mode 100644 index 0000000..5730b02 --- /dev/null +++ b/client/surface_manager.cpp @@ -0,0 +1,62 @@ +#include "surface_manager.h" + +#include + +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; +} \ No newline at end of file diff --git a/client/surface_manager.h b/client/surface_manager.h new file mode 100644 index 0000000..9d8e97f --- /dev/null +++ b/client/surface_manager.h @@ -0,0 +1,25 @@ +#ifndef SURFACEMANAGER_H_ +#define SURFACEMANAGER_H_ + +#include "SDL/SDL.h" + +#include +#include + +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 mapType; + mapType surfaceMap; +}; + +#endif diff --git a/client/unit.cpp b/client/unit.cpp index e69de29..15c7946 100644 --- a/client/unit.cpp +++ b/client/unit.cpp @@ -0,0 +1,64 @@ +#include "surface_manager.h" + +#include "SDL/SDL.h" + +#include +#include + +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; +} \ No newline at end of file diff --git a/docs/pseudocode.txt b/docs/pseudocode.txt index f7d615e..309cd30 100644 --- a/docs/pseudocode.txt +++ b/docs/pseudocode.txt @@ -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 diff --git a/rsc/graphics/tilesets/MishMash.bmp b/rsc/graphics/tilesets/MishMash.bmp new file mode 100644 index 0000000..36093a8 Binary files /dev/null and b/rsc/graphics/tilesets/MishMash.bmp differ