From 9ca793417149cbaa7b5f4b7797bc929e6e191f28 Mon Sep 17 00:00:00 2001 From: Hannes Schulze Date: Sat, 1 Feb 2025 01:14:03 +0100 Subject: use custom integer types --- lib/nes/common/CMakeLists.txt | 1 + lib/nes/common/box.hh | 4 +++- lib/nes/common/display.hh | 9 +++++---- lib/nes/common/rgb.hh | 12 ++++++------ lib/nes/common/types.hh | 26 ++++++++++++++++++++++++++ 5 files changed, 41 insertions(+), 11 deletions(-) create mode 100644 lib/nes/common/types.hh (limited to 'lib/nes/common') diff --git a/lib/nes/common/CMakeLists.txt b/lib/nes/common/CMakeLists.txt index 1a66929..bd83a57 100644 --- a/lib/nes/common/CMakeLists.txt +++ b/lib/nes/common/CMakeLists.txt @@ -1,6 +1,7 @@ target_sources( nes PRIVATE + types.hh debug.hh rgb.hh list.hh diff --git a/lib/nes/common/box.hh b/lib/nes/common/box.hh index 442d60f..3a65613 100644 --- a/lib/nes/common/box.hh +++ b/lib/nes/common/box.hh @@ -1,5 +1,7 @@ #pragma once +#include "nes/common/types.hh" + namespace nes { /// Wrapper around an object, allowing for it to be uninitialized or reassigned even if the object type doesn't @@ -10,7 +12,7 @@ namespace nes class box { bool initialized_{ false }; - alignas(T) unsigned char buffer_[sizeof(T)]; + alignas(T) u8 buffer_[sizeof(T)]; public: /// Create an empty box. diff --git a/lib/nes/common/display.hh b/lib/nes/common/display.hh index 85af915..eb35366 100644 --- a/lib/nes/common/display.hh +++ b/lib/nes/common/display.hh @@ -1,6 +1,7 @@ #pragma once #include "nes/common/rgb.hh" +#include "nes/common/types.hh" namespace nes { @@ -10,8 +11,8 @@ namespace nes class display { public: - static constexpr auto width = unsigned{ 256 }; - static constexpr auto height = unsigned{ 240 }; + static constexpr auto width = u32{ 256 }; + static constexpr auto height = u32{ 240 }; virtual ~display() = default; @@ -23,9 +24,9 @@ namespace nes /// Switch between front and back buffers. virtual auto switch_buffers() -> void = 0; /// Update the pixel at the given position in the back buffer. - virtual auto set(unsigned x, unsigned y, rgb value) -> void = 0; + virtual auto set(u32 x, u32 y, rgb value) -> void = 0; /// Load the pixel value at the given position in the back buffer. - virtual auto get(unsigned x, unsigned y) const -> rgb = 0; + virtual auto get(u32 x, u32 y) const -> rgb = 0; protected: explicit display() = default; diff --git a/lib/nes/common/rgb.hh b/lib/nes/common/rgb.hh index d62c70c..0b8ec1c 100644 --- a/lib/nes/common/rgb.hh +++ b/lib/nes/common/rgb.hh @@ -1,6 +1,6 @@ #pragma once -#include +#include "nes/common/types.hh" namespace nes { @@ -10,14 +10,14 @@ namespace nes public: explicit rgb() = default; - explicit rgb(std::uint8_t const r, std::uint8_t const g, std::uint8_t const b) + explicit rgb(u8 const r, u8 const g, u8 const b) : r{ r } , g{ g } , b{ b } { } - static auto from_hex(std::uint32_t const hex) -> rgb + static auto from_hex(u32 const hex) -> rgb { auto res = rgb{}; res.r = (hex & 0x00FF0000) >> 16; @@ -26,8 +26,8 @@ namespace nes return res; } - std::uint8_t r{ 0 }; - std::uint8_t g{ 0 }; - std::uint8_t b{ 0 }; + u8 r{ 0 }; + u8 g{ 0 }; + u8 b{ 0 }; }; } // namespace nes \ No newline at end of file diff --git a/lib/nes/common/types.hh b/lib/nes/common/types.hh new file mode 100644 index 0000000..1b768cf --- /dev/null +++ b/lib/nes/common/types.hh @@ -0,0 +1,26 @@ +#pragma once + +namespace nes +{ + // Some saner type definitions for common integer types. + + using u8 = unsigned char; + using u16 = unsigned short; + using u32 = unsigned int; + using u64 = unsigned long long; + + using i8 = signed char; + using i16 = signed short; + using i32 = signed int; + using i64 = signed long long; + + static_assert(sizeof(u8) == 1); + static_assert(sizeof(u16) == 2); + static_assert(sizeof(u32) == 4); + static_assert(sizeof(u64) == 8); + + static_assert(sizeof(i8) == 1); + static_assert(sizeof(i16) == 2); + static_assert(sizeof(i32) == 4); + static_assert(sizeof(i64) == 8); +} // namespace nes \ No newline at end of file -- cgit v1.3.1