Kumo
A hand-rolled game engine.
git clone https://git.sentomk.me/Kumo.git
commit b32be4d3db015625cf81f17edfe8d290d21be80f parent 33402bf8f218ac0cf874aff36de66e7683fefbf2 Author: sentomk <sentomk@proton.me> Date: Tue, 13 Oct 2026 20:48:38 +0800 refactor: take size_t and the fixed-width integers from core/types.h The standard promises size_t and the fixed-width integers in namespace std only. Whether the global names exist too depends on the standard library and on what else was included: libc++ on macOS has them, libstdc++ often does not, so frame_buffer.h, which used size_t and included only <cstdint> and <vector>, did not compile with GNU. core/types.h includes <cstddef> and <cstdint> and brings the names into the global namespace, so that the engine keeps writing size_t and uint32_t and the decision is made in one place. It also checks, at compile time, that int is 32 bits and pointers are 64 bits. Files of the engine and its tests that use these names include it instead of the C++ headers. types_test.cpp includes core/types.h first, to show that it is enough on its own. Diffstat:
| A | src/core/types.h | | | 38 | ++++++++++++++++++++++++++++++++++++++ |
| M | src/render/frame_buffer.h | | | 3 | ++- |
| M | src/render/raster.h | | | 3 | +-- |
| M | tests/e2e/golden_test.cpp | | | 1 | + |
| M | tests/unit/raster_test.cpp | | | 3 | +-- |
| A | tests/unit/types_test.cpp | | | 30 | ++++++++++++++++++++++++++++++ |
6 files changed, 73 insertions(+), 5 deletions(-)
diff --git a/src/core/types.h b/src/core/types.h @@ -0,0 +1,38 @@ +/// @file core/types.h +/// @brief The integer types of the engine, usable without `std::`. +/// +/// The standard library promises `size_t` and the fixed-width integers such as +/// `uint32_t` in namespace std only. Whether the global names exist as well +/// depends on the implementation and on which other headers happen to be +/// included: libc++ (macOS) usually has them, libstdc++ (GNU) often does not, +/// so code that compiled on one failed on the other. +/// +/// This header settles it in one place. It includes the headers that declare +/// the types and brings the names into the global namespace. Every file of the +/// engine that uses them includes this header, and writes the plain names. +/// +/// A using-declaration is fine when the global name already exists: it names +/// the same type, and libstdc++ does the same in its own `<stddef.h>`. +#pragma once + +#include <cstddef> +#include <cstdint> + +using std::ptrdiff_t; +using std::size_t; + +using std::int16_t; +using std::int32_t; +using std::int64_t; +using std::int8_t; +using std::intptr_t; +using std::uint16_t; +using std::uint32_t; +using std::uint64_t; +using std::uint8_t; +using std::uintptr_t; + +// What the rest of the engine takes for granted. If a platform ever differs, +// the build stops here instead of computing something else. +static_assert(sizeof(int) == 4, "Kumo assumes a 32-bit int"); +static_assert(sizeof(void*) == 8, "Kumo assumes a 64-bit platform"); diff --git a/src/render/frame_buffer.h b/src/render/frame_buffer.h @@ -3,9 +3,10 @@ /// done with one: hash it, save it as an image. #pragma once -#include <cstdint> #include <vector> +#include "core/types.h" + namespace kumo { /// @brief A color packed as 0xAARRGGBB. diff --git a/src/render/raster.h b/src/render/raster.h @@ -5,8 +5,7 @@ /// same pixels on every compiler and CPU. #pragma once -#include <cstdint> - +#include "core/types.h" #include "render/frame_buffer.h" namespace kumo { diff --git a/tests/e2e/golden_test.cpp b/tests/e2e/golden_test.cpp @@ -2,6 +2,7 @@ #include <cstdio> #include "check.h" +#include "core/types.h" #include "demo_scene.h" #include "render/frame_buffer.h" diff --git a/tests/unit/raster_test.cpp b/tests/unit/raster_test.cpp @@ -2,10 +2,9 @@ #include <algorithm> #include <array> -#include <cstddef> -#include <cstdint> #include "check.h" +#include "core/types.h" #include "render/frame_buffer.h" using kumo::Color; diff --git a/tests/unit/types_test.cpp b/tests/unit/types_test.cpp @@ -0,0 +1,30 @@ +// core/types.h comes first on purpose: it has to be enough on its own to make +// the unqualified names available. +#include "core/types.h" + +#include "check.h" + +TEST(fixed_width_integers_have_their_sizes) { + CHECK_EQ(sizeof(int8_t), 1u); + CHECK_EQ(sizeof(int16_t), 2u); + CHECK_EQ(sizeof(int32_t), 4u); + CHECK_EQ(sizeof(int64_t), 8u); + CHECK_EQ(sizeof(uint8_t), 1u); + CHECK_EQ(sizeof(uint16_t), 2u); + CHECK_EQ(sizeof(uint32_t), 4u); + CHECK_EQ(sizeof(uint64_t), 8u); +} + +TEST(sizes_and_pointers_are_64_bit) { + CHECK_EQ(sizeof(size_t), 8u); + CHECK_EQ(sizeof(ptrdiff_t), 8u); + CHECK_EQ(sizeof(intptr_t), 8u); + CHECK_EQ(sizeof(uintptr_t), 8u); +} + +TEST(signedness_is_what_the_names_say) { + CHECK(static_cast<int32_t>(-1) < 0); + CHECK(static_cast<ptrdiff_t>(-1) < 0); + CHECK(static_cast<uint32_t>(-1) > 0); + CHECK(static_cast<size_t>(-1) > 0); +}
