Permalink
Browse files

Changed xd to use boost::intrusive_ptr instead of own handles

Changed xd::entity to be a base class for custom entities
Other bug fixes  etc
  • Loading branch information...
1 parent cf33366 commit 71b4bf71378cc13d603996adc1b5b1ba8c97ef4e @rekotiira committed Aug 27, 2012
Showing with 271 additions and 252 deletions.
  1. +3 −2 README
  2. +5 −6 include/xd/asset_manager.hpp
  3. +0 −4 include/xd/asset_serializer.hpp
  4. +4 −4 include/xd/audio/music.hpp
  5. +7 −5 include/xd/audio/sound.hpp
  6. +13 −11 include/xd/detail/entity.hpp
  7. +1 −1 include/xd/detail/iterate_create.hpp
  8. +2 −2 include/xd/detail/iterate_factory_create.hpp
  9. +34 −31 include/xd/entity.hpp
  10. +3 −3 include/xd/factory.hpp
  11. +7 −7 include/xd/graphics/font.hpp
  12. +4 −4 include/xd/graphics/image.hpp
  13. +4 −4 include/xd/graphics/shader_program.hpp
  14. +2 −2 include/xd/graphics/shaders.hpp
  15. +4 −4 include/xd/graphics/simple_text_renderer.hpp
  16. +10 −10 include/xd/graphics/sprite_batch.hpp
  17. +2 −0 include/xd/graphics/stock_text_formatter.hpp
  18. +6 −7 include/xd/graphics/text_formatter.hpp
  19. +4 −4 include/xd/graphics/text_renderer.hpp
  20. +4 −4 include/xd/graphics/texture.hpp
  21. +4 −4 include/xd/graphics/vertex_batch.hpp
  22. +2 −1 include/xd/handle.hpp
  23. +1 −1 include/xd/lua/detail/iterate_scheduler_yield.hpp
  24. +3 −2 include/xd/lua/scheduler.hpp
  25. +4 −4 include/xd/lua/scheduler_task.hpp
  26. +1 −0 include/xd/lua/virtual_machine.hpp
  27. +3 −4 include/xd/system/window.hpp
  28. +2 −1 include/xd/weak_handle.hpp
  29. +2 −2 src/graphics/font.cpp
  30. +2 −2 src/graphics/shaders.cpp
  31. +4 −4 src/graphics/simple_text_renderer.cpp
  32. +7 −7 src/graphics/sprite_batch.cpp
  33. +2 −2 src/graphics/text_formatter.cpp
  34. +4 −4 src/graphics/text_renderer.cpp
  35. +8 −2 src/lua/scheduler.cpp
  36. +92 −88 src/test/main.cpp
  37. +5 −3 src/test/test.cpp
  38. +6 −6 src/test/test.h
