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
+13 -1
View File
@@ -12,6 +12,7 @@ include(cmake/palladium.cmake)
option(PD_BUILD_TESTS "Sets if TestApp and TestBench get build" OFF)
option(PD_BUILD_SHARED "Build Shared Library" 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")
add_compile_options(-Wno-psabi)
@@ -37,6 +38,9 @@ set(PD_SOURCES
source/core/timer.cpp
source/core/timetrace.cpp
# Image
source/image/image.cpp
# Drivers
source/drivers/os.cpp
source/drivers/gfx.cpp
@@ -55,7 +59,15 @@ else()
target_compile_definitions(palladium PUBLIC PD_BUILD_STATIC)
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
PUBLIC $<$<CXX_COMPILER_ID:GNU,Clang>:
-fmacro-prefix-map=${CMAKE_CURRENT_SOURCE_DIR}/source=source
+1
View File
@@ -25,4 +25,5 @@ SOFTWARE.
#include <pd/core/core.hpp>
#include <pd/drivers/drivers.hpp>
#include <pd/image/image.hpp>
#include <pd/lithium/lithium.hpp>
+2
View File
@@ -44,6 +44,7 @@ SOFTWARE.
namespace PD {
enum class LogLevel {
None,
Info,
Warning,
Error,
@@ -56,6 +57,7 @@ using u16 = unsigned short;
using u32 = unsigned int;
using u64 = unsigned long long;
using ptr = uintptr_t;
PD_API void LogFilter(LogLevel minimum);
PD_API void Log(const std::string& txt, LogLevel lvl = LogLevel::Info);
template <typename... 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* pColorRed = "\033[31m";
static LogLevel pFilter = LogLevel::Info;
PD_API void LogFilter(LogLevel lvl) {
pFilter = lvl;
}
PD_API void Log(const std::string& txt, LogLevel lvl) {
if ((int)lvl < (int)pFilter) return;
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) {
PD::LogFilter(PD::LogLevel::Warning);
Driver drv = Driver::OpenGL3;
if (argc == 2) {
if (std::string(argv[1]) == "gl2") {
@@ -36,16 +37,22 @@ int main(int argc, char** argv) {
pOs->Init();
PD::Gfx::Init();
PD::Li::Drawlist pList;
PD::Image img(ResourcePath("icon.png"));
auto pTex = PD::Gfx::LoadTexture(img, img.Width(), img.Height());
while (pOs->Mainloop()) {
pOs->ClearViewPort();
PD::Li::ResetPools();
pList.PathRect(pOs->SizeTranslate(0.05), pOs->SizeTranslate(0.4f), 10.f);
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::Draw(pList);
pList.Clear();
pOs->SwapBuffers();
}
PD::Gfx::DeleteTexture(pTex);
PD::Gfx::Deinit();
pOs->Deinit();
delete pOs;
+9 -1
View File
@@ -14,7 +14,15 @@ endif()
# STB
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")
# GLFW