diff --git a/src/nostalgia/core/CMakeLists.txt b/src/nostalgia/core/CMakeLists.txt index b8db6a0e..cfd34013 100644 --- a/src/nostalgia/core/CMakeLists.txt +++ b/src/nostalgia/core/CMakeLists.txt @@ -25,6 +25,7 @@ endif() install( FILES + color.hpp config.hpp consts.hpp context.hpp diff --git a/src/nostalgia/core/color.hpp b/src/nostalgia/core/color.hpp new file mode 100644 index 00000000..3f7db244 --- /dev/null +++ b/src/nostalgia/core/color.hpp @@ -0,0 +1,103 @@ +/* + * Copyright 2016 - 2021 gary@drinkingtea.net + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#pragma once + +#include +#include + +#include "context.hpp" + +namespace nostalgia::core { + +using Color16 = uint16_t; + +/** + * Nostalgia Core logically uses 16 bit colors, but must translate that to 32 + * bit colors in some implementations. + */ + +using Color32 = uint32_t; + +[[nodiscard]] +constexpr Color32 toColor32(Color16 nc) noexcept { + Color32 r = static_cast(((nc & 0b0000000000011111) >> 0) * 8); + Color32 g = static_cast(((nc & 0b0000001111100000) >> 5) * 8); + Color32 b = static_cast(((nc & 0b0111110000000000) >> 10) * 8); + Color32 a = 255; + return r | (g << 8) | (b << 16) | (a << 24); +} + + +[[nodiscard]] +constexpr uint8_t red16(Color16 c) noexcept { + return c & 0b0000000000011111; +} + +[[nodiscard]] +constexpr uint8_t green16(Color16 c) noexcept { + return (c & 0b0000001111100000) >> 5; +} + +[[nodiscard]] +constexpr uint8_t blue16(Color16 c) noexcept { + return (c & 0b0111110000000000) >> 10; +} + +[[nodiscard]] +constexpr uint8_t alpha16(Color16 c) noexcept { + return c >> 15; +} + +[[nodiscard]] +constexpr uint8_t red32(Color16 c) noexcept { + return red16(c) * 8; +} + +[[nodiscard]] +constexpr uint8_t green32(Color16 c) noexcept { + return green16(c) * 8; +} + +[[nodiscard]] +constexpr uint8_t blue32(Color16 c) noexcept { + return blue16(c) * 8; +} + +[[nodiscard]] +constexpr uint8_t alpha32(Color16 c) noexcept { + return (c >> 15) * 255; +} + + +[[nodiscard]] +constexpr uint8_t red32(Color32 c) noexcept { + return (c & 0x000000ff) >> 0; +} + +[[nodiscard]] +constexpr uint8_t green32(Color32 c) noexcept { + return (c & 0x0000ff00) >> 8; +} + +[[nodiscard]] +constexpr uint8_t blue32(Color32 c) noexcept { + return (c & 0x00ff0000) >> 16; +} + + +[[nodiscard]] +constexpr Color16 color16(uint8_t r, uint8_t g, uint8_t b) { + return r | (g << 5) | (b << 10); +} + +static_assert(color16(0, 31, 0) == 992); +static_assert(color16(16, 31, 0) == 1008); +static_assert(color16(16, 31, 8) == 9200); + +} diff --git a/src/nostalgia/core/gfx.cpp b/src/nostalgia/core/gfx.cpp index 529ff06d..40abbd85 100644 --- a/src/nostalgia/core/gfx.cpp +++ b/src/nostalgia/core/gfx.cpp @@ -141,42 +141,4 @@ char charMap[128] = { 0, // ~ }; -Color32 toColor32(Color16 nc) noexcept { - Color32 r = static_cast(((nc & 0b0000000000011111) >> 0) * 8); - Color32 g = static_cast(((nc & 0b0000001111100000) >> 5) * 8); - Color32 b = static_cast(((nc & 0b0111110000000000) >> 10) * 8); - Color32 a = 255; - return r | (g << 8) | (b << 16) | (a << 24); -} - - -uint8_t red32(Color32 c) noexcept { - return (c & 0x000000ff) >> 0; -} - -uint8_t green32(Color32 c) noexcept { - return (c & 0x0000ff00) >> 8; -} - -uint8_t blue32(Color32 c) noexcept { - return (c & 0x00ff0000) >> 16; -} - - -uint8_t red32(Color16 c) noexcept { - return red16(c) * 8; -} - -uint8_t green32(Color16 c) noexcept { - return green16(c) * 8; -} - -uint8_t blue32(Color16 c) noexcept { - return blue16(c) * 8; -} - -static_assert(color16(0, 31, 0) == 992); -static_assert(color16(16, 31, 0) == 1008); -static_assert(color16(16, 31, 8) == 9200); - } diff --git a/src/nostalgia/core/gfx.hpp b/src/nostalgia/core/gfx.hpp index db75a7f7..d05d4649 100644 --- a/src/nostalgia/core/gfx.hpp +++ b/src/nostalgia/core/gfx.hpp @@ -11,6 +11,7 @@ #include #include +#include "color.hpp" #include "context.hpp" namespace nostalgia::core { @@ -22,14 +23,6 @@ enum class TileSheetSpace { Sprite }; -using Color16 = uint16_t; - -/** - * Nostalgia Core logically uses 16 bit colors, but must translate that to 32 - * bit colors in some implementations. - */ -using Color32 = uint32_t; - struct NostalgiaPalette { static constexpr auto TypeName = "net.drinkingtea.nostalgia.core.NostalgiaPalette"; static constexpr auto Fields = 1; @@ -110,45 +103,6 @@ ox::Error loadSpriteTileSheet(Context *ctx, ox::FileAddress tilesheetAddr, ox::FileAddress paletteAddr); -[[nodiscard]] Color32 toColor32(Color16 nc) noexcept; - -[[nodiscard]] uint8_t red32(Color16 c) noexcept; - -[[nodiscard]] uint8_t green32(Color16 c) noexcept; - -[[nodiscard]] uint8_t blue32(Color16 c) noexcept; - -[[nodiscard]] constexpr uint8_t alpha32(Color16 c) noexcept { - return (c >> 15) * 255; -} - - -[[nodiscard]] constexpr Color16 color16(uint8_t r, uint8_t g, uint8_t b) { - return r | (g << 5) | (b << 10); -} - -[[nodiscard]] uint8_t red32(Color32 c) noexcept; - -[[nodiscard]] uint8_t green32(Color32 c) noexcept; - -[[nodiscard]] uint8_t blue32(Color32 c) noexcept; - -[[nodiscard]] constexpr uint8_t red16(Color16 c) noexcept { - return c & 0b0000000000011111; -} - -[[nodiscard]] constexpr uint8_t green16(Color16 c) noexcept { - return (c & 0b0000001111100000) >> 5; -} - -[[nodiscard]] constexpr uint8_t blue16(Color16 c) noexcept { - return (c & 0b0111110000000000) >> 10; -} - -[[nodiscard]] constexpr uint8_t alpha16(Color16 c) noexcept { - return c >> 15; -} - void puts(Context *ctx, int column, int row, const char *str); void setTile(Context *ctx, int layer, int column, int row, uint8_t tile);