View
@@ -47,9 +47,10 @@ Some of the features XD supports:
TODO:
- Change GLFW to SFML for various reasons
- Make API calls that use GL functions not change the previous state of GL (use DSA extension when available)
-- Separate font from text rendering, and make the API to render text simpler
-- Support multiple sizes per font object, instead of just one
- More advanced features for text rendering, such as bounding boxes with auto-wrapping
+- Allow creationg of textures from custom data (not just loaded images), for texture generation
+- Add separate class for sprite, sprite_batch will use it internally
+- Make sprite_batch to be more efficient
- Finish binding rest of the XD API to Lua
- Generic scene graph (scene/entity structuring/ordering)
- Joystick and gamepad support
@@ -1,8 +1,7 @@
#ifndef H_XD_ASSET_MANAGER
#define H_XD_ASSET_MANAGER
-#include <xd/handle.hpp>
-#include <xd/weak_handle.hpp>
+#include <xd/ref_counted.hpp>
#include <xd/asset_serializer.hpp>
#include <boost/any.hpp>
#include <unordered_map>
@@ -20,7 +19,7 @@ namespace xd
public:
#ifndef BOOST_NO_VARIADIC_TEMPLATES
template <typename T, typename... Args>
- typename T::handle load(Args&&... args)
+ typename T::ptr load(Args&&... args)
{
// create serializer for asset type and get cache key
asset_serializer<T> serializer;
@@ -41,13 +40,13 @@ namespace xd
loaded_assets.erase(it2);
}
// not loaded in either map, create it
- typename T::handle resource(new T(std::forward<Args>(args)...));
+ typename T::ptr resource(new T(std::forward<Args>(args)...));
loaded_assets.insert(std::make_pair(cache_key, resource));
return resource;
}
template <typename T, typename... Args>
- typename T::handle load_persistent(Args&&... args)
+ typename T::ptr load_persistent(Args&&... args)
{
// create serializer for asset type and get cache key
asset_serializer<T> serializer;
@@ -72,7 +71,7 @@ namespace xd
loaded_assets.erase(it2);
}
// not loaded in either map, create it
- typename T::handle resource(new T(std::forward<Args>(args)...));
+ typename T::ptr resource(new T(std::forward<Args>(args)...));
persistent_assets.insert(std::make_pair(cache_key, resource));
return resource;
}
@@ -1,10 +1,6 @@
#ifndef H_XD_ASSET_SERIALIZER
#define H_XD_ASSET_SERIALIZER
-#include <xd/handle.hpp>
-#include <xd/weak_handle.hpp>
-#include <boost/any.hpp>
-
namespace xd
{
template <typename T>
@@ -2,7 +2,8 @@
#define H_XD_AUDIO_MUSIC
#include <xd/config.hpp>
-#include <xd/handle.hpp>
+#include <xd/ref_counted.hpp>
+#include <boost/intrusive_ptr.hpp>
#include <boost/noncopyable.hpp>
#include <string>
@@ -15,11 +16,10 @@ namespace xd
struct music_handle;
}
- class XD_API music : public boost::noncopyable
+ class XD_API music : public xd::ref_counted, public boost::noncopyable
{
public:
- typedef handle<music> handle;
- typedef weak_handle<music> weak_handle;
+ typedef boost::intrusive_ptr<music> ptr;
music(const std::string& filename);
virtual ~music();
@@ -2,7 +2,8 @@
#define H_XD_AUDIO_SOUND
#include <xd/config.hpp>
-#include <xd/handle.hpp>
+#include <xd/ref_counted.hpp>
+#include <boost/intrusive_ptr.hpp>
#include <boost/noncopyable.hpp>
#include <string>
@@ -15,13 +16,14 @@ namespace xd
struct sound_handle;
}
- class XD_API sound : public boost::noncopyable
+ class XD_API sound : public xd::ref_counted, public boost::noncopyable
{
+ private:
+ sound(const std::string& filename);
+
public:
- typedef handle<sound> handle;
- typedef weak_handle<sound> weak_handle;
+ typedef boost::intrusive_ptr<sound> ptr;
- sound(const std::string& filename);
virtual ~sound();
void play();
@@ -2,48 +2,50 @@
#define H_XD_DETAIL_ENTITY
#include <xd/detail/identity.hpp>
-#include <xd/handle.hpp>
+#include <xd/ref_counted.hpp>
+#include <boost/intrusive_ptr.hpp>
+
+namespace xd {
+ template <typename T> class entity;
+}
namespace xd { namespace detail {
template <typename T>
- class component_base
+ class component_base : public xd::ref_counted
{
public:
virtual ~component_base() {}
private:
- friend T;
+ friend xd::entity<T>;
virtual void init(T&) {}
};
template <typename T>
class logic_component : public virtual component_base<T>
{
public:
- typedef xd::handle<detail::logic_component<T>> handle;
- typedef xd::weak_handle<detail::logic_component<T>> weak_handle;
+ typedef boost::intrusive_ptr<detail::logic_component<T>> ptr;
private:
- friend T;
+ friend xd::entity<T>;
virtual void update(T&) = 0;
};
template <typename T>
class render_component : public virtual component_base<T>
{
public:
- typedef xd::handle<detail::render_component<T>> handle;
- typedef xd::weak_handle<detail::render_component<T>> weak_handle;
+ typedef boost::intrusive_ptr<detail::render_component<T>> ptr;
private:
- friend T;
+ friend xd::entity<T>;
virtual void render(T&) = 0;
};
template <typename T>
class component : public logic_component<T>, public render_component<T>
{
public:
- typedef xd::handle<detail::component<T>> handle;
- typedef xd::weak_handle<detail::component<T>> weak_handle;
+ typedef boost::intrusive_ptr<detail::component<T>> ptr;
};
} }
@@ -12,7 +12,7 @@
#define XD_FORWARD(Z, N, D) std::forward<BOOST_PP_CAT(T, N)>(BOOST_PP_CAT(p, N))
template <typename R BOOST_PP_ENUM_TRAILING_PARAMS(XD_N, typename T)>
-typename R::handle create(BOOST_PP_ENUM_BINARY_PARAMS(XD_N, T, && p))
+typename R::ptr create(BOOST_PP_ENUM_BINARY_PARAMS(XD_N, T, && p))
{
return xd::factory::create<R>(BOOST_PP_ENUM(XD_N, XD_FORWARD, _));
}
@@ -12,9 +12,9 @@
#define XD_FORWARD(Z, N, D) std::forward<BOOST_PP_CAT(T, N)>(BOOST_PP_CAT(p, N))
template <typename R BOOST_PP_ENUM_TRAILING_PARAMS(XD_N, typename T)>
-static typename R::handle create(BOOST_PP_ENUM_BINARY_PARAMS(XD_N, T, && p))
+static typename R::ptr create(BOOST_PP_ENUM_BINARY_PARAMS(XD_N, T, && p))
{
- return typename R::handle(new R(BOOST_PP_ENUM(XD_N, XD_FORWARD, _)));
+ return typename R::ptr(new R(BOOST_PP_ENUM(XD_N, XD_FORWARD, _)));
}
#undef XD_FORWARD
View
@@ -3,12 +3,14 @@
#include <xd/detail/entity.hpp>
-#include <xd/handle.hpp>
+#include <xd/ref_counted.hpp>
#include <xd/event_bus.hpp>
+#include <boost/intrusive_ptr.hpp>
#include <boost/config.hpp>
#include <boost/any.hpp>
#include <unordered_map>
#include <functional>
+#include <algorithm>
#include <memory>
#include <list>
#include <map>
@@ -35,28 +37,28 @@ namespace xd
{
};
- class entity_base
+ class entity_placeholder
{
protected:
- ~entity_base() {}
+ ~entity_placeholder() {}
};
- template <typename Base = entity_base>
- class entity : public Base
+ template <typename Class = entity_placeholder>
+ class entity : public xd::ref_counted
{
public:
// required for xd::factory
- typedef xd::handle<entity> handle;
- typedef xd::weak_handle<entity> weak_handle;
- // component handle typedefs
- typedef typename xd::component<entity<Base>>::handle component_handle;
- typedef typename xd::logic_component<entity<Base>>::handle logic_component_handle;
- typedef typename xd::render_component<entity<Base>>::handle render_component_handle;
+ typedef boost::intrusive_ptr<entity> ptr;
+ // component ptr typedefs
+ typedef typename xd::component<Class>::ptr component_ptr;
+ typedef typename xd::logic_component<Class>::ptr logic_component_ptr;
+ typedef typename xd::render_component<Class>::ptr render_component_ptr;
entity()
{
}
+/*
#ifndef BOOST_NO_VARIADIC_TEMPLATES
// constructor that delegates parameters to the entity_base
template <typename... Args>
@@ -74,6 +76,7 @@ namespace xd
: Base(base)
{
}
+*/
virtual ~entity()
{
@@ -155,75 +158,75 @@ namespace xd
get_event_bus<T>()[name](args);
}
- void add_component(const logic_component_handle& component, int priority = 0)
+ void add_component(const logic_component_ptr& component, int priority = 0)
{
m_components[priority].logic_components.push_back(component);
- component->init(*this);
+ component->init(*static_cast<Class*>(this));
}
- void add_component(const render_component_handle& component, int priority = 0)
+ void add_component(const render_component_ptr& component, int priority = 0)
{
m_components[priority].render_components.push_back(component);
- component->init(*this);
+ component->init(*static_cast<Class*>(this));
}
- void add_component(const component_handle& component, int priority = 0)
+ void add_component(const component_ptr& component, int priority = 0)
{
m_components[priority].logic_components.push_back(component);
m_components[priority].render_components.push_back(component);
- component->init(*this);
+ component->init(*static_cast<Class*>(this));
}
- void del_component(const logic_component_handle& component, int priority)
+ void del_component(const logic_component_ptr& component, int priority)
{
logic_component_list_t& components = m_components[priority].logic_components;
- auto i = components.find(component);
+ auto i = std::find(components.begin(), components.end(), component);
if (i != components.end()) {
components.erase(i);
}
}
- void del_component(const logic_component_handle& component)
+ void del_component(const logic_component_ptr& component)
{
for (auto i = m_components.begin(); i != m_components.end(); ++i) {
del_component(component, i->first);
}
}
- void del_component(const render_component_handle& component, int priority)
+ void del_component(const render_component_ptr& component, int priority)
{
logic_component_list_t& components = m_components[priority].logic_components;
- auto i = components.find(component);
+ auto i = std::find(components.begin(), components.end(), component);
if (i != components.end()) {
components.erase(i);
}
}
- void del_component(const render_component_handle& component)
+ void del_component(const render_component_ptr& component)
{
for (auto i = m_components.begin(); i != m_components.end(); ++i) {
del_component(component, i->first);
}
}
- void del_component(const component_handle& component, int priority)
+ void del_component(const component_ptr& component, int priority)
{
components_set& components = m_components[priority];
{
- auto i = components.logic_components.find(component);
+ auto i = std::find(components.logic_components.begin(), components.logic_components.end(), component);
if (i != components.end()) {
components.erase(i);
}
}
{
- auto i = components.render_components.find(component);
+ auto i = std::find(components.render_components.begin(), components.render_components.end(), component);
if (i != components.end()) {
components.erase(i);
}
}
}
- void del_component(const component_handle& component)
+ void del_component(const component_ptr& component)
{
for (auto i = m_components.begin(); i != m_components.end(); ++i) {
del_component(component, i->first);
@@ -239,7 +242,7 @@ namespace xd
{
for (auto i = m_components.begin(); i != m_components.end(); ++i) {
for (auto j = i->second.logic_components.begin(); j != i->second.logic_components.end(); ++j) {
- (*j)->update(*this);
+ (*j)->update(*static_cast<Class*>(this));
}
}
}
@@ -248,14 +251,14 @@ namespace xd
{
for (auto i = m_components.begin(); i != m_components.end(); ++i) {
for (auto j = i->second.render_components.begin(); j != i->second.render_components.end(); ++j) {
- (*j)->render(*this);
+ (*j)->render(*static_cast<Class*>(this));
}
}
}
private:
- typedef std::list<logic_component_handle> logic_component_list_t;
- typedef std::list<render_component_handle> render_component_list_t;
+ typedef std::list<logic_component_ptr> logic_component_list_t;
+ typedef std::list<render_component_ptr> render_component_list_t;
// data
std::unordered_map<std::size_t, boost::any> m_type_to_data;
Oops, something went wrong.

0 comments on commit 71b4bf7

Please sign in to comment.