Giter Site home page Giter Site logo

unterrainerinformatik / pooling Goto Github PK

View Code? Open in Web Editor NEW
0.0 4.0 0.0 34 KB

This class implements a lock-free object pool for your game-code that sports automatic creation and events to hook.

License: The Unlicense

C# 97.12% Batchfile 2.88%
pool lock-free hooks game events no-gc

pooling's Introduction

NuGet NuGet license Twitter Follow

General

This section contains various useful projects that should help your development-process.

This section of our GIT repositories is free. You may copy, use or rewrite every single one of its contained projects to your hearts content.
In order to get help with basic GIT commands you may try the GIT cheat-sheet on our homepage.

This repository located on our homepage is private since this is the master- and release-branch. You may clone it, but it will be read-only.
If you want to contribute to our repository (push, open pull requests), please use the copy on github located here: the public github repository

IconPooling

This class implements a lock-free object pool.
Such things are very handy when developing games because you wouldn't want to run the garbage collector at all in order to reduce lags. But there are times when you have to use a class instead of a struct and that's the point where you want to hold on to your objects and re-use them.

This pool does exactly that and it offers you some convenience features like events to hook into or automatic object-creation (using variable constructor signatures) as well.

Just make the class you'd like to pool implement the PoolItem interface, create a new pool and give it the right constructor fields that are needed to create your object.
After using the object, just return it to the pool.

This pool isn't created using a fixed size, but is allowed to grow as big as you need it to be while reusing all objects as much as it can.

If you like this repo, please don't forget to star it. Thank you.

Example

Pool<Sprite> PoolInstance = new Pool<Sprite>(new object[] {spriteBatch, game, tokens.AttackSpriteToken});
// Optional: Maybe you'd like to add a event handler.
// We'll just call a method on the sprite class upon return of the object:
PoolInstance.Returned += (sender, e) => e.Item.Return();

Then, later on, retrieve a new or reused item from the pool:

Sprite s = PoolInstance.Get();

...
s.Update(gameTime);
...

This either gives you a reused sprite, or, when needed, automatically creates a new Sprite from scratch.

When you're done with it, return it to the pool:

PoolInstance.Return(s);
// If you've specified the event hanlder above, this will be the time when it's called.

After you're done, clean up the pool:

PoolInstance.Dispose();

That takes care of your registered events and objects in the pool.

pooling's People

Contributors

gufalcon avatar

Watchers

 avatar  avatar  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.