Good idea or No ?

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
MrGodin
Posts: 721
Joined: November 30th, 2013, 7:40 pm
Location: Merville, British Columbia Canada

Good idea or No ?

Post by MrGodin » December 14th, 2017, 4:01 pm

I was messing around thinking of a systems factory and came up with this. Not sure how good of an idea it is. Systems are created in the main.cpp

Code: Select all

#include "system.h" // simple base class
using SystemID = std::size_t;


inline SystemID getUniqueSystemTypeID()
{
	static SystemID cID = 0;
	return cID++;
}
template <typename T>
inline SystemID getSystemTypeID() noexcept
{
	static_assert(std::is_base_of<System, T>::value,
		"T must must inherit from System");
	static SystemID id = getUniqueSystemTypeID();
	return id;
}

constexpr static  SystemID maxSystems = 32;

using SystemBitSet = std::bitset<maxSystems>;
using SystemArray = std::array<System*, maxSystems>;



class SystemFactory
{
	SystemBitSet m_systemBitset;
	SystemArray m_systemArray;
	std::vector < std::unique_ptr<System> > m_systems;
	template <typename SystemType>
	bool Has()const
	{
		return m_systemBitset[getSystemTypeID<SystemType>()];
	}
public:
	SystemFactory();
	~SystemFactory();
	template <typename SystemType, typename... SystemArgs>
	SystemType& Make(SystemArgs&&... mArgs)
	{
		
		assert(!Has < SystemType >());
		
		SystemType* sys(new SystemType(std::forward<SystemArgs>(mArgs)...));
		std::unique_ptr<System> uPtr{ sys };
		m_systems.emplace_back(std::move(uPtr));
		m_systemArray[getSystemTypeID<SystemType>()] = sys;
		m_systemBitset[getSystemTypeID<SystemType>()] = true;
		return *sys;
	}
	template<typename SystemType>
	SystemType* Get()
	{
		assert(Has < SystemType >());
		return (SystemType*)m_systemArray[getSystemTypeID<SystemType>()];
		
		
	}
};


main.cpp

Code: Select all

int WINAPI WinMain(	HINSTANCE ,	HINSTANCE,	LPSTR,	int )
{
	
	HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);
	
	if (SUCCEEDED(CoInitialize(NULL)))
	{
		m_sysFactory = MakeUnique<SystemFactory>();
	
		// create in this order
		m_sysFactory->Make<D2DWindow>(scrnW, scrnH);
	
		Locator::SetGraphics(&m_sysFactory->Make<Renderer>());
		Locator::SetSoundManager(&m_sysFactory->Make<SoundManager>());
		Locator::SetTextManager(&m_sysFactory->Make<TextManager>());
		Locator::SetImageManager(&m_sysFactory->Make<ImageManager>());
		
		m_sysFactory->Make<Game>(*m_sysFactory->Get<D2DWindow>());
		
		
		SimpleTimer timer;
		timer.Reset();
		
		bool ok = true;
		while (ok)
		{
			timer.Update();
	
			if ((ok = m_sysFactory->Get<D2DWindow>()->RunMessageLoop()))
				 ok = m_sysFactory->Get<Game>()->Play(timer);
		}
		  CoUninitialize();
	}
}
Curiosity killed the cat, satisfaction brought him back

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Good idea or No ?

Post by chili » December 15th, 2017, 4:30 am

What was your motivation for implementing this system?
Chili

MrGodin
Posts: 721
Joined: November 30th, 2013, 7:40 pm
Location: Merville, British Columbia Canada

Re: Good idea or No ?

Post by MrGodin » December 15th, 2017, 5:22 am

I was thinking of another way of doing singletons. I guess my mind wanders but i am trying to use C++ features when i can. I like the static class of Locator so i don't need to pass around different Managers.
So instead of passing lets say .. Object->Draw( Graphics& gfx) .. i'd do something like.
Object::Draw()
{
Locator::Graphics()->.. whatever

}
Object->Update(const float& dt)
{
// do math
if(m_targetAquired)
{

Locator->Audio->Play("shot");
}
}
This sorta of thing. I used to do it all in the Game class (create managers ect) but i felt this way was better.
Curiosity killed the cat, satisfaction brought him back

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Good idea or No ?

Post by chili » December 15th, 2017, 5:46 am

But I guess the question is 'why'? What advantages are you trying to gain by doing singletons in this different way?
Chili

MrGodin
Posts: 721
Joined: November 30th, 2013, 7:40 pm
Location: Merville, British Columbia Canada

Re: Good idea or No ?

Post by MrGodin » December 15th, 2017, 5:53 am

I suppose it boils down to a personal preference. As a musician as well, I have always tried to find my little way of doing shit, as i am sure you ca appreciate ;)..
Peace
Curiosity killed the cat, satisfaction brought him back

MrGodin
Posts: 721
Joined: November 30th, 2013, 7:40 pm
Location: Merville, British Columbia Canada

Re: Good idea or No ?

Post by MrGodin » December 15th, 2017, 5:55 am

and ... templates are quicker :P lol
Curiosity killed the cat, satisfaction brought him back

Post Reply