Skip to content

Commit e7ef887

Browse files
author
Alessio Linares
committed
General improvements
1 parent 33a0b4b commit e7ef887

12 files changed

+265
-16
lines changed

Color.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#ifndef HB_COLOR_h
2+
#define HB_COLOR_h
3+
4+
namespace hb
5+
{
6+
struct Color {
7+
float r;
8+
float g;
9+
float b;
10+
float a;
11+
12+
Color() {
13+
this->r = 0.0f;
14+
this->g = 0.0f;
15+
this->b = 0.0f;
16+
this->a = 1.0f;
17+
}
18+
19+
Color(float r, float g, float b, float a = 1.0f) {
20+
this->r = r;
21+
this->g = g;
22+
this->b = b;
23+
this->a = a;
24+
}
25+
26+
Color(int r, int g, int b, int a = 255) {
27+
this->r = (float)r/255.0f;
28+
this->g = (float)g/255.0f;
29+
this->b = (float)b/255.0f;
30+
this->a = (float)a/255.0f;
31+
}
32+
};
33+
}
34+
35+
#endif

FunctionComponent.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include "FunctionComponent.h"
2+
using namespace hb;
3+
4+
FunctionComponent::FunctionComponent()
5+
{
6+
m_pre_update = [&] (GameObject*) {};
7+
m_update = [&] (GameObject*) {};
8+
m_post_update = [&] (GameObject*) {};
9+
}
10+
11+
12+
void FunctionComponent::setPreUpdateFunction(std::function<void(GameObject*)> pre_update)
13+
{
14+
m_pre_update = pre_update;
15+
}
16+
17+
18+
void FunctionComponent::setUpdateFunction(std::function<void(GameObject*)> update)
19+
{
20+
m_update = update;
21+
}
22+
23+
24+
void FunctionComponent::setPostUpdateFunction(std::function<void(GameObject*)> post_update)
25+
{
26+
m_post_update = post_update;
27+
}
28+
29+
30+
void FunctionComponent::preUpdate()
31+
{
32+
m_pre_update(getGameObject());
33+
}
34+
35+
36+
void FunctionComponent::update()
37+
{
38+
m_update(getGameObject());
39+
}
40+
41+
42+
void FunctionComponent::postUpdate()
43+
{
44+
m_post_update(getGameObject());
45+
}

FunctionComponent.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#ifndef HB_FUNCTION_COMPONENT_H
2+
#define HB_FUNCTION_COMPONENT_H
3+
#include <functional>
4+
#include "GameObject.h"
5+
6+
namespace hb
7+
{
8+
class FunctionComponent : public GameObject::Component
9+
{
10+
public:
11+
FunctionComponent();
12+
void setPreUpdateFunction(std::function<void(GameObject*)> pre_update);
13+
void setUpdateFunction(std::function<void(GameObject*)> update);
14+
void setPostUpdateFunction(std::function<void(GameObject*)> post_update);
15+
virtual void preUpdate() override;
16+
virtual void update() override;
17+
virtual void postUpdate() override;
18+
19+
private:
20+
std::function<void(GameObject*)> m_pre_update;
21+
std::function<void(GameObject*)> m_update;
22+
std::function<void(GameObject*)> m_post_update;
23+
};
24+
}
25+
#endif

GameObject.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ std::vector<GameObject*> GameObject::getGameObjectsByName(const std::string& nam
2727
return v;
2828
}
2929

30+
3031
void GameObject::destroyAll()
3132
{
3233
std::vector<std::pair<int, GameObject*>> v(s_game_objects_by_id.begin(), s_game_objects_by_id.end());
@@ -63,18 +64,23 @@ void GameObject::updateAll()
6364
}
6465

6566

66-
GameObject::GameObject(const Vector3d& init_pos):
67-
Transform(init_pos),
67+
GameObject::GameObject():
68+
Transform(),
6869
m_marked_to_destroy(false)
6970
{
70-
m_identifier = s_game_object_identifier++;
71+
m_identifier = s_game_object_identifier++;
7172
s_game_objects_by_id.insert(std::pair<int, GameObject*>(m_identifier, this));
7273
}
7374

7475

