Add PD::Image and add a setter for logfilter

This commit is contained in:
2026-03-21 15:52:09 +01:00
parent 6fab4a07a9
commit 4dab1987d1
8 changed files with 239 additions and 66 deletions
+77 -65
View File
@@ -12,52 +12,64 @@ include(cmake/palladium.cmake)
option(PD_BUILD_TESTS "Sets if TestApp and TestBench get build" OFF) option(PD_BUILD_TESTS "Sets if TestApp and TestBench get build" OFF)
option(PD_BUILD_SHARED "Build Shared Library" OFF) option(PD_BUILD_SHARED "Build Shared Library" OFF)
option(PD_BUILD_TOOLS "Build Palladium Tools" OFF) option(PD_BUILD_TOOLS "Build Palladium Tools" OFF)
option(PD_INCLUDE_STB_IMAGE "Inlude stb image symbols in palladium" ON)
if(${CMAKE_SYSTEM_NAME} STREQUAL "Nintendo3DS") if(${CMAKE_SYSTEM_NAME} STREQUAL "Nintendo3DS")
add_compile_options(-Wno-psabi) add_compile_options(-Wno-psabi)
endif() endif()
if(${PD_BUILD_TOOLS}) if(${PD_BUILD_TOOLS})
add_subdirectory(tools) add_subdirectory(tools)
endif() endif()
add_subdirectory(vendor) add_subdirectory(vendor)
# # Include Library Source # # Include Library Source
set(PD_SOURCES set(PD_SOURCES
# Common # Common
source/common.cpp source/common.cpp
# Core # Core
source/core/bits.cpp source/core/bits.cpp
source/core/color.cpp source/core/color.cpp
source/core/io.cpp source/core/io.cpp
source/core/mat.cpp source/core/mat.cpp
source/core/strings.cpp source/core/strings.cpp
source/core/timer.cpp source/core/timer.cpp
source/core/timetrace.cpp source/core/timetrace.cpp
# Drivers # Image
source/drivers/os.cpp source/image/image.cpp
source/drivers/gfx.cpp
# Lithium # Drivers
source/lithium/pools.cpp source/drivers/os.cpp
source/lithium/math.cpp source/drivers/gfx.cpp
source/lithium/drawlist.cpp
# Lithium
source/lithium/pools.cpp
source/lithium/math.cpp
source/lithium/drawlist.cpp
) )
if(${PD_BUILD_SHARED}) if(${PD_BUILD_SHARED})
add_library(palladium SHARED ${PD_SOURCES}) add_library(palladium SHARED ${PD_SOURCES})
target_compile_definitions(palladium PRIVATE PD_BUILD_SHARED) target_compile_definitions(palladium PRIVATE PD_BUILD_SHARED)
else() else()
add_library(palladium STATIC ${PD_SOURCES}) add_library(palladium STATIC ${PD_SOURCES})
target_compile_definitions(palladium PUBLIC PD_BUILD_STATIC) target_compile_definitions(palladium PUBLIC PD_BUILD_STATIC)
endif() endif()
target_compile_definitions(palladium PUBLIC PD_DEBUG) target_link_libraries(palladium
PUBLIC stb
)
target_compile_definitions(palladium
PUBLIC
PD_DEBUG
$<$<OR:$<BOOL:${PD_INCLUDE_STB_IMAGE}>,$<BOOL:${PD_BUILD_SHARED}>>:PD_INCLUDE_STB_IMAGE> # Always on in shared build
)
target_compile_options(palladium target_compile_options(palladium
PUBLIC $<$<CXX_COMPILER_ID:GNU,Clang>: PUBLIC $<$<CXX_COMPILER_ID:GNU,Clang>:
-fmacro-prefix-map=${CMAKE_CURRENT_SOURCE_DIR}/source=source -fmacro-prefix-map=${CMAKE_CURRENT_SOURCE_DIR}/source=source
-fmacro-prefix-map=${CMAKE_CURRENT_SOURCE_DIR}/include=include -fmacro-prefix-map=${CMAKE_CURRENT_SOURCE_DIR}/include=include
> >
@@ -66,75 +78,75 @@ target_compile_options(palladium
add_library(palladium::palladium ALIAS palladium) add_library(palladium::palladium ALIAS palladium)
target_include_directories(palladium target_include_directories(palladium
PUBLIC PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include> $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include> $<INSTALL_INTERFACE:include>
) )
target_compile_options(palladium target_compile_options(palladium
PRIVATE PRIVATE
$<$<AND:$<CONFIG:Debug>,$<CXX_COMPILER_ID:GNU,Clang>>:-O0 -g> $<$<AND:$<CONFIG:Debug>,$<CXX_COMPILER_ID:GNU,Clang>>:-O0 -g>
$<$<AND:$<CONFIG:Release>,$<CXX_COMPILER_ID:GNU,Clang>>:-O3> $<$<AND:$<CONFIG:Release>,$<CXX_COMPILER_ID:GNU,Clang>>:-O3>
$<$<AND:$<CONFIG:Debug>,$<CXX_COMPILER_ID:MSVC>>:/Od /Zi> $<$<AND:$<CONFIG:Debug>,$<CXX_COMPILER_ID:MSVC>>:/Od /Zi>
$<$<AND:$<CONFIG:Release>,$<CXX_COMPILER_ID:MSVC>>:/O2> $<$<AND:$<CONFIG:Release>,$<CXX_COMPILER_ID:MSVC>>:/O2>
) )
install( install(
TARGETS palladium TARGETS palladium
EXPORT palladiumTargets EXPORT palladiumTargets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
) )
install( install(
DIRECTORY include/ DIRECTORY include/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
) )
install(EXPORT palladiumTargets install(EXPORT palladiumTargets
FILE palladiumTargets.cmake FILE palladiumTargets.cmake
NAMESPACE palladium:: NAMESPACE palladium::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/palladium DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/palladium
) )
include(CMakePackageConfigHelpers) include(CMakePackageConfigHelpers)
configure_package_config_file( configure_package_config_file(
cmake/palladiumConfig.cmake.in cmake/palladiumConfig.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/palladiumConfig.cmake ${CMAKE_CURRENT_BINARY_DIR}/palladiumConfig.cmake
INSTALL_DESTINATION INSTALL_DESTINATION
${CMAKE_INSTALL_LIBDIR}/cmake/palladium ${CMAKE_INSTALL_LIBDIR}/cmake/palladium
) )
write_basic_package_version_file( write_basic_package_version_file(
${CMAKE_CURRENT_BINARY_DIR}/palladiumConfigVersion.cmake ${CMAKE_CURRENT_BINARY_DIR}/palladiumConfigVersion.cmake
VERSION VERSION
${PROJECT_VERSION} ${PROJECT_VERSION}
COMPATIBILITY COMPATIBILITY
SameMajorVersion SameMajorVersion
) )
install( install(
FILES FILES
${CMAKE_CURRENT_BINARY_DIR}/palladiumConfig.cmake ${CMAKE_CURRENT_BINARY_DIR}/palladiumConfig.cmake
${CMAKE_CURRENT_BINARY_DIR}/palladiumConfigVersion.cmake ${CMAKE_CURRENT_BINARY_DIR}/palladiumConfigVersion.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/palladium DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/palladium
) )
find_program(CLANG_FORMAT clang-format) find_program(CLANG_FORMAT clang-format)
file(GLOB_RECURSE PD_FMTFILES file(GLOB_RECURSE PD_FMTFILES
CONFIGURE_DEPENDS CONFIGURE_DEPENDS
${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp
${CMAKE_CURRENT_SOURCE_DIR}/include/*.hpp ${CMAKE_CURRENT_SOURCE_DIR}/include/*.hpp
${CMAKE_CURRENT_SOURCE_DIR}/backends/source/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/backends/source/*.cpp
${CMAKE_CURRENT_SOURCE_DIR}/backends/include/*.hpp ${CMAKE_CURRENT_SOURCE_DIR}/backends/include/*.hpp
${CMAKE_CURRENT_SOURCE_DIR}/tests/core/source/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/tests/core/source/*.cpp
${CMAKE_CURRENT_SOURCE_DIR}/tests/gfx/source/*.cpp ${CMAKE_CURRENT_SOURCE_DIR}/tests/gfx/source/*.cpp
) )
add_custom_target(pd-clang-format add_custom_target(pd-clang-format
COMMAND ${CLANG_FORMAT} --style=file -i ${PD_FMTFILES} COMMAND ${CLANG_FORMAT} --style=file -i ${PD_FMTFILES}
COMMENT "Formatting Project Sources" COMMENT "Formatting Project Sources"
) )
add_subdirectory(backends) add_subdirectory(backends)
+1
View File
@@ -25,4 +25,5 @@ SOFTWARE.
#include <pd/core/core.hpp> #include <pd/core/core.hpp>
#include <pd/drivers/drivers.hpp> #include <pd/drivers/drivers.hpp>
#include <pd/image/image.hpp>
#include <pd/lithium/lithium.hpp> #include <pd/lithium/lithium.hpp>
+2
View File
@@ -44,6 +44,7 @@ SOFTWARE.
namespace PD { namespace PD {
enum class LogLevel { enum class LogLevel {
None,
Info, Info,
Warning, Warning,
Error, Error,
@@ -56,6 +57,7 @@ using u16 = unsigned short;
using u32 = unsigned int; using u32 = unsigned int;
using u64 = unsigned long long; using u64 = unsigned long long;
using ptr = uintptr_t; using ptr = uintptr_t;
PD_API void LogFilter(LogLevel minimum);
PD_API void Log(const std::string& txt, LogLevel lvl = LogLevel::Info); PD_API void Log(const std::string& txt, LogLevel lvl = LogLevel::Info);
template <typename... Args> template <typename... Args>
void Log(std::format_string<Args...> fmt, Args&&... args) { void Log(std::format_string<Args...> fmt, Args&&... args) {
+48
View File
@@ -0,0 +1,48 @@
#pragma once
#include <pd/core/core.hpp>
namespace PD {
class PD_API Image {
public:
enum class Format {
RGBA, // bpp == 4
RGB, // bpp == 3
RGB565, // bpp == 2 (not supported in laoding)
BGR, // bpp == 3
ABGR, // bpp == 4
BGRA, // bpp == 4
};
Image();
Image(const std::string& path);
Image(const std::vector<u8>& buf);
Image(const std::vector<u8>& pixels, int w, int h, int bpp = 4);
~Image();
void Load(const std::string& path);
void Load(const std::vector<u8>& buf);
void Load(const u8* buf, size_t size);
void Copy(const std::vector<u8>& pixels, int w, int h, int bpp = 4);
const int& Width() const { return pSize.x; }
const int& Height() const { return pSize.x; }
const ivec2& Size() const { return pSize; }
const std::vector<u8>& data() const { return pData; }
std::vector<u8>& data() { return pData; }
operator const std::vector<u8>&() const { return pData; }
operator std::vector<u8>&() { return pData; }
operator const ivec2&() const { return pSize; }
operator const Format&() const { return pFormat; }
void Flip(bool horizontal = true, bool vertical = true);
static int Format2Bpp(Format fmt);
static Format GuessFmtFromBpp(int bpp);
private:
std::vector<u8> pData;
ivec2 pSize;
Format pFormat = Format::RGBA;
};
} // namespace PD
+5
View File
@@ -6,6 +6,11 @@ constexpr const char* pColorNo = "\033[0m";
constexpr const char* pColorYellow = "\033[33m"; constexpr const char* pColorYellow = "\033[33m";
constexpr const char* pColorRed = "\033[31m"; constexpr const char* pColorRed = "\033[31m";
static LogLevel pFilter = LogLevel::Info; static LogLevel pFilter = LogLevel::Info;
PD_API void LogFilter(LogLevel lvl) {
pFilter = lvl;
}
PD_API void Log(const std::string& txt, LogLevel lvl) { PD_API void Log(const std::string& txt, LogLevel lvl) {
if ((int)lvl < (int)pFilter) return; if ((int)lvl < (int)pFilter) return;
const char* clr = pColorNo; const char* clr = pColorNo;
+90
View File
@@ -0,0 +1,90 @@
#include <pd/image/image.hpp>
#if defined(PD_INCLUDE_STB_IMAGE)
#define STB_IMAGE_IMPLEMENTATION
#endif
#include <stb_image.h>
namespace PD {
PD_API Image::Image() {}
PD_API Image::Image(const std::string& path) { Load(path); }
PD_API Image::Image(const std::vector<u8>& buf) { Load(buf); }
PD_API Image::Image(const std::vector<u8>& pixels, int w, int h, int bpp) {
Copy(pixels, w, h, bpp);
}
PD_API Image::~Image() {}
PD_API void Image::Load(const u8* buf, size_t size) {
int w = 0, h = 0, c = 0;
u8* img = stbi_load_from_memory(buf, size, &w, &h, &c, 4);
if (c == 3) {
stbi_image_free(img);
img = stbi_load_from_memory(buf, size, &w, &h, &c, 3);
pFormat = Format::RGB;
}
pData.assign(img, img + (w * h * c));
pSize = ivec2(w, h);
stbi_image_free(img);
}
PD_API void Image::Load(const std::string& path) {
int w = 0, h = 0, c = 0;
u8* img = stbi_load(path.c_str(), &w, &h, &c, 4);
if (c == 3) {
stbi_image_free(img);
img = stbi_load(path.c_str(), &w, &h, &c, 3);
pFormat = Format::RGB;
}
pData.assign(img, img + (w * h * c));
pSize = ivec2(w, h);
stbi_image_free(img);
}
PD_API void Image::Load(const std::vector<u8>& buf) {
Load(buf.data(), buf.size());
}
PD_API void Image::Copy(const std::vector<u8>& pixels, int w, int h, int bpp) {
pData = pixels;
pSize = ivec2(w, h);
pFormat = GuessFmtFromBpp(bpp);
}
PD_API int Image::Format2Bpp(Format fmt) {
switch (fmt) {
case Format::RGBA:
case Format::ABGR:
case Format::BGRA:
return 4;
case Format::BGR:
case Format::RGB:
return 3;
case Format::RGB565:
return 2;
}
return 0;
}
PD_API Image::Format Image::GuessFmtFromBpp(int bpp) {
/** Only return defaults here */
switch (bpp) {
case 4:
return Format::RGBA;
case 3:
return Format::RGB;
case 2:
return Format::RGB565;
default:
return Format::RGBA;
}
}
PD_API void Image::Flip(bool hz, bool vt) {
// TODO
}
} // namespace PD
+7
View File
@@ -16,6 +16,7 @@ const char* ResourcePath(const char* in) {
} }
int main(int argc, char** argv) { int main(int argc, char** argv) {
PD::LogFilter(PD::LogLevel::Warning);
Driver drv = Driver::OpenGL3; Driver drv = Driver::OpenGL3;
if (argc == 2) { if (argc == 2) {
if (std::string(argv[1]) == "gl2") { if (std::string(argv[1]) == "gl2") {
@@ -36,16 +37,22 @@ int main(int argc, char** argv) {
pOs->Init(); pOs->Init();
PD::Gfx::Init(); PD::Gfx::Init();
PD::Li::Drawlist pList; PD::Li::Drawlist pList;
PD::Image img(ResourcePath("icon.png"));
auto pTex = PD::Gfx::LoadTexture(img, img.Width(), img.Height());
while (pOs->Mainloop()) { while (pOs->Mainloop()) {
pOs->ClearViewPort(); pOs->ClearViewPort();
PD::Li::ResetPools(); PD::Li::ResetPools();
pList.PathRect(pOs->SizeTranslate(0.05), pOs->SizeTranslate(0.4f), 10.f); pList.PathRect(pOs->SizeTranslate(0.05), pOs->SizeTranslate(0.4f), 10.f);
pList.PathFill(0xff00ffff); pList.PathFill(0xff00ffff);
pList.BindTexture(pTex);
pList.DrawRectFilled(pOs->SizeTranslate(PD::fvec2(0.02f, 0.5f)),
pOs->SizeTranslate(PD::fvec2(0.14, 0.3)), 0xffffffff);
PD::Gfx::Reset(); PD::Gfx::Reset();
PD::Gfx::Draw(pList); PD::Gfx::Draw(pList);
pList.Clear(); pList.Clear();
pOs->SwapBuffers(); pOs->SwapBuffers();
} }
PD::Gfx::DeleteTexture(pTex);
PD::Gfx::Deinit(); PD::Gfx::Deinit();
pOs->Deinit(); pOs->Deinit();
delete pOs; delete pOs;
+9 -1
View File
@@ -14,7 +14,15 @@ endif()
# STB # STB
add_library(stb INTERFACE) add_library(stb INTERFACE)
target_include_directories(stb INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/stb) target_include_directories(stb
INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/stb>
$<INSTALL_INTERFACE:include>
)
install(TARGETS stb
EXPORT palladiumTargets
INCLUDES DESTINATION include
)
if(NOT ${CMAKE_SYSTEM_NAME} STREQUAL "Nintendo3DS" AND NOT ${CMAKE_SYSTEM_NAME} STREQUAL "NintendoSwitch") if(NOT ${CMAKE_SYSTEM_NAME} STREQUAL "Nintendo3DS" AND NOT ${CMAKE_SYSTEM_NAME} STREQUAL "NintendoSwitch")
# GLFW # GLFW