Skip to content

Commit

Permalink
Merge pull request #68 from grunt-lucas/2.x/more-types
Browse files Browse the repository at this point in the history
Adding more basic data types
  • Loading branch information
grunt-lucas authored Oct 24, 2024
2 parents aa385ec + 0ac4cae commit 69dd506
Show file tree
Hide file tree
Showing 25 changed files with 273 additions and 68 deletions.
2 changes: 1 addition & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Language: Cpp
BasedOnStyle: LLVM
BreakBeforeBraces: Stroustrup
ColumnLimit: 120
ColumnLimit: 100
ReflowComments: true
IndentWidth: 4
TabWidth: 4
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/linux_amd64_clang.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ jobs:
cd build
cmake --build .
- name: Run test suite
- name: Run the tests
run: |
./build/Porytiles-1.x/tests/Porytiles1xTestSuite
./build/Porytiles-2.x/tests/Porytiles2xUnitTests
3 changes: 2 additions & 1 deletion .github/workflows/linux_amd64_gcc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ jobs:
cd build
cmake --build .
- name: Run test suite
- name: Run the tests
run: |
./build/Porytiles-1.x/tests/Porytiles1xTestSuite
./build/Porytiles-2.x/tests/Porytiles2xUnitTests
3 changes: 2 additions & 1 deletion .github/workflows/macos_arm64_clang.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ jobs:
cd build
cmake --build .
- name: Run test suite
- name: Run the tests
run: |
./build/Porytiles-1.x/tests/Porytiles1xTestSuite
./build/Porytiles-2.x/tests/Porytiles2xUnitTests
3 changes: 2 additions & 1 deletion .github/workflows/macos_arm64_gcc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ jobs:
cd build
cmake --build .
- name: Run test suite
- name: Run the tests
run: |
./build/Porytiles-1.x/tests/Porytiles1xTestSuite
./build/Porytiles-2.x/tests/Porytiles2xUnitTests
2 changes: 2 additions & 0 deletions .github/workflows/pr_dev_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ on:
jobs:
build-linux-amd64-clang:
uses: ./.github/workflows/linux_amd64_clang.yml
build-macos-arm64-clang:
uses: ./.github/workflows/macos_arm64_clang.yml
6 changes: 1 addition & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@

# Build artifacts and executables
build/**
cmake-build-debug/**
Porytiles-1.x/build/**
Porytiles-1.x/cmake-build-debug/**
Porytiles-2.x/build/**
Porytiles-2.x/cmake-build-debug/**
cmake-build-*/**

# LLVM coverage files
default.profraw
Expand Down
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.10)
cmake_minimum_required(VERSION 3.20)

project(Porytiles CXX)

Expand All @@ -8,7 +8,7 @@ enable_testing()
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
# set(CMAKE_VERBOSE_MAKEFILE on)
set(CMAKE_VERBOSE_MAKEFILE OFF)

add_subdirectory(Porytiles-1.x)
add_subdirectory(Porytiles-2.x)
9 changes: 9 additions & 0 deletions Porytiles-1.x/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
project(Porytiles1x CXX)

# Enable compilation warnings for 1.x
if(MSVC)
add_compile_options(/W4 /WX)
else()
# Cannot enable -Wextra in 1.x since some of our header-only dependencies throw warnings
# that break the build.
add_compile_options(-Wall -Wpedantic -Werror)
endif()

# Enable testing
enable_testing()

Expand Down
17 changes: 6 additions & 11 deletions Porytiles-2.x/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
project(Porytiles2x CXX)

# Fetch and build Google Test
include(FetchContent)
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG v1.15.2
)
FetchContent_MakeAvailable(googletest)
# Enable compilation warnings for 2.x
if(MSVC)
add_compile_options(/W4 /WX)
else()
add_compile_options(-Wall -Wextra -Wpedantic -Werror)
endif()

# Set a variable that points to the global include directory for Porytiles-2.x
set(PORYTILES_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/Porytiles-2.x/include)

add_subdirectory(lib)
add_subdirectory(tests)
add_subdirectory(tools)

# Export the library
export(PACKAGE Porytiles2xLib)
66 changes: 52 additions & 14 deletions Porytiles-2.x/include/porytiles/Color/Bgr15.h
Original file line number Diff line number Diff line change
@@ -1,59 +1,97 @@
#ifndef PORYTILES_COLOR_BGR15_H
#define PORYTILES_COLOR_BGR15_H

#include <compare>
#include <cstdint>

