Giter Site home page Giter Site logo

fishinggrapes / ec_framework Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 38 KB

A simple and efficient implementation of Entity-Component architecture using custom allocators for improved cache locality of components.

License: MIT License

C++ 100.00%

ec_framework's Introduction

EC_Framework

An implementation of Entity-Component architecture inspired by Unity3D and Unreal Engine.

Features

  • A Entity-Componenet framework with a simple API, where GameObjects are composed of components.
  • Fast iterations through components using a custom allocator which ensures that components of a given type are always tightly-packed together.
  • Safety Measures to avoid adding duplicate components to GameObjects, removing or accessing non-existent components from GameObjects.
  • Automatic deallocation of components and it's resources on GameObject destruction. This means, No need to worry about components once added.
  • The framework also provides methods to remove a component on-demand from a GameObject.

Usage

Include the header GameObject.h wherever ypu need to interact with GameObjects and Components in your project. Include the header ComponentRegistry.h where you have the game loop, so as to call component callbacks. Following is an example of how to use the framework:

#include <iostream>
#include <Windows.h>
#include <WinUser.h>

//Include this wherever you need to interact with GameObjects and Components
#include "GameObject.h"
//Include this in the file with the game loop so as to call the component callbacks accordingly
#include "ComponentRegistry.h"

//These are user-created components 
#include "Transform.h"
#include "MeshRenderer.h"
#include "Tag.h"

int main( )
{
	//Register components so that only their selected virtual functions are called
        //ComponenetRegistry::Register<T>(update, render);
	ComponentRegistry::Register<Transform>( true, false );
	ComponentRegistry::Register<MeshRenderer>( false, true );
	ComponentRegistry::Register<Tag>( false, false );

        //Instantiating a GameObject
	GameObject* simpleObject = GameObject::Instantiate( "Simple Object" );
	dptr<Transform> t1 = simpleObject->AddComponent<Transform>( );
	dptr<Tag> tag1 = simpleObject->AddComponent<Tag>( );

	//Tring to add multiple components of the same type to a GameObject
	dptr<Tag> tag2 = simpleObject->AddComponent<Tag>( );


	GameObject* meshObject = GameObject::Instantiate( "Player Object" );
	dptr<Transform> t2 = meshObject->AddComponent<Transform>( );
	dptr<MeshRenderer> m1 = meshObject->AddComponent<MeshRenderer>( );

	//Removing a component from a GameObject
	simpleObject->RemoveComponent<Tag>( );
  
	//Trying to get a component that doesn't exist (results in nullptr)
	dptr<Tag> tag3 = simpleObject->GetComponent<Tag>( );
	if (tag3 == nullptr)
	{
		std::cout << simpleObject->GetName( ) << " doesn't have a Tag." << std::endl;
	}
  
	//Querying if a component exists in the GameObject
	bool hasTransform = simpleObject->HasComponent<Transform>( );
	if (hasTransform)
	{
		std::cout << simpleObject->GetName( ) << " has a Transform." << std::endl;
	}


        //Game Loop
	bool running = true;
	while (running)
	{
                //Calls OnUpdate on all components which has registred it's OnUpdate function
		ComponentRegistry::Update( );
		ComponentRegistry::Render( );

		//Escape from the game loop
		if (GetKeyState( VK_ESCAPE ) & 0x8000)
		{
			running = false;
		}
	}

	//Destroys all the components composing the GameObject and the GameObject itself
	GameObject::Destroy( meshObject );
	GameObject::Destroy( simpleObject );
  
  return 0;
}

And, here is how to setup a component:

#include <iostream>
#include "Component.h"

//256 is the block size used by the memory allocator
class Transform : public Component<Transform, 256>
{
public:
	std::vector<float_t> Position;
	std::vector<float_t> Orientation;
	std::vector<float_t> Scale;

	Transform( )
	{
		Position.resize( 3, 0.0f );
		Orientation.resize( 3, 0.0f );
		Scale.resize( 3, 0.0f );
	}

	~Transform( )
	{
	}

	virtual void OnStart( ) override
	{
		std::cout << "Instantiated Transform of " << gameObject->GetName( ) << std::endl;
	}
	virtual void OnUpdate( ) override
	{
		std::cout << "Transform of " << gameObject->GetName( ) << " Updated." << std::endl;
	}
	virtual void OnRender( ) override
	{

	}
	virtual void OnDestroy( ) override
	{
		std::cout << "Destroyed Transform of " << gameObject->GetName( ) << std::endl;
	}

};

Growing List Allocator

growinglistallocator

I needed to iterate through a collection of components(similar to Unity3D) every frame. There are game objects which are composed of components and these components contain behaviour like Transform, Camera, Light, Mesh Renderer and so on, which need to be updated every frame. I could not afford to iterate through components at different places in memory. So I wrote this allocactor which is nothing but a list of fixed-size memory blocks and actively keeps the objects tightly packed on allocation and deallocation, so as to ensure the best cache locality and fast iterations.

ec_framework's People

Contributors

fishinggrapes avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.