Skip to content
/ r2 Public

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

License

Notifications You must be signed in to change notification settings

robrohan/r2

Repository files navigation

ℝ² Simple, C99 Vector, Matrix, UTF8 String, and Terminal Library

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

C/C++ CI

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:

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:

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:
$ cd my_project
$ mkdir vendor
$ cd vendor
$ git submodule add https://github.com/robrohan/r2.git
  1. Then the vendor directory to your compiler flags: -I./vendor
  2. In the main part of your application (only once), import the library with the implementation flag:
...
# include <limits.h>
# define R2_MATHS_IMPLEMENTATION
# include "r2/r2_maths.h"
# include <string.h>
...
  1. Anywhere else you need the functions, use the include without the flag:
# include <limits.h>
# include "r2/r2_maths.h"
# include <string.h>
  1. Go for gold. See tests/r2_maths.c for some example usages, or look at r2_maths.h for reference.
  vec3 v1 = {.x = 3.f, .y = 3.f, .z = 3.f};
  vec3 v2 = {.x = -30.f, .y = 30.f, .z = -30.f};
  vec4 *out = calloc(sizeof(vec4), 1);
  vec3_cross(&v1, &v2, out);

Getting Started / Running

Testing

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, working, and the emcc environment variables setup:

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

Then just run make test_wasm or if you wish run test.sh passing in the emcc compiler and output to html:

CC=emcc OUT=run_tests.html ./test.sh

You can then use run some sort of http server within the main directory (busboy for example), and then load the HTML page in your browser.