Moved working systems into InGame

This commit is contained in:
Kayne Ruse
2013-04-29 17:25:56 +10:00
parent c9447fcf3b
commit ecdf658463
15 changed files with 186 additions and 151 deletions
+9
View File
@@ -0,0 +1,9 @@
#ifndef DEFINES_H_
#define DEFINES_H_
enum class Direction {
NORTH = 1, SOUTH = 2,
EAST = 3, WEST = 4
};
#endif
+1 -1
View File
@@ -14,7 +14,7 @@ SDL_Surface* Image::SetSurface(SDL_Surface* p) {
return surface; 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; surface = p;
clip = r; clip = r;
return surface; return surface;
+1 -1
View File
@@ -13,7 +13,7 @@ public:
SDL_Surface* SetSurface(SDL_Surface*, SDL_Rect); SDL_Surface* SetSurface(SDL_Surface*, SDL_Rect);
SDL_Surface* GetSurface() const; SDL_Surface* GetSurface() const;
void DrawTo(SDL_Surface*, Sint16 x, Sint16 y); void DrawTo(SDL_Surface* const, Sint16 x, Sint16 y);
//Clip handlers //Clip handlers
SDL_Rect SetClip(SDL_Rect r) { return clip = r; } SDL_Rect SetClip(SDL_Rect r) { return clip = r; }
+102
View File
@@ -0,0 +1,102 @@
#include "in_game.h"
#include <iostream>
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;
}
}
+36
View File
@@ -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
+4 -2
View File
@@ -1,7 +1,7 @@
CXXFLAGS+=-std=c++11 -DDEBUG CXXFLAGS+=-std=c++11 -DDEBUG
LIB=-lmingw32 -lSDL_net -lSDLmain -lSDL -lwsock32 -liphlpapi LIB=-lmingw32 -lSDL_net -lSDLmain -lSDL -lwsock32 -liphlpapi
OBJ=base_scene.o scene_manager.o main.o OBJ=base_scene.o scene_manager.o main.o surface_manager.o image.o sprite_sheet.o player.o
SRC=test_systems.cpp SRC=test_systems.cpp in_game.cpp
all: debug all: debug
@@ -14,5 +14,7 @@ debug: $(OBJ)
clean: clean:
-$(RM) *.o *.a *.exe -$(RM) *.o *.a *.exe
rebuild: clean all
unit: unit:
$(CXX) $(CXXFLAGS) unit.cpp surface_manager.cpp image.cpp sprite_sheet.cpp player.cpp -lmingw32 -lSDLmain -lSDL $(CXX) $(CXXFLAGS) unit.cpp surface_manager.cpp image.cpp sprite_sheet.cpp player.cpp -lmingw32 -lSDLmain -lSDL
+2 -6
View File
@@ -1,16 +1,12 @@
#ifndef PLAYER_H_ #ifndef PLAYER_H_
#define PLAYER_H_ #define PLAYER_H_
#include "defines.h"
#include "vector2.h" #include "vector2.h"
#include "sprite_sheet.h" #include "sprite_sheet.h"
#include "SDL/SDL.h" #include "SDL/SDL.h"
enum class Direction {
NORTH, SOUTH,
EAST, WEST
};
class Player { class Player {
public: public:
Player(SDL_Surface*, int w, int h); Player(SDL_Surface*, int w, int h);
@@ -27,7 +23,7 @@ public:
Vector2 ShiftMotion(Vector2 v) { return motion += v; } Vector2 ShiftMotion(Vector2 v) { return motion += v; }
Vector2 GetMotion() const { return motion; } 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); void FaceDirection(Direction);
SpriteSheet* GetSpriteSheet() { return &sprite; }; SpriteSheet* GetSpriteSheet() { return &sprite; };
+1
View File
@@ -9,6 +9,7 @@ enum class SceneList {
//custom indexes //custom indexes
TESTSYSTEMS, TESTSYSTEMS,
INGAME,
}; };
#endif #endif
+4
View File
@@ -8,6 +8,7 @@
//Add the custom scene headers here //Add the custom scene headers here
#include "test_systems.h" #include "test_systems.h"
#include "in_game.h"
//------------------------- //-------------------------
//Public access members //Public access members
@@ -61,6 +62,9 @@ void SceneManager::LoadScene(SceneList sceneIndex) {
switch(sceneIndex) { switch(sceneIndex) {
//add scene creation calls here //add scene creation calls here
case SceneList::FIRST: case SceneList::FIRST:
case SceneList::INGAME:
activeScene = new InGame();
break;
case SceneList::TESTSYSTEMS: case SceneList::TESTSYSTEMS:
activeScene = new TestSystems(); activeScene = new TestSystems();
break; break;
+9 -2
View File
@@ -1,7 +1,7 @@
#include "sprite_sheet.h" #include "sprite_sheet.h"
SpriteSheet::SpriteSheet(SDL_Surface* s, int w, int h) SpriteSheet::SpriteSheet(SDL_Surface* s, Uint16 w, Uint16 h)
: Image(s, {0, 0, (Uint16)w, (Uint16)h}) : Image(s, {0, 0, w, h})
{ {
currentFrame = 0; maxFrames = GetSurface()->w / GetClipW(); currentFrame = 0; maxFrames = GetSurface()->w / GetClipW();
currentStrip = 0; maxStrips = GetSurface()->h / GetClipH(); currentStrip = 0; maxStrips = GetSurface()->h / GetClipH();
@@ -18,3 +18,10 @@ void SpriteSheet::Update(int delta) {
SetClipX(currentFrame * GetClipW()); SetClipX(currentFrame * GetClipW());
SetClipY(currentStrip * GetClipH()); 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;
}
+8 -2
View File
@@ -5,13 +5,19 @@
#include "SDL/SDL.h" #include "SDL/SDL.h"
class SpriteSheet : public Image { class SpriteSheet : protected Image {
public: public:
SpriteSheet(SDL_Surface*, int w, int h); SpriteSheet(SDL_Surface*, Uint16 w, Uint16 h);
virtual ~SpriteSheet() {} virtual ~SpriteSheet() {}
void Update(int delta); 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 SetWidth(int i) { return SetClipW(i); };
int SetHeight(int i) { return SetClipH(i); }; int SetHeight(int i) { return SetClipH(i); };
int SetFrames(int i) { currentFrame = 0; return maxFrames = i; }; int SetFrames(int i) { currentFrame = 0; return maxFrames = i; };
+8 -4
View File
@@ -7,10 +7,7 @@ SurfaceManager::SurfaceManager() {
} }
SurfaceManager::~SurfaceManager() { SurfaceManager::~SurfaceManager() {
for (auto it : surfaceMap) { FreeAll();
SDL_FreeSurface(it.second);
}
surfaceMap.clear();
} }
SDL_Surface* SurfaceManager::Load(std::string key, std::string fname) { 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* SurfaceManager::LoadSurface(std::string key, std::string fname) {
SDL_Surface* ptr = SDL_LoadBMP(fname.c_str()); SDL_Surface* ptr = SDL_LoadBMP(fname.c_str());
if (ptr == nullptr) { if (ptr == nullptr) {
+1
View File
@@ -16,6 +16,7 @@ public:
SDL_Surface* Get(std::string key); SDL_Surface* Get(std::string key);
SDL_Surface* Set(std::string key, SDL_Surface* ptr); SDL_Surface* Set(std::string key, SDL_Surface* ptr);
void Free(std::string key); void Free(std::string key);
void FreeAll();
private: private:
SDL_Surface* LoadSurface(std::string key, std::string fname); SDL_Surface* LoadSurface(std::string key, std::string fname);
typedef std::map<std::string, SDL_Surface*> mapType; typedef std::map<std::string, SDL_Surface*> mapType;
-124
View File
@@ -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 <exception>
#include <iostream> #include <iostream>
#include <ctime>
#include <vector>
using namespace std; 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<SpriteSheet> 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[]) { int SDL_main(int argc, char* argv[]) {
try {
go(argc, argv);
}
catch(exception& e) {
cerr << "Error: " << e.what() << endl;
return 1;
}
return 0; return 0;
} }
-9
View File
@@ -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: Player:
index --global index on the server index --global index on the server
position position