From ecdf6584632c5d877e48c7e8ebf0e04b6dd831cb Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Mon, 29 Apr 2013 17:25:56 +1000 Subject: [PATCH] Moved working systems into InGame --- client/defines.h | 9 +++ client/image.cpp | 2 +- client/image.h | 2 +- client/in_game.cpp | 102 ++++++++++++++++++++++++++++++ client/in_game.h | 36 +++++++++++ client/makefile | 6 +- client/player.h | 8 +-- client/scene_list.h | 1 + client/scene_manager.cpp | 4 ++ client/sprite_sheet.cpp | 11 +++- client/sprite_sheet.h | 10 ++- client/surface_manager.cpp | 12 ++-- client/surface_manager.h | 1 + client/unit.cpp | 124 ------------------------------------- docs/pseudocode.txt | 9 --- 15 files changed, 186 insertions(+), 151 deletions(-) create mode 100644 client/defines.h create mode 100644 client/in_game.cpp create mode 100644 client/in_game.h diff --git a/client/defines.h b/client/defines.h new file mode 100644 index 0000000..17fbb60 --- /dev/null +++ b/client/defines.h @@ -0,0 +1,9 @@ +#ifndef DEFINES_H_ +#define DEFINES_H_ + +enum class Direction { + NORTH = 1, SOUTH = 2, + EAST = 3, WEST = 4 +}; + +#endif diff --git a/client/image.cpp b/client/image.cpp index eca7239..864585e 100644 --- a/client/image.cpp +++ b/client/image.cpp @@ -14,7 +14,7 @@ SDL_Surface* Image::SetSurface(SDL_Surface* p) { return surface; } -SDL_Surface* Image::SetSurface(SDL_Surface* p, SDL_Rect r) { +SDL_Surface* Image::SetSurface(SDL_Surface* const p, SDL_Rect r) { surface = p; clip = r; return surface; diff --git a/client/image.h b/client/image.h index fddf2b8..bf611c9 100644 --- a/client/image.h +++ b/client/image.h @@ -13,7 +13,7 @@ public: SDL_Surface* SetSurface(SDL_Surface*, SDL_Rect); SDL_Surface* GetSurface() const; - void DrawTo(SDL_Surface*, Sint16 x, Sint16 y); + void DrawTo(SDL_Surface* const, Sint16 x, Sint16 y); //Clip handlers SDL_Rect SetClip(SDL_Rect r) { return clip = r; } diff --git a/client/in_game.cpp b/client/in_game.cpp new file mode 100644 index 0000000..85f03f4 --- /dev/null +++ b/client/in_game.cpp @@ -0,0 +1,102 @@ +#include "in_game.h" + +#include + +using namespace std; + +//------------------------- +//Public access members +//------------------------- + +InGame::InGame() { +#ifdef DEBUG + cout << "entering InGame" << endl; +#endif + surfaceMgr.Load("player", "rsc/graphics/sprites/elliot2.bmp"); + surfaceMgr.Load("flower", "rsc/graphics/sprites/aniflower.bmp"); + + player = new Player(surfaceMgr.Get("player"), 32, 48); +} + +InGame::~InGame() { + delete player; + surfaceMgr.FreeAll(); +#ifdef DEBUG + cout << "leaving InGame" << endl; +#endif +} + +//------------------------- +//Frame loop +//------------------------- + +void InGame::FrameStart() { + // +} + +void InGame::FrameEnd() { + // +} + +void InGame::Update() { + delta.Calculate(); + player->Update(delta.GetDelta()); +} + +void InGame::Render(SDL_Surface* const screen) { + SDL_FillRect(screen, 0, 0); + player->DrawTo(screen); +} + +//------------------------- +//Event handlers +//------------------------- + +void InGame::MouseMotion(SDL_MouseMotionEvent const& motion) { + // +} + +void InGame::MouseButtonDown(SDL_MouseButtonEvent const& button) { + // +} + +void InGame::MouseButtonUp(SDL_MouseButtonEvent const& button) { + // +} + +void InGame::KeyDown(SDL_KeyboardEvent const& key) { + switch(key.keysym.sym) { + case SDLK_ESCAPE: + QuitEvent(); + break; + case SDLK_w: + player->WalkInDirection(Direction::NORTH); + break; + case SDLK_s: + player->WalkInDirection(Direction::SOUTH); + break; + case SDLK_a: + player->WalkInDirection(Direction::WEST); + break; + case SDLK_d: + player->WalkInDirection(Direction::EAST); + break; + } +} + +void InGame::KeyUp(SDL_KeyboardEvent const& key) { + switch(key.keysym.sym) { + case SDLK_w: + player->WalkInDirection(Direction::SOUTH); + break; + case SDLK_s: + player->WalkInDirection(Direction::NORTH); + break; + case SDLK_a: + player->WalkInDirection(Direction::EAST); + break; + case SDLK_d: + player->WalkInDirection(Direction::WEST); + break; + } +} diff --git a/client/in_game.h b/client/in_game.h new file mode 100644 index 0000000..ea6fdc2 --- /dev/null +++ b/client/in_game.h @@ -0,0 +1,36 @@ +#ifndef INGAME_H_ +#define INGAME_H_ + +#include "base_scene.h" + +#include "delta.h" +#include "player.h" +#include "surface_manager.h" + +class InGame : public BaseScene { +public: + //Public access members + InGame(); + virtual ~InGame(); + +protected: + //Frame loop + virtual void FrameStart(); + virtual void FrameEnd(); + virtual void Update(); + virtual void Render(SDL_Surface* const); + + //Event handlers + virtual void MouseMotion (SDL_MouseMotionEvent const&); + virtual void MouseButtonDown (SDL_MouseButtonEvent const&); + virtual void MouseButtonUp (SDL_MouseButtonEvent const&); + virtual void KeyDown (SDL_KeyboardEvent const&); + virtual void KeyUp (SDL_KeyboardEvent const&); + + //members + Delta delta; + SurfaceManager surfaceMgr; + Player* player; +}; + +#endif diff --git a/client/makefile b/client/makefile index 3861c6e..9bda937 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 +OBJ=base_scene.o scene_manager.o main.o surface_manager.o image.o sprite_sheet.o player.o +SRC=test_systems.cpp in_game.cpp all: debug @@ -14,5 +14,7 @@ debug: $(OBJ) clean: -$(RM) *.o *.a *.exe +rebuild: clean all + unit: $(CXX) $(CXXFLAGS) unit.cpp surface_manager.cpp image.cpp sprite_sheet.cpp player.cpp -lmingw32 -lSDLmain -lSDL diff --git a/client/player.h b/client/player.h index b994c87..544f924 100644 --- a/client/player.h +++ b/client/player.h @@ -1,16 +1,12 @@ #ifndef PLAYER_H_ #define PLAYER_H_ +#include "defines.h" #include "vector2.h" #include "sprite_sheet.h" #include "SDL/SDL.h" -enum class Direction { - NORTH, SOUTH, - EAST, WEST -}; - class Player { public: Player(SDL_Surface*, int w, int h); @@ -27,7 +23,7 @@ public: Vector2 ShiftMotion(Vector2 v) { return motion += v; } Vector2 GetMotion() const { return motion; } - void DrawTo(SDL_Surface* s) { sprite.DrawTo(s, position.x, position.y); } + void DrawTo(SDL_Surface* const s) { sprite.DrawTo(s, position.x, position.y); } void FaceDirection(Direction); SpriteSheet* GetSpriteSheet() { return &sprite; }; diff --git a/client/scene_list.h b/client/scene_list.h index 86c6b87..970ae80 100644 --- a/client/scene_list.h +++ b/client/scene_list.h @@ -9,6 +9,7 @@ enum class SceneList { //custom indexes TESTSYSTEMS, + INGAME, }; #endif diff --git a/client/scene_manager.cpp b/client/scene_manager.cpp index e5d6635..7a5828a 100644 --- a/client/scene_manager.cpp +++ b/client/scene_manager.cpp @@ -8,6 +8,7 @@ //Add the custom scene headers here #include "test_systems.h" +#include "in_game.h" //------------------------- //Public access members @@ -61,6 +62,9 @@ void SceneManager::LoadScene(SceneList sceneIndex) { switch(sceneIndex) { //add scene creation calls here case SceneList::FIRST: + case SceneList::INGAME: + activeScene = new InGame(); + break; case SceneList::TESTSYSTEMS: activeScene = new TestSystems(); break; diff --git a/client/sprite_sheet.cpp b/client/sprite_sheet.cpp index c23e983..bb172a5 100644 --- a/client/sprite_sheet.cpp +++ b/client/sprite_sheet.cpp @@ -1,7 +1,7 @@ #include "sprite_sheet.h" -SpriteSheet::SpriteSheet(SDL_Surface* s, int w, int h) - : Image(s, {0, 0, (Uint16)w, (Uint16)h}) +SpriteSheet::SpriteSheet(SDL_Surface* s, Uint16 w, Uint16 h) + : Image(s, {0, 0, w, h}) { currentFrame = 0; maxFrames = GetSurface()->w / GetClipW(); currentStrip = 0; maxStrips = GetSurface()->h / GetClipH(); @@ -18,3 +18,10 @@ void SpriteSheet::Update(int delta) { SetClipX(currentFrame * GetClipW()); SetClipY(currentStrip * GetClipH()); } + +void SpriteSheet::SetSurface(SDL_Surface* s, Uint16 w, Uint16 h) { + Image::SetSurface(s, {0, 0, w, h}); + currentFrame = 0; maxFrames = GetSurface()->w / GetClipW(); + currentStrip = 0; maxStrips = GetSurface()->h / GetClipH(); + interval = ticks = 0; +} \ No newline at end of file diff --git a/client/sprite_sheet.h b/client/sprite_sheet.h index 5a35005..5be8d31 100644 --- a/client/sprite_sheet.h +++ b/client/sprite_sheet.h @@ -5,13 +5,19 @@ #include "SDL/SDL.h" -class SpriteSheet : public Image { +class SpriteSheet : protected Image { public: - SpriteSheet(SDL_Surface*, int w, int h); + SpriteSheet(SDL_Surface*, Uint16 w, Uint16 h); virtual ~SpriteSheet() {} void Update(int delta); + void SetSurface(SDL_Surface*, Uint16 w, Uint16 h); + + using Image::GetSurface; + using Image::DrawTo; + + //these aren't regulated; be careful int SetWidth(int i) { return SetClipW(i); }; int SetHeight(int i) { return SetClipH(i); }; int SetFrames(int i) { currentFrame = 0; return maxFrames = i; }; diff --git a/client/surface_manager.cpp b/client/surface_manager.cpp index b8ea466..95136f0 100644 --- a/client/surface_manager.cpp +++ b/client/surface_manager.cpp @@ -7,10 +7,7 @@ SurfaceManager::SurfaceManager() { } SurfaceManager::~SurfaceManager() { - for (auto it : surfaceMap) { - SDL_FreeSurface(it.second); - } - surfaceMap.clear(); + FreeAll(); } SDL_Surface* SurfaceManager::Load(std::string key, std::string fname) { @@ -53,6 +50,13 @@ void SurfaceManager::Free(std::string key) { } } +void SurfaceManager::FreeAll() { + for (auto it : surfaceMap) { + SDL_FreeSurface(it.second); + } + surfaceMap.clear(); +} + SDL_Surface* SurfaceManager::LoadSurface(std::string key, std::string fname) { SDL_Surface* ptr = SDL_LoadBMP(fname.c_str()); if (ptr == nullptr) { diff --git a/client/surface_manager.h b/client/surface_manager.h index 9d8e97f..e2552ce 100644 --- a/client/surface_manager.h +++ b/client/surface_manager.h @@ -16,6 +16,7 @@ public: SDL_Surface* Get(std::string key); SDL_Surface* Set(std::string key, SDL_Surface* ptr); void Free(std::string key); + void FreeAll(); private: SDL_Surface* LoadSurface(std::string key, std::string fname); typedef std::map mapType; diff --git a/client/unit.cpp b/client/unit.cpp index c8811af..e1b17a2 100644 --- a/client/unit.cpp +++ b/client/unit.cpp @@ -1,131 +1,7 @@ -#include "delta.h" -#include "player.h" -#include "sprite_sheet.h" -#include "image.h" -#include "surface_manager.h" - -#include "SDL/SDL.h" - -#include #include -#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; - - //load all resources - sMgr.Load("player", "rsc/graphics/sprites/elliot2.bmp"); - sMgr.Load("flower", "rsc/graphics/sprites/aniflower.bmp"); - sMgr.Load("tileset", "rsc/graphics/tilesets/MishMash.bmp"); - - Player player(sMgr.Get("player"), 32, 48); - Image tiles(sMgr.Get("tileset")); - SpriteSheet rose(sMgr.Get("flower"), 32, 32); - - player.GetSpriteSheet()->SetInterval(200); - rose.SetInterval(200); - -// vector flowerVector; -// for (int i = 0; i < 100; i++) { -// SpriteSheet ss(sMgr.Get("flower"), 32, 32); -// ss.SetInterval(i%5 + 95); -// flowerVector.push_back(ss); -// } - - Delta delta; - - 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_w: - case SDLK_UP: - player.WalkInDirection(Direction::NORTH); - break; - case SDLK_s: - case SDLK_DOWN: - player.WalkInDirection(Direction::SOUTH); - break; - case SDLK_a: - case SDLK_LEFT: - player.WalkInDirection(Direction::WEST); - break; - case SDLK_d: - case SDLK_RIGHT: - player.WalkInDirection(Direction::EAST); - break; - } - break; - case SDL_KEYUP: - switch(event.key.keysym.sym) { - case SDLK_w: - player.WalkInDirection(Direction::SOUTH); - break; - case SDLK_s: - player.WalkInDirection(Direction::NORTH); - break; - case SDLK_a: - player.WalkInDirection(Direction::EAST); - break; - case SDLK_d: - player.WalkInDirection(Direction::WEST); - break; - } - } - } - - delta.Calculate(); - - player.Update(delta.GetDelta()); - rose.Update(0); - -// for (int i = 0; i < 100; i++) { -// flowerVector[i].Update(delta.GetDelta()); -// } - - SDL_FillRect(screen, 0, 0); - - tiles.DrawTo(screen, 0, 0); - player.DrawTo(screen); - rose.DrawTo(screen, 50, 100); - -// for (int i = 0; i < 100; i++) { -// flowerVector[i].DrawTo(screen, 20+i*4, i*4); -// } - - SDL_Flip(screen); - - //debugging -// cout << player.GetSpriteSheet()->GetCurrentFrame() << endl; - } - - 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 df0ec3a..9bfc308 100644 --- a/docs/pseudocode.txt +++ b/docs/pseudocode.txt @@ -19,15 +19,6 @@ Player: ------------------------- -Player's movement in a server - -#define PLAYER_FACE_DOWN 0 -#define PLAYER_FACE_UP 1 -#define PLAYER_FACE_LEFT 2 -#define PLAYER_FACE_RIGHT 3 - - - Player: index --global index on the server position