mirror of
https://github.com/HandmadeMath/HandmadeMath.git
synced 2025-09-05 17:58:14 +00:00
Style and docs pass for release
Remove V2I, V3I, V4I (and style changes) Totally useless. Add update tool, update docs Tweak docs Create ci.yml Big style pass Maybe fix CI on Windows Report coverage errors Fix a missing coverage case Try setting up MSVC another way Update readmes Fix remaining use of the name UpdateTool
This commit is contained in:
@@ -1,9 +0,0 @@
|
||||
root = true
|
||||
|
||||
[*.{c,cpp,h}]
|
||||
charset = utf-8
|
||||
indent_style = space
|
||||
indent_size = 4
|
||||
end_of_line = lf
|
||||
insert_final_newline = true
|
||||
trim_trailing_whitespace = true
|
34
.github/workflows/ci.yml
vendored
Normal file
34
.github/workflows/ci.yml
vendored
Normal file
@@ -0,0 +1,34 @@
|
||||
name: CI
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ "master" ]
|
||||
pull_request:
|
||||
branches: [ "master" ]
|
||||
|
||||
jobs:
|
||||
test:
|
||||
name: Test on ${{ matrix.os }}
|
||||
runs-on: ${{ matrix.os }}
|
||||
strategy:
|
||||
matrix:
|
||||
os: [ubuntu-latest, windows-latest, macOS-latest]
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: TheMrMilchmann/setup-msvc-dev@v2
|
||||
with:
|
||||
arch: x64
|
||||
if: matrix.os == 'windows-latest'
|
||||
- name: Test (Windows, MSVC)
|
||||
run: ./run_test_msvc.bat
|
||||
working-directory: ./test
|
||||
if: matrix.os == 'windows-latest'
|
||||
- name: Test (Windows, clang)
|
||||
run: ./run_test_clang.bat
|
||||
working-directory: ./test
|
||||
if: matrix.os == 'windows-latest'
|
||||
- name: Test (${{ matrix.os }})
|
||||
run: make all
|
||||
working-directory: ./test
|
||||
if: matrix.os != 'windows-latest'
|
0
.gitmodules
vendored
0
.gitmodules
vendored
23
.travis.yml
23
.travis.yml
@@ -1,23 +0,0 @@
|
||||
language: cpp
|
||||
os:
|
||||
- linux
|
||||
- osx
|
||||
compiler:
|
||||
- clang
|
||||
- gcc
|
||||
matrix:
|
||||
include:
|
||||
# Windows x64 builds (MSVC)
|
||||
- os: windows
|
||||
script:
|
||||
- ./test.bat travis
|
||||
|
||||
before_install:
|
||||
- eval "${MATRIX_EVAL}"
|
||||
install:
|
||||
- cd test
|
||||
script:
|
||||
- make c
|
||||
- make c_no_sse
|
||||
- make cpp
|
||||
- make cpp_no_sse
|
@@ -1,43 +0,0 @@
|
||||
# Understanding the structure of Handmade Math
|
||||
|
||||
Most of the functions in Handmade Math are very short, and are the kind of functions you want to have inlined. Because of this, most functions in Handmade Math are defined with `HINLINE`, which is defined as `static inline`.
|
||||
|
||||
The exceptions are functions like `HMM_Rotate`, which are long enough that it doesn't make sense to inline them. These functions are defined with an `HEXTERN` prototype, and implemented in the `#ifdef HANDMADE_MATH_IMPLEMENTATION` block.
|
||||
|
||||
# Quick style guide
|
||||
|
||||
* Put braces on a new line
|
||||
* Float literals should have digits both before and after the decimal.
|
||||
```cpp
|
||||
// Good
|
||||
0.0f;
|
||||
0.5f;
|
||||
1.0f;
|
||||
3.14159f;
|
||||
|
||||
// Bad
|
||||
1.f
|
||||
.0f
|
||||
```
|
||||
* Put parentheses around the returned value:
|
||||
```cpp
|
||||
HINLINE float
|
||||
HMM_MyFunction()
|
||||
{
|
||||
return (1.0f);
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
## Other style notes
|
||||
|
||||
* If a new function is defined with different names for different datatypes, also add C++ overloaded versions of the functions. For example, if you have `HMM_LengthVec2(hmm_vec2)` and `HMM_LengthVec3(hmm_vec3)`, also provide `HMM_Length(hmm_vec2)` and `HMM_Length(hmm_vec3)`.
|
||||
|
||||
It is fine for the overloaded versions to call the C versions.
|
||||
* Only use operator overloading for analogous operators in C. That means `+` for vector addition is fine, but no using `^` for cross product or `|` for dot product.
|
||||
* Try to define functions in the same order as the prototypes.
|
||||
* Don't forget that Handmade Math uses column-major order for matrices!
|
||||
|
||||
# Versioning
|
||||
|
||||
We use [semantic versioning](http://semver.org/) because it's reasonable.
|
2113
HandmadeMath.h
2113
HandmadeMath.h
File diff suppressed because it is too large
Load Diff
61
README.md
61
README.md
@@ -1,52 +1,25 @@
|
||||
# Handmade Math
|
||||
|
||||
[](https://travis-ci.org/StrangeZak/Handmade-Math)
|
||||
A single-file, cross-platform, public domain game math library for both C and C++. Supports vectors, matrices, quaternions, and all the utilities you'd expect.
|
||||
|
||||
A single-file, cross-platform, public domain game math library for C/C++.
|
||||
To get started, go download [the latest release](https://github.com/HandmadeMath/HandmadeMath/releases).
|
||||
|
||||
To get started, go download [the latest release](https://github.com/HandmadeMath/Handmade-Math/releases).
|
||||
> If you are upgrading to version 2 of Handmade Math, save yourself some time and use our [automatic update tool](./update).
|
||||
|
||||
-----
|
||||
Here's what sets Handmade Math apart:
|
||||
|
||||
Version | Changes |
|
||||
----------------|----------------|
|
||||
**1.13.0** | Fully inlined HandmadeMath.h. No need for HANDMADE_MATH_IMPLEMENTATION anymore |
|
||||
**1.12.1** | Added extra parentheses around some macros |
|
||||
**1.12.0** | Added Unary Minus operator for `HMM_Vec2`, `HMM_Vec3`, and `HMM_Vec4`. |
|
||||
**1.11.1** | Added HMM_PREFIX macro to a few functions that were missing it. |
|
||||
**1.11.0** | Added ability to customize or remove the default `HMM_` prefix on function names by defining a macro called `HMM_PREFIX(name)`. |
|
||||
**1.10.1** | Removed stdint.h, this doesn't exist on some really old compilers and we didn't really use it anyways. |
|
||||
**1.10.0** | Made HMM_Perspective use vertical FOV instead of horizontal FOV for consistency with other graphics APIs. |
|
||||
**1.9.0** | Added SSE versions of quaternion operations. |
|
||||
**1.8.0** | Added fast vector normalization routines that use fast inverse square roots.
|
||||
**1.7.1** | Changed operator[] to take a const ref int instead of an int.
|
||||
**1.7.0** | Renamed the 'Rows' member of hmm_mat4 to 'Columns'. Since our matrices are column-major, this should have been named 'Columns' from the start. 'Rows' is still present, but has been deprecated.
|
||||
**1.6.0** | Added array subscript operators for vector and matrix types in C++. This is provided as a convenience, but be aware that it may incur an extra function call in unoptimized builds.
|
||||
**1.5.1** | Fixed a bug with uninitialized elements in HMM_LookAt.
|
||||
**1.5.0** | Changed internal structure for better performance and inlining. As a result, `HANDMADE_MATH_NO_INLINE` has been removed and no longer has any effect.
|
||||
**1.4.0** | Fixed bug when using C mode. SSE'd all vec4 operations. Removed zeroing for better performance.
|
||||
**1.3.0** | Removed need to `#define HANDMADE_MATH_CPP_MODE`. C++ definitions are now included automatically in C++ environments.
|
||||
**1.2.0** | Added equality functions for `HMM_Vec2`, `HMM_Vec3`, and `HMM_Vec4`, and SSE'd `HMM_MultiplyMat4` and `HMM_Transpose`.
|
||||
**1.1.5** | Added `Width` and `Height` to `HMM_Vec2`, and made it so you can supply your own `SqrtF`.
|
||||
**1.1.4** | Fixed SSE being included on platforms that don't support it, and fixed divide-by-zero errors when normalizing zero vectors.
|
||||
**1.1.3** | Fixed compile error in C mode
|
||||
**1.1.2** | Fixed invalid HMMDEF's in the function definitions
|
||||
**1.1.1** | Resolved compiler warnings on gcc and g++
|
||||
**1.1** | Quaternions! |
|
||||
**1.0** | Lots of testing |
|
||||
**0.7** | Added HMM_Vec2, and HMM_Vec4 versions of HMM_LengthSquared, HMM_Length, and HMM_Normalize. |
|
||||
**0.6** | Made HMM_Power faster, Fixed possible efficiency problem with HMM_Normalize, RENAMED HMM_LengthSquareRoot to HMM_LengthSquared, RENAMED HMM_RSqrtF to HMM_RSquareRootF, RENAMED HMM_SqrtF to HMM_SquareRootF, REMOVED Inner function (user should use Dot now), REMOVED HMM_FastInverseSquareRoot function declaration |
|
||||
**0.5.2** | Fixed SSE code in HMM_SqrtF and HMM_RSqrtF |
|
||||
**0.5.1** | Fixed HMM_Translate producing row-major matrices, ensured column-major order for matrices throughout |
|
||||
**0.5** | Added scalar operations on vectors and matrices, added += and -= for hmm_mat4, reconciled headers and implementations, tidied up in general |
|
||||
**0.4** | Added SSE Optimized HMM_SqrtF, HMM_RSqrtF, Removed use of C Runtime |
|
||||
**0.3** | Added +=,-=, *=, /= for hmm_vec2, hmm_vec3, hmm_vec4 |
|
||||
**0.2b** | Disabled warning C4201 on MSVC, Added 64bit precision on HMM_PI |
|
||||
**0.2a** | Prefixed Macros |
|
||||
**0.2** | Updated Documentation, Fixed C Compliance, Prefixed all functions, and added better operator overloading |
|
||||
**0.1** | Initial Version |
|
||||
- **A simple single-header library.** Just `#include "HandmadeMath.h"`.
|
||||
- **Supports both C and C++.** While libraries like GLM only support C++, Handmade Math supports both C and C++, with convenient overloads wherever possible. For example, C++ codebases get operator overloading, and C11 codebases get `_Generic` versions of common operations.
|
||||
- **Swizzling, sort of.** Handmade Math's vector types use unions to provide several ways of accessing the same underlying data. For example, the components of an `HMM_Vec3` can be accessed as `XYZ`, `RGB`, or `UVW` - or subsets can be accessed like `.XY` and `.YZ`.
|
||||
- **Your choice of angle unit.** While Handmade Math uses radians by default, you can configure it to use degrees or [turns](https://www.computerenhance.com/p/turns-are-better-than-radians) instead.
|
||||
|
||||
|
||||
## Usage
|
||||
|
||||
Simply `#include "HandmadeMath.h"`. All functions are `static inline`, so no need for an "implementation" file as with some other single-header libraries.
|
||||
|
||||
A few config options are available. See the header comment in [the source](./HandmadeMath.h) for details.
|
||||
|
||||
-----
|
||||
|
||||
## FAQ
|
||||
|
||||
@@ -57,3 +30,7 @@ This library is in the public domain. You can do whatever you want with it.
|
||||
**Where can I contact you to ask questions?**
|
||||
|
||||
Feel free to make Github issues for any questions, concerns, or problems you encounter.
|
||||
|
||||
**What if I don't want the `HMM_` prefix?**
|
||||
|
||||
Do a find and replace in the library source.
|
||||
|
@@ -210,6 +210,8 @@ hmt_category* _hmt_categories = 0;
|
||||
int _hmt_num_covercases = 0;
|
||||
hmt_covercase* _hmt_covercases = 0;
|
||||
|
||||
int _hmt_num_covererrors = 0;
|
||||
|
||||
hmt_category _hmt_new_category(const char* name) {
|
||||
hmt_category cat = {
|
||||
name, // name
|
||||
@@ -299,6 +301,7 @@ void _hmt_count_cover(const char* name, int line) {
|
||||
hmt_covercase* covercase = _hmt_find_covercase(name);
|
||||
if (covercase == 0) {
|
||||
printf(HMT_RED "ERROR (line %d): Could not find coverage case with name \"%s\".\n" HMT_RESET, line, name);
|
||||
_hmt_num_covererrors++;
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -391,6 +394,11 @@ int hmt_check_all_coverage() {
|
||||
|
||||
printf("\n");
|
||||
|
||||
if (_hmt_num_covererrors > 0) {
|
||||
printf(HMT_RED "There were %d other coverage errors; scroll up to see them.\n", _hmt_num_covererrors);
|
||||
return 1;
|
||||
}
|
||||
|
||||
return (count_failures > 0);
|
||||
}
|
||||
|
||||
|
@@ -6,7 +6,6 @@ TEST(Initialization, Vectors)
|
||||
// Test vec2
|
||||
//
|
||||
HMM_Vec2 v2 = HMM_V2(1.0f, 2.0f);
|
||||
HMM_Vec2 v2i = HMM_V2I(1, 2);
|
||||
|
||||
EXPECT_FLOAT_EQ(v2.X, 1.0f);
|
||||
EXPECT_FLOAT_EQ(v2.Y, 2.0f);
|
||||
@@ -23,26 +22,10 @@ TEST(Initialization, Vectors)
|
||||
EXPECT_FLOAT_EQ(v2[1], 2.0f);
|
||||
#endif
|
||||
|
||||
EXPECT_FLOAT_EQ(v2i.X, 1.0f);
|
||||
EXPECT_FLOAT_EQ(v2i.Y, 2.0f);
|
||||
EXPECT_FLOAT_EQ(v2i.U, 1.0f);
|
||||
EXPECT_FLOAT_EQ(v2i.V, 2.0f);
|
||||
EXPECT_FLOAT_EQ(v2i.Left, 1.0f);
|
||||
EXPECT_FLOAT_EQ(v2i.Right, 2.0f);
|
||||
EXPECT_FLOAT_EQ(v2i.Width, 1.0f);
|
||||
EXPECT_FLOAT_EQ(v2i.Height, 2.0f);
|
||||
EXPECT_FLOAT_EQ(v2i.Elements[0], 1.0f);
|
||||
EXPECT_FLOAT_EQ(v2i.Elements[1], 2.0f);
|
||||
#ifdef __cplusplus
|
||||
EXPECT_FLOAT_EQ(v2i[0], 1.0f);
|
||||
EXPECT_FLOAT_EQ(v2i[1], 2.0f);
|
||||
#endif
|
||||
|
||||
//
|
||||
// Test vec3
|
||||
//
|
||||
HMM_Vec3 v3 = HMM_V3(1.0f, 2.0f, 3.0f);
|
||||
HMM_Vec3 v3i = HMM_V3I(1, 2, 3);
|
||||
|
||||
EXPECT_FLOAT_EQ(v3.X, 1.0f);
|
||||
EXPECT_FLOAT_EQ(v3.Y, 2.0f);
|
||||
@@ -70,37 +53,10 @@ TEST(Initialization, Vectors)
|
||||
EXPECT_FLOAT_EQ(v3[2], 3.0f);
|
||||
#endif
|
||||
|
||||
EXPECT_FLOAT_EQ(v3i.X, 1.0f);
|
||||
EXPECT_FLOAT_EQ(v3i.Y, 2.0f);
|
||||
EXPECT_FLOAT_EQ(v3i.Z, 3.0f);
|
||||
EXPECT_FLOAT_EQ(v3i.U, 1.0f);
|
||||
EXPECT_FLOAT_EQ(v3i.V, 2.0f);
|
||||
EXPECT_FLOAT_EQ(v3i.W, 3.0f);
|
||||
EXPECT_FLOAT_EQ(v3i.R, 1.0f);
|
||||
EXPECT_FLOAT_EQ(v3i.G, 2.0f);
|
||||
EXPECT_FLOAT_EQ(v3i.B, 3.0f);
|
||||
EXPECT_FLOAT_EQ(v3i.Elements[0], 1.0f);
|
||||
EXPECT_FLOAT_EQ(v3i.Elements[1], 2.0f);
|
||||
EXPECT_FLOAT_EQ(v3i.Elements[2], 3.0f);
|
||||
EXPECT_FLOAT_EQ(v3i.XY.Elements[0], 1.0f);
|
||||
EXPECT_FLOAT_EQ(v3i.XY.Elements[1], 2.0f);
|
||||
EXPECT_FLOAT_EQ(v3i.YZ.Elements[0], 2.0f);
|
||||
EXPECT_FLOAT_EQ(v3i.YZ.Elements[1], 3.0f);
|
||||
EXPECT_FLOAT_EQ(v3i.UV.Elements[0], 1.0f);
|
||||
EXPECT_FLOAT_EQ(v3i.UV.Elements[1], 2.0f);
|
||||
EXPECT_FLOAT_EQ(v3i.VW.Elements[0], 2.0f);
|
||||
EXPECT_FLOAT_EQ(v3i.VW.Elements[1], 3.0f);
|
||||
#ifdef __cplusplus
|
||||
EXPECT_FLOAT_EQ(v3i[0], 1.0f);
|
||||
EXPECT_FLOAT_EQ(v3i[1], 2.0f);
|
||||
EXPECT_FLOAT_EQ(v3i[2], 3.0f);
|
||||
#endif
|
||||
|
||||
//
|
||||
// Test vec4
|
||||
//
|
||||
HMM_Vec4 v4 = HMM_V4(1.0f, 2.0f, 3.0f, 4.0f);
|
||||
HMM_Vec4 v4i = HMM_V4I(1, 2, 3, 4);
|
||||
HMM_Vec4 v4v = HMM_V4V(v3, 4.0f);
|
||||
|
||||
EXPECT_FLOAT_EQ(v4.X, 1.0f);
|
||||
@@ -132,35 +88,6 @@ TEST(Initialization, Vectors)
|
||||
EXPECT_FLOAT_EQ(v4[3], 4.0f);
|
||||
#endif
|
||||
|
||||
EXPECT_FLOAT_EQ(v4i.X, 1.0f);
|
||||
EXPECT_FLOAT_EQ(v4i.Y, 2.0f);
|
||||
EXPECT_FLOAT_EQ(v4i.Z, 3.0f);
|
||||
EXPECT_FLOAT_EQ(v4i.W, 4.0f);
|
||||
EXPECT_FLOAT_EQ(v4i.R, 1.0f);
|
||||
EXPECT_FLOAT_EQ(v4i.G, 2.0f);
|
||||
EXPECT_FLOAT_EQ(v4i.B, 3.0f);
|
||||
EXPECT_FLOAT_EQ(v4i.A, 4.0f);
|
||||
EXPECT_FLOAT_EQ(v4i.XY.Elements[0], 1.0f);
|
||||
EXPECT_FLOAT_EQ(v4i.XY.Elements[1], 2.0f);
|
||||
EXPECT_FLOAT_EQ(v4i.YZ.Elements[0], 2.0f);
|
||||
EXPECT_FLOAT_EQ(v4i.YZ.Elements[1], 3.0f);
|
||||
EXPECT_FLOAT_EQ(v4i.ZW.Elements[0], 3.0f);
|
||||
EXPECT_FLOAT_EQ(v4i.ZW.Elements[1], 4.0f);
|
||||
EXPECT_FLOAT_EQ(v4i.XY.Elements[0], 1.0f);
|
||||
EXPECT_FLOAT_EQ(v4i.XY.Elements[1], 2.0f);
|
||||
EXPECT_FLOAT_EQ(v4i.XYZ.Elements[0], 1.0f);
|
||||
EXPECT_FLOAT_EQ(v4i.XYZ.Elements[1], 2.0f);
|
||||
EXPECT_FLOAT_EQ(v4i.XYZ.Elements[2], 3.0f);
|
||||
EXPECT_FLOAT_EQ(v4i.RGB.Elements[0], 1.0f);
|
||||
EXPECT_FLOAT_EQ(v4i.RGB.Elements[1], 2.0f);
|
||||
EXPECT_FLOAT_EQ(v4i.RGB.Elements[2], 3.0f);
|
||||
#ifdef __cplusplus
|
||||
EXPECT_FLOAT_EQ(v4i[0], 1.0f);
|
||||
EXPECT_FLOAT_EQ(v4i[1], 2.0f);
|
||||
EXPECT_FLOAT_EQ(v4i[2], 3.0f);
|
||||
EXPECT_FLOAT_EQ(v4i[3], 4.0f);
|
||||
#endif
|
||||
|
||||
EXPECT_FLOAT_EQ(v4v.X, 1.0f);
|
||||
EXPECT_FLOAT_EQ(v4v.Y, 2.0f);
|
||||
EXPECT_FLOAT_EQ(v4v.Z, 3.0f);
|
||||
@@ -193,6 +120,39 @@ TEST(Initialization, Vectors)
|
||||
|
||||
TEST(Initialization, MatrixEmpty)
|
||||
{
|
||||
//
|
||||
// Test mat2
|
||||
//
|
||||
HMM_Mat2 m2 = HMM_M2();
|
||||
for (int Column = 0; Column < 2; ++Column)
|
||||
{
|
||||
for (int Row = 0; Row < 2; ++Row)
|
||||
{
|
||||
EXPECT_FLOAT_EQ(m2.Elements[Column][Row], 0.0f);
|
||||
#ifdef __cplusplus
|
||||
EXPECT_FLOAT_EQ(m2[Column][Row], 0.0f);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Test mat3
|
||||
//
|
||||
HMM_Mat3 m3 = HMM_M3();
|
||||
for (int Column = 0; Column < 3; ++Column)
|
||||
{
|
||||
for (int Row = 0; Row < 3; ++Row)
|
||||
{
|
||||
EXPECT_FLOAT_EQ(m3.Elements[Column][Row], 0.0f);
|
||||
#ifdef __cplusplus
|
||||
EXPECT_FLOAT_EQ(m3[Column][Row], 0.0f);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Test mat4
|
||||
//
|
||||
HMM_Mat4 m4 = HMM_M4();
|
||||
for (int Column = 0; Column < 4; ++Column)
|
||||
{
|
||||
@@ -208,6 +168,41 @@ TEST(Initialization, MatrixEmpty)
|
||||
|
||||
TEST(Initialization, MatrixDiagonal)
|
||||
{
|
||||
//
|
||||
// Test mat2
|
||||
//
|
||||
HMM_Mat2 m2d = HMM_M2D(1.0f);
|
||||
for (int Column = 0; Column < 2; ++Column)
|
||||
{
|
||||
for (int Row = 0; Row < 2; ++Row)
|
||||
{
|
||||
if (Column == Row) {
|
||||
EXPECT_FLOAT_EQ(m2d.Elements[Column][Row], 1.0f);
|
||||
} else {
|
||||
EXPECT_FLOAT_EQ(m2d.Elements[Column][Row], 0.0f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Test mat3
|
||||
//
|
||||
HMM_Mat3 m3d = HMM_M3D(1.0f);
|
||||
for (int Column = 0; Column < 3; ++Column)
|
||||
{
|
||||
for (int Row = 0; Row < 3; ++Row)
|
||||
{
|
||||
if (Column == Row) {
|
||||
EXPECT_FLOAT_EQ(m3d.Elements[Column][Row], 1.0f);
|
||||
} else {
|
||||
EXPECT_FLOAT_EQ(m3d.Elements[Column][Row], 0.0f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
// Test mat4
|
||||
//
|
||||
HMM_Mat4 m4d = HMM_M4D(1.0f);
|
||||
for (int Column = 0; Column < 4; ++Column)
|
||||
{
|
||||
@@ -244,3 +239,83 @@ TEST(Initialization, Quaternion)
|
||||
EXPECT_FLOAT_EQ(qv.Z, 3.0f);
|
||||
EXPECT_FLOAT_EQ(qv.W, 4.0f);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
TEST(Initialization, MatrixOverloads)
|
||||
{
|
||||
// Operator overloads for matrix columns must allow mutation.
|
||||
|
||||
//
|
||||
// Test mat2
|
||||
//
|
||||
HMM_Mat2 m2 = {0};
|
||||
m2[0][0] = 1.0f;
|
||||
m2[0][1] = 2.0f;
|
||||
m2[1][0] = 3.0f;
|
||||
m2[1][1] = 4.0f;
|
||||
EXPECT_FLOAT_EQ(m2.Elements[0][0], 1.0f);
|
||||
EXPECT_FLOAT_EQ(m2.Elements[0][1], 2.0f);
|
||||
EXPECT_FLOAT_EQ(m2.Elements[1][0], 3.0f);
|
||||
EXPECT_FLOAT_EQ(m2.Elements[1][1], 4.0f);
|
||||
|
||||
//
|
||||
// Test mat3
|
||||
//
|
||||
HMM_Mat3 m3 = {0};
|
||||
m3[0][0] = 1.0f;
|
||||
m3[0][1] = 2.0f;
|
||||
m3[0][2] = 3.0f;
|
||||
m3[1][0] = 4.0f;
|
||||
m3[1][1] = 5.0f;
|
||||
m3[1][2] = 6.0f;
|
||||
m3[2][0] = 7.0f;
|
||||
m3[2][1] = 8.0f;
|
||||
m3[2][2] = 9.0f;
|
||||
EXPECT_FLOAT_EQ(m3.Elements[0][0], 1.0f);
|
||||
EXPECT_FLOAT_EQ(m3.Elements[0][1], 2.0f);
|
||||
EXPECT_FLOAT_EQ(m3.Elements[0][2], 3.0f);
|
||||
EXPECT_FLOAT_EQ(m3.Elements[1][0], 4.0f);
|
||||
EXPECT_FLOAT_EQ(m3.Elements[1][1], 5.0f);
|
||||
EXPECT_FLOAT_EQ(m3.Elements[1][2], 6.0f);
|
||||
EXPECT_FLOAT_EQ(m3.Elements[2][0], 7.0f);
|
||||
EXPECT_FLOAT_EQ(m3.Elements[2][1], 8.0f);
|
||||
EXPECT_FLOAT_EQ(m3.Elements[2][2], 9.0f);
|
||||
|
||||
//
|
||||
// Test mat4
|
||||
//
|
||||
HMM_Mat4 m4 = {0};
|
||||
m4[0][0] = 1.0f;
|
||||
m4[0][1] = 2.0f;
|
||||
m4[0][2] = 3.0f;
|
||||
m4[0][3] = 4.0f;
|
||||
m4[1][0] = 5.0f;
|
||||
m4[1][1] = 6.0f;
|
||||
m4[1][2] = 7.0f;
|
||||
m4[1][3] = 8.0f;
|
||||
m4[2][0] = 9.0f;
|
||||
m4[2][1] = 10.0f;
|
||||
m4[2][2] = 11.0f;
|
||||
m4[2][3] = 12.0f;
|
||||
m4[3][0] = 13.0f;
|
||||
m4[3][1] = 14.0f;
|
||||
m4[3][2] = 15.0f;
|
||||
m4[3][3] = 16.0f;
|
||||
EXPECT_FLOAT_EQ(m4.Elements[0][0], 1.0f);
|
||||
EXPECT_FLOAT_EQ(m4.Elements[0][1], 2.0f);
|
||||
EXPECT_FLOAT_EQ(m4.Elements[0][2], 3.0f);
|
||||
EXPECT_FLOAT_EQ(m4.Elements[0][3], 4.0f);
|
||||
EXPECT_FLOAT_EQ(m4.Elements[1][0], 5.0f);
|
||||
EXPECT_FLOAT_EQ(m4.Elements[1][1], 6.0f);
|
||||
EXPECT_FLOAT_EQ(m4.Elements[1][2], 7.0f);
|
||||
EXPECT_FLOAT_EQ(m4.Elements[1][3], 8.0f);
|
||||
EXPECT_FLOAT_EQ(m4.Elements[2][0], 9.0f);
|
||||
EXPECT_FLOAT_EQ(m4.Elements[2][1], 10.0f);
|
||||
EXPECT_FLOAT_EQ(m4.Elements[2][2], 11.0f);
|
||||
EXPECT_FLOAT_EQ(m4.Elements[2][3], 12.0f);
|
||||
EXPECT_FLOAT_EQ(m4.Elements[3][0], 13.0f);
|
||||
EXPECT_FLOAT_EQ(m4.Elements[3][1], 14.0f);
|
||||
EXPECT_FLOAT_EQ(m4.Elements[3][2], 15.0f);
|
||||
EXPECT_FLOAT_EQ(m4.Elements[3][3], 16.0f);
|
||||
}
|
||||
#endif
|
||||
|
@@ -28,18 +28,6 @@ TEST(ScalarMath, Trigonometry)
|
||||
// checking that things work by default.
|
||||
}
|
||||
|
||||
TEST(ScalarMath, ExpF)
|
||||
{
|
||||
EXPECT_NEAR(HMM_ExpF(0.0f), 1.0f, 0.0001f);
|
||||
EXPECT_NEAR(HMM_ExpF(1.0f), 2.7182818285f, 0.0001f);
|
||||
}
|
||||
|
||||
TEST(ScalarMath, LogF)
|
||||
{
|
||||
EXPECT_NEAR(HMM_LogF(1.0f), 0.0f, 0.0001f);
|
||||
EXPECT_NEAR(HMM_LogF(2.7182818285f), 1.0f, 0.0001f);
|
||||
}
|
||||
|
||||
TEST(ScalarMath, SquareRoot)
|
||||
{
|
||||
EXPECT_FLOAT_EQ(HMM_SqrtF(16.0f), 4.0f);
|
||||
@@ -50,20 +38,6 @@ TEST(ScalarMath, RSquareRootF)
|
||||
EXPECT_NEAR(HMM_InvSqrtF(10.0f), 0.31616211f, 0.0001f);
|
||||
}
|
||||
|
||||
TEST(ScalarMath, Power)
|
||||
{
|
||||
EXPECT_FLOAT_EQ(HMM_Power(2.0f, 0), 1.0f);
|
||||
EXPECT_FLOAT_EQ(HMM_Power(2.0f, 4), 16.0f);
|
||||
EXPECT_FLOAT_EQ(HMM_Power(2.0f, -2), 0.25f);
|
||||
}
|
||||
|
||||
TEST(ScalarMath, PowerF)
|
||||
{
|
||||
EXPECT_FLOAT_EQ(HMM_PowerF(2.0f, 0.0f), 1.0f);
|
||||
EXPECT_NEAR(HMM_PowerF(2.0f, 4.1f), 17.148376f, 0.0001f);
|
||||
EXPECT_NEAR(HMM_PowerF(2.0f, -2.5f), 0.176777f, 0.0001f);
|
||||
}
|
||||
|
||||
TEST(ScalarMath, Lerp)
|
||||
{
|
||||
EXPECT_FLOAT_EQ(HMM_Lerp(-2.0f, 0.0f, 2.0f), -2.0f);
|
||||
|
36
update/README.md
Normal file
36
update/README.md
Normal file
@@ -0,0 +1,36 @@
|
||||
# Handmade Math 2.0 Update Tool
|
||||
|
||||
Due to the large number of breaking naming changes in Handmade Math 2, we provide a small tool to update your programs automatically. It's a C program that takes a list of files and updates their text, along with some scripts to recursively run the program on all code in a directory.
|
||||
|
||||
You can compile the tool yourself with any C/C++ compiler:
|
||||
|
||||
```bash
|
||||
# MSVC (Windows)
|
||||
cl update_hmm.c
|
||||
|
||||
# gcc
|
||||
gcc update_hmm.c -o update_hmm
|
||||
|
||||
# clang
|
||||
clang update_hmm.c -o update_hmm
|
||||
```
|
||||
|
||||
Once built, the tool can be run on any C or C++ files:
|
||||
|
||||
```bash
|
||||
# Windows
|
||||
update_hmm.exe MyGame.cpp MyPlatformLayer.cpp
|
||||
|
||||
# Other platforms
|
||||
update_hmm MyGame.cpp MyPlatformLayer.cpp
|
||||
```
|
||||
|
||||
Or, update all C/C++ files in a directory by running one of the provided shell scripts:
|
||||
|
||||
```bash
|
||||
# Windows
|
||||
update_hmm_all.bat "path\to\project"
|
||||
|
||||
# Other platforms
|
||||
update_hmm_all.sh path/to/project
|
||||
```
|
562
update/update_hmm.c
Normal file
562
update/update_hmm.c
Normal file
@@ -0,0 +1,562 @@
|
||||
/* Compile:
|
||||
Windows (MSVC): cl update_hmm.c
|
||||
Linux (GCC): gcc update_hmm.c -o update_hmm
|
||||
*/
|
||||
|
||||
/** LCF stuff **/
|
||||
/* I used my personally library when writing this so I am dumping the necessary things here
|
||||
so that it's all in one file. */
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
/* Types */
|
||||
#define global static
|
||||
#define internal static
|
||||
typedef int32_t s32; global s32 s32_MAX = 0x7FFFFFFF; global s32 s32_MIN = -1 - 0x7FFFFFFF;
|
||||
typedef int64_t s64; global s64 s64_MAX = 0x7FFFFFFFFFFFFFFF; global s64 s64_MIN = -1 - 0x7FFFFFFFFFFFFFFF;
|
||||
typedef uint32_t u32; global u32 u32_MAX = 0xFFFFFFFF; global u32 u32_MIN = 0;
|
||||
typedef uint64_t u64; global u64 u64_MAX = 0xFFFFFFFFFFFFFFFF; global u64 u64_MIN = 0;
|
||||
typedef u32 b32;
|
||||
typedef u64 b64;
|
||||
#define MIN(a,b) (((a)<(b))?(a):(b))
|
||||
#define CLAMPTOP(a,b) MIN(a,b)
|
||||
|
||||
/* Memory */
|
||||
struct lcf_Arena {
|
||||
u64 pos;
|
||||
u64 size;
|
||||
u64 alignment;
|
||||
u64 commited_pos;
|
||||
};
|
||||
typedef struct lcf_Arena Arena;
|
||||
#define KB(x) ((x) << 10)
|
||||
#define GB(x) ((x) << 30)
|
||||
Arena* Arena_create(u64 size);
|
||||
void* Arena_take(Arena *a, u64 size);
|
||||
void* Arena_take_custom(Arena *a, u64 size, u64 alignment);
|
||||
#define Arena_take_array(a, type, count) ((type*) Arena_take(a, sizeof(type)*count))
|
||||
void Arena_reset_all(Arena *a);
|
||||
|
||||
#define LCF_MEMORY_PROVIDE_MEMORY "stdlib"
|
||||
#define LCF_MEMORY_RESERVE_MEMORY(name) void* name(u64 size)
|
||||
#define LCF_MEMORY_COMMIT_MEMORY(name) b32 name(void* memory, u64 size)
|
||||
#define LCF_MEMORY_DECOMMIT_MEMORY(name) void name(void* memory, u64 size)
|
||||
#define LCF_MEMORY_FREE_MEMORY(name) void name(void* memory, u64 size)
|
||||
/* This implementation of an arena doesn't take advantage of virtual memory at all.
|
||||
It's just convenient to have something portable so I can use the Arena API I'm used to. */
|
||||
internal LCF_MEMORY_RESERVE_MEMORY(_lcf_memory_default_reserve) {
|
||||
return malloc(size);
|
||||
}
|
||||
internal LCF_MEMORY_COMMIT_MEMORY(_lcf_memory_default_commit) {
|
||||
(void) size, memory;
|
||||
return 1; /* malloc commits memory automatically */
|
||||
}
|
||||
internal LCF_MEMORY_DECOMMIT_MEMORY(_lcf_memory_default_decommit) {
|
||||
(void) size, memory;
|
||||
return;
|
||||
}
|
||||
internal LCF_MEMORY_FREE_MEMORY(_lcf_memory_default_free) {
|
||||
(void) size;
|
||||
free(memory);
|
||||
}
|
||||
#define LCF_MEMORY_reserve _lcf_memory_default_reserve
|
||||
#define LCF_MEMORY_commit _lcf_memory_default_commit
|
||||
#define LCF_MEMORY_decommit _lcf_memory_default_decommit
|
||||
#define LCF_MEMORY_free _lcf_memory_default_free
|
||||
#define LCF_MEMORY_RESERVE_SIZE GB(1)
|
||||
#define LCF_MEMORY_COMMIT_SIZE KB(4)
|
||||
#define LCF_MEMORY_ALIGNMENT (sizeof(void*))
|
||||
|
||||
Arena* Arena_create(u64 size) {
|
||||
Arena* a = (Arena*) LCF_MEMORY_reserve(size);
|
||||
LCF_MEMORY_commit(a, LCF_MEMORY_COMMIT_SIZE);
|
||||
a->size = size;
|
||||
a->pos = sizeof(Arena);
|
||||
a->commited_pos = LCF_MEMORY_COMMIT_SIZE;
|
||||
a->alignment = LCF_MEMORY_ALIGNMENT;
|
||||
return a;
|
||||
}
|
||||
#define B_PTR(p) (u8*)(p)
|
||||
|
||||
internal b32 is_power_of_2(u64 x) {
|
||||
return ((x & (x-1)) == 0);
|
||||
}
|
||||
internal u64 next_alignment(u64 ptr, u64 alignment) {
|
||||
/* Fast replacement for mod because alignment is power of 2 */
|
||||
u64 modulo = ptr & (alignment-1);
|
||||
|
||||
if (modulo != 0) {
|
||||
ptr += alignment - modulo;
|
||||
}
|
||||
|
||||
return ptr;
|
||||
}
|
||||
void* Arena_take_custom(Arena *a, u64 size, u64 alignment) {
|
||||
void* result = 0;
|
||||
|
||||
/* Align pos pointer to check if "size" can fit */
|
||||
u64 mem = (u64) a;
|
||||
u64 aligned_pos = next_alignment(mem + a->pos, alignment) - mem;
|
||||
u64 new_pos = aligned_pos + size;
|
||||
|
||||
/* Check that there is space */
|
||||
if (new_pos < a->size) {
|
||||
u64 commited_pos = a->commited_pos;
|
||||
|
||||
/* Commit memory if needed */
|
||||
if (new_pos > commited_pos) {
|
||||
u64 new_commited_pos = next_alignment(mem + new_pos, LCF_MEMORY_COMMIT_SIZE)-mem;
|
||||
if (LCF_MEMORY_commit(a, new_commited_pos)) {
|
||||
a->commited_pos = commited_pos = new_commited_pos;
|
||||
}
|
||||
}
|
||||
|
||||
/* If enough memory is commited, set result and pos. */
|
||||
if (new_pos <= commited_pos) {
|
||||
result = (void*)(mem + aligned_pos);
|
||||
a->pos = new_pos;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
void* Arena_take(Arena *a, u64 size) {
|
||||
return Arena_take_custom(a, size, LCF_MEMORY_ALIGNMENT);
|
||||
}
|
||||
void Arena_reset_all(Arena *a) {
|
||||
a->pos = 0;
|
||||
}
|
||||
|
||||
/* String */
|
||||
typedef char chr8;
|
||||
struct str8 {
|
||||
u64 len;
|
||||
chr8 *str;
|
||||
};
|
||||
typedef struct str8 str8;
|
||||
#define str8_PRINTF_ARGS(s) (int)(s).len, (s).str
|
||||
#define str8_lit(s) str8_from((chr8*)(s),(u64)sizeof(s)-1) /* -1 to exclude null character */
|
||||
#define str8_is_empty(s) ((b32)((s).len == 0))
|
||||
#define LCF_STRING_NO_MATCH 0x8000000000000000
|
||||
#define str8_iter_custom(s, i, c) \
|
||||
s64 i = 0; \
|
||||
chr8 c = s.str[i]; \
|
||||
for (; (i < (s64) s.len); i++, c = s.str[i])
|
||||
|
||||
#define str8_iter(s) str8_iter_custom(s, i, c)
|
||||
|
||||
str8 str8_from(chr8* s, u64 len);
|
||||
str8 str8_from_cstring(chr8 *cstr);
|
||||
str8 str8_first(str8 s, u64 len);
|
||||
str8 str8_skip(str8 s, u64 len);
|
||||
b32 chr8_is_whitespace(chr8 c);
|
||||
b32 str8_contains_char(str8 s, chr8 c);
|
||||
u64 str8_char_location(str8 s, chr8 c);
|
||||
|
||||
#define RET_STR8(s,l) \
|
||||
str8 _str8; \
|
||||
_str8.str = (s); \
|
||||
_str8.len = (l); \
|
||||
return _str8
|
||||
str8 str8_from(chr8* s, u64 len) {
|
||||
RET_STR8(s, len);
|
||||
}
|
||||
str8 str8_from_cstring(chr8 *cstr) {
|
||||
chr8* p2 = cstr;
|
||||
while(*p2 != 0)
|
||||
p2++;
|
||||
RET_STR8(cstr, (u64)(p2 - cstr));
|
||||
}
|
||||
str8 str8_first(str8 s, u64 len) {
|
||||
u64 len_clamped = CLAMPTOP(len, s.len);
|
||||
RET_STR8(s.str, len_clamped);
|
||||
}
|
||||
str8 str8_skip(str8 s, u64 len) {
|
||||
u64 len_clamped = CLAMPTOP(len, s.len);
|
||||
RET_STR8(s.str + len_clamped, s.len - len_clamped);
|
||||
}
|
||||
b32 chr8_is_whitespace(chr8 c) {
|
||||
switch (c) {
|
||||
case ' ':
|
||||
case '\n':
|
||||
case '\t':
|
||||
case '\r':
|
||||
return 1;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
b32 str8_contains_char(str8 s, chr8 find) {
|
||||
return str8_char_location(s,find) != LCF_STRING_NO_MATCH;
|
||||
}
|
||||
u64 str8_char_location(str8 s, chr8 find) {
|
||||
str8_iter(s) {
|
||||
if (c == find) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
return LCF_STRING_NO_MATCH;
|
||||
}
|
||||
#undef RET_STR8
|
||||
|
||||
struct Str8Node {
|
||||
struct Str8Node *next;
|
||||
struct str8 str;
|
||||
};
|
||||
struct Str8List {
|
||||
struct Str8Node *first;
|
||||
struct Str8Node *last;
|
||||
u64 count;
|
||||
u64 total_len;
|
||||
};
|
||||
typedef struct Str8Node Str8Node;
|
||||
typedef struct Str8List Str8List;
|
||||
|
||||
void Str8List_add_node(Str8List *list, Str8Node *n);
|
||||
void Str8List_add(Arena *arena, Str8List *list, str8 str);
|
||||
void Str8List_add_node(Str8List *list, Str8Node *n) {
|
||||
if (list->last) {
|
||||
list->last->next = n;
|
||||
} else {
|
||||
list->first = n;
|
||||
}
|
||||
list->last = n;
|
||||
list->count++;
|
||||
list->total_len += n->str.len;
|
||||
}
|
||||
void Str8List_add(Arena *arena, Str8List *list, str8 str) {
|
||||
Str8Node *n = Arena_take_array(arena, Str8Node, 1);
|
||||
n->str = str;
|
||||
n->next = 0;
|
||||
Str8List_add_node(list, n);
|
||||
}
|
||||
|
||||
/* CRT - stdio */
|
||||
str8 stdio_load_entire_file(Arena *arena, str8 filepath);
|
||||
b32 stdio_write_file(str8 filepath, Str8List text);
|
||||
|
||||
str8 stdio_load_entire_file(Arena *arena, str8 filepath) {
|
||||
str8 file_content = {0};
|
||||
|
||||
FILE *file = fopen(filepath.str, "rb");
|
||||
if (file != 0) {
|
||||
fseek(file, 0, SEEK_END);
|
||||
u64 file_len = ftell(file);
|
||||
fseek(file, 0, SEEK_SET);
|
||||
file_content.str = (chr8*) Arena_take(arena, file_len+1);
|
||||
if (file_content.str != 0) {
|
||||
file_content.len = file_len;
|
||||
fread(file_content.str, 1, file_len, file);
|
||||
file_content.str[file_content.len] = 0;
|
||||
}
|
||||
fclose(file);
|
||||
}
|
||||
return file_content;
|
||||
}
|
||||
b32 stdio_write_file(str8 filepath, Str8List text) {
|
||||
u64 bytes_written = 0;
|
||||
FILE *file = fopen(filepath.str, "wb");
|
||||
if (file != 0) {
|
||||
Str8Node* n = text.first;
|
||||
for (s64 i = 0; i < text.count; i++, n = n->next) {
|
||||
if (!fwrite(n->str.str, n->str.len, 1, file)) {
|
||||
break;
|
||||
}
|
||||
bytes_written += n->str.len;
|
||||
}
|
||||
fclose(file);
|
||||
}
|
||||
return bytes_written == text.total_len;
|
||||
}
|
||||
|
||||
/** HMM2.0 Update Tool **/
|
||||
enum Targets {
|
||||
/* hmm_ and HMM_ prefixes */
|
||||
PREFIX_TYPE, PREFIX_FUNCTION,
|
||||
PREFIXES_Size,
|
||||
/* Struct/Union types */
|
||||
TYPE_VEC, TYPE_MAT, TYPE_QUATERNION, TYPE_BOOL,
|
||||
TYPE_INTERNAL_ELEMENTS_SSE,
|
||||
TYPES_Size,
|
||||
/* Types in Function Names */
|
||||
FUN_VEC, FUN_MAT, FUN_QUATERNION,
|
||||
/* Function Names for Common Operations */
|
||||
FUN_EQUALS, FUN_SUBTRACT, FUN_MULTIPLY, FUN_DIVIDE,
|
||||
FUN_INVERSE, FUN_R_SQUARE_ROOT, FUN_SQUARE_ROOT,
|
||||
FUN_LENGTH_SQUARED, FUN_LENGTH, FUN_FAST_NORM, FUN_NORM,
|
||||
FUN_SLERP, FUN_BY,
|
||||
FUN_LINEAR_COMBINE_SSE, FUN_TRANSPOSE,
|
||||
FUNCTIONS_Size,
|
||||
/* Handedness */
|
||||
HAND_PERSPECTIVE, HAND_ROTATE,
|
||||
HAND_ORTHO, HAND_LOOK_AT, HAND_QUAT_AXIS_ANGLE, HAND_MAT_TO_QUAT,
|
||||
HAND_Size,
|
||||
};
|
||||
|
||||
Str8List update_file_content(Arena* arena, str8 file_content) {
|
||||
Str8List out = {0};
|
||||
|
||||
str8 Find[HAND_Size];
|
||||
str8 Repl[HAND_Size];
|
||||
{ /* NOTE: Initialization */
|
||||
Find[PREFIX_TYPE] = str8_lit("hmm_");
|
||||
Find[PREFIX_FUNCTION] = str8_lit("HMM_");
|
||||
Repl[PREFIX_TYPE] = Find[PREFIX_FUNCTION];
|
||||
|
||||
Find[TYPE_VEC] = str8_lit("vec");
|
||||
Repl[TYPE_VEC] = str8_lit("Vec");
|
||||
Find[TYPE_MAT] = str8_lit("mat");
|
||||
Repl[TYPE_MAT] = str8_lit("Mat");
|
||||
Find[TYPE_QUATERNION] = str8_lit("quaternion");
|
||||
Repl[TYPE_QUATERNION] = str8_lit("Quat");
|
||||
Find[TYPE_BOOL] = str8_lit("bool");
|
||||
Repl[TYPE_BOOL] = str8_lit("Bool");
|
||||
Find[TYPE_INTERNAL_ELEMENTS_SSE] = str8_lit(".InternalElementsSSE");
|
||||
Repl[TYPE_INTERNAL_ELEMENTS_SSE] = str8_lit(".SSE");
|
||||
|
||||
Find[FUN_VEC] = str8_lit("Vec");
|
||||
Repl[FUN_VEC] = str8_lit("V");
|
||||
Find[FUN_MAT] = str8_lit("Mat");
|
||||
Repl[FUN_MAT] = str8_lit("M");
|
||||
Find[FUN_QUATERNION] = str8_lit("Quaternion");
|
||||
Repl[FUN_QUATERNION] = str8_lit("Q");
|
||||
Find[FUN_EQUALS] = str8_lit("Equals");
|
||||
Repl[FUN_EQUALS] = str8_lit("Eq");
|
||||
Find[FUN_SUBTRACT] = str8_lit("Subtract");
|
||||
Repl[FUN_SUBTRACT] = str8_lit("Sub");
|
||||
Find[FUN_MULTIPLY] = str8_lit("Multiply");
|
||||
Repl[FUN_MULTIPLY] = str8_lit("Mul");
|
||||
Find[FUN_DIVIDE] = str8_lit("Divide");
|
||||
Repl[FUN_DIVIDE] = str8_lit("Div");
|
||||
Find[FUN_INVERSE] = str8_lit("Inverse");
|
||||
Repl[FUN_INVERSE] = str8_lit("Inv");
|
||||
Find[FUN_R_SQUARE_ROOT] = str8_lit("RSquareRoot");
|
||||
Repl[FUN_R_SQUARE_ROOT] = str8_lit("InvSqrt");
|
||||
Find[FUN_SQUARE_ROOT] = str8_lit("SquareRoot");
|
||||
Repl[FUN_SQUARE_ROOT] = str8_lit("Sqrt");
|
||||
Find[FUN_LENGTH_SQUARED] = str8_lit("Squared");
|
||||
Repl[FUN_LENGTH_SQUARED] = str8_lit("Sqr"); /* FIXME: not working for some reason */
|
||||
Find[FUN_LENGTH] = str8_lit("Length");
|
||||
Repl[FUN_LENGTH] = str8_lit("Len");
|
||||
|
||||
Find[FUN_SLERP] = str8_lit("Slerp");
|
||||
Repl[FUN_SLERP] = str8_lit("SLerp");
|
||||
Find[FUN_BY] = str8_lit("By");
|
||||
Repl[FUN_BY] = str8_lit("");
|
||||
Find[FUN_LINEAR_COMBINE_SSE] = str8_lit("LinearCombineSSE"); /* TODO: emit warning */
|
||||
Repl[FUN_LINEAR_COMBINE_SSE] = str8_lit("LinearCombineV4M4");
|
||||
Find[FUN_TRANSPOSE] = str8_lit("Transpose");
|
||||
Repl[FUN_TRANSPOSE] = str8_lit("TransposeM4");
|
||||
Find[FUN_FAST_NORM] = str8_lit("Fast"); /* TODO: emit warning, lower precision. */
|
||||
Repl[FUN_FAST_NORM] = str8_lit("");
|
||||
Find[FUN_NORM] = str8_lit("Normalize");
|
||||
Repl[FUN_NORM] = str8_lit("Norm");
|
||||
|
||||
Find[HAND_PERSPECTIVE] = str8_lit("Perspective");
|
||||
Find[HAND_ROTATE] = str8_lit("Rotate");
|
||||
Find[HAND_ORTHO] = str8_lit("Orthographic");
|
||||
Find[HAND_LOOK_AT] = str8_lit("LookAt");
|
||||
Find[HAND_QUAT_AXIS_ANGLE] = str8_lit("FromAxisAngle");
|
||||
Find[HAND_MAT_TO_QUAT] = str8_lit("ToQuaternion");
|
||||
}
|
||||
|
||||
/* Match with a bunch of sliding windows, skipping when there can't be a match */
|
||||
u64 MatchProgress[HAND_Size] = {0};
|
||||
b32 FoundTypePrefix = 0;
|
||||
b32 FoundFunctionPrefix = 0;
|
||||
u32 Line = 1;
|
||||
str8_iter(file_content) {
|
||||
if (c == '\n') {
|
||||
Line++;
|
||||
}
|
||||
if (FoundTypePrefix || FoundFunctionPrefix) {
|
||||
if (chr8_is_whitespace(c)
|
||||
|| str8_contains_char(str8_lit("(){}[]:;,.<>~?!@#$%^&+-*/'\""), c)) {
|
||||
FoundTypePrefix = 0;
|
||||
FoundFunctionPrefix = 0;
|
||||
}
|
||||
}
|
||||
for (u32 t = 0; t < PREFIXES_Size; t++) {
|
||||
if (c == Find[t].str[MatchProgress[t]]) {
|
||||
MatchProgress[t]++;
|
||||
if (MatchProgress[t] == Find[t].len) {
|
||||
if (t == PREFIX_TYPE) {
|
||||
FoundTypePrefix = 1;
|
||||
} else if (t == PREFIX_FUNCTION) {
|
||||
FoundFunctionPrefix = 1;
|
||||
}
|
||||
MatchProgress[t] = 0;
|
||||
}
|
||||
} else {
|
||||
MatchProgress[t] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* Replace hmm_ types */
|
||||
if (FoundTypePrefix) {
|
||||
for (u32 t = PREFIXES_Size+1; t < TYPES_Size; t++) {
|
||||
if (c == Find[t].str[MatchProgress[t]]) {
|
||||
MatchProgress[t]++;
|
||||
if (MatchProgress[t] == Find[t].len) {
|
||||
MatchProgress[t] = 0;
|
||||
printf("\t[%u]: Find: %.*s, Repl: %.*s.\n", Line, str8_PRINTF_ARGS(Find[t]), str8_PRINTF_ARGS(Repl[t]));
|
||||
Str8List_add(arena, &out,
|
||||
str8_first(file_content,
|
||||
i + 1 - (Find[t].len + Find[PREFIX_TYPE].len)));
|
||||
Str8List_add(arena, &out, Repl[PREFIX_TYPE]);
|
||||
Str8List_add(arena, &out, Repl[t]);
|
||||
file_content = str8_skip(file_content, i+1);
|
||||
i = -1;
|
||||
}
|
||||
} else {
|
||||
MatchProgress[t] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* If in a HMM_ function, do function name replacements */
|
||||
if (FoundFunctionPrefix) {
|
||||
for (u32 t = TYPES_Size+1; t < FUNCTIONS_Size; t++) {
|
||||
if (c == Find[t].str[MatchProgress[t]]) {
|
||||
MatchProgress[t]++;
|
||||
if (MatchProgress[t] == Find[t].len) {
|
||||
MatchProgress[t] = 0;
|
||||
printf("\t[%u]: Find: %.*s, Repl: %.*s.\n", Line, str8_PRINTF_ARGS(Find[t]), str8_PRINTF_ARGS(Repl[t]));
|
||||
Str8List_add(arena, &out, str8_first(file_content, i + 1 - Find[t].len));
|
||||
Str8List_add(arena, &out, Repl[t]);
|
||||
file_content = str8_skip(file_content, i+1);
|
||||
i = -1;
|
||||
|
||||
/* NOTE(lcf): Special case because Find[] overlaps here */
|
||||
if (t == FUN_R_SQUARE_ROOT) {
|
||||
MatchProgress[FUN_SQUARE_ROOT] = 0;
|
||||
}
|
||||
|
||||
if (t == FUN_LINEAR_COMBINE_SSE) {
|
||||
printf("\t[%u]: HMM_LinearCombineSSE is now HMM_LinearCombineV4M4, and will now use a fallback method when SSE is not available. \n\tYou no longer need to check for the availability of SSE.\n", Line);
|
||||
}
|
||||
|
||||
if (t == FUN_VEC) {
|
||||
/* NOTE(lcf): if pattern is Vec2i, this is now i */
|
||||
c = file_content.str[1];
|
||||
if (c == 'i') {
|
||||
Str8List_add(arena, &out, str8_first(file_content, 1));
|
||||
Str8List_add(arena, &out, str8_lit("I"));
|
||||
file_content = str8_skip(file_content, 2);
|
||||
} else if (c == 'v') {
|
||||
Str8List_add(arena, &out, str8_first(file_content, 1));
|
||||
Str8List_add(arena, &out, str8_lit("V"));
|
||||
file_content = str8_skip(file_content, 2);
|
||||
} else if (c == 'f') {
|
||||
Str8List_add(arena, &out, str8_first(file_content, 1));
|
||||
Str8List_add(arena, &out, str8_lit("F"));
|
||||
file_content = str8_skip(file_content, 2);
|
||||
}
|
||||
} else if (t == FUN_MAT) {
|
||||
/* if pattern is Mat4d, this is now d */
|
||||
c = file_content.str[1];
|
||||
if (c == 'd') {
|
||||
Str8List_add(arena, &out, str8_first(file_content, 1));
|
||||
Str8List_add(arena, &out, str8_lit("D"));
|
||||
file_content = str8_skip(file_content, 2);
|
||||
} else if (c == 'f') {
|
||||
Str8List_add(arena, &out, str8_first(file_content, 1));
|
||||
Str8List_add(arena, &out, str8_lit("F"));
|
||||
file_content = str8_skip(file_content, 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
MatchProgress[t] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Handedness cases. */
|
||||
if (FoundFunctionPrefix) {
|
||||
for (u32 t = FUNCTIONS_Size+1; t < HAND_Size; t++) {
|
||||
if (c == Find[t].str[MatchProgress[t]]) {
|
||||
MatchProgress[t]++;
|
||||
if (MatchProgress[t] == Find[t].len) {
|
||||
MatchProgress[t] = 0;
|
||||
|
||||
chr8 check = file_content.str[i+1];
|
||||
if (check == '(') {
|
||||
printf("\t[%u]: Find: %.*s, Appending: _RH for old default handedness.\n", Line, str8_PRINTF_ARGS(Find[t]));
|
||||
Str8List_add(arena, &out, str8_first(file_content, i + 1));
|
||||
Str8List_add(arena, &out, str8_lit("_RH("));
|
||||
file_content = str8_skip(file_content, i+2);
|
||||
i = -1;
|
||||
|
||||
if (t == HAND_PERSPECTIVE || t == HAND_ROTATE) {
|
||||
printf("\t[%u]: ", Line);
|
||||
if (t == HAND_PERSPECTIVE) {
|
||||
printf("HMM_Perspective_RH()");
|
||||
} else {
|
||||
printf("HMM_Rotate_RH()");
|
||||
}
|
||||
printf(" now takes Radians. Wrapping Degrees with HMM_AngleDeg()\n");
|
||||
u64 end_arg = str8_char_location(file_content, ',');
|
||||
if (end_arg != LCF_STRING_NO_MATCH) {
|
||||
Str8List_add(arena, &out, str8_lit("HMM_AngleDeg("));
|
||||
Str8List_add(arena, &out, str8_first(file_content, end_arg));
|
||||
Str8List_add(arena, &out, str8_lit(")"));
|
||||
file_content = str8_skip(file_content, end_arg);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
MatchProgress[t] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Str8List_add(arena, &out, file_content);
|
||||
|
||||
return out;
|
||||
}
|
||||
|
||||
void print_usage() {
|
||||
printf("Updates C and C++ source code to use Handmade Math version 2.\n");
|
||||
#ifdef _WIN32
|
||||
printf("Usage: update_hmm.exe <filename> [<filename>...]\n");
|
||||
#else
|
||||
printf("Usage: update_hmm <filename> [<filename>...]\n");
|
||||
#endif
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
Arena *tempa = Arena_create(GB(1));
|
||||
|
||||
if (argc == 1) {
|
||||
print_usage();
|
||||
return 1;
|
||||
}
|
||||
|
||||
s32 argi = 1;
|
||||
str8 arg = str8_from_cstring(argv[argi]);
|
||||
|
||||
if (arg.len == 2 && (arg.str[1] == 'h' || arg.str[1] == '?')) {
|
||||
print_usage();
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (; argi < argc; argi++) {
|
||||
arg = str8_from_cstring(argv[argi]);
|
||||
str8 file_content = stdio_load_entire_file(tempa, arg);
|
||||
if (str8_is_empty(file_content)) {
|
||||
printf("X - Invalid file name: %.*s\n\n", str8_PRINTF_ARGS(arg));
|
||||
continue;
|
||||
}
|
||||
printf("O - Updating file: %.*s -------------------\n", str8_PRINTF_ARGS(arg));
|
||||
Str8List result = update_file_content(tempa, file_content);
|
||||
printf("\n");
|
||||
stdio_write_file(arg, result);
|
||||
|
||||
Arena_reset_all(tempa);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
12
update/update_hmm_all.bat
Normal file
12
update/update_hmm_all.bat
Normal file
@@ -0,0 +1,12 @@
|
||||
@REM Batch script to run update_hmm.exe on all your code files.
|
||||
@REM Example:
|
||||
@REM "update_hmm_all.bat Code\Project\" -> Recursively update all files/folders in .\Code\Project\
|
||||
|
||||
for /r %1 %%v in (*.c) do update_hmm.exe "%%v"
|
||||
for /r %1 %%v in (*.h) do update_hmm.exe "%%v"
|
||||
for /r %1 %%v in (*.cpp) do update_hmm.exe "%%v"
|
||||
for /r %1 %%v in (*.hpp) do update_hmm.exe "%%v"
|
||||
|
||||
@REM @REM Uncomment for sokol-samples
|
||||
@REM for /r %1 %%v in (*.glsl) do update_hmm.exe "%%v"
|
||||
@REM for /r %1 %%v in (*.hlsl) do update_hmm.exe "%%v"
|
12
update/update_hmm_all.sh
Normal file
12
update/update_hmm_all.sh
Normal file
@@ -0,0 +1,12 @@
|
||||
# Bash script to run update_hmm on all your code files.
|
||||
# Example:
|
||||
# "update_hmm_all Code/Project/" -> Recursively update all files/folders in ./Code/Project/
|
||||
echo $1
|
||||
for file in "$1"/*.{c,h,cpp,hpp} "$1"/**/*.{c,h,cpp,hpp} ; do
|
||||
./update_hmm "$file"
|
||||
done
|
||||
|
||||
# # Uncomment for sokol-samples
|
||||
# for file in "$1"/*.{glsl,hlsl} "$1"/**/*.{glsl,hlsl} ; do
|
||||
# ./update_hmm "$file"
|
||||
# done
|
Reference in New Issue
Block a user