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

https://github.com/robrohan/r2

A vector, quaternion, and matrix single-file public domain library for C99
https://github.com/robrohan/r2

c c99 game-2d game-development gamedev maths matrix matrix-multiplication

Last synced: 5 months ago
JSON representation

A vector, quaternion, and matrix single-file public domain library for C99

Awesome Lists containing this project

README

          

# ℝ² Some Simple, C99 Vector, Matrix, UTF8 String, and Terminal Libraries

Single-file public domain headers (or MIT licensed) libraries for C (C99)

![C/C++ CI](https://github.com/robrohan/r2/workflows/C/C++%20CI/badge.svg)

The main goal of these libraries is to be fast, and usable for games
programming - with a sprinkle of machine learning and general maths.

This library is in the style of: https://github.com/nothings/stb

Current Libraries:

- Vector, matrix and quaternion: [r2_maths.h](./r2_maths.h)
- UTF-8 String library: [r2_strings.h](./r2_strings.h)
- Simple ncurses like library thing: [r2_termui.h](./r2_termui.h)
- Minimal unit testing: [r2_unit.h](./r2_unit.h)

## Installation

Copy any of the header files you want to use into your code base. See the
header files for instructions (mostly you just include them).

Example:

```bash
curl https://raw.githubusercontent.com/robrohan/r2/main/r2_termui.h > ./src/r2_termui.h
```

## Using the Vector, Quaternion, and Matrix Functions

1. Clone this library as a git submodule into a target project:

```bash
$ cd my_project
$ mkdir vendor
$ cd vendor
$ git submodule add https://github.com/robrohan/r2.git
```

2. Then the vendor directory to your compiler flags: `-I./vendor`
3. In the _main_ part of your application (only once), import the library with
the implementation flag:

```c
...
# include
# define R2_MATHS_IMPLEMENTATION
# include "r2/r2_maths.h"
# include
...
```

4. Anywhere else you need the functions, use the include without the flag:

```c
# include
# include "r2/r2_maths.h"
# include
```

5. Go for gold. See `tests/r2_maths.c` for some example usages, or look at
`r2_maths.h` for reference.

```c
vec3 v1 = {.x = 3.f, .y = 3.f, .z = 3.f};
vec3 v2 = {.x = -30.f, .y = 30.f, .z = -30.f};
vec4 out = {0};
vec3_cross(&v1, &v2, &out);
```

### Testing

```sh
make test
```

_Note_: If you are on windows, currently, you'll have to write something like
a `test.bat` yourself (or some magic to import the files into Visual Studio).
You can use `test.sh` as a template.

### Testing in Web Assembly

To compile and run the code in web assembly, first make sure you have
[emscripten setup](https://emscripten.org/docs/getting_started/downloads.html),
working, and the `emcc` environment variables setup:

```sh
source ~/Projects/spikes/emsdk/emsdk_env.sh
```

Then just run `make test_wasm` passing in the `emcc` compiler and output to `html`:

```bash
make test_wasm
```

You can then use run some sort of http server against the `bin` directory. For
example ([busboy](https://github.com/robrohan/busboy)), or python. Then load
the HTML page in a browser.