Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/ggabriel96/lasso

A generic game loop implementation in C++
https://github.com/ggabriel96/lasso

cpp game-development game-loop

Last synced: 3 months ago
JSON representation

A generic game loop implementation in C++

Awesome Lists containing this project

README

        

# lasso

A generic game loop implementation in C++ based on
[Fix Your Timestep!](https://gafferongames.com/post/fix_your_timestep/)

lasso is at early stages of design and development and is subject to
incompatible changes. All feedback is welcome and appreciated!
Detailed documentation is being built on the
[wiki](https://github.com/ggabriel96/lasso/wiki).

# Getting started

This project supports the [Meson](https://mesonbuild.com/) build system,
so you may want to install it (although it is not required). A legacy
[`CMakeLists.txt`](CMakeLists.txt) still exists, but is not
*actively* maintained.
A compiler with
[Concepts TS](https://en.cppreference.com/w/cpp/experimental/constraints)
support (e.g. GCC 6 or above with options `-std=c++2a` and `-fconcepts`)
is required to use lasso. Then, having Meson installed,
run the following commands at the root of this project:

```sh
meson build
cd build && meson install
```

Alternatively, you may just put [`lasso.h`](include/lasso.h) somewhere in
the source tree of your project.

## Using lasso

The `meson install` command above will put (install) it at `/{prefix}/lasso/`
(relative to the default Meson
[paths](https://mesonbuild.com/Builtin-options.html)).
On Linux, that is `/usr/local/include/lasso/`.
Then you just need to have `/usr/local/include/` in your header search path
and you are ready to `#include `.
Conversely, if you have somehow put [`lasso.h`](include/lasso.h) in your
source tree, you need to `#include "lasso.h"`.

lasso knows how to call into your game via the C++ concept below:

```cpp
template concept bool GameLogic =
requires (T logic,
LoopStatus const &status,
duration const &delta) {
{ logic.init() } -> void;
{ logic.simulate(status, delta) } -> void;
{ logic.render(status, delta) } -> void;
{ logic.is_done() } -> bool;
{ logic.terminate() } -> void;
};
```

That means you need a `class` or `struct` that implements the
following member functions:

- `void init();`, which is called once right before the loop starts and you
can use it to initialize anything you need (beyond the constructor of your
class);
- `void simulate(LoopStatus const &, duration const &);`,
which is called if `delta` or more nanoseconds have passed since the last
call to advance your simulation (input, physics, AI, etc.);
- `void render(LoopStatus const &, duration const &);`,
which is called once in every iteration to render what has been simulated;
- `bool is_done();`, which is called once in every iteration to
determine whether the loop should terminate;
- `void terminate();`, which is called once right after the loop ends and you
can use it to clean up anything you need (additionally to what will be
done in the destructor of your class).

Additional member functions may be added to GameLogic
and its existing ones might be modified in the future.
There are no constraints on other member functions or
variables that the class may have.
Examples of classes implementing this concept can be seen in the
[examples folder](examples/), especially the [`Game.h`](examples/Game.h).
and [`Game.cpp`](examples/Game.cpp) blueprints.

After having implemented your class (let us call it `T`),
running it is as simple as:

```cpp
T t;
// ...
lasso::MainLoop{}.run(t); // or pass in T{} directly
```

## Building the examples

The [Simple and Fast Multimedia Library (SFML)](https://github.com/SFML/SFML)
must be installed to compile and run an [example](examples/).
If you want to enable the compilation of the examples, you may run:

```sh
meson build -Dexamples=true
cd build && ninja
```

or, after having initially run Meson and while at the `build/` directory:

```sh
meson configure -Dexamples=true
ninja
```

In order for an example to locate [`Roboto-Regular.ttf`](examples/data/fonts/)
and hence render text, you must run the compiled executable from either the
build or the [`examples/`](examples/) directory (or set any of them as the
working directory in your IDE; preferably the build one).