[nostalgia/core] Move color types and functions into separate file

This commit is contained in:
Gary Talent 2021-03-18 20:09:40 -05:00
parent 9860fec00e
commit e76fbb0095
4 changed files with 105 additions and 85 deletions

View File

@ -25,6 +25,7 @@ endif()
install(
FILES
color.hpp
config.hpp
consts.hpp
context.hpp

View File

@ -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 <ox/std/types.hpp>
#include <nostalgia/common/point.hpp>
#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<Color32>(((nc & 0b0000000000011111) >> 0) * 8);
Color32 g = static_cast<Color32>(((nc & 0b0000001111100000) >> 5) * 8);
Color32 b = static_cast<Color32>(((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);
}

View File

@ -141,42 +141,4 @@ char charMap[128] = {
0, // ~
};
Color32 toColor32(Color16 nc) noexcept {
Color32 r = static_cast<Color32>(((nc & 0b0000000000011111) >> 0) * 8);
Color32 g = static_cast<Color32>(((nc & 0b0000001111100000) >> 5) * 8);
Color32 b = static_cast<Color32>(((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);
}

View File

@ -11,6 +11,7 @@
#include <ox/std/types.hpp>
#include <nostalgia/common/point.hpp>
#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);