namespace porytiles::color {

/**
* @brief Value object representing a color in 15-bit BGR format.
*
* @details
* TODO 2.x : fill in explanation about BGR format, 5 bits per color, top bit unused, etc.
*/
class Bgr15 {
std::uint16_t bgr;

public:
/**
* @brief Default constructor for Bgr15.
*
* Initializes a Bgr15 object with default values, equivalent to zero-initialization.
* This constructor is implicitly defined.
* @brief Default constructor for Bgr15. Initializes to 0.
*/
Bgr15() = default;

/**
* @brief Construct a Bgr15 object from a 16-bit BGR value.
*
* This constructor initializes a Bgr15 object using a 16-bit integer
* that represents a color in 15-bit BGR format.
*
* @param bgr The 16-bit BGR value used to initialize the object.
*/
explicit constexpr Bgr15(const std::uint16_t bgr) : bgr{bgr} {}

/**
* @brief Constructs a Bgr15 object with the given 8-bit RGB color components.
*
* This constructor takes individual red, green, and blue color components,
* scales them down to 5 bits each, and composes them into a 15-bit BGR value.
* @details
* This constructor takes individual red, green, and blue color components, scales them down to
* 5 bits each, and composes them into a 15-bit BGR value.
*
* @param red The 8-bit red component of the color.
* @param green The 8-bit green component of the color.
* @param blue The 8-bit blue component of the color.
*/
explicit constexpr Bgr15(std::uint8_t red, std::uint8_t green, std::uint8_t blue)
explicit constexpr Bgr15(const std::uint8_t red, const std::uint8_t green,
const std::uint8_t blue)
{
bgr = static_cast<std::uint16_t>(((blue / 8) << 10) | ((green / 8) << 5) | (red / 8));
bgr = static_cast<std::uint16_t>((blue >> 3 << 10) | (green >> 3 << 5) | red >> 3);
}

/**
* @brief Returns the raw 16-bit BGR value.
*
* This method provides direct access to the internal 15-bit BGR representation
* of the color.
*
* @return The 16-bit BGR value.
*/
[[nodiscard]] std::uint16_t getRawValue() const;

/**
* @brief Computes the blue component from the 15-bit BGR color value.
*
* @details
* Extracts the blue color component from the internal 15-bit BGR representation, scales it back
* up to an 8-bit value, and returns it.
*
* @return The 8-bit blue component derived from the 15-bit BGR value.
*/
[[nodiscard]] std::uint8_t computeBlueComponent() const;

/**
* @brief Computes the green component from the 15-bit BGR color value.
*
* @details
* Extracts the green color component from the internal 15-bit BGR representation, scales it
* back up to an 8-bit value, and returns it.
*
* @return The 8-bit green component derived from the 15-bit BGR value.
*/
[[nodiscard]] std::uint8_t computeGreenComponent() const;

/**
* @brief Computes the red component from the 15-bit BGR color value.
*
* @details
* Extracts the red color component from the internal 15-bit BGR representation, scales it back
* up to an 8-bit value, and returns it.
*
* @return The 8-bit red component derived from the 15-bit BGR value.
*/
[[nodiscard]] std::uint8_t computeRedComponent() const;

/**
* @brief Provides a three-way comparison for Bgr15 objects.
*
* @param other The Bgr15 object to compare with.
* @return A std::strong_ordering result indicating the relative order of the objects.
*/
std::strong_ordering operator<=>(const Bgr15 &other) const = default;
};

} // end namespace porytiles::color
Expand Down
15 changes: 15 additions & 0 deletions Porytiles-2.x/include/porytiles/Color/ColorConverters.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#ifndef PORYTILES_COLOR_COLOR_CONVERTERS_H
#define PORYTILES_COLOR_COLOR_CONVERTERS_H

#include "porytiles/Color/Bgr15.h"
#include "porytiles/Color/Rgba32.h"

namespace porytiles::color {

Bgr15 rgbaToBgr(const Rgba32 &rgba);

Rgba32 bgrToRgba(const Bgr15 &bgr);

} // namespace porytiles::color

#endif // PORYTILES_COLOR_COLOR_CONVERTERS_H
72 changes: 46 additions & 26 deletions Porytiles-2.x/include/porytiles/Color/Rgba32.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef PORYTILES_COLOR_RGBA_H
#define PORYTILES_COLOR_RGBA_H
#ifndef PORYTILES_COLOR_RGBA32_H
#define PORYTILES_COLOR_RGBA32_H