75-
GameObject::GameObject():
76-
GameObject(Vector3d())
77-
{}
76+
GameObject::GameObject(std::initializer_list<GameObject::Component*> components):
77+
GameObject()
78+
{
79+
for (Component* c : components)
80+
{
81+
addComponent(c);
82+
}
83+
}
7884

7985

8086
GameObject::~GameObject()
@@ -186,5 +192,6 @@ void GameObject::destroy()
186192
void GameObject::addComponent(Component* component)
187193
{
188194
m_components.push_back(component);
189-
m_components[m_components.size() - 1]->setGameObject(this);
195+
component->setGameObject(this);
196+
component->init();
190197
}

GameObject.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
#include <vector>
44
#include <string>
55
#include <unordered_map>
6+
#include <initializer_list>
67
#include "Transform.h"
7-
8+
// TODO: Add to game object a std::unordered_map<std::string, union{.....}> to store gameobject variables. Make getters too.
89
namespace hb
910
{
1011
class GameObject : public Transform
@@ -20,6 +21,7 @@ namespace hb
2021
m_relative = true;
2122
}
2223
inline virtual ~Component(){}
24+
virtual void init(){}
2325
virtual void preUpdate(){}
2426
virtual void update(){}
2527
virtual void postUpdate(){}
@@ -56,7 +58,7 @@ namespace hb
5658
static void updateAll();
5759

