Skip to content

Commit

Permalink
Fix exiting release resource bug (change order of Game member)
Browse files Browse the repository at this point in the history
  • Loading branch information
ibkernel committed May 3, 2017
1 parent cd32170 commit 7448fd9
Show file tree
Hide file tree
Showing 16 changed files with 69 additions and 40 deletions.
6 changes: 4 additions & 2 deletions master-touch-typing.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,8 @@
B0BA8A601EB38D0900F4951B /* State_MainMenu.hpp */,
B0BA8A681EB459D800F4951B /* State_Paused.cpp */,
B0BA8A691EB459D800F4951B /* State_Paused.hpp */,
B09ECC041EB86BE8001036E1 /* State_GameOver.cpp */,
B09ECC051EB86BE8001036E1 /* State_GameOver.hpp */,
B079E90E1EB2281000D7A830 /* StateManager.cpp */,
B079E90F1EB2281000D7A830 /* StateManager.hpp */,
B09ECBD21EB71649001036E1 /* TextureManager.hpp */,
Expand All @@ -170,8 +172,6 @@
B0EEE4BE1EB08A0500253BCF /* Window.hpp */,
B0EEE4AE1EB0833C00253BCF /* Resources */,
B0EEE4A71EB0833C00253BCF /* Supporting Files */,
B09ECC041EB86BE8001036E1 /* State_GameOver.cpp */,
B09ECC051EB86BE8001036E1 /* State_GameOver.hpp */,
);
path = "master-touch-typing";
sourceTree = "<group>";
Expand Down Expand Up @@ -397,6 +397,7 @@
isa = XCBuildConfiguration;
buildSettings = {
DEVELOPMENT_TEAM = 3JLWU9ZHMM;
GCC_OPTIMIZATION_LEVEL = s;
INFOPLIST_FILE = "master-touch-typing/master-touch-typing-Info.plist";
LD_RUNPATH_SEARCH_PATHS = "@loader_path/../Frameworks";
OTHER_CPLUSPLUSFLAGS = (
Expand All @@ -412,6 +413,7 @@
isa = XCBuildConfiguration;
buildSettings = {
DEVELOPMENT_TEAM = 3JLWU9ZHMM;
GCC_OPTIMIZATION_LEVEL = s;
INFOPLIST_FILE = "master-touch-typing/master-touch-typing-Info.plist";
LD_RUNPATH_SEARCH_PATHS = "@loader_path/../Frameworks";
OTHER_CPLUSPLUSFLAGS = (
Expand Down
1 change: 1 addition & 0 deletions master-touch-typing/Enemy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ void Enemy::OnEntityCollision(EntityBase* l_collider, bool l_attack)
{
if (m_state == EntityState::Dying){ return; }
if (l_attack){ return; }
// enemy will not hurt each other
if (l_collider->GetType() != EntityType::Player){ return; }
Character* player = (Character*)l_collider;
SetState(EntityState::Attacking);
Expand Down
20 changes: 11 additions & 9 deletions master-touch-typing/EntityBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,16 @@
#include "Map.hpp"
#include <cmath>

EntityBase::EntityBase(EntityManager* l_entityMgr)
:m_entityManager(l_entityMgr), m_name("BaseEntity"),
m_type(EntityType::Base), m_id(0), m_referenceTile(nullptr),
m_state(EntityState::Idle), m_collidingOnX(false), m_collidingOnY(false), m_friction({0.8f, 0.8f}){}

bool SortCollisions(const CollisionElement& l_1, const CollisionElement& l_2)
{
return l_1.m_area > l_2.m_area;
}

EntityBase::EntityBase(EntityManager* l_entityMgr)
:m_entityManager(l_entityMgr), m_name("BaseEntity"),
m_type(EntityType::Base), m_id(0), m_referenceTile(nullptr),
m_state(EntityState::Idle), m_collidingOnX(false), m_collidingOnY(false){}

EntityBase::~EntityBase(){}

void EntityBase::SetPosition(float l_x, float l_y){
Expand Down Expand Up @@ -122,6 +121,7 @@ void EntityBase::Update(float l_dT){
frictionValue = m_referenceTile->m_friction;
if(m_referenceTile->m_deadly){ SetState(EntityState::Dying); }
} else if(map->GetDefaultTile()){
// no tile underneath the entity (e.g. mid air)
frictionValue = map->GetDefaultTile()->m_friction;
} else {
frictionValue = m_friction;
Expand All @@ -138,6 +138,8 @@ void EntityBase::Update(float l_dT){
ResolveCollisions();
}

// origin of the bounding box must be at the top left corner
// entities position is set to (width / 2, height)
void EntityBase::UpdateAABB(){
m_AABB = sf::FloatRect(m_position.x - (m_size.x / 2),m_position.y - m_size.y, m_size.x,m_size.y);
}
Expand All @@ -149,16 +151,15 @@ void EntityBase::CheckCollisions(){
int toX = floor((m_AABB.left + m_AABB.width) / tileSize);
int fromY = floor(m_AABB.top / tileSize);
int toY = floor((m_AABB.top + m_AABB.height) / tileSize);

for(int x = fromX; x <= toX; ++x){
for(int y = fromY; y <= toY; ++y){
Tile* tile = gameMap->GetTile(x,y);
if (!tile){ continue; }

sf::FloatRect tileBounds(x * tileSize,y * tileSize,tileSize,tileSize);
sf::FloatRect intersection;
m_AABB.intersects(tileBounds,intersection);
float area = intersection.width * intersection.height;

CollisionElement e(area, tile->m_properties, tileBounds);
m_collisions.emplace_back(e);
if(tile->m_warp && m_type == EntityType::Player){
Expand All @@ -174,13 +175,14 @@ void EntityBase::ResolveCollisions(){
Map* gameMap = m_entityManager->GetContext()->m_gameMap;
unsigned int tileSize = gameMap->GetTileSize();
for (auto &itr : m_collisions){
if (!m_AABB.intersects(itr.m_tileBounds)){ continue; }
if (!m_AABB.intersects(itr.m_tileBounds)){continue; }

// Debug
if(m_entityManager->GetContext()->m_debugOverlay.Debug()){
sf::Vector2f tempPos(itr.m_tileBounds.left, itr.m_tileBounds.top);
sf::RectangleShape* rect = new sf::RectangleShape(sf::Vector2f(tileSize,tileSize));
rect->setPosition(tempPos);
rect->setFillColor(sf::Color(255,255,0,150));
rect->setFillColor(sf::Color(255,255,255,150));
m_entityManager->GetContext()->m_debugOverlay.Add(rect);
}
// End debug.
Expand Down
5 changes: 3 additions & 2 deletions master-touch-typing/EntityBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ struct CollisionElement{
};

using Collisions = std::vector<CollisionElement>;
// always resolve the biggest collision first
bool SortCollisions(const CollisionElement& l_1, const CollisionElement& l_2);

class EntityManager;

class EntityBase{
friend class EntityManager;
friend class EntityManager;
public:
EntityBase(EntityManager* l_entityMgr);
virtual ~EntityBase();
Expand Down Expand Up @@ -90,5 +92,4 @@ class EntityBase{
EntityManager* m_entityManager;
};


#endif /* EntityBase_hpp */
8 changes: 8 additions & 0 deletions master-touch-typing/EntityManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ EntityManager::EntityManager(SharedContext* l_context, unsigned int l_maxEntitie
RegisterEntity<Player>(EntityType::Player);
RegisterEntity<Enemy>(EntityType::Enemy);
}

EntityManager::~EntityManager(){ Purge(); }

int EntityManager::Add(const EntityType& l_type, const std::string& l_name)
Expand All @@ -28,6 +29,7 @@ int EntityManager::Add(const EntityType& l_type, const std::string& l_name)

m_entities.emplace(m_idCounter,entity);

// if is enemy, entity is typed cast to Enemy
if(l_type == EntityType::Enemy){
auto itr = m_enemyTypes.find(l_name);
if(itr != m_enemyTypes.end()){
Expand All @@ -40,6 +42,7 @@ int EntityManager::Add(const EntityType& l_type, const std::string& l_name)
return m_idCounter - 1;
}

// find by name
EntityBase* EntityManager::Find(const std::string& l_name){
for(auto &itr : m_entities){
if(itr.second->GetName() == l_name){
Expand All @@ -49,6 +52,7 @@ EntityBase* EntityManager::Find(const std::string& l_name){
return nullptr;
}

// find by ID
EntityBase* EntityManager::Find(unsigned int l_id){
auto itr = m_entities.find(l_id);
if (itr == m_entities.end()){ return nullptr; }
Expand Down Expand Up @@ -113,6 +117,10 @@ void EntityManager::EntityCollisionCheck(){
itr2->second->OnEntityCollision(itr->second, false);
}

// attack collision
// Character will have to keep another bounding
// box to perform attack.

EntityType t1 = itr->second->GetType();
EntityType t2 = itr2->second->GetType();
if (t1 == EntityType::Player || t1 == EntityType::Enemy){
Expand Down
3 changes: 2 additions & 1 deletion master-touch-typing/EntityManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,14 @@ class EntityManager{

void ProcessRemovals();
void LoadEnemyTypes(const std::string& l_name);
// check and resolve collision between entities
void EntityCollisionCheck();

EntityContainer m_entities;
EnemyTypes m_enemyTypes;
EntityFactory m_entityFactory;
SharedContext* m_context;
unsigned int m_idCounter;
unsigned int m_idCounter;//keep track the highest ID
unsigned int m_maxEntities;

std::vector<unsigned int> m_entitiesToRemove;
Expand Down
9 changes: 5 additions & 4 deletions master-touch-typing/Game.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ class Game{

Window* GetWindow();
private:
void RestartClock();

sf::Clock m_clock;
sf::Time m_elapsed;
SharedContext m_context;
Window m_window;
StateManager m_stateManager;
EntityManager m_entityManager;
TextureManager m_textureManager;
sf::Clock m_clock;
sf::Time m_elapsed;
void RestartClock();
StateManager m_stateManager;
};

#endif /* Game_hpp */
14 changes: 10 additions & 4 deletions master-touch-typing/Map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@
#include "StateManager.hpp"


Map::Map(SharedContext* l_context, BaseState* l_currentState)
:m_context(l_context), m_defaultTile(l_context), m_maxMapSize(32, 32),
m_tileCount(0), m_tileSetCount(0), m_mapGravity(512.f), m_loadNextMap(false),
m_currentState(l_currentState)
Map::Map(SharedContext* l_context, BaseState* l_currentState):
m_context(l_context),
m_defaultTile(l_context),// id = 0
m_maxMapSize(32, 32), //default map size
m_tileCount(0),
m_tileSetCount(0),
m_mapGravity(512.f),
m_loadNextMap(false),
m_currentState(l_currentState)
{
m_context->m_gameMap = this;
LoadTiles("tiles.cfg");
Expand Down Expand Up @@ -157,6 +162,7 @@ void Map::Update(float l_dT){
if(m_nextMap != ""){
LoadMap("media/maps/"+m_nextMap);
} else {
// Finished all stages
m_currentState->GetStateManager()->SwitchTo(StateType::GameOver);
}
m_nextMap = "";
Expand Down
7 changes: 5 additions & 2 deletions master-touch-typing/Map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ struct TileInfo{
m_texture = l_texture;
m_id = l_id;
m_sprite.setTexture(*tmgr->GetResource(m_texture));
// get tile by id
sf::IntRect tileBoundaries(m_id % (Sheet::Sheet_Width / Sheet::Tile_Size) * Sheet::Tile_Size,
m_id / (Sheet::Sheet_Height / Sheet::Tile_Size) * Sheet::Tile_Size,
Sheet::Tile_Size,Sheet::Tile_Size);
Expand All @@ -56,20 +57,22 @@ struct TileInfo{
std::string m_texture;
};

// remove redundancy
struct Tile{
TileInfo* m_properties;
bool m_warp; // Is the tile a warp.
// Other flags unique to each tile.
};

using TileMap = std::unordered_map<TileID,Tile*>;
using TileSet = std::unordered_map<TileID,TileInfo*>;
using TileMap = std::unordered_map<TileID,Tile*>; // map
using TileSet = std::unordered_map<TileID,TileInfo*>; // unique tile set

class Map{
public:
Map(SharedContext* l_context, BaseState* l_currentState);
~Map();

// get tile at specific coordinate
Tile* GetTile(unsigned int l_x, unsigned int l_y);
TileInfo* GetDefaultTile();

Expand Down
8 changes: 6 additions & 2 deletions master-touch-typing/ResourceManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ class ResourceManager{
LoadPaths(l_pathsFile);
}

virtual ~ResourceManager(){ PurgeResources(); }
virtual ~ResourceManager(){
PurgeResources();
}

T* GetResource(const std::string& l_id){
auto res = Find(l_id);
Expand All @@ -48,6 +50,8 @@ class ResourceManager{
return true;
}

// A tile is used many times
// free memory only if no more used
bool ReleaseResource(const std::string& l_id){
auto res = Find(l_id);
if (!res){ return false; }
Expand Down Expand Up @@ -109,4 +113,4 @@ class ResourceManager{
std::unordered_map<std::string, std::string> m_paths;
};

#endif /* ResourceManager_hpp */
#endif // ResourceManager.hpp
1 change: 1 addition & 0 deletions master-touch-typing/SharedContext.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
// keeps the pointer to the main window class and event manager

class Map;

struct SharedContext{
SharedContext():
m_wind(nullptr),
Expand Down
17 changes: 7 additions & 10 deletions master-touch-typing/StateManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
#include "State_Paused.hpp"
#include "State_GameOver.hpp"

enum class StateType{
Intro=1, MainMenu, Game, Paused, GameOver, Credits
};
enum class StateType{ Intro = 1, MainMenu, Game, Paused, GameOver, Credits };

// vector is a stack-like order
// State container.
using StateContainer = std::vector<std::pair<StateType, BaseState*>>;
// Type container.
using TypeContainer = std::vector<StateType>;
// State factory.
using StateFactory = std::unordered_map<StateType, std::function<BaseState*(void)>, EnumClassHash>;

class StateManager{
Expand All @@ -42,25 +42,22 @@ class StateManager{

void SwitchTo(const StateType& l_type);
void Remove(const StateType& l_type);

private:
// Methods.
void CreateState(const StateType& l_type);
void RemoveState(const StateType& l_type);

template<class T>
void RegisterState(const StateType& l_type){
// Create a mapping between type and BaseState pointer
m_stateFactory[l_type] = [this]()-> BaseState*
m_stateFactory[l_type] = [this]() -> BaseState*
{
// each state need pointer to StateManager
return new T(this);
};
}


// Members.
SharedContext* m_shared;
StateContainer m_states;
// keeping the states we want to remove and processed by ProcessRquests
TypeContainer m_toRemove;
StateFactory m_stateFactory;
};
Expand Down
1 change: 1 addition & 0 deletions master-touch-typing/State_Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ void State_Game::OnCreate(){
m_view.zoom(0.6f);
m_stateMgr->GetContext()->m_wind->GetRenderWindow()->setView(m_view);

// Load First stage's map
m_gameMap = new Map(m_stateMgr->GetContext(), this);
m_gameMap->LoadMap("media/Maps/map1.map");
}
Expand Down
2 changes: 1 addition & 1 deletion master-touch-typing/State_GameOver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void State_GameOver::OnCreate(){
m_text.setOrigin(m_text.getLocalBounds().width / 2,
m_text.getLocalBounds().height / 2);
m_text.setPosition(400, 300);

m_stateMgr->Remove(StateType::Game);
}

Expand Down
3 changes: 1 addition & 2 deletions master-touch-typing/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ void Window::Setup(const std::string& l_title, const sf::Vector2u& l_size)
m_isFullscreen = false;
m_isDone = false;
m_isFocused = true;

m_eventManager.AddCallback(StateType(0),"Fullscreen_toggle",&Window::ToggleFullscreen,this);
m_eventManager.AddCallback(StateType(0),"Fullscreen_toggle", &Window::ToggleFullscreen, this);
m_eventManager.AddCallback(StateType(0),"Window_close", &Window::Close, this);

Create();
Expand Down
Loading

0 comments on commit 7448fd9

Please sign in to comment.