#include <compare>
#include <cstdint>
Expand All @@ -9,6 +9,9 @@ namespace porytiles::color {

/**
* @brief Value object representing a color in 32-bit RGBA format.
*
* @details
* TODO 2.x : fill in explanation about RGBA format, 8 bits per color, alpha channel, etc
*/
class Rgba32 {
std::uint8_t red;
Expand All @@ -21,53 +24,73 @@ class Rgba32 {
static constexpr std::uint8_t ALPHA_OPAQUE = 0xff;

/**
* @brief Default constructor for Rgba32.
*
* Initializes an Rgba32 object with default values.
* The default constructor is implicitly defined and initializes the color components to zero.
*
* @return A default-initialized Rgba32 object.
* @brief Default constructor for Rgba32. Initializes the color and alpha components to zero.
*/
Rgba32() = default;

/**
* @brief Constructs an Rgba32 object with specified red, green, blue, and alpha component values.
*
* This constructor initializes an Rgba32 object with the provided values for each color component.
* @brief Constructs an Rgba32 object with specified red, green, blue, and alpha component
* values.
*
* @param red The red component value.
* @param green The green component value.
* @param blue The blue component value.
* @param alpha The alpha (transparency) component value.
* @return An Rgba32 object with the specified component values.
*/
explicit constexpr Rgba32(const std::uint8_t red, const std::uint8_t green, const std::uint8_t blue,
const std::uint8_t alpha)
explicit constexpr Rgba32(const std::uint8_t red, const std::uint8_t green,
const std::uint8_t blue, const std::uint8_t alpha)
: red{red}, green{green}, blue{blue}, alpha{alpha}
{
}

/**
* @brief Constructs an Rgba32 object with specified RGB values and an implicit alpha value of 255.
*
* This constructor initializes an Rgba32 object with the given red, green, and blue component values,
* and automatically sets the alpha component to 255 (fully opaque).
* @brief Constructs an Rgba32 object with specified RGB values and an implicit alpha value of
* 255 (fully opaque).
*
* @param red The red component of the color.
* @param green The green component of the color.
* @param blue The blue component of the color.
* @return An Rgba32 object with the specified RGB values and an alpha value of 255.
*/
explicit constexpr Rgba32(const std::uint8_t red, const std::uint8_t green, const std::uint8_t blue)
explicit constexpr Rgba32(const std::uint8_t red, const std::uint8_t green,
const std::uint8_t blue)
: red{red}, green{green}, blue{blue}, alpha{ALPHA_OPAQUE}
{
}

/**
* @brief Retrieves the red component of the Rgba32 color.
*
* @return The red component as an 8-bit unsigned integer.
*/
[[nodiscard]] std::uint8_t getRedComponent() const;

/**
* @brief Retrieves the green component of the Rgba32 color.
*
* @return The green component as an 8-bit unsigned integer.
*/
[[nodiscard]] std::uint8_t getGreenComponent() const;

/**
* @brief Retrieves the blue component of the Rgba32 color.
*
* @return The blue component as an 8-bit unsigned integer.
*/
[[nodiscard]] std::uint8_t getBlueComponent() const;

/**
* @brief Retrieves the alpha component of the Rgba32 color.
*
* @return The alpha component as an 8-bit unsigned integer.
*/
[[nodiscard]] std::uint8_t getAlphaComponent() const;

/**
* @brief Converts the RGBA color to a JASC-PAL formatted string.
*
* This method converts the red, green, and blue components of the Rgba32 object
* into a space-separated string format commonly used in JASC-PAL color palette files.
* @details
* This method converts the red, green, and blue components of the Rgba32 object into a
* space-separated string format commonly used in JASC-PAL color palette files.
*
* @return A string representation of the color in JASC-PAL format.
*/
Expand All @@ -76,9 +99,6 @@ class Rgba32 {
/**
* @brief Provides a three-way comparison for Rgba32 objects.
*
* The <=> operator allows Rgba32 objects to be compared using the built-in
* strong ordering mechanism, facilitating easy and efficient comparisons.
*
* @param other The Rgba32 object to compare with.
* @return A std::strong_ordering result indicating the relative order of the objects.
*/
Expand All @@ -87,4 +107,4 @@ class Rgba32 {

} // end namespace porytiles::color

#endif // PORYTILES_COLOR_RGBA_H
#endif // PORYTILES_COLOR_RGBA32_H
12 changes: 12 additions & 0 deletions Porytiles-2.x/include/porytiles/Tile/Rgba32Tile.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef PORYTILES_TILE_RGBA32_TILE_H
#define PORYTILES_TILE_RGBA32_TILE_H

namespace porytiles::color {

class Rgba32Tile {

};

} // end namespace porytiles::color

#endif // PORYTILES_TILE_RGBA32_TILE_H
2 changes: 2 additions & 0 deletions Porytiles-2.x/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ set_target_properties(Porytiles2xLib PROPERTIES OUTPUT_NAME "${CANONICAL_LIB_NAM
target_include_directories(Porytiles2xLib PUBLIC ${PORYTILES_INCLUDE_DIR} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})

# Export the library
# TODO 2.x : what's the difference between these two
export(TARGETS Porytiles2xLib FILE ${CMAKE_CURRENT_BINARY_DIR}/PorytilesLibraryTargets.cmake)
export(PACKAGE Porytiles2xLib)
Loading

0 comments on commit 69dd506

Please sign in to comment.