5860
GameObject();
59-
GameObject(const Vector3d& init_pos);
61+
GameObject(std::initializer_list<Component*> components);
6062
virtual ~GameObject();
6163
int getIdentifier() const;
6264
const std::string& getName() const;
@@ -67,6 +69,16 @@ namespace hb
6769
void destroy();
6870
void addComponent(Component* component);
6971
template <typename ComponentType>
72+
ComponentType* getComponent() const
73+
{
74+
for (Component* component : m_components)
75+
{
76+
if (dynamic_cast<ComponentType*>(component))
77+
return dynamic_cast<ComponentType*>(component);
78+
}
79+
return nullptr;
80+
}
81+
template <typename ComponentType>
7082
std::vector<ComponentType*> getComponents() const
7183
{
7284
std::vector<ComponentType*> v;

Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
CC := g++
2-
ARCH := $(shell getconf LONG_BIT)
32

43
CFLAGS := -std=c++11 -Wall
54

Resource.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#ifndef HB_RESOURCE_H
2+
#define HB_RESOURCE_H
3+
4+
5+
namespace hb
6+
{
7+
template <typename ResourceType, typename ResourceId, typename ResourceManager>
8+
class Resource
9+
{
10+
public:
11+
Resource(const ResourceType& resource, const ResourceId& id)
12+
{
13+
m_id = ResourceManager::instance()->addResource(id, resource);
14+
}
15+
Resource(const Resource& other)
16+
{
17+
m_id = ResourceManager::instance()->addResource(other.id(), other.get());
18+
}
19+
Resource& operator=(const Resource& other)
20+
{
21+
m_id = ResourceManager::instance()->addResource(other.id(), other.get());
22+
return *this;
23+
}
24+
~Resource()
25+
{
26+
ResourceManager::instance()->release(m_id);
27+
}
28+
29+
const ResourceType& get() const
30+
{
31+
return ResourceManager::instance()->get(m_id);
32+
}
33+
ResourceType& get()
34+
{
35+
return ResourceManager::instance()->get(m_id);
36+
}
37+
const ResourceId& id() const
38+
{
39+
return ResourceManager::instance()->getId(m_id);
40+
}
41+
42+
protected:
43+
int m_id;
44+
};
45+
}
46+
#endif

ResourceManager.h

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,17 @@
55

66
namespace hb
77
{
8-
template <typename Resource, typename ResourceId, typename Hash = std::hash<ResourceId>>
8+
template <typename ManagerType, typename Resource, typename ResourceId, typename Hash = std::hash<ResourceId>>
99
class ResourceManager
1010
{
1111
public:
12+
13+
static ManagerType* instance()
14+
{
15+
if (s_instance == nullptr)
16+
s_instance = new ManagerType();
17+
return s_instance;
18+
}
1219
// Constructor
1320
ResourceManager()
1421
{
@@ -39,7 +46,7 @@ namespace hb
3946
return ret;
4047
}
4148
// Release a Resource resource with identifier id
42-
void release(int id)
49+
virtual void release(int id)
4350
{
4451
auto i = m_info_table.find(id);
4552
if (i == m_info_table.end())
@@ -66,7 +73,23 @@ namespace hb
6673
// Get Resource with identifier id
6774
const Resource& get(int id) const
6875
{
69-
return m_info_table.find(id)->second.data;
76+
auto it = m_info_table.find(id);
77+
assert(it != m_info_table.end());
78+
return it->second.data;
79+
}
80+
// Get Resource with identifier id
81+
Resource& get(int id)
82+
{
83+
auto it = m_info_table.find(id);
84+
assert(it != m_info_table.end());
85+
return it->second.data;
86+
}
87+
// Get ResourceId of resource with identifier id
88+
const ResourceId& getId(int id) const
89+
{
90+
auto it = m_info_table.find(id);
91+
assert(it != m_info_table.end());
92+
return it->second.it->first;
7093
}
7194
// Returns wether the Resource resource with identifier id is loaded
7295
bool isLoaded(int id) const
@@ -81,13 +104,22 @@ namespace hb
81104
// Returns number of active requests for resource id
82105
int countResourceUsage(int id) const
83106
{
84-
return m_info_table.find(id)->second.count;
107+
int count = 0;
108+
auto it = m_info_table.find(id);
109+
if (it != m_info_table.end())
110+
count = it->second.count;
111+
return count;
85112
}
86-
// Returns total number of resources loaded
113+
// Returns number of resources currently loaded
87114
int size() const
88115
{
89116
return m_id_table.size();
90117
}
118+
// Returns number of all resources ever loaded
119+
int resourceCount() const
120+
{
121+
return m_resource_count;
122+
}
91123
// Release all resources
92124
void clear()
93125
{
@@ -97,6 +129,7 @@ namespace hb
97129
}
98130

99131
private:
132+
static ManagerType* s_instance;
100133
struct ResourceInfo
101134
{
102135
int id, count;
@@ -109,4 +142,6 @@ namespace hb
109142
std::unordered_map<int, ResourceInfo> m_info_table;
110143
};
111144
}
145+
template <typename ManagerType, typename Resource, typename ResourceId, typename Hash>
146+
ManagerType* hb::ResourceManager<ManagerType, Resource, ResourceId, Hash>::s_instance = nullptr;
112147
#endif

Vector2d.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,9 @@ Vector2d& operator /=(Vector2d& left, double right)
8181
bool operator ==(const Vector2d& left, const Vector2d& right)
8282
{
8383
return (left.x == right.x and left.y == right.y);
84+
}
85+
86+
bool operator !=(const Vector2d& left, const Vector2d& right)
87+
{
88+
return not (left.x == right.x and left.y == right.y);
8489
}

Vector2d.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#ifndef HB_VECTOR_2D_H
22
#define HB_VECTOR_2D_H
3+
#include <cmath>
34

45
namespace hb
56
{
@@ -10,6 +11,21 @@ namespace hb
1011
Vector2d(): x(0), y(0){};
1112
Vector2d(double x, double y): x(x), y(y){};
1213
Vector2d(const Vector2d& v): x(v.x), y(v.y){};
14+
15+
double module() const
16+
{return sqrt(pow(x, 2) + pow(y, 2));}
17+
18+
Vector2d normalized() const
19+
{
20+
Vector2d v = *this;
21+
double module = this->module();
22+
if (module != 0.)
23+
{
24+
v.x /= module;
25+
v.y /= module;
26+
}
27+
return v;
28+
}
1329
};
1430

1531
}
@@ -25,4 +41,5 @@ hb::Vector2d& operator *=(hb::Vector2d& left, double right);
2541
hb::Vector2d operator /(const hb::Vector2d& left, double right);
2642
hb::Vector2d& operator /=(hb::Vector2d& left, double right);
2743
bool operator ==(const hb::Vector2d& left, const hb::Vector2d& right);
44+
bool operator !=(const hb::Vector2d& left, const hb::Vector2d& right);
2845
#endif

0 commit comments

Comments
 (0)