2 Commits

Author SHA1 Message Date
3d8b57ead6 add .gitattributes file 2026-03-16 20:28:30 +01:00
ac37ce9e12 Fix test 2026-03-14 21:15:40 +01:00
155 changed files with 55259 additions and 27295 deletions

3
.gitattributes vendored
View File

@@ -1 +1,2 @@
vendor/* linguist-vendored
include/pd/external/* linguist-vendored
backends/desktop/glad/* linguist-vendored

12
.gitmodules vendored
View File

@@ -1,6 +1,6 @@
[submodule "vendor/stb"]
path = vendor/stb
url = https://github.com/nothings/stb.git
[submodule "vendor/glfw"]
path = vendor/glfw
url = https://github.com/glfw/glfw.git
[submodule "backends/desktop/glfw"]
path = backends/desktop/glfw
url = https://github.com/glfw/glfw
[submodule "backends/3ds/libpicasso"]
path = backends/3ds/libpicasso
url = https://github.com/npid7/libpicasso

View File

@@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.22)
include(GNUInstallDirs)
# Set Project
project(palladium LANGUAGES C CXX VERSION 0.7.0)
project(palladium LANGUAGES C CXX VERSION 0.6.0)
# Required to add this Variable
set(PD_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include)
@@ -12,49 +12,72 @@ 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_BUILD_DESKTOP "Build Desktop Backend" OFF)
option(PD_BUILD_3DS "Build 3ds Backend" OFF)
option(PD_BUNDLE_GLFW "Bundle GLFW in install" OFF)
if(${PD_BUILD_TOOLS})
add_subdirectory(tools)
endif()
add_subdirectory(vendor)
# # Include Library Source
set(PD_SOURCES
# Common
source/common.cpp
# Core
source/core/bits.cpp
source/core/bit_util.cpp
source/core/color.cpp
source/core/io.cpp
source/core/mat.cpp
source/core/strings.cpp
source/core/timer.cpp
source/core/timetrace.cpp
# Drivers
source/drivers/os.cpp
source/drivers/context.cpp
source/drivers/gfx.cpp
source/drivers/hid.cpp
source/drivers/os.cpp
# External
source/external/stb.cpp
# Image
source/image/image.cpp
source/image/img_blur.cpp
source/image/img_convert.cpp
# Lithium
source/lithium/pools.cpp
source/lithium/command.cpp
source/lithium/drawlist.cpp
source/lithium/font.cpp
source/lithium/fonts.cpp
source/lithium/renderer.cpp
# UI7
source/ui7/container/button.cpp
source/ui7/container/checkbox.cpp
source/ui7/container/coloredit.cpp
source/ui7/container/container.cpp
source/ui7/container/dragdata.cpp
source/ui7/container/dynobj.cpp
source/ui7/container/image.cpp
source/ui7/container/label.cpp
source/ui7/container/slider.cpp
source/ui7/io.cpp
source/ui7/layout.cpp
source/ui7/menu.cpp
source/ui7/theme.cpp
source/ui7/ui7.cpp
)
if(${PD_BUILD_SHARED})
add_library(palladium SHARED ${PD_SOURCES})
target_compile_definitions(palladium PRIVATE PD_BUILD_SHARED)
target_compile_definitions(palladium PRIVATE -DPD_BUILD_SHARED)
else()
add_library(palladium STATIC ${PD_SOURCES})
target_compile_definitions(palladium PUBLIC PD_BUILD_STATIC)
target_compile_definitions(palladium PUBLIC -DPD_BUILD_STATIC)
endif()
target_compile_definitions(palladium PUBLIC PD_DEBUG)
target_compile_options(palladium
PUBLIC
$<$<CXX_COMPILER_ID:GNU,Clang>:
-fmacro-prefix-map=${CMAKE_CURRENT_SOURCE_DIR}/source=source
-fmacro-prefix-map=${CMAKE_CURRENT_SOURCE_DIR}/include=include
>
)
add_library(palladium::palladium ALIAS palladium)
target_include_directories(palladium
@@ -63,24 +86,33 @@ target_include_directories(palladium
$<INSTALL_INTERFACE:include>
)
target_compile_options(palladium
PRIVATE
$<$<AND:$<CONFIG:Debug>,$<CXX_COMPILER_ID:GNU,Clang>>:-O0 -g>
$<$<AND:$<CONFIG:Release>,$<CXX_COMPILER_ID:GNU,Clang>>:-O3>
if(${CMAKE_SYSTEM_NAME} STREQUAL "Nintendo3DS")
target_compile_options(palladium PRIVATE
$<$<COMPILE_LANGUAGE:C>:-Wall>
$<$<COMPILE_LANGUAGE:C>:-Wno-psabi>
$<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions>
)
endif()
$<$<AND:$<CONFIG:Debug>,$<CXX_COMPILER_ID:MSVC>>:/Od /Zi>
$<$<AND:$<CONFIG:Release>,$<CXX_COMPILER_ID:MSVC>>:/O2>
target_compile_options(palladium PRIVATE
$<$<CONFIG:Debug>:-O0 -g>
$<$<CONFIG:Release>:-O3>
)
install(
TARGETS palladium
if(PD_BUILD_DESKTOP AND NOT PD_BUILD_3DS)
add_subdirectory(backends/desktop)
endif()
if(PD_BUILD_3DS AND NOT PD_BUILD_DESKTOP)
add_subdirectory(backends/3ds)
endif()
install(TARGETS palladium
EXPORT palladiumTargets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
install(
DIRECTORY include/
install(DIRECTORY include/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
install(EXPORT palladiumTargets
@@ -93,37 +125,39 @@ include(CMakePackageConfigHelpers)
configure_package_config_file(
cmake/palladiumConfig.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/palladiumConfig.cmake
INSTALL_DESTINATION
${CMAKE_INSTALL_LIBDIR}/cmake/palladium
INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/palladium
)
write_basic_package_version_file(
${CMAKE_CURRENT_BINARY_DIR}/palladiumConfigVersion.cmake
VERSION
${PROJECT_VERSION}
COMPATIBILITY
SameMajorVersion
VERSION ${PROJECT_VERSION}
COMPATIBILITY SameMajorVersion
)
install(
FILES
install(FILES
${CMAKE_CURRENT_BINARY_DIR}/palladiumConfig.cmake
${CMAKE_CURRENT_BINARY_DIR}/palladiumConfigVersion.cmake
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/palladium
)
find_program(CLANG_FORMAT clang-format)
file(GLOB_RECURSE PD_FMTFILES
CONFIGURE_DEPENDS
file(GLOB_RECURSE PD_FMTFILES CONFIGURE_DEPENDS
${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp
${CMAKE_CURRENT_SOURCE_DIR}/include/*.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/pd/core/*.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/pd/drivers/*.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/pd/image/*.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/pd/lithium/*.hpp
${CMAKE_CURRENT_SOURCE_DIR}/include/pd/ui7/*.hpp
${CMAKE_CURRENT_SOURCE_DIR}/backends/desktop/include/*.hpp
${CMAKE_CURRENT_SOURCE_DIR}/backends/3ds/include/*.hpp
${CMAKE_CURRENT_SOURCE_DIR}/backends/desktop/include/*.cpp
${CMAKE_CURRENT_SOURCE_DIR}/backends/3ds/include/*.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test/source*.cpp
)
add_custom_target(pd-clang-format
COMMAND ${CLANG_FORMAT} --style=file -i ${PD_FMTFILES}
COMMENT "Formatting Project Sources"
)
add_subdirectory(backends)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/tests)

View File

@@ -1,6 +1,6 @@
MIT License
Copyright (c) 2024 - 2026 René Amthor (tobid7)
Copyright (c) 2024 - 2026 René Amthor tobid7
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

37
backends/3ds/CMakeLists.txt Executable file
View File

@@ -0,0 +1,37 @@
cmake_minimum_required(VERSION 3.22)
project(pd-3ds LANGUAGES CXX VERSION 0.6.0)
set(SRC
source/bknd-gfx.cpp
source/bknd-hid.cpp
source/pd-3ds.cpp
)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/libpicasso)
add_library(pd-3ds STATIC ${SRC})
target_include_directories(pd-3ds PUBLIC
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
target_link_libraries(pd-3ds PUBLIC m palladium ctru citro3d pica::pica)
add_library(palladium::pd-3ds ALIAS pd-3ds)
install(TARGETS pd-3ds
EXPORT palladiumTargets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
install(DIRECTORY include/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
install(TARGETS pica
EXPORT palladiumTargets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)

32
backends/3ds/include/pd-3ds.hpp Executable file
View File

@@ -0,0 +1,32 @@
#pragma once
/*
MIT License
Copyright (c) 2024 - 2026 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <pd-3ds/bknd-gfx.hpp>
#include <pd-3ds/bknd-hid.hpp>
namespace PD {
PD::Li::Font::Ref LoadSystemFont(Context& ctx);
void Init(void* data = nullptr);
} // namespace PD

View File

@@ -0,0 +1,64 @@
#pragma once
/*
MIT License
Copyright (c) 2024 - 2026 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <3ds.h>
#include <citro3d.h>
#include <pd-3ds/linearAllocator.hpp>
#include <pd/lithium/lithium.hpp>
namespace PD {
class GfxC3D : public GfxDriver {
public:
GfxC3D(PDDriverData data = nullptr) : GfxDriver("Citro3D") {}
~GfxC3D() = default;
PD_SHARED(GfxC3D);
void Init() override;
void Deinit() override;
void NewFrame() override;
void BindTex(PD::Li::TexAddress addr) override;
void RenderDrawData(const PD::Li::CmdPool& Commands) override;
PD::Li::Texture::Ref LoadTex(
const std::vector<PD::u8>& pixels, int w, int h,
PD::Li::Texture::Type type = PD::Li::Texture::Type::RGBA32,
PD::Li::Texture::Filter filter =
PD::Li::Texture::Filter::LINEAR) override;
void DestroyTex(PD::Li::Texture::Ref tex) override;
std::vector<Li::Vertex, LinearAllocator<Li::Vertex>> VertexBuffer;
std::vector<u16, LinearAllocator<u16>> IndexBuffer;
int pLocProjection = 0;
DVLB_s* ShaderCode;
shaderProgram_s Shader;
C3D_AttrInfo ShaderInfo;
std::vector<u8> pRawShader;
u32 pCountVertices = 0;
u32 pCountIndices = 0;
u32 pCountDrawCalls = 0;
u32 pDrawCommands = 0;
};
} // namespace PD

View File

@@ -23,32 +23,16 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <pd/common.hpp>
#include <pd/drivers/drivers.hpp>
namespace PD {
namespace Std {
template <typename T>
class Vector {
class Hid3DS : public HidDriver {
public:
// for c++ std stuff
using value_type = T;
using iterator = value_type*;
using const_iterator = const value_type*;
Hid3DS(PDDriverData data = nullptr);
~Hid3DS() = default;
Vector() {
Size = 0;
Cap = 0;
Data = nullptr;
}
~Vector() {
if (Data) {
delete Data;
}
}
PD_SHARED(Hid3DS);
T* Data = nullptr;
size_t Size = 0;
size_t Cap = 0;
void Update() override;
};
} // namespace Std
} // namespace PD

View File

@@ -1,12 +1,31 @@
#pragma once
#ifdef __3DS__
#include <3ds.h>
#endif
/*
MIT License
Copyright (c) 2024 - 2026 René Amthor (tobid7)
#include <limits>
#include <memory>
#include <stdexcept>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <3ds.h>
#include <pd/core/common.hpp>
// Custom C++ Allocator class to interface with libctru linear heap memory
// based on this guide:
@@ -23,19 +42,11 @@ class LinearAllocator {
T* allocate(std::size_t n) {
if (n > max_size()) {
throw std::runtime_error("[PD] LinearAllocator: Bad alloc!");
PD::Throw("[PD] LinearAllocator: Bad alloc!");
}
#ifdef __3DS__
return static_cast<T*>(linearAlloc(n * sizeof(T)));
#else
return static_cast<T*>(malloc(n * sizeof(T)));
#endif
}
#ifdef __3DS__
void deallocate(T* p, std::size_t) noexcept { linearFree(p); }
#else
void deallocate(T* p, std::size_t) noexcept { free(p); }
#endif
template <class U, class... Args>
void construct(U* p, Args&&... args) {
@@ -54,14 +65,8 @@ class LinearAllocator {
return false;
}
#ifdef __3DS__
// Use linearSpace free as max_size to not allocate out of bounds
// or to b eable to see a crash report screen.
size_t max_size() const noexcept { return linearSpaceFree(); }
#else
size_t max_size() const noexcept {
return std::numeric_limits<size_t>::max();
}
#endif
};
} // namespace PD

281
backends/3ds/source/bknd-gfx.cpp Executable file
View File

@@ -0,0 +1,281 @@
/*
MIT License
Copyright (c) 2024 - 2026 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <pd-3ds/bknd-gfx.hpp>
#include <pica.hpp>
const char* LIShaderCTR = R"(
; LI7 Shader
; Constants
.constf myconst(0.0, 1.0, 0.00392156862745, 0.0)
.alias ones myconst.yyyy ; Vector full of ones
; Uniforms
.fvec projection[4]
; Outputs
.out out_position position
.out out_color color
.out out_uv texcoord0
; Inputs
.alias in_xy v0
.alias in_uvc v1
.alias in_col v2
.entry vmain
.proc vmain
mov r0.xy, in_xy.xy
mov r0.w, ones
dp4 out_position.x, projection[0], r0
dp4 out_position.y, projection[1], r0
dp4 out_position.z, projection[2], r0
dp4 out_position.w, projection[3], r0
mov out_uv, in_uvc.xy
mul r1, myconst.zzzz, in_col
mov out_color, r1
end
.end
)";
namespace PD {
GPU_TEXCOLOR GetTexFmt(Li::Texture::Type type) {
if (type == Li::Texture::RGBA32)
return GPU_RGBA8;
else if (type == Li::Texture::RGB24)
return GPU_RGB8;
else if (type == Li::Texture::A8)
return GPU_A8;
return GPU_RGBA8; // Default
}
int GetBPP(Li::Texture::Type type) {
if (type == Li::Texture::RGBA32)
return 4;
else if (type == Li::Texture::RGB24)
return 3;
else if (type == Li::Texture::A8)
return 1;
return 0; // Error
}
void FragCfg(GPU_TEXCOLOR clr) {
C3D_DepthTest(false, GPU_GREATER, GPU_WRITE_ALL);
C3D_TexEnv* env = C3D_GetTexEnv(0);
C3D_TexEnvInit(env);
switch (clr) {
case GPU_A4:
case GPU_A8:
case GPU_L4:
case GPU_L8:
C3D_TexEnvSrc(env, C3D_Alpha, GPU_TEXTURE0);
C3D_TexEnvFunc(env, C3D_RGB, GPU_REPLACE);
C3D_TexEnvFunc(env, C3D_Alpha, GPU_MODULATE);
break;
case GPU_RGB565:
C3D_TexEnvSrc(env, C3D_Alpha, GPU_TEXTURE0);
C3D_TexEnvFunc(env, C3D_RGB, GPU_MODULATE);
C3D_TexEnvFunc(env, C3D_Alpha, GPU_REPLACE);
break;
default:
C3D_TexEnvSrc(env, C3D_Both, GPU_TEXTURE0);
C3D_TexEnvFunc(env, C3D_Both, GPU_MODULATE);
break;
}
}
void GfxC3D::Init() {
VertexBuffer.resize(4 * 8192);
IndexBuffer.resize(6 * 8192);
Flags |= LiBackendFlags_FlipUV_Y;
pRawShader = Pica::AssembleCode(LIShaderCTR);
ShaderCode = DVLB_ParseFile((uint32_t*)&pRawShader[0], pRawShader.size());
shaderProgramInit(&Shader);
shaderProgramSetVsh(&Shader, &ShaderCode->DVLE[0]);
pLocProjection =
shaderInstanceGetUniformLocation(Shader.vertexShader, "projection");
AttrInfo_Init(&ShaderInfo);
AttrInfo_AddLoader(&ShaderInfo, 0, GPU_FLOAT, 2);
AttrInfo_AddLoader(&ShaderInfo, 1, GPU_FLOAT, 2);
AttrInfo_AddLoader(&ShaderInfo, 2, GPU_UNSIGNED_BYTE, 4);
}
void GfxC3D::Deinit() {
shaderProgramFree(&Shader);
DVLB_Free(ShaderCode);
}
void GfxC3D::NewFrame() {
C3D_BindProgram(&Shader);
C3D_SetAttrInfo(&ShaderInfo);
CurrentIndex = 0;
CurrentVertex = 0;
FrameCounter++;
/** Probably completly incorrect but just do it like that */
VertexCounter = pCountVertices;
IndexCounter = pCountIndices;
DrawCalls = pCountDrawCalls;
DrawCommands = pDrawCommands;
pCountDrawCalls = 0;
pCountIndices = 0;
pCountVertices = 0;
pDrawCommands = 0;
}
void GfxC3D::BindTex(PD::Li::TexAddress addr) {
C3D_TexBind(0, reinterpret_cast<C3D_Tex*>(addr));
}
void GfxC3D::RenderDrawData(const PD::Li::CmdPool& Commands) {
pDrawCommands += Commands.Size();
shaderProgramUse(&Shader);
C3D_SetAttrInfo(&ShaderInfo);
C3D_Mtx proj;
Mtx_OrthoTilt(&proj, 0.f, ViewPort.x, ViewPort.y, 0.f, 1.f, -1.f, false);
C3D_FVUnifMtx4x4(GPU_VERTEX_SHADER, pLocProjection, &proj);
// Mat4 proj = Mat4::Ortho(0.f, ViewPort.x, ViewPort.y, 0.f, 1.f, -1.f);
// C3D_FVUnifMtx4x4(GPU_VERTEX_SHADER, pLocProjection, (C3D_Mtx*)&proj);
size_t index = 0;
while (index < Commands.Size()) {
PD::Li::TexAddress Tex = Commands.GetCmd(index)->Tex;
if (!Tex) {
index++;
continue;
}
bool ScissorEnabled = Commands.GetCmd(index)->ScissorOn;
ivec4 ScissorRect = Commands.GetCmd(index)->ScissorRect;
size_t StartIndex = CurrentIndex;
while (index < Commands.Size() && Commands.GetCmd(index)->Tex == Tex &&
Commands.GetCmd(index)->ScissorOn == ScissorEnabled &&
Commands.GetCmd(index)->ScissorRect == ScissorRect) {
auto c = Commands.GetCmd(index);
pCountIndices += c->IndexBuffer.size();
pCountVertices += c->VertexBuffer.size();
for (size_t i = 0; i < c->IndexBuffer.size(); i++) {
IndexBuffer[CurrentIndex++] = CurrentVertex + c->IndexBuffer.at(i);
}
for (size_t i = 0; i < c->VertexBuffer.size(); i++) {
VertexBuffer[CurrentVertex++] = c->VertexBuffer.at(i);
}
index++;
}
if (ScissorEnabled) {
// Figure out this setup was pain
C3D_SetScissor(GPU_SCISSOR_NORMAL,
std::clamp(ViewPort.y - (ScissorRect.y + ScissorRect.w), 0,
ViewPort.y),
std::clamp(ViewPort.x - (ScissorRect.x + ScissorRect.z), 0,
ViewPort.x),
ViewPort.y - ScissorRect.y, ViewPort.x - ScissorRect.x);
} else {
C3D_SetScissor(GPU_SCISSOR_DISABLE, 0, 0, 0, 0);
}
FragCfg(((C3D_Tex*)Tex)->fmt);
BindTex(Tex);
auto bufInfo = C3D_GetBufInfo();
BufInfo_Init(bufInfo);
BufInfo_Add(bufInfo, VertexBuffer.data(), sizeof(Li::Vertex), 3, 0x210);
C3D_DrawElements(GPU_TRIANGLES, CurrentIndex - StartIndex,
C3D_UNSIGNED_SHORT, IndexBuffer.data() + StartIndex);
pCountDrawCalls++;
}
C3D_DepthTest(true, GPU_GREATER, GPU_WRITE_ALL);
}
PD::Li::Texture::Ref GfxC3D::LoadTex(const std::vector<PD::u8>& pixels, int w,
int h, PD::Li::Texture::Type type,
PD::Li::Texture::Filter filter) {
if (w > 1024 || h > 1024) {
return nullptr;
}
// Don't check here as check done before
PD::Li::Texture::Ref res = PD::Li::Texture::New();
int bpp = GetBPP(type);
ivec2 tex_size(w, h);
// Pow2
if (!PD::BitUtil::IsSingleBit(w)) {
tex_size.x = PD::BitUtil::GetPow2((unsigned int)w);
}
if (!PD::BitUtil::IsSingleBit(h)) {
tex_size.y = PD::BitUtil::GetPow2((unsigned int)h);
}
res->Size.x = w;
res->Size.y = h;
res->UV = fvec4(0.f, 1.f, ((float)w / (float)tex_size.x),
1.0 - ((float)h / (float)tex_size.y));
// Texture Setup
auto fltr = (filter == Li::Texture::NEAREST ? GPU_NEAREST : GPU_LINEAR);
auto tex_fmt = GetTexFmt(type);
auto tex = new C3D_Tex;
C3D_TexInit(tex, (u16)tex_size.x, (u16)tex_size.y, tex_fmt);
C3D_TexSetFilter(tex, fltr, fltr);
// Using std::fill_n instead cause i hate this error lines
// under the memset func in my editor
std::fill_n((PD::u8*)tex->data, tex->size, 0);
// memset(tex->data, 0, tex->size);s
/// Probably Remove this if statement in future
/// This are the things confirmed as working
if (bpp == 3 || bpp == 4 || bpp == 1) {
for (int x = 0; x < w; x++) {
for (int y = 0; y < h; y++) {
int dst_pos = ((((y >> 3) * ((int)tex_size.x >> 3) + (x >> 3)) << 6) +
((x & 1) | ((y & 1) << 1) | ((x & 2) << 1) |
((y & 2) << 2) | ((x & 4) << 2) | ((y & 4) << 3))) *
bpp;
int src_pos = (y * w + x) * bpp;
/// Best idea i had
for (int i = 0; i < bpp; i++) {
((u8*)tex->data)[dst_pos + bpp - 1 - i] = pixels[src_pos + i];
}
}
}
C3D_TexFlush(tex);
}
tex->border = 0x00000000;
C3D_TexSetWrap(tex, GPU_REPEAT, GPU_REPEAT);
res->Address = (Li::TexAddress)tex;
std::cout << std::format("Tex {:#08x} Addr {:#08X}",
(Li::TexAddress)res.get(), res->Address)
<< std::endl;
return res;
}
void GfxC3D::DestroyTex(PD::Li::Texture::Ref tex) {
C3D_Tex* t = reinterpret_cast<C3D_Tex*>(tex->Address);
C3D_TexDelete(t);
delete t;
tex->Address = 0;
}
} // namespace PD

View File

@@ -0,0 +1,86 @@
/*
MIT License
Copyright (c) 2024 - 2026 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <3ds.h>
#include <pd-3ds/bknd-hid.hpp>
namespace PD {
Hid3DS::Hid3DS(PDDriverData data) : HidDriver("Hid3DS") {
this->Flags |= Flags_HasTouch;
this->Flags |= FLags_HasGamepad;
pBinds[KEY_A] = A;
pBinds[KEY_B] = B;
pBinds[KEY_X] = X;
pBinds[KEY_Y] = Y;
pBinds[KEY_START] = Start;
pBinds[KEY_SELECT] = Select;
pBinds[KEY_L] = L;
pBinds[KEY_R] = R;
pBinds[KEY_DUP] = DUp;
pBinds[KEY_DDOWN] = DDown;
pBinds[KEY_DLEFT] = DLeft;
pBinds[KEY_DRIGHT] = DRight;
pBinds[KEY_CPAD_UP] = CPUp;
pBinds[KEY_CPAD_DOWN] = CPDown;
pBinds[KEY_CPAD_LEFT] = CPLeft;
pBinds[KEY_CPAD_RIGHT] = CPRight;
pBinds[KEY_CSTICK_UP] = CSUp;
pBinds[KEY_CSTICK_DOWN] = CSDown;
pBinds[KEY_CSTICK_LEFT] = CSLeft;
pBinds[KEY_CSTICK_RIGHT] = CSRight;
pBinds[KEY_ZL] = ZL;
pBinds[KEY_ZR] = ZR;
pBinds[KEY_TOUCH] = Touch;
}
void Hid3DS::Update() {
hidScanInput();
for (int i = 0; i < 2; i++) {
KeyEvents[i][Event_Down] = 0;
KeyEvents[i][Event_Held] = 0;
KeyEvents[i][Event_Up] = 0;
}
u32 kd = hidKeysDown();
u32 kh = hidKeysHeld();
u32 ku = hidKeysUp();
for (auto& b : pBinds) {
if (b.first & kd) {
KeyEvents[0][Event_Down] |= b.second;
}
if (b.first & kh) {
KeyEvents[0][Event_Held] |= b.second;
}
if (b.first & ku) {
KeyEvents[0][Event_Up] |= b.second;
}
}
if (pLocked) {
SwapTab();
}
touchPosition t;
hidTouchRead(&t);
pMouse[1] = pMouse[0]; // Cycle pMouse pos
pMouse[0] = fvec2(t.px, t.py);
}
} // namespace PD

131
backends/3ds/source/pd-3ds.cpp Executable file
View File

@@ -0,0 +1,131 @@
/*
MIT License
Copyright (c) 2024 - 2026 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <3ds.h>
#include <palladium>
#include <pd-3ds.hpp>
namespace PD {
PD::Li::Font::Ref LoadSystemFont(Context& ctx) {
TT::Scope st(*ctx.Os(), "LI_SystemFont"); // Trace loading time
Li::Font::Ref ret = Li::Font::New(ctx);
fontEnsureMapped(); // Call this to be sure the font is mapped
// Get some const references for system font loading
const auto fnt = fontGetSystemFont();
const auto fnt_info = fontGetInfo(fnt);
const auto glyph_info = fontGetGlyphInfo(fnt);
// Resize the Texture list by the num of sysfont textures
ret->Textures.resize(glyph_info->nSheets + 1);
/// Modify the Pixel Height by 1.1f to fit the
/// Size og ttf font Rendering
ret->PixelHeight = glyph_info->cellHeight;
// Load the Textures and make sure they don't auto unload
for (size_t i = 0; i < glyph_info->nSheets; i++) {
auto stex = Li::Texture::New();
auto tx = new C3D_Tex;
tx->data = fontGetGlyphSheetTex(fnt, i);
tx->fmt = (GPU_TEXCOLOR)glyph_info->sheetFmt;
tx->size = glyph_info->sheetSize;
tx->width = glyph_info->sheetWidth;
tx->height = glyph_info->sheetHeight;
tx->param = GPU_TEXTURE_MAG_FILTER(GPU_LINEAR) |
GPU_TEXTURE_MIN_FILTER(GPU_LINEAR) |
GPU_TEXTURE_WRAP_S(GPU_REPEAT) | GPU_TEXTURE_WRAP_T(GPU_REPEAT);
tx->border = 0xffffffff;
tx->lodParam = 0;
stex->Address = (Li::TexAddress)tx;
stex->Size = fvec2(tx->width, tx->height);
stex->UV = fvec4(0, 1, 1, 0);
ret->Textures[i] = stex;
}
std::vector<unsigned int> charSet;
// Write the Charset into a vector
for (auto cmap = fnt_info->cmap; cmap; cmap = cmap->next) {
if (cmap->mappingMethod == CMAP_TYPE_DIRECT) {
if (cmap->codeEnd >= cmap->codeBegin) {
charSet.reserve(charSet.size() + cmap->codeEnd - cmap->codeBegin + 1);
for (auto i = cmap->codeBegin; i <= cmap->codeEnd; ++i) {
if (cmap->indexOffset + (i - cmap->codeBegin) == 0xFFFF) break;
charSet.emplace_back(i);
}
}
} else if (cmap->mappingMethod == CMAP_TYPE_TABLE) {
if (cmap->codeEnd >= cmap->codeBegin) {
charSet.reserve(charSet.size() + cmap->codeEnd - cmap->codeBegin + 1);
for (auto i = cmap->codeBegin; i <= cmap->codeEnd; ++i) {
if (cmap->indexTable[i - cmap->codeBegin] == 0xFFFF) continue;
charSet.emplace_back(i);
}
}
} else if (cmap->mappingMethod == CMAP_TYPE_SCAN) {
charSet.reserve(charSet.size() + cmap->nScanEntries);
for (unsigned i = 0; i < cmap->nScanEntries; ++i) {
if (cmap->scanEntries[i].code >= cmap->codeBegin &&
cmap->scanEntries[i].code <= cmap->codeEnd) {
if (cmap->scanEntries[i].glyphIndex != 0xFFFF) {
charSet.emplace_back(cmap->scanEntries[i].code);
}
}
}
} else {
continue;
}
}
// Sort the charset and make sure all values are unique
std::sort(charSet.begin(), charSet.end());
charSet.erase(std::unique(charSet.begin(), charSet.end()));
// Setup the Codepoint map by the charset
for (auto cp : charSet) {
int gidx = fontGlyphIndexFromCodePoint(fnt, cp);
if (gidx >= 0xFFFF) continue;
Li::Font::Codepoint codepoint;
fontGlyphPos_s dat;
fontCalcGlyphPos(&dat, fnt, gidx, GLYPH_POS_CALC_VTXCOORD, 1.f, 1.f);
codepoint.pCodepoint = cp;
codepoint.SimpleUV = fvec4(dat.texcoord.left, dat.texcoord.top,
dat.texcoord.right, dat.texcoord.bottom);
if (dat.sheetIndex < (int)ret->Textures.size()) {
codepoint.Tex = ret->Textures[dat.sheetIndex];
} else {
codepoint.pInvalid = true;
}
codepoint.Size = fvec2(dat.vtxcoord.right, dat.vtxcoord.bottom);
codepoint.Offset = 0;
ret->CodeMap[cp] = codepoint;
}
return ret;
}
void Init(void* data) {
// Dekstop Init Stage
// First use default OS Driver
// PD::OS::Init();
// PD::Gfx::Init(PD::GfxC3D::New());
// PD::Hid::Init(PD::Hid3DS::New());
}
} // namespace PD

View File

@@ -1,63 +0,0 @@
cmake_minimum_required(VERSION 3.22)
project(pd-system)
option(PD_ENABLE_OPENGL2 "Enable OpenGL 2.1 (On Supported Hardware)" ON)
option(PD_ENABLE_OPENGL3 "Enable OpenGL 3.3 (On Supported Hardware)" ON)
option(PD_ENABLE_DIRECTX9 "Enable DirectX9 Support" ON)
option(PD_ENABLE_CITRO3D "Enable Citro3D Support (3DS)" OFF)
option(PD_ENABLE_VULKAN "Not implemented yet" OFF)
if(NOT WIN32) # cause we are not on windows...
set(PD_ENABLE_DIRECTX9 OFF)
endif()
add_library(pd-system STATIC
${CMAKE_CURRENT_SOURCE_DIR}/source/gl-helper.cpp
${CMAKE_CURRENT_SOURCE_DIR}/source/gfx_opengl2.cpp
${CMAKE_CURRENT_SOURCE_DIR}/source/gfx_opengl3.cpp
${CMAKE_CURRENT_SOURCE_DIR}/source/gfx_directx9.cpp
${CMAKE_CURRENT_SOURCE_DIR}/source/gfx_citro3d.cpp
)
target_include_directories(pd-system
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
target_compile_options(palladium
PUBLIC
$<$<CXX_COMPILER_ID:GNU,Clang>:
-fmacro-prefix-map=${CMAKE_CURRENT_SOURCE_DIR}/source=source
-fmacro-prefix-map=${CMAKE_CURRENT_SOURCE_DIR}/include=include
>
)
target_compile_definitions(pd-system
PUBLIC
$<$<BOOL:${PD_ENABLE_OPENGL2}>:PD_ENABLE_OPENGL2>
$<$<BOOL:${PD_ENABLE_OPENGL3}>:PD_ENABLE_OPENGL3>
$<$<BOOL:${PD_ENABLE_VULKAN}>:PD_ENABLE_VULKAN>
$<$<BOOL:${PD_ENABLE_DIRECTX9}>:PD_ENABLE_DIRECTX9>
)
# Palladium
target_link_libraries(pd-system PUBLIC palladium::palladium)
# glad (if we have any OpenGL version included)
if(PD_ENABLE_OPENGL2 OR PD_ENABLE_OPENGL3)
target_link_libraries(pd-system
PUBLIC
glad
)
endif()
# DirectX9
if(PD_ENABLE_DIRECTX9)
target_link_libraries(pd-system
PUBLIC
d3d9
d3dcompiler
)
endif()

62
backends/desktop/CMakeLists.txt Executable file
View File

@@ -0,0 +1,62 @@
cmake_minimum_required(VERSION 3.22)
project(pd-desktop LANGUAGES CXX VERSION 0.5.0)
set(PD_GL_VERSION 21 CACHE STRING "OpenGL Version (2.1 -> 21, 3.3 -> 33)")
if(APPLE)
set(PD_GL_VERSION 33 CACHE STRING "33" FORCE)
endif()
message("Using OpenGL: " ${PD_GL_VERSION})
add_subdirectory(glad)
add_subdirectory(glfw)
set(SRC
source/bknd-gfx.cpp
source/bknd-hid.cpp
source/pd-desktop.cpp
)
add_library(pd-desktop STATIC ${SRC})
target_include_directories(pd-desktop
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
target_link_libraries(pd-desktop PUBLIC palladium glad glfw)
target_compile_definitions(pd-desktop
PRIVATE PD_OPENGL=${PD_GL_VERSION}
)
add_library(palladium::pd-desktop ALIAS pd-desktop)
install(TARGETS pd-desktop
EXPORT palladiumTargets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
install(DIRECTORY include/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
install(TARGETS glad
EXPORT palladiumTargets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
if(PD_BUNDLE_GLFW)
install(TARGETS glfw
EXPORT palladiumTargets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
endif()

6
vendor/glad/CMakeLists.txt → backends/desktop/glad/CMakeLists.txt vendored Normal file → Executable file
View File

@@ -1,10 +1,6 @@
cmake_minimum_required(VERSION 3.22)
project(glad LANGUAGES C)
add_library(glad STATIC
${CMAKE_CURRENT_SOURCE_DIR}/src/glad.c
)
add_library(glad source/glad.c)
target_include_directories(glad
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

1
backends/desktop/glfw Submodule

Submodule backends/desktop/glfw added at e7ea71be03

View File

@@ -0,0 +1,31 @@
#pragma once
/*
MIT License
Copyright (c) 2024 - 2026 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <pd-desktop/bknd-gfx.hpp>
#include <pd-desktop/bknd-hid.hpp>
namespace PD {
void Init(void* data = nullptr);
}

View File

@@ -0,0 +1,89 @@
#pragma once
/*
MIT License
Copyright (c) 2024 - 2026 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <glad/glad.h>
/** SEPARATOR */
#include <GLFW/glfw3.h>
/** SEPARATOR */
#include <pd/lithium/lithium.hpp>
namespace PD {
class GfxGL2 : public GfxDriver {
public:
GfxGL2(PDDriverData data = nullptr) : GfxDriver("OpenGL2") {}
~GfxGL2() = default;
PD_SHARED(GfxGL2);
void Init() override;
void Deinit() override;
void NewFrame() override;
void BindTex(PD::Li::TexAddress addr) override;
void RenderDrawData(const Li::CmdPool& Commands) override;
PD::Li::Texture::Ref LoadTex(
const std::vector<PD::u8>& pixels, int w, int h,
PD::Li::Texture::Type type = PD::Li::Texture::Type::RGBA32,
PD::Li::Texture::Filter filter =
PD::Li::Texture::Filter::LINEAR) override;
std::vector<Li::Vertex> VertexBuffer;
std::vector<PD::u16> IndexBuffer;
GLuint Shader;
GLuint pLocProjection;
GLuint pLocTex;
GLuint pLocAlfa;
GLuint VBO, IBO, VAO; // vao is only used in > 3.3
};
class GfxGL3 : public GfxDriver2 {
public:
GfxGL3() : GfxDriver2("OpenGL3") {}
~GfxGL3() = default;
PD_SHARED(GfxGL3);
void Init() override;
void Deinit() override;
// void NewFrame() override;
void TexBind(PD::Li::TexAddress addr) override;
void Draw(const Li::CmdPool& pool) override;
PD::Li::TexAddress TexLoad(
const std::vector<PD::u8>& pixels, int w, int h,
PD::Li::Texture::Type type = PD::Li::Texture::Type::RGBA32,
PD::Li::Texture::Filter filter =
PD::Li::Texture::Filter::LINEAR) override;
void TexDelete(PD::Li::TexAddress tex) override;
std::vector<Li::Vertex> VertexBuffer;
std::vector<PD::u16> IndexBuffer;
GLuint Shader;
GLuint pLocProjection;
GLuint pLocTex;
GLuint pLocAlfa;
GLuint VBO, IBO, VAO; // vao is only used in > 3.3
};
} // namespace PD

View File

@@ -0,0 +1,72 @@
#pragma once
/*
MIT License
Copyright (c) 2024 - 2026 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <GLFW/glfw3.h>
#include <pd/drivers/drivers.hpp>
namespace PD {
class HidGLFW : public HidDriver {
public:
HidGLFW(PDDriverData data);
~HidGLFW() = default;
PD_SHARED(HidGLFW);
void Update() override;
void GetInputStr(std::string& str) override;
void HandleTextOps();
bool pTimedHeld(KbKey k) {
if (pTimings.count(k)) {
if (IsEvent(Event_Up, k)) {
pTimings.erase(k);
return false;
}
return 0; //(PD::OS::GetTime() - pTimings[k]) > 50;
}
if (!IsEvent(Event_Held, k)) {
if (pTimings.count(k)) {
pTimings.erase(k);
return false;
}
}
if (IsEvent(Event_Held, k)) {
pTimings[k] = 0; // PD::OS::GetTime();
return true;
}
return false;
}
/** Data section */
GLFWwindow* Window;
int PrevState;
std::unordered_map<int, int> PrevStates;
static GLFWcharfun pOldTextCB;
static std::string* pText;
bool pInTextMode = false;
PD::u64 pLastUpdate = 0;
std::unordered_map<KbKey, u64> pTimings;
};
} // namespace PD

View File

@@ -0,0 +1,502 @@
/*
MIT License
Copyright (c) 2024 - 2026 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <pd-desktop/bknd-gfx.hpp>
namespace PD {
#if PD_OPENGL >= 21 && PD_OPENGL < 33
const char* vertex_shader = R"(
#version 120
attribute vec2 pos;
attribute vec2 uv;
attribute vec4 color;
varying vec2 oUV;
varying vec4 oColor;
// Probably forgot about this matrix and
// searched hours for why the rendering isn't working :/
uniform mat4 projection;
void main() {
gl_Position = projection*vec4(pos, 0.0, 1.0);
oUV = uv;
oColor = color;
}
)";
const char* frag_shader = R"(
#version 120
varying vec2 oUV;
varying vec4 oColor;
uniform sampler2D tex;
uniform bool alfa;
void main() {
vec4 tc = texture2D(tex, oUV);
if (alfa) {
gl_FragColor = vec4(oColor.rgb, tc.a * oColor.a);
} else {
gl_FragColor = tc * oColor;
}
}
)";
#elif PD_OPENGL >= 33
const char* vertex_shader = R"(
#version 330 core
layout(location = 0) in vec2 pos;
layout(location = 1) in vec2 uv;
layout(location = 2) in vec4 color;
out vec2 oUV;
out vec4 oColor;
// Probably forgot about this matrix and
// searched hours for why the rendering isn't working :/
uniform mat4 projection;
void main() {
gl_Position = projection*vec4(pos, 0.0, 1.0);
oUV = uv;
oColor = color;
}
)";
const char* frag_shader = R"(
#version 330 core
in vec2 oUV;
in vec4 oColor;
uniform sampler2D tex;
uniform bool alfa;
out vec4 FragColor;
void main() {
vec4 tc = texture(tex, oUV);
if (alfa) {
FragColor = vec4(oColor.rgb, tc.a * oColor.a);
} else {
FragColor = tc * oColor;
}
}
)";
#else
// Need to show error
#endif
GLuint compileShader(const std::string& source, GLenum type) {
GLuint shader = glCreateShader(type);
const char* src = source.c_str();
glShaderSource(shader, 1, &src, nullptr);
glCompileShader(shader);
GLint success;
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (!success) {
char infoLog[512];
glGetShaderInfoLog(shader, 512, nullptr, infoLog);
std::cerr << "Shader Compilation Error: " << infoLog << std::endl;
}
return shader;
}
GLuint createShaderProgram(const std::string& vertexShaderSource,
const std::string& fragmentShaderSource) {
GLuint vertexShader = compileShader(vertexShaderSource, GL_VERTEX_SHADER);
GLuint fragmentShader =
compileShader(fragmentShaderSource, GL_FRAGMENT_SHADER);
GLuint shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
GLint success;
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
if (!success) {
char infoLog[512];
glGetProgramInfoLog(shaderProgram, 512, nullptr, infoLog);
std::cerr << "Shader Program Linking Error: " << infoLog << std::endl;
}
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
return shaderProgram;
}
void SetupShaderAttribs(GLuint Shader) {
GLint _pos = glGetAttribLocation(Shader, "pos");
GLint _uv = glGetAttribLocation(Shader, "uv");
GLint _color = glGetAttribLocation(Shader, "color");
glVertexAttribPointer(_pos, 2, GL_FLOAT, GL_FALSE, sizeof(PD::Li::Vertex),
(void*)offsetof(PD::Li::Vertex, Pos));
glEnableVertexAttribArray(_pos);
glVertexAttribPointer(_uv, 2, GL_FLOAT, GL_FALSE, sizeof(PD::Li::Vertex),
(void*)offsetof(PD::Li::Vertex, UV));
glEnableVertexAttribArray(_uv);
glVertexAttribPointer(_color, 4, GL_UNSIGNED_BYTE, GL_TRUE,
sizeof(PD::Li::Vertex),
(void*)offsetof(PD::Li::Vertex, Color));
glEnableVertexAttribArray(_color);
}
/** Actual Backend */
void GfxGL2::Init() {
VertexBuffer.resize(4 * 8192);
IndexBuffer.resize(6 * 8192);
Shader = createShaderProgram(vertex_shader, frag_shader);
glUseProgram(Shader);
#if PD_OPENGL >= 33
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
#endif
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
#if PD_OPENGL < 33
SetupShaderAttribs(Shader); // GL 2.1
#else
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(PD::Li::Vertex),
(void*)offsetof(PD::Li::Vertex, Pos));
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(PD::Li::Vertex),
(void*)offsetof(PD::Li::Vertex, UV));
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(PD::Li::Vertex),
(void*)offsetof(PD::Li::Vertex, Color));
#endif
glGenBuffers(1, &IBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
pLocTex = glGetUniformLocation(Shader, "tex");
pLocAlfa = glGetUniformLocation(Shader, "alfa");
pLocProjection = glGetUniformLocation(Shader, "projection");
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
#if PD_OPENGL >= 33
glBindVertexArray(0);
#endif
}
void GfxGL2::Deinit() {
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &IBO);
#if PD_OPENGL >= 33
glDeleteBuffers(1, &VAO);
#endif
}
void GfxGL2::NewFrame() {
/*
glViewport(0, 0, ViewPort.x, ViewPort.y);
glClearColor(ClearColor.x, ClearColor.y, ClearColor.z, ClearColor.w);
glClear(GL_COLOR_BUFFER_BIT);
*/
Projection = Mat4::Ortho(0.f, ViewPort.x, ViewPort.y, 0.f, -1.f, 1.f);
glUseProgram(Shader);
glUniformMatrix4fv(pLocProjection, 1, GL_FALSE, Projection.m.data());
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
FrameCounter++;
VertexCounter = CurrentVertex;
IndexCounter = CurrentIndex;
CurrentVertex = 0;
CurrentIndex = 0;
}
void GfxGL2::BindTex(PD::Li::TexAddress addr) {
// Actually not using the Address as Address
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, (GLuint)addr);
glUniform1i(pLocTex, 0);
GLint fmt = 0;
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, &fmt);
glUniform1i(pLocAlfa, fmt == GL_ALPHA);
}
void GfxGL2::RenderDrawData(const Li::CmdPool& Commands) {
size_t index = 0;
while (index < Commands.Size()) {
PD::Li::TexAddress Tex = Commands.GetCmd(index)->Tex;
if (!Tex) {
index++;
continue;
}
size_t StartIndex = CurrentIndex;
bool ScissorOn = Commands.GetCmd(index)->ScissorOn;
ivec4 ScissorRect = Commands.GetCmd(index)->ScissorRect;
while (index < Commands.Size() && Commands.GetCmd(index)->Tex == Tex &&
Commands.GetCmd(index)->ScissorOn == ScissorOn &&
Commands.GetCmd(index)->ScissorRect == ScissorRect) {
auto c = Commands.GetCmd(index);
for (size_t i = 0; i < c->IndexBuffer.size(); i++) {
IndexBuffer[CurrentIndex++] = CurrentVertex + c->IndexBuffer.at(i);
}
for (size_t i = 0; i < c->VertexBuffer.size(); i++) {
VertexBuffer[CurrentVertex++] = c->VertexBuffer.at(i);
}
index++;
}
if (ScissorOn) {
glScissor(ScissorRect.x, ViewPort.y - (ScissorRect.y + ScissorRect.w),
ScissorRect.z, ScissorRect.w);
glEnable(GL_SCISSOR_TEST);
} else {
glDisable(GL_SCISSOR_TEST);
}
BindTex(Tex);
#if PD_OPENGL >= 33
glBindVertexArray(VAO);
#endif
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, CurrentVertex * sizeof(PD::Li::Vertex),
&VertexBuffer[0], GL_DYNAMIC_DRAW);
#if PD_OPENGL < 33
// For some reason we need to set these every frame for every buffer
// Found that out when creating My 3d Engine
SetupShaderAttribs(Shader);
#endif
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, CurrentIndex * sizeof(PD::u16),
&IndexBuffer[0], GL_DYNAMIC_DRAW);
glDrawElements(GL_TRIANGLES, CurrentIndex - StartIndex, GL_UNSIGNED_SHORT,
(void*)(StartIndex * sizeof(PD::u16)));
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
BindTex(0);
#if PD_OPENGL >= 33
glBindVertexArray(0);
#endif
glDisable(GL_SCISSOR_TEST);
}
}
PD::Li::Texture::Ref GfxGL2::LoadTex(const std::vector<PD::u8>& pixels, int w,
int h, PD::Li::Texture::Type type,
PD::Li::Texture::Filter filter) {
GLuint texID;
glGenTextures(1, &texID);
glBindTexture(GL_TEXTURE_2D, texID);
// Set base format (Always using RGBA as base)
GLenum fmt = GL_RGBA;
if (type == PD::Li::Texture::Type::RGB24) {
fmt = GL_RGB;
} else if (type == PD::Li::Texture::Type::A8) {
fmt = GL_ALPHA;
}
glTexImage2D(GL_TEXTURE_2D, 0, fmt, w, h, 0, fmt, GL_UNSIGNED_BYTE,
pixels.data());
if (filter == PD::Li::Texture::Filter::LINEAR) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
} else if (filter == PD::Li::Texture::Filter::NEAREST) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
}
glBindTexture(GL_TEXTURE_2D, 0);
auto res = PD::Li::Texture::New(texID, PD::ivec2(w, h));
std::cout << std::format("Texture loaded: {} {}", texID, PD::ivec2(w, h))
<< std::endl;
return res;
}
void GfxGL3::Init() {
VertexBuffer.resize(4 * 8192);
IndexBuffer.resize(6 * 8192);
Shader = createShaderProgram(vertex_shader, frag_shader);
glUseProgram(Shader);
#if PD_OPENGL >= 33
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
#endif
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
#if PD_OPENGL < 33
SetupShaderAttribs(Shader); // GL 2.1
#else
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(PD::Li::Vertex),
(void*)offsetof(PD::Li::Vertex, Pos));
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(PD::Li::Vertex),
(void*)offsetof(PD::Li::Vertex, UV));
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(PD::Li::Vertex),
(void*)offsetof(PD::Li::Vertex, Color));
#endif
glGenBuffers(1, &IBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
pLocTex = glGetUniformLocation(Shader, "tex");
pLocAlfa = glGetUniformLocation(Shader, "alfa");
pLocProjection = glGetUniformLocation(Shader, "projection");
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
#if PD_OPENGL >= 33
glBindVertexArray(0);
#endif
}
void GfxGL3::Deinit() {
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &IBO);
#if PD_OPENGL >= 33
glDeleteBuffers(1, &VAO);
#endif
}
// void NewFrame() {
//}
void GfxGL3::TexBind(PD::Li::TexAddress addr) {
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, (GLuint)addr);
glUniform1i(pLocTex, 0);
GLint fmt = 0;
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, &fmt);
glUniform1i(pLocAlfa, fmt == GL_ALPHA);
}
void GfxGL3::Draw(const Li::CmdPool& pool) {
size_t index = 0;
while (index < pool.Size()) {
PD::Li::TexAddress Tex = pool.GetCmd(index)->Tex;
if (Tex == 0) {
index++;
continue;
}
size_t StartIndex = CurrentIndex;
bool ScissorOn = pool.GetCmd(index)->ScissorOn;
ivec4 ScissorRect = pool.GetCmd(index)->ScissorRect;
while (index < pool.Size() && pool.GetCmd(index)->Tex == Tex &&
pool.GetCmd(index)->ScissorOn == ScissorOn &&
pool.GetCmd(index)->ScissorRect == ScissorRect) {
auto c = pool.GetCmd(index);
for (size_t i = 0; i < c->IndexBuffer.size(); i++) {
IndexBuffer[CurrentIndex++] = CurrentVertex + c->IndexBuffer.at(i);
}
for (size_t i = 0; i < c->VertexBuffer.size(); i++) {
VertexBuffer[CurrentVertex++] = c->VertexBuffer.at(i);
}
index++;
}
if (ScissorOn) {
glScissor(ScissorRect.x, ViewPort.y - (ScissorRect.y + ScissorRect.w),
ScissorRect.z, ScissorRect.w);
glEnable(GL_SCISSOR_TEST);
} else {
glDisable(GL_SCISSOR_TEST);
}
TexBind(Tex);
#if PD_OPENGL >= 33
glBindVertexArray(VAO);
#endif
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, CurrentVertex * sizeof(PD::Li::Vertex),
&VertexBuffer[0], GL_DYNAMIC_DRAW);
#if PD_OPENGL < 33
// For some reason we need to set these every frame for every buffer
// Found that out when creating My 3d Engine
SetupShaderAttribs(Shader);
#endif
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, CurrentIndex * sizeof(PD::u16),
&IndexBuffer[0], GL_DYNAMIC_DRAW);
glDrawElements(GL_TRIANGLES, CurrentIndex - StartIndex, GL_UNSIGNED_SHORT,
(void*)(StartIndex * sizeof(PD::u16)));
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
TexBind(0);
#if PD_OPENGL >= 33
glBindVertexArray(0);
#endif
}
}
PD::Li::TexAddress GfxGL3::TexLoad(const std::vector<PD::u8>& pixels, int w,
int h, PD::Li::Texture::Type type,
PD::Li::Texture::Filter filter) {
GLuint texID;
glGenTextures(1, &texID);
glBindTexture(GL_TEXTURE_2D, texID);
// Set base format (Always using RGBA as base)
GLenum fmt = GL_RGBA;
if (type == PD::Li::Texture::Type::RGB24) {
fmt = GL_RGB;
} else if (type == PD::Li::Texture::Type::A8) {
fmt = GL_ALPHA;
}
glTexImage2D(GL_TEXTURE_2D, 0, fmt, w, h, 0, fmt, GL_UNSIGNED_BYTE,
pixels.data());
if (filter == PD::Li::Texture::Filter::LINEAR) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
} else if (filter == PD::Li::Texture::Filter::NEAREST) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
}
glBindTexture(GL_TEXTURE_2D, 0);
return texID;
}
void GfxGL3::TexDelete(PD::Li::TexAddress tex) {
GLuint tex_ = tex;
glDeleteTextures(1, &tex_);
}
} // namespace PD

View File

@@ -0,0 +1,178 @@
/*
MIT License
Copyright (c) 2024 - 2026 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <pd-desktop/bknd-hid.hpp>
namespace PD {
std::string* HidGLFW::pText = nullptr;
GLFWcharfun HidGLFW::pOldTextCB = nullptr;
// Default Call back (If no Text input is requsted)
void NullTextCB(GLFWwindow* win, unsigned int c) {
// Chain
if (HidGLFW::pOldTextCB) {
HidGLFW::pOldTextCB(win, c);
}
}
// Text callback if requested
void TextCB(GLFWwindow* win, unsigned int c) {
// Chain
if (HidGLFW::pOldTextCB) {
HidGLFW::pOldTextCB(win, c);
}
if (!HidGLFW::pText) {
return;
}
*HidGLFW::pText += (char)c;
}
HidGLFW::HidGLFW(PDDriverData data) : HidDriver("HidGLFW") {
if (!data) {
std::cout << "[HidGLFW] Error: Data pointer was null" << std::endl;
}
Window = reinterpret_cast<GLFWwindow*>(data);
HidGLFW::pOldTextCB = glfwSetCharCallback(Window, NullTextCB);
Flags |= Flags_HasKeyboard;
Flags |= Flags_HasMouse;
pBinds[GLFW_MOUSE_BUTTON_LEFT] = Touch;
/*pBinds[GLFW_KEY_F3] = Kb_3;
pBinds[GLFW_KEY_ESCAPE] = Kb_Escape;
pBinds[GLFW_KEY_F11] = Kb_F11;
pBinds[GLFW_KEY_ESCAPE] = Kb_Escape;
pBinds[GLFW_KEY_Q] = Kb_Q;
pBinds[GLFW_KEY_W] = Kb_W;
pBinds[GLFW_KEY_E] = Kb_E;
pBinds[GLFW_KEY_R] = Kb_R;
pBinds[GLFW_KEY_T] = Kb_T;
pBinds[GLFW_KEY_Z] = Kb_Z;
pBinds[GLFW_KEY_U] = Kb_U;
pBinds[GLFW_KEY_I] = Kb_I;
pBinds[GLFW_KEY_O] = Kb_O;
pBinds[GLFW_KEY_P] = Kb_P;
pBinds[GLFW_KEY_A] = Kb_A;
pBinds[GLFW_KEY_S] = Kb_S;
pBinds[GLFW_KEY_D] = Kb_D;
pBinds[GLFW_KEY_F] = Kb_F;
pBinds[GLFW_KEY_G] = Kb_G;
pBinds[GLFW_KEY_H] = Kb_H;
pBinds[GLFW_KEY_J] = Kb_J;
pBinds[GLFW_KEY_K] = Kb_K;
pBinds[GLFW_KEY_L] = Kb_L;
pBinds[GLFW_KEY_Y] = Kb_Y;
pBinds[GLFW_KEY_X] = Kb_X;
pBinds[GLFW_KEY_C] = Kb_C;
pBinds[GLFW_KEY_V] = Kb_V;
pBinds[GLFW_KEY_B] = Kb_B;
pBinds[GLFW_KEY_N] = Kb_N;
pBinds[GLFW_KEY_M] = Kb_M;
pBinds[GLFW_KEY_LEFT_SHIFT] = Kb_LShift;
pBinds[GLFW_KEY_F1] = Kb_F1;
pBinds[GLFW_KEY_F2] = Kb_F2;
pBinds[GLFW_KEY_F3] = Kb_F3;
pBinds[GLFW_KEY_F4] = Kb_F4;
pBinds[GLFW_KEY_F5] = Kb_F5;
pBinds[GLFW_KEY_F6] = Kb_F6;
pBinds[GLFW_KEY_F7] = Kb_F7;
pBinds[GLFW_KEY_F8] = Kb_F8;
pBinds[GLFW_KEY_F9] = Kb_F9;
pBinds[GLFW_KEY_F10] = Kb_F10;
pBinds[GLFW_KEY_F11] = Kb_F11;
pBinds[GLFW_KEY_F12] = Kb_F12;
pBinds[GLFW_KEY_1] = Kb_1;
pBinds[GLFW_KEY_2] = Kb_2;
pBinds[GLFW_KEY_3] = Kb_3;
pBinds[GLFW_KEY_4] = Kb_4;
pBinds[GLFW_KEY_5] = Kb_5;
pBinds[GLFW_KEY_6] = Kb_6;
pBinds[GLFW_KEY_7] = Kb_7;
pBinds[GLFW_KEY_8] = Kb_8;
pBinds[GLFW_KEY_9] = Kb_9;
pBinds[GLFW_KEY_0] = Kb_0;
pBinds[GLFW_KEY_BACKSPACE] = Kb_Backspace;
pBinds[GLFW_KEY_ENTER] = Kb_Enter;*/
}
void HidGLFW::Update() {
// Clear States
for (int i = 0; i < 2; i++) {
KeyEvents[i][Event_Down] = 0;
KeyEvents[i][Event_Held] = 0;
KeyEvents[i][Event_Up] = 0;
for (auto& it : KbKeyEvents[i]) {
it.second = Event_Null;
}
}
// Keyboard Logic
/*for (auto& it : pBinds) {
int kbstate = glfwGetKey(Window, it.first);
if (kbstate == GLFW_PRESS) {
if (PrevStates[it.first] == GLFW_RELEASE) {
KbKeyEvents[0][it.second] = Event_Down;
}
KbKeyEvents[0][it.second] = Event_Held;
} else if (kbstate == GLFW_RELEASE && PrevStates[it.first] == GLFW_PRESS) {
KbKeyEvents[0][it.second] = Event_Up;
}
PrevStates[it.first] = kbstate;
}*/
// Mouse Logic (Todo: Support all mouse buttons)
int state = glfwGetMouseButton(Window, GLFW_MOUSE_BUTTON_LEFT);
if (state == GLFW_PRESS) {
if (PrevState == GLFW_RELEASE) {
KeyEvents[0][Event_Down] |= Touch;
}
KeyEvents[0][Event_Held] |= Touch;
} else if (state == GLFW_RELEASE && PrevState == GLFW_PRESS) {
KeyEvents[0][Event_Up] |= Touch;
}
PrevState = state;
if (pLocked) {
SwapTab();
}
double x, y;
glfwGetCursorPos(Window, &x, &y);
pMouse[1] = pMouse[0]; // Cycle pMouse pos
pMouse[0] = fvec2(x, y);
/*if (pInTextMode && (PD::OS::GetTime() - pLastUpdate) > 50) {
pLastUpdate = PD::OS::GetTime();
HandleTextOps();
}*/
}
void HidGLFW::GetInputStr(std::string& str) {
pText = &str;
glfwSetCharCallback(Window, TextCB);
pInTextMode = true;
}
void HidGLFW::HandleTextOps() {
if (!pText) {
return;
}
/*if (pTimedHeld(Kb_Backspace)) {
if (!pText->empty()) {
pText->pop_back();
}
} else if (pTimedHeld(Kb_Enter)) {
*pText += '\n';
}*/
}
} // namespace PD

View File

@@ -0,0 +1,42 @@
/*
MIT License
Copyright (c) 2024 - 2026 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <palladium>
#include <pd-desktop.hpp>
namespace PD {
void Init(void* data) {
/*if (!data) {
std::cout << "[PD-DRIVERS] Error: pd-desktop requires GLFWwindow* "
"reference as data "
"input!"
<< std::endl;
abort();
}
// Dekstop Init Stage
// First use default OS Driver
PD::OS::Init();
PD::Gfx::Init(PD::GfxGL2::New());
PD::Hid::Init(PD::HidGLFW::New(reinterpret_cast<GLFWwindow*>(data)));*/
}
} // namespace PD

View File

View File

View File

View File

View File

View File

@@ -1,38 +0,0 @@
#pragma once
#include <pd/drivers/gfx.hpp>
#include <pd_system/ctr-linear-allocator.hpp>
namespace PD {
struct GfxCitro3DConfig {
// Vertex Allocator
template <typename T>
using VertexAlloc = LinearAllocator<T>;
// Index Allocator
template <typename T>
using IndexAlloc = LinearAllocator<T>;
static constexpr size_t NumVertices = 32768; // 8192*4
static constexpr size_t NumIndices = 49152; // 8192*6
};
class GfxCitro3D : public GfxDriverBase<GfxCitro3DConfig> {
public:
GfxCitro3D() : GfxDriverBase("Citro3D") {}
~GfxCitro3D() {}
void SysInit() override;
void SysDeinit() override;
void Submit(size_t count, size_t start) override;
void BindTexture(TextureID id) override;
void SysReset() override;
TextureID LoadTexture(const std::vector<PD::u8>& pixels, int w, int h,
TextureFormat type = TextureFormat::RGBA32,
TextureFilter filter = TextureFilter::Linear) override;
void DeleteTexture(const TextureID& tex) override;
private:
struct Impl;
Impl* impl = nullptr;
};
} // namespace PD

View File

@@ -1,39 +0,0 @@
#pragma once
#include <pd/drivers/gfx.hpp>
namespace PD {
struct GfxDirectX9Config {
// Vertex Allocator
template <typename T>
using VertexAlloc = std::allocator<T>;
// Index Allocator
template <typename T>
using IndexAlloc = std::allocator<T>;
static constexpr size_t NumVertices = 32768; // 8192*4
static constexpr size_t NumIndices = 49152; // 8192*6
};
class GfxDirectX9 : public GfxDriverBase<GfxDirectX9Config> {
public:
GfxDirectX9(void* device = nullptr)
: GfxDriverBase("DirectX9"), pDevice(device) {}
~GfxDirectX9() {}
void SysInit() override;
void SysDeinit() override;
void Submit(size_t count, size_t start) override;
void BindTexture(TextureID id) override;
void SysReset() override;
TextureID LoadTexture(const std::vector<PD::u8>& pixels, int w, int h,
TextureFormat type = TextureFormat::RGBA32,
TextureFilter filter = TextureFilter::Linear) override;
void DeleteTexture(const TextureID& tex) override;
private:
struct Impl;
Impl* impl = nullptr;
void* pDevice = nullptr;
};
} // namespace PD

View File

@@ -1,44 +0,0 @@
#pragma once
#include <pd/drivers/gfx.hpp>
namespace PD {
struct GfxOpenGL2Config {
// Vertex Allocator
template <typename T>
using VertexAlloc = std::allocator<T>;
// Index Allocator
template <typename T>
using IndexAlloc = std::allocator<T>;
static constexpr size_t NumVertices = 32768; // 8192*4
static constexpr size_t NumIndices = 49152; // 8192*6
};
class GfxOpenGL2 : public GfxDriverBase<GfxOpenGL2Config> {
public:
GfxOpenGL2() : GfxDriverBase("OpenGL2") {}
~GfxOpenGL2() {}
void SysInit() override;
void SysDeinit() override;
void Submit(size_t count, size_t start) override;
void BindTexture(TextureID id) override;
void SysReset() override;
TextureID LoadTexture(const std::vector<PD::u8>& pixels, int w, int h,
TextureFormat type = TextureFormat::RGBA32,
TextureFilter filter = TextureFilter::Linear) override;
void DeleteTexture(const TextureID& tex) override;
private:
void pSetupShaderAttribs(u32 shader);
u32 pShader = 0;
u32 VBO = 0;
u32 IBO = 0;
int pLocTex = 0;
int pLocAlfa = 0;
int pLocProjection = 0;
static const char* pVertCode;
static const char* pFragCode;
};
} // namespace PD

View File

@@ -1,44 +0,0 @@
#pragma once
#include <pd/drivers/gfx.hpp>
namespace PD {
struct GfxOpenGL3Config {
// Vertex Allocator
template <typename T>
using VertexAlloc = std::allocator<T>;
// Index Allocator
template <typename T>
using IndexAlloc = std::allocator<T>;
static constexpr size_t NumVertices = 32768; // 8192*4
static constexpr size_t NumIndices = 49152; // 8192*6
};
class GfxOpenGL3 : public GfxDriverBase<GfxOpenGL3Config> {
public:
GfxOpenGL3() : GfxDriverBase("OpenGL3") {}
~GfxOpenGL3() {}
void SysInit() override;
void SysDeinit() override;
void Submit(size_t count, size_t start) override;
void BindTexture(TextureID id) override;
void SysReset() override;
TextureID LoadTexture(const std::vector<PD::u8>& pixels, int w, int h,
TextureFormat type = TextureFormat::RGBA32,
TextureFilter filter = TextureFilter::Linear) override;
void DeleteTexture(const TextureID& tex) override;
private:
u32 pShader = 0;
u32 VBO = 0;
u32 IBO = 0;
u32 VAO = 0;
int pLocTex = 0;
int pLocAlfa = 0;
int pLocProjection = 0;
static const char* pVertCode;
static const char* pFragCode;
};
} // namespace PD

View File

@@ -1,7 +0,0 @@
#pragma once
#include <pd/common.hpp>
namespace PD {
u32 CreateShaderProgram(const char* vert, const char* frag);
}

View File

@@ -1,6 +0,0 @@
#pragma once
#include <pd_system/gfx_citro3d.hpp>
#include <pd_system/gfx_directx9.hpp>
#include <pd_system/gfx_opengl2.hpp>
#include <pd_system/gfx_opengl3.hpp>

View File

@@ -1,69 +0,0 @@
// Well yes, i finally try it
#include <pd_system/gfx_citro3d.hpp>
#if defined(PD_ENABLE_CITRO3D) // && defined(__3DS__)
#include <3ds.h>
#include <citro3d.h>
#include <pd/drivers/drivers.hpp>
namespace PD {
struct GfxCitro3D::Impl {};
void GfxCitro3D::SysInit() {
if (impl) return;
PDLOG("GfxCitro3D::SysInit();");
impl = new Impl();
}
void GfxCitro3D::SysDeinit() {
if (!impl) return;
delete impl;
impl = nullptr;
PDLOG("GfxCitro3D::SysDeinit()");
}
void GfxCitro3D::Submit(size_t count, size_t start) {
if (!impl) return;
}
void GfxCitro3D::BindTexture(TextureID id) {
if (!impl) return;
}
void GfxCitro3D::SysReset() {
if (!impl) return;
}
TextureID GfxCitro3D::LoadTexture(const std::vector<PD::u8>& pixels, int w,
int h, TextureFormat type,
TextureFilter filter) {
if (!impl) return 0;
return 0;
}
void GfxCitro3D::DeleteTexture(const TextureID& tex) {
if (!tex) return;
}
} // namespace PD
#else
namespace PD {
void GfxCitro3D::SysInit() {
PDLOG(
"GfxCitro3D::SysInit: Citro3D Driver is not included in "
"palladium-system");
}
void GfxCitro3D::SysDeinit() {}
void GfxCitro3D::Submit(size_t count, size_t start) {}
void GfxCitro3D::BindTexture(TextureID id) {}
void GfxCitro3D::SysReset() {}
TextureID GfxCitro3D::LoadTexture(const std::vector<PD::u8>& pixels, int w,
int h, TextureFormat type,
TextureFilter filter) {
return 0;
}
void GfxCitro3D::DeleteTexture(const TextureID& tex) {}
} // namespace PD
#endif

View File

@@ -1,267 +0,0 @@
// Well yes, i finally try it
#include <pd_system/gfx_directx9.hpp>
// Sicher ist sicher
#if defined(PD_ENABLE_DIRECTX9) && defined(_WIN32)
#include <d3d9.h>
#include <d3dcompiler.h>
#include <pd/drivers/drivers.hpp>
namespace PD {
static const char* g_vsCode = R"(
float4x4 projection;
struct VS_IN {
float2 pos : POSITION0;
float2 uv : TEXCOORD0;
float4 col : COLOR0;
};
struct VS_OUT {
float4 pos : POSITION0;
float2 uv : TEXCOORD0;
float4 col : COLOR0;
};
VS_OUT main(VS_IN input) {
VS_OUT o;
o.pos = mul(projection, float4(input.pos, 0.0, 1.0));
o.uv = input.uv;
o.col = input.col;
return o;
}
)";
static const char* g_psCode = R"(
sampler2D tex : register(s0);
bool alfa;
struct PS_IN {
float2 uv : TEXCOORD0;
float4 col : COLOR0;
};
float4 main(PS_IN input) : COLOR0 {
float4 tc = tex2D(tex, input.uv);
if (alfa)
return float4(input.col.rgb, tc.a * input.col.a);
else
return tc * input.col;
}
)";
struct GfxDirectX9::Impl {
IDirect3DDevice9* Device = nullptr;
IDirect3DVertexBuffer9* VBO = nullptr;
IDirect3DIndexBuffer9* IBO = nullptr;
IDirect3DVertexDeclaration9* Decl = nullptr;
IDirect3DVertexShader9* VS = nullptr;
IDirect3DPixelShader9* FS = nullptr;
IDirect3DTexture9* CurrentTex = nullptr;
};
void GfxDirectX9::SysInit() {
if (impl) return;
PDLOG("GfxDirectX9::SysInit();");
impl = new Impl();
impl->Device = (IDirect3DDevice9*)pDevice;
if (impl->Device) {
D3DVERTEXELEMENT9 elements[]{
{0, 0, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION,
0},
{0, 8, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD,
0},
{0, 16, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR,
0},
D3DDECL_END()};
impl->Device->CreateVertexDeclaration(elements, &impl->Decl);
impl->Device->CreateVertexBuffer(
GfxDirectX9Config::NumVertices * sizeof(PD::Li::Vertex),
D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY, 0, D3DPOOL_DEFAULT, &impl->VBO,
nullptr);
impl->Device->CreateIndexBuffer(GfxDirectX9Config::NumIndices * sizeof(u16),
D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY,
D3DFMT_INDEX16, D3DPOOL_DEFAULT, &impl->IBO,
nullptr);
ID3DBlob* vsBlob = nullptr;
ID3DBlob* errBlob = nullptr;
HRESULT hr = D3DCompile(g_vsCode, strlen(g_vsCode), nullptr, nullptr,
nullptr, "main", "vs_2_0", 0, 0, &vsBlob, &errBlob);
if (FAILED(hr)) {
PDLOG("Vertex Shader compile error: {}",
errBlob ? (char*)errBlob->GetBufferPointer() : "");
} else {
impl->Device->CreateVertexShader((DWORD*)vsBlob->GetBufferPointer(),
&impl->VS);
}
if (vsBlob) vsBlob->Release();
if (errBlob) errBlob->Release();
ID3DBlob* psBlob = nullptr;
errBlob = nullptr;
hr = D3DCompile(g_psCode, strlen(g_psCode), nullptr, nullptr, nullptr,
"main", "ps_2_0", 0, 0, &psBlob, &errBlob);
if (FAILED(hr)) {
PDLOG("Pixel Shader compile error: {}",
errBlob ? (char*)errBlob->GetBufferPointer() : "");
} else {
impl->Device->CreatePixelShader((DWORD*)psBlob->GetBufferPointer(),
&impl->FS);
}
if (psBlob) psBlob->Release();
if (errBlob) errBlob->Release();
} else {
PDLOG(
"GfxDirectX9::SysInit Error: pDevice is not set!\nYOu need to include "
"your D3D9 Device as "
"folowing:\nPD::Gfx::UseDriver<PD::GfxDirectX9>(D3D9Device);");
}
}
void GfxDirectX9::SysDeinit() {
if (!impl || !impl->Device) return;
if (impl->VBO) impl->VBO->Release();
if (impl->IBO) impl->IBO->Release();
if (impl->Decl) impl->Decl->Release();
if (impl->VS) impl->VS->Release();
if (impl->FS) impl->FS->Release();
delete impl;
impl = nullptr;
PDLOG("GfxDirectX9::SysDeinit()");
}
void GfxDirectX9::Submit(size_t count, size_t start) {
if (!impl || !impl->Device || !impl->VBO || !impl->IBO) return;
impl->Device->SetVertexShaderConstantF(
0, reinterpret_cast<const float*>(&Projection), 4);
void* vptr;
impl->VBO->Lock(0, 0, &vptr, D3DLOCK_DISCARD);
memcpy(vptr, GetVertexBufPtr(0), CurrentVertex * sizeof(PD::Li::Vertex));
impl->VBO->Unlock();
void* iptr;
impl->IBO->Lock(0, 0, &iptr, D3DLOCK_DISCARD);
memcpy(iptr, GetIndexBufPtr(0), CurrentIndex * sizeof(u16));
impl->IBO->Unlock();
impl->Device->SetStreamSource(0, impl->VBO, 0, sizeof(PD::Li::Vertex));
impl->Device->SetIndices(impl->IBO);
impl->Device->SetVertexDeclaration(impl->Decl);
impl->Device->SetVertexShader(impl->VS);
impl->Device->SetPixelShader(impl->FS);
impl->Device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, CurrentVertex,
start, count / 3);
}
void GfxDirectX9::BindTexture(TextureID id) {
if (!impl || !impl->Device) return;
impl->CurrentTex = (IDirect3DTexture9*)id;
impl->Device->SetTexture(0, impl->CurrentTex);
bool a8 = false;
if (impl->CurrentTex) {
D3DSURFACE_DESC desc;
impl->CurrentTex->GetLevelDesc(0, &desc);
a8 = (desc.Format == D3DFMT_A8);
}
float v = a8 ? 1.0f : 0.0f;
impl->Device->SetPixelShaderConstantF(0, &v, 1);
}
void GfxDirectX9::SysReset() {
if (!impl || !impl->Device) return;
impl->Device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
impl->Device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
impl->Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
}
TextureID GfxDirectX9::LoadTexture(const std::vector<PD::u8>& pixels, int w,
int h, TextureFormat type,
TextureFilter filter) {
if (!impl || !impl->Device) return 0;
IDirect3DTexture9* tex = nullptr;
D3DFORMAT fmt = D3DFMT_A8R8G8B8;
if (type == TextureFormat::RGB24)
fmt = D3DFMT_X8R8G8B8;
else if (type == TextureFormat::A8)
fmt = D3DFMT_A8;
HRESULT hr = impl->Device->CreateTexture(w, h, 1, 0, fmt, D3DPOOL_MANAGED,
&tex, nullptr);
if (FAILED(hr) || !tex) return 0;
D3DLOCKED_RECT rect;
tex->LockRect(0, &rect, nullptr, 0);
if (type == TextureFormat::RGB24) {
u8* dstRow = reinterpret_cast<u8*>(rect.pBits);
for (int y = 0; y < h; ++y) {
u32* dst = reinterpret_cast<u32*>(dstRow);
for (int x = 0; x < w; ++x) {
u8 r = pixels[(y * w + x) * 3 + 0];
u8 g = pixels[(y * w + x) * 3 + 1];
u8 b = pixels[(y * w + x) * 3 + 2];
dst[x] = (0xFF << 24) | (r << 16) | (g << 8) | b; // X8R8G8B8
}
dstRow += rect.Pitch;
}
} else if (type == TextureFormat::RGBA32) {
u8* dstRow = reinterpret_cast<u8*>(rect.pBits);
for (int y = 0; y < h; ++y) {
u32* dst = reinterpret_cast<u32*>(dstRow);
for (int x = 0; x < w; ++x) {
u8 r = pixels[(y * w + x) * 4 + 0];
u8 g = pixels[(y * w + x) * 4 + 1];
u8 b = pixels[(y * w + x) * 4 + 2];
u8 a = pixels[(y * w + x) * 4 + 3];
dst[x] = (a << 24) | (r << 16) | (g << 8) | b; // A8R8G8B8
}
dstRow += rect.Pitch;
}
} else if (type == TextureFormat::A8) {
u8* dstRow = reinterpret_cast<u8*>(rect.pBits);
for (int y = 0; y < h; ++y) {
memcpy(dstRow, &pixels[y * w], w);
dstRow += rect.Pitch;
}
}
tex->UnlockRect(0);
PDLOG("GfxDirectX9::LoadTexture -> [{}] 0x{:X}, [{}, {}]", PD::ivec2(w, h),
(TextureID)tex, type, filter);
return (TextureID)tex;
}
void GfxDirectX9::DeleteTexture(const TextureID& tex) {
if (!tex) return;
IDirect3DTexture9* t = (IDirect3DTexture9*)tex;
t->Release();
}
} // namespace PD
#else
namespace PD {
void GfxDirectX9::SysInit() {
PDLOG(
"GfxDirectX9::SysInit: DirectX9 Driver is not included in "
"palladium-system");
}
void GfxDirectX9::SysDeinit() {}
void GfxDirectX9::Submit(size_t count, size_t start) {}
void GfxDirectX9::BindTexture(TextureID id) {}
void GfxDirectX9::SysReset() {}
TextureID GfxDirectX9::LoadTexture(const std::vector<PD::u8>& pixels, int w,
int h, TextureFormat type,
TextureFilter filter) {
return 0;
}
void GfxDirectX9::DeleteTexture(const TextureID& tex) {}
} // namespace PD
#endif

View File

@@ -1,184 +0,0 @@
#include <pd_system/gfx_opengl2.hpp>
#if defined(PD_ENABLE_OPENGL2)
#include <glad/glad.h>
#include <pd/drivers/drivers.hpp>
#include <pd_system/gl-helper.hpp>
namespace PD {
const char* GfxOpenGL2::pVertCode = R"(
#version 120
attribute vec2 pos;
attribute vec2 uv;
attribute vec4 color;
varying vec2 oUV;
varying vec4 oColor;
// Probably forgot about this matrix and
// searched hours for why the rendering isn't working :/
uniform mat4 projection;
void main() {
gl_Position = projection*vec4(pos, 0.0, 1.0);
oUV = uv;
oColor = color;
}
)";
const char* GfxOpenGL2::pFragCode = R"(
#version 120
varying vec2 oUV;
varying vec4 oColor;
uniform sampler2D tex;
uniform bool alfa;
void main() {
vec4 tc = texture2D(tex, oUV);
if (alfa) {
gl_FragColor = vec4(oColor.rgb, tc.a * oColor.a);
} else {
gl_FragColor = tc * oColor;
}
}
)";
void GfxOpenGL2::pSetupShaderAttribs(u32 shader) {
GLint _pos = glGetAttribLocation(shader, "pos");
GLint _uv = glGetAttribLocation(shader, "uv");
GLint _color = glGetAttribLocation(shader, "color");
glVertexAttribPointer(_pos, 2, GL_FLOAT, GL_FALSE, sizeof(PD::Li::Vertex),
(void*)offsetof(PD::Li::Vertex, pos));
glEnableVertexAttribArray(_pos);
glVertexAttribPointer(_uv, 2, GL_FLOAT, GL_FALSE, sizeof(PD::Li::Vertex),
(void*)offsetof(PD::Li::Vertex, uv));
glEnableVertexAttribArray(_uv);
glVertexAttribPointer(_color, 4, GL_UNSIGNED_BYTE, GL_TRUE,
sizeof(PD::Li::Vertex),
(void*)offsetof(PD::Li::Vertex, color));
glEnableVertexAttribArray(_color);
}
void GfxOpenGL2::SysInit() {
pShader = CreateShaderProgram(pVertCode, pFragCode);
glUseProgram(pShader);
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glGenBuffers(1, &IBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
pSetupShaderAttribs(pShader);
pLocTex = glGetUniformLocation(pShader, "tex");
pLocAlfa = glGetUniformLocation(pShader, "alfa");
pLocProjection = glGetUniformLocation(pShader, "projection");
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
PDLOG(
"GfxOpenGL2::SysInit():\n pShader = {}\n pLocTex = {}\n pLocAlfa = "
"{}\n pLocProjection = {}\n VBO = {}\n IBO = {}",
pShader, pLocTex, pLocAlfa, pLocProjection, VBO, IBO);
}
void GfxOpenGL2::SysDeinit() {
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &IBO);
PDLOG("GfxOpenGL2::SysDeinit()");
}
void GfxOpenGL2::Submit(size_t count, size_t start) {
BindTexture(CurrentTex);
glUseProgram(pShader);
glUniformMatrix4fv(pLocProjection, 1, GL_FALSE, Projection.m.data());
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, CurrentVertex * sizeof(PD::Li::Vertex),
GetVertexBufPtr(0), GL_DYNAMIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, CurrentIndex * sizeof(PD::u16),
GetIndexBufPtr(0), GL_DYNAMIC_DRAW);
pSetupShaderAttribs(pShader);
GLint ibo = 0;
glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING, &ibo);
glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_SHORT,
reinterpret_cast<void*>(start * sizeof(u16)));
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
BindTexture(0);
}
void GfxOpenGL2::BindTexture(TextureID id) {
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, (GLuint)id);
glUniform1i(pLocTex, 0);
GLint fmt = 0;
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, &fmt);
glUniform1i(pLocAlfa, fmt == GL_ALPHA);
}
void GfxOpenGL2::SysReset() {
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
TextureID GfxOpenGL2::LoadTexture(const std::vector<PD::u8>& pixels, int w,
int h, TextureFormat type,
TextureFilter filter) {
GLuint texID;
glGenTextures(1, &texID);
glBindTexture(GL_TEXTURE_2D, texID);
// Set base format (Always using RGBA as base)
GLenum fmt = GL_RGBA;
if (type == TextureFormat::RGB24) {
fmt = GL_RGB;
} else if (type == TextureFormat::A8) {
fmt = GL_ALPHA;
}
glTexImage2D(GL_TEXTURE_2D, 0, fmt, w, h, 0, fmt, GL_UNSIGNED_BYTE,
pixels.data());
if (filter == TextureFilter::Linear) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
} else if (filter == TextureFilter::Nearest) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
}
glBindTexture(GL_TEXTURE_2D, 0);
PDLOG("GfxOpenGL2::LoadTexture -> [{}] {}, [{}, {}]", PD::ivec2(w, h), texID,
type, filter);
return texID;
}
void GfxOpenGL2::DeleteTexture(const TextureID& tex) {
GLuint tex_ = tex;
glDeleteTextures(1, &tex_);
}
} // namespace PD
#else
namespace PD {
void GfxOpenGL2::SysInit() {
PDLOG(
"GfxOpenGL2::SysInit: OpenGL2 Driver is not included in "
"palladium-system");
}
void GfxOpenGL2::SysDeinit() {}
void GfxOpenGL2::Submit(size_t count, size_t start) {}
void GfxOpenGL2::BindTexture(TextureID id) {}
void GfxOpenGL2::SysReset() {}
TextureID GfxOpenGL2::LoadTexture(const std::vector<PD::u8>& pixels, int w,
int h, TextureFormat type,
TextureFilter filter) {
return 0;
}
void GfxOpenGL2::DeleteTexture(const TextureID& tex) {}
void GfxOpenGL2::pSetupShaderAttribs(u32 shader) {}
} // namespace PD
#endif

View File

@@ -1,180 +0,0 @@
#include <pd_system/gfx_opengl3.hpp>
#if defined(PD_ENABLE_OPENGL3)
#include <glad/glad.h>
#include <pd/drivers/drivers.hpp>
#include <pd_system/gl-helper.hpp>
namespace PD {
const char* GfxOpenGL3::pVertCode = R"(
#version 330 core
layout(location = 0) in vec2 pos;
layout(location = 1) in vec2 uv;
layout(location = 2) in vec4 color;
out vec2 oUV;
out vec4 oColor;
// Probably forgot about this matrix and
// searched hours for why the rendering isn't working :/
uniform mat4 projection;
void main() {
gl_Position = projection*vec4(pos, 0.0, 1.0);
oUV = uv;
oColor = color;
}
)";
const char* GfxOpenGL3::pFragCode = R"(
#version 330 core
in vec2 oUV;
in vec4 oColor;
uniform sampler2D tex;
uniform bool alfa;
out vec4 FragColor;
void main() {
vec4 tc = texture(tex, oUV);
if (alfa) {
FragColor = vec4(oColor.rgb, tc.a * oColor.a);
} else {
FragColor = tc * oColor;
}
}
)";
void GfxOpenGL3::SysInit() {
pShader = CreateShaderProgram(pVertCode, pFragCode);
glUseProgram(pShader);
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(PD::Li::Vertex),
(void*)offsetof(PD::Li::Vertex, pos));
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(PD::Li::Vertex),
(void*)offsetof(PD::Li::Vertex, uv));
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(PD::Li::Vertex),
(void*)offsetof(PD::Li::Vertex, color));
glGenBuffers(1, &IBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
pLocTex = glGetUniformLocation(pShader, "tex");
pLocAlfa = glGetUniformLocation(pShader, "alfa");
pLocProjection = glGetUniformLocation(pShader, "projection");
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindVertexArray(0);
PDLOG(
"GfxOpenGL3::SysInit():\n pShader = {}\n pLocTex = {}\n pLocAlfa = "
"{}\n pLocProjection = {}\n VBO = {}\n IBO = {}, VAO = {}",
pShader, pLocTex, pLocAlfa, pLocProjection, VBO, IBO, VAO);
}
void GfxOpenGL3::SysDeinit() {
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &IBO);
glDeleteVertexArrays(1, &VAO);
PDLOG("GfxOpenGL3::SysDeinit()");
}
void GfxOpenGL3::Submit(size_t count, size_t start) {
BindTexture(CurrentTex);
glUseProgram(pShader);
glUniformMatrix4fv(pLocProjection, 1, GL_FALSE, Projection.m.data());
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, CurrentVertex * sizeof(PD::Li::Vertex),
GetVertexBufPtr(0), GL_DYNAMIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, CurrentIndex * sizeof(u16),
GetIndexBufPtr(0), GL_DYNAMIC_DRAW);
glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_SHORT,
reinterpret_cast<void*>(start * sizeof(u16)));
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindVertexArray(0);
BindTexture(0);
}
void GfxOpenGL3::BindTexture(TextureID id) {
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, (GLuint)id);
glUniform1i(pLocTex, 0);
GLint fmt = 0;
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, &fmt);
glUniform1i(pLocAlfa, fmt == GL_ALPHA);
}
void GfxOpenGL3::SysReset() {
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
TextureID GfxOpenGL3::LoadTexture(const std::vector<PD::u8>& pixels, int w,
int h, TextureFormat type,
TextureFilter filter) {
GLuint texID;
glGenTextures(1, &texID);
glBindTexture(GL_TEXTURE_2D, texID);
// Set base format (Always using RGBA as base)
GLenum fmt = GL_RGBA;
if (type == TextureFormat::RGB24) {
fmt = GL_RGB;
} else if (type == TextureFormat::A8) {
fmt = GL_ALPHA;
}
glTexImage2D(GL_TEXTURE_2D, 0, fmt, w, h, 0, fmt, GL_UNSIGNED_BYTE,
pixels.data());
if (filter == TextureFilter::Linear) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
} else if (filter == TextureFilter::Nearest) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
}
glBindTexture(GL_TEXTURE_2D, 0);
PDLOG("GfxOpenGL3::LoadTexture -> [{}] {}, [{}, {}]", PD::ivec2(w, h), texID,
type, filter);
return texID;
}
void GfxOpenGL3::DeleteTexture(const TextureID& tex) {
GLuint tex_ = tex;
glDeleteTextures(1, &tex_);
}
} // namespace PD
#else
namespace PD {
void GfxOpenGL3::SysInit() {
PDLOG(
"GfxOpenGL3::SysInit: OpenGL3 Driver is not included in "
"palladium-system");
}
void GfxOpenGL3::SysDeinit() {}
void GfxOpenGL3::Submit(size_t count, size_t start) {}
void GfxOpenGL3::BindTexture(TextureID id) {}
void GfxOpenGL3::SysReset() {}
TextureID GfxOpenGL3::LoadTexture(const std::vector<PD::u8>& pixels, int w,
int h, TextureFormat type,
TextureFilter filter) {
return 0;
}
void GfxOpenGL3::DeleteTexture(const TextureID& tex) {}
void GfxOpenGL3::pSetupShaderAttribs(u32 shader) {}
} // namespace PD
#endif

View File

@@ -1,46 +0,0 @@
#include <glad/glad.h>
#include <iostream>
#include <pd_system/gl-helper.hpp>
namespace PD {
GLuint compileShader(const char* src, GLenum type) {
GLuint shader = glCreateShader(type);
glShaderSource(shader, 1, &src, nullptr);
glCompileShader(shader);
GLint success;
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (!success) {
char infoLog[512];
glGetShaderInfoLog(shader, 512, nullptr, infoLog);
std::cerr << "Shader Compilation Error: " << infoLog << std::endl;
}
return shader;
}
u32 CreateShaderProgram(const char* vert, const char* frag) {
GLuint vertexShader = compileShader(vert, GL_VERTEX_SHADER);
GLuint fragmentShader = compileShader(frag, GL_FRAGMENT_SHADER);
GLuint shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
GLint success;
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
if (!success) {
char infoLog[512];
glGetProgramInfoLog(shaderProgram, 512, nullptr, infoLog);
std::cerr << "Shader Program Linking Error: " << infoLog << std::endl;
}
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
if (success) PDLOG("Shader [{}] compiled sucessfully", shaderProgram);
return shaderProgram;
}
} // namespace PD

View File

@@ -11,6 +11,51 @@ set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
# Enable Compile Command Export
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
option(PD_INCLUDE_DIR "")
### Helper Function to Build Librarys without have always
### These includes and definition defines
function(pd_add_lib TARGET_NAME)
set(opts "BUILD_SHARED")
set(one_val_args "")
set(multi_val_args SRC_FILES DEPENDS)
cmake_parse_arguments(ARG "${opts}" "${one_val_args}" "${multi_val_args}" ${ARGN})
string(REPLACE "-" "_" FLAG_NAME_T ${TARGET_NAME})
string(TOUPPER ${FLAG_NAME_T} FLAG_NAME_F)
if(ARG_BUILD_SHARED)
add_library(${TARGET_NAME} SHARED ${ARG_SRC_FILES})
target_compile_definitions(${TARGET_NAME} PUBLIC -D${FLAG_NAME_F}_BUILD_SHARED=1)
message("Building SHARED library: ${FLAG_NAME_F}_BUILD_SHARED=1")
else()
add_library(${TARGET_NAME} STATIC ${ARG_SRC_FILES})
target_compile_definitions(${TARGET_NAME} PUBLIC -D${FLAG_NAME_F}_BUILD_SHARED=0)
message("Building STATIC library: ${FLAG_NAME_F}_BUILD_SHARED=0")
endif()
target_include_directories(${TARGET_NAME} PUBLIC
${PD_INCLUDE_DIR}
${DEVKITPRO}/portlibs/3ds/include
)
target_compile_definitions(${TARGET_NAME} PRIVATE
-D_GNU_SOURCE=1
-DPALLADIUM_VERSION="${PROJECT_VERSION}"
-DPALLADIUM_GIT_COMMIT="${GIT_SHORT_HASH}"
-DPALLADIUM_GIT_BRANCH="${GIT_BRANCH}"
-DBUILD_CTR=1
)
if(${CMAKE_SYSTEM_NAME} STREQUAL "Nintendo3DS")
target_compile_options(${TARGET_NAME} PRIVATE
-Wno-abi
)
endif()
### For the libs that depend on another
if(ARG_DEPENDS)
target_link_libraries(${TARGET_NAME} PUBLIC ${ARG_DEPENDS})
endif()
install(TARGETS ${TARGET_NAME})
endfunction()
### GIT HELPER ###
function(pd_git_get_hash ret)

View File

@@ -25,4 +25,7 @@ SOFTWARE.
#include <pd/core/core.hpp>
#include <pd/drivers/drivers.hpp>
#include <pd/external/json.hpp>
#include <pd/image/image.hpp>
#include <pd/lithium/lithium.hpp>
#include <pd/ui7/ui7.hpp>

View File

@@ -23,10 +23,13 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <pd/common.hpp>
#include <pd/core/common.hpp>
namespace PD {
namespace Bits {
/**
* Binary Utillity Functions
*/
namespace BitUtil {
/**
* Check if a 32 Bit number only set a sigle bit to 1
* @param v 32 bit unsigned int
@@ -39,5 +42,5 @@ PD_API bool IsSingleBit(u32 v);
* @return Next Number thats a Pow of 2
*/
PD_API u32 GetPow2(u32 v);
} // namespace Bits
} // namespace BitUtil
} // namespace PD

View File

@@ -23,7 +23,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <pd/common.hpp>
#include <pd/core/common.hpp>
#include <pd/core/strings.hpp>
namespace PD {
@@ -140,11 +140,6 @@ class PD_API Color {
*/
constexpr operator u32() const { return Get(); }
const float rf() const { return static_cast<float>(r) / 255.f; }
const float gf() const { return static_cast<float>(g) / 255.f; }
const float bf() const { return static_cast<float>(b) / 255.f; }
const float af() const { return static_cast<float>(a) / 255.f; }
/** Public Access Data section */
u8 r;
u8 g;

89
include/pd/core/common.hpp Executable file
View File

@@ -0,0 +1,89 @@
#pragma once
/*
MIT License
Copyright (c) 2024 - 2026 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <array>
#include <chrono>
#include <cinttypes>
#include <cmath>
#include <filesystem> // Requires C++ 17 or later
#include <format> // Requires C++ 20 or later
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numbers>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
/** Dynamic Lib loading */
#include <pd/pd_p_api.hpp>
/** Memory Management */
#define PD_SHARED(x) \
using Ref = std::shared_ptr<x>; \
template <typename... Args> \
static Ref New(Args&&... args) { \
return std::make_shared<x>(std::forward<Args>(args)...); \
}
#define PD_RAW(x) \
using Ref = x*; \
template <typename... Args> \
static Ref New(Args&&... args) { \
x* v = new x; \
new (v) x(std::forward<Args>(args)...); \
return v; \
} \
static void Delete(Ref ref) { delete ref; }
#define PD_UNIQUE(x) \
using Ref = std::unique_ptr<x>; \
template <typename... Args> \
static Ref New(Args&&... args) { \
return std::make_unique<x>(std::forward<Args>(args)...); \
}
#define PD_BIT(x) (1 << x)
namespace PD {
[[noreturn]] inline void Throw(const std::string& str) {
#ifdef _EXCEPTIONS
throw std::runtime_error("PD Error " + str);
#else
std::cout << "PD Error " << str << std::endl;
std::abort();
#endif
}
/** Types */
using u8 = unsigned char;
using u16 = unsigned short;
using u32 = unsigned int;
using u64 = unsigned long long;
} // namespace PD

View File

@@ -23,11 +23,9 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <pd/core/bits.hpp>
#include <pd/core/bit_util.hpp>
#include <pd/core/color.hpp>
#include <pd/core/fnv.hpp>
#include <pd/core/formatters.hpp>
#include <pd/core/fquat.hpp>
#include <pd/core/hashid.hpp>
#include <pd/core/io.hpp>
#include <pd/core/mat.hpp>

View File

@@ -23,7 +23,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <pd/common.hpp>
#include <pd/core/common.hpp>
namespace PD {
/**

View File

@@ -1,98 +0,0 @@
#pragma once
/*
MIT License
Copyright (c) 2024 - 2026 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <pd/core/color.hpp>
template <>
struct std::formatter<PD::Color> : std::formatter<std::string> {
enum class ColorFmt {
Hex,
Rgbf,
Rgb,
Rgba,
Rgbaf,
};
ColorFmt mode = ColorFmt::Hex;
constexpr auto parse(std::format_parse_context& ctx) {
auto it = ctx.begin();
auto end = ctx.end();
if (it == end || *it == '}') return it;
if (check(it, end, 5, "rgbaf")) {
mode = ColorFmt::Rgbaf;
it += 5;
} else if (check(it, end, 4, "rgba")) {
mode = ColorFmt::Rgba;
it += 4;
} else if (check(it, end, 4, "rgbf")) {
mode = ColorFmt::Rgbf;
it += 4;
} else if (check(it, end, 3, "rgb")) {
mode = ColorFmt::Rgb;
it += 3;
} else if (check(it, end, 3, "hex")) {
mode = ColorFmt::Hex;
it += 3;
} else {
throw std::format_error("invalid format for PD::Color");
}
if (it == end || *it != '}')
throw std::format_error("invalid format for PD::Color");
return it;
}
template <typename FormatContext>
auto format(const PD::Color& value, FormatContext& ctx) const {
switch (mode) {
case ColorFmt::Hex:
return std::format_to(ctx.out(), "#{:02X}{:02X}{:02X}{:02X}", value.r,
value.g, value.b, value.a);
case ColorFmt::Rgb:
return std::format_to(ctx.out(), "{}, {}, {}", value.r, value.g,
value.b);
case ColorFmt::Rgba:
return std::format_to(ctx.out(), "{}, {}, {}, {}", value.r, value.g,
value.b, value.a);
case ColorFmt::Rgbf:
return std::format_to(ctx.out(), "{}, {}, {}", value.rf(), value.gf(),
value.bf());
case ColorFmt::Rgbaf:
return std::format_to(ctx.out(), "{}, {}, {}, {}", value.rf(),
value.gf(), value.bf(), value.af());
default:
return ctx.out();
}
}
constexpr bool check(std::format_parse_context::const_iterator it,
std::format_parse_context::const_iterator end,
size_t len, const char* what) {
return (end - it >= static_cast<std::ptrdiff_t>(len) &&
std::string_view(&*it, len) == what); // msvc things...
}
};

View File

@@ -25,7 +25,7 @@ SOFTWARE.
// This file is based on fvec4
#include <pd/common.hpp>
#include <pd/core/common.hpp>
#include <pd/core/vec4.hpp>
namespace PD {

View File

@@ -23,7 +23,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <pd/common.hpp>
#include <pd/core/common.hpp>
namespace PD {
/**

View File

@@ -23,7 +23,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <pd/common.hpp>
#include <pd/core/common.hpp>
#include <pd/core/vec3.hpp>
namespace PD {

View File

@@ -1,34 +1,20 @@
#pragma once
#include <iostream>
#include <memory>
#include <pd/common.hpp>
#include <pd/core/common.hpp>
namespace PD {
template <typename T, typename Alloc = std::allocator<T>>
class Pool {
public:
using value_type = T;
using iterator = T*;
using const_iterator = const iterator*;
Pool() = default;
~Pool() {
if (pData) {
pAlloc.deallocate(pData, pCap);
pData = nullptr;
}
}
~Pool() = default;
static Pool* Create(size_t size) { return new Pool(size); }
void Init(size_t size) {
pPos = 0;
pCap = size;
pData = pAlloc.allocate(size);
for (size_t i = 0; i < pCap; i++) {
std::allocator_traits<Alloc>::construct(pAlloc, &pData[i]);
}
PDLOG("Pool::Init({})", size);
pPool.resize(size);
}
Pool(const Pool&) = delete;
@@ -38,7 +24,7 @@ class Pool {
T* Allocate(size_t num = 1) {
if (CheckLimits(num)) return nullptr;
T* ret = &pData[pPos];
T* ret = &pPool[pPos];
pPos += num;
return ret;
}
@@ -54,29 +40,12 @@ class Pool {
return false;
}
void Reset() {
for (size_t i = 0; i < pCap; i++) {
std::allocator_traits<Alloc>::destroy(pAlloc, &pData[i]);
}
pPos = 0;
for (size_t i = 0; i < pCap; i++) {
std::allocator_traits<Alloc>::construct(pAlloc, &pData[i]);
}
}
size_t size() const { return pPos; }
size_t capacity() const { return pCap; }
T& at(size_t idx) { return pData[idx]; }
const T& at(size_t idx) const { return pData[idx]; }
T& operator[](size_t idx) { return at(idx); }
const T& operator[](size_t idx) const { return at(idx); }
void Reset() { pPos = 0; }
private:
Pool(size_t size) : pCap(size), pPos(0) { pData = pAlloc.allocate(size); }
Pool(size_t size) : pCap(size), pPos(0) { pPool.resize(size); }
size_t pCap = 0;
size_t pPos = 0;
Alloc pAlloc;
T* pData = nullptr;
std::vector<T, Alloc> pPool;
};
} // namespace PD

View File

@@ -23,7 +23,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <pd/common.hpp>
#include <pd/core/common.hpp>
namespace PD {
/**
@@ -92,7 +92,9 @@ PD_API const std::string PathRemoveExtension(const std::string& path);
*/
template <typename T>
inline const std::string ToHex(const T& v) {
return std::format("{0:0{1}X}", v, sizeof(v) * 2);
std::stringstream s;
s << "0x" << std::setfill('0') << std::setw(sizeof(v) * 2) << std::hex << v;
return s.str();
}
/**
* Generate a Hash out of a string
@@ -105,22 +107,24 @@ PD_API u32 FastHash(const std::string& s);
* Based on their Macros
* @return CompilerName: Version
*/
inline const char* GetCompilerVersion() {
/// As the function looks like this Project is meant to
/// Be ported to other systems as well
#define __mks(x) #x
#define mkstring(x) __mks(x)
inline const std::string GetCompilerVersion() {
/// As the function looks like this Project is meant to
/// Be ported to other systems as well
std::stringstream res;
#ifdef __clang__ // Check clang first
return "Clang: " mkstring(__clang_major__) "." mkstring(
__clang_minor__) "." mkstring(__clang_patchlevel__);
res << "Clang: " << __clang_major__ << ".";
res << __clang_minor__ << ".";
res << __clang_patchlevel__;
#elif __GNUC__
return "GCC: " mkstring(__GNUC__) "." mkstring(__GNUC_MINOR__) "." mkstring(
__GNUC_PATCHLEVEL__);
res << "GCC: " << __GNUC__;
res << "." << __GNUC_MINOR__ << ".";
res << __GNUC_PATCHLEVEL__;
#elif _MSC_VER
return "MSVC; " mkstring(_MSC_VER);
res << "MSVC: " << _MSC_VER;
#else
return "Unknown Compiler";
res << "Unknown Compiler";
#endif
return res.str();
}
} // namespace Strings
class U8Iterator {

View File

@@ -23,9 +23,10 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <pd/common.hpp>
#include <pd/core/common.hpp>
namespace PD {
class OsDriver;
/**
* Timer class
*/
@@ -35,12 +36,14 @@ class PD_API Timer {
* Constructor
* @param auto_start [default true] sets if timer should start after creation
*/
Timer(bool auto_start = true);
Timer(OsDriver& os, bool auto_start = true);
/**
* Unused Deconstructor
*/
~Timer() {}
PD_SHARED(Timer);
/**
* Resume Timer if Paused
*/
@@ -79,5 +82,7 @@ class PD_API Timer {
u64 pNow;
/** Is Running */
bool pIsRunning = false;
/** Os Driver reference */
OsDriver& pOs;
};
} // namespace PD

View File

@@ -23,9 +23,10 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <pd/common.hpp>
#include <pd/core/common.hpp>
namespace PD {
class OsDriver;
/**
* Class to calculate Maximum/Minimum and Average Timings
*/
@@ -38,6 +39,8 @@ class TimeStats {
TimeStats(int l) : len(l), val(l, 0) {}
~TimeStats() = default;
PD_SHARED(TimeStats);
/**
* Add a New Value to the list
* @param v value to add
@@ -149,9 +152,11 @@ namespace TT {
class Res {
public:
/** Constructore that Inits a protocol at size of 60 frames */
Res() : start(0), end(0), protocol(60) {}
Res() : start(0), end(0) { protocol = TimeStats::New(60); }
~Res() = default;
PD_SHARED(Res);
/**
* Setter for the ID (Name)
* @param v ID of the Trace
@@ -179,7 +184,7 @@ class Res {
void SetEnd(u64 v) {
end = v;
diff = end - start;
protocol.Add(GetLastDiff());
protocol->Add(GetLastDiff());
}
/**
* Getter for the End Time
@@ -196,7 +201,7 @@ class Res {
* Get Protcol Reference
* @return Protocol Ref
*/
TimeStats& GetProtocol() { return protocol; }
TimeStats::Ref GetProtocol() { return protocol; }
private:
/** Trace ID */
@@ -208,18 +213,18 @@ class Res {
/** Last Diff */
u64 diff;
/** Protocol */
TimeStats protocol;
TimeStats::Ref protocol;
};
/**
* Begin a Trace
* @param id Name of the Trace
*/
PD_API void Beg(const std::string& id);
PD_API void Beg(OsDriver& os, const std::string& id);
/**
* End a Trace
* @param id Name of the Trace
*/
PD_API void End(const std::string& id);
PD_API void End(OsDriver& os, const std::string& id);
/**
* Collect Start end end of the trace by tracking
* when the Scope object goes out of scope
@@ -241,18 +246,20 @@ class Scope {
* Constructor requiring a Name for the Trace
* @param id Name of the Trace
*/
Scope(const std::string& id) {
Scope(OsDriver& os, const std::string& id) : pOs(os) {
this->ID = id;
Beg(id);
Beg(pOs, id);
}
/**
* Deconstructor getting the end time when going out of scope
*/
~Scope() { End(ID); }
~Scope() { End(pOs, ID); }
private:
/** Trace Name/ID */
std::string ID;
/** Os Driver Reference */
OsDriver& pOs;
};
} // namespace TT
} // namespace PD

View File

@@ -23,7 +23,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <pd/common.hpp>
#include <pd/core/common.hpp>
#ifndef M_PI
#define M_PI 3.14159265358979323846

View File

@@ -1,6 +1,6 @@
#pragma once
#include <pd/common.hpp>
#include <pd/core/common.hpp>
namespace PD {
/**

View File

@@ -23,7 +23,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <pd/common.hpp>
#include <pd/core/common.hpp>
#include <pd/core/vec2.hpp>
#include <pd/core/vec3.hpp>
#include <pd/core/vec4.hpp>

View File

@@ -25,7 +25,7 @@ SOFTWARE.
// This file is generated by lazyvec 2.0.0
#include <pd/common.hpp>
#include <pd/core/common.hpp>
namespace PD {
template <typename T>

View File

@@ -25,7 +25,7 @@ SOFTWARE.
// This file is generated by lazyvec 2.0.0
#include <pd/common.hpp>
#include <pd/core/common.hpp>
// Extended includes (rename if you use other filenames/paths)
#include <pd/core/vec2.hpp>

View File

@@ -25,7 +25,7 @@ SOFTWARE.
// This file is generated by lazyvec 2.0.0
#include <pd/common.hpp>
#include <pd/core/common.hpp>
// Extended includes (rename if you use other filenames/paths)
#include <pd/core/vec2.hpp>
#include <pd/core/vec3.hpp>

View File

@@ -0,0 +1,79 @@
#pragma once
/*
MIT License
Copyright (c) 2024 - 2026 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <pd/core/core.hpp>
#include <pd/drivers/gfx.hpp>
#include <pd/drivers/hid.hpp>
#include <pd/drivers/os.hpp>
namespace PD {
class PD_API Context {
public:
Context()
: pOs(OsDriver::New()), pGfx(GfxDriver::New()), pHid(HidDriver::New()) {}
~Context() {}
PD_RAW(Context);
static Context::Ref Create();
template <typename Driver>
void UseGfxDriver(PDDriverData data) {
static_assert(std::is_base_of<GfxDriver, Driver>::value,
"Driver must extend GfxDriver");
pGfx.reset();
pGfx = Driver::New(data);
}
template <typename Driver>
void UseHidDriver(PDDriverData data) {
static_assert(std::is_base_of<HidDriver, Driver>::value,
"Driver must extend HidDriver");
pHid.reset();
pHid = Driver::New(data);
}
template <typename Driver>
void UseOsDriver(PDDriverData data) {
static_assert(std::is_base_of<OsDriver, Driver>::value,
"Driver must extend OsDriver");
pOs.reset();
pOs = Driver::New(data);
}
PD::Li::Texture::Ref GetSolidTex();
OsDriver::Ref Os() { return pOs; }
GfxDriver::Ref Gfx() { return pGfx; }
HidDriver::Ref Hid() { return pHid; }
private:
OsDriver::Ref pOs = nullptr;
GfxDriver::Ref pGfx = nullptr;
HidDriver::Ref pHid = nullptr;
PD::Li::Texture::Ref pSolidTex = nullptr;
};
} // namespace PD

View File

@@ -1,5 +1,29 @@
#pragma once
#include <pd/drivers/formatters.hpp>
/*
MIT License
Copyright (c) 2024 - 2026 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <pd/drivers/context.hpp>
#include <pd/drivers/gfx.hpp>
#include <pd/drivers/hid.hpp>
#include <pd/drivers/os.hpp>

View File

@@ -1,44 +0,0 @@
#pragma once
#include <pd/drivers/gfx.hpp>
template <>
struct std::formatter<PD::TextureFilter> {
constexpr auto parse(std::format_parse_context& ctx) { return ctx.begin(); }
template <typename FormatContext>
auto format(const PD::TextureFilter& value, FormatContext& ctx) const {
std::string_view ret = "Unknown";
switch (value) {
case PD::TextureFilter::Linear:
ret = "Linear";
break;
case PD::TextureFilter::Nearest:
ret = "Nearest";
break;
}
return std::format_to(ctx.out(), "{}", ret);
}
};
template <>
struct std::formatter<PD::TextureFormat> {
constexpr auto parse(std::format_parse_context& ctx) { return ctx.begin(); }
template <typename FormatContext>
auto format(const PD::TextureFormat& value, FormatContext& ctx) const {
std::string_view ret = "Unknown";
switch (value) {
case PD::TextureFormat::RGBA32:
ret = "RGBA32";
break;
case PD::TextureFormat::RGB24:
ret = "RGB24";
break;
case PD::TextureFormat::A8:
ret = "A8";
break;
}
return std::format_to(ctx.out(), "{}", ret);
}
};

View File

@@ -1,166 +1,119 @@
#pragma once
#include <pd/core/mat.hpp>
#include <pd/drivers/interface.hpp>
/*
MIT License
Copyright (c) 2024 - 2026 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <pd/core/core.hpp>
#include <pd/drivers/types.hpp>
#include <pd/lithium/command.hpp>
#include <pd/lithium/texture.hpp>
using LiBackendFlags = PD::u32;
enum LiBackendFlags_ {
LiBackendFlags_None = 0,
LiBackendFlags_FlipUV_Y = 1 << 0, // Essential for font loading
LiBackendFlags_FlipUV_Y = PD_BIT(0), // Essential for font loading
};
namespace PD {
using TextureID = ptr;
enum class TextureFilter {
Linear,
Nearest,
};
enum class TextureFormat {
RGBA32,
RGB24,
A8,
};
// Pre interface class
class PD_API GfxDriver : public DriverInterface {
// The backend api nobody asked for
class GfxDriver2 {
public:
GfxDriver(std::string_view name);
virtual ~GfxDriver() = default;
GfxDriver2(const std::string& name = "NullGfx") : pName(name) {};
~GfxDriver2() = default;
virtual void Init() {}
virtual void Deinit() { SysDeinit(); }
void SetViewPort(const ivec2& size);
void SetViewPort(int x, int y);
virtual void BindTexture(TextureID id) {}
void Reset();
virtual TextureID LoadTexture(const std::vector<PD::u8>& pixels, int w, int h,
TextureFormat type = TextureFormat::RGBA32,
TextureFilter filter = TextureFilter::Linear) {
virtual void Deinit() {}
virtual void Draw(const PD::Li::CmdPool& pool) {}
virtual void TexBind(PD::Li::TexAddress addr) {}
virtual PD::Li::TexAddress TexLoad(
const std::vector<PD::u8>& pixels, int w, int h,
PD::Li::Texture::Type type = PD::Li::Texture::Type::RGBA32,
PD::Li::Texture::Filter filter = PD::Li::Texture::Filter::LINEAR) {
return 0;
}
virtual void DeleteTexture(const TextureID& tex) {}
virtual void Draw(const Pool<Li::Command>& commands) {}
virtual void TexDelete(PD::Li::TexAddress tex) {}
protected:
virtual void SysDeinit() {}
virtual void SysInit() {}
virtual void SysReset() {}
virtual void Submit(size_t count, size_t start) {}
virtual void ResetPools() = 0;
// Counters
size_t CountDrawcalls = 0;
size_t CountCommands = 0;
size_t CountVertices = 0;
size_t CountIndices = 0;
const std::string pName;
LiBackendFlags pFlags = 0;
size_t CurrentIndex = 0;
size_t CurrentVertex = 0;
TextureID CurrentTex = 0;
Mat4 Projection;
ivec2 ViewPort;
};
struct DefaultGfxConfig {
// Vertex Allocator
template <typename T>
using VertexAlloc = std::allocator<T>;
// Index Allocator
template <typename T>
using IndexAlloc = std::allocator<T>;
static constexpr size_t NumVertices = 32768; // 8192*4
static constexpr size_t NumIndices = 49152; // 8192*6
};
template <typename Config = DefaultGfxConfig>
class GfxDriverBase : public GfxDriver {
class GfxDriver {
public:
using VtxPool =
Pool<Li::Vertex, typename Config::template VertexAlloc<Li::Vertex>>;
using IdxPool = Pool<u16, typename Config::template VertexAlloc<u16>>;
GfxDriverBase(std::string_view name = "Default") : GfxDriver(name) {}
virtual ~GfxDriverBase() {}
GfxDriver(const std::string& name = "NullGfx") : pName(name) {}
GfxDriver(PDDriverData data) : pName("NullGfx") {}
~GfxDriver() = default;
void Init() override {
pVtxPool.Init(Config::NumVertices);
pIdxPool.Init(Config::NumIndices);
SysInit();
PD_SHARED(GfxDriver);
void PostInit();
virtual void Init() {}
virtual void Deinit() {}
virtual void NewFrame() {}
virtual void BindTex(Li::TexAddress addr) {}
virtual void RenderDrawData(const Li::CmdPool& Commands) {}
void SetViewPort(const ivec2& vp) { ViewPort = vp; }
void SetViewPort(int w, int h) { ViewPort = PD::ivec2(w, h); }
virtual Li::Texture::Ref LoadTex(
const std::vector<u8>& pixels, int w, int h,
Li::Texture::Type type = Li::Texture::Type::RGBA32,
Li::Texture::Filter filter = Li::Texture::Filter::LINEAR) {
// Texture loading not supported (when this func not get override)
return nullptr;
}
void Draw(const Pool<Li::Command>& commands) override {
CountCommands += commands.size();
size_t index = 0;
while (index < commands.size()) {
CurrentTex = commands[index].Tex;
if (!CurrentTex) {
index++;
continue;
}
size_t startidx = CurrentIndex;
while (index < commands.size() && CurrentTex == commands[index].Tex) {
const auto& c = commands[index];
CountVertices += c.VertexCount;
CountIndices += c.IndexCount;
auto pIdx = pIdxPool.Allocate(c.IndexCount);
auto pVtx = pVtxPool.Allocate(c.VertexCount);
for (size_t i = 0; i < c.IndexCount; i++) {
pIdx[i] = CurrentVertex + c.FirstIndex[i];
}
CurrentIndex += c.IndexCount;
CurrentVertex += c.VertexCount;
for (size_t i = 0; i < c.VertexCount; i++) {
pVtx[i] = c.FirstVertex[i];
}
index++;
}
Submit(CurrentIndex - startidx, startidx);
}
}
virtual void DestroyTex(PD::Li::Texture::Ref tex) {}
protected:
u16* GetIndexBufPtr(size_t start) { return &pIdxPool[start]; }
Li::Vertex* GetVertexBufPtr(size_t start) { return &pVtxPool[start]; }
void ResetPools() override {
pVtxPool.Reset();
pIdxPool.Reset();
}
Li::Texture::Ref GetSolidTex() { return pSolid; }
private:
VtxPool pVtxPool;
IdxPool pIdxPool;
};
const std::string& GetName() const { return pName; }
class PD_API Gfx {
public:
Gfx() = default;
~Gfx() = default;
const std::string pName = "NullGfx";
LiBackendFlags Flags = 0;
ivec2 ViewPort;
Mat4 Projection;
Li::Texture::Ref pSolid;
size_t CurrentVertex = 0;
size_t CurrentIndex = 0;
template <typename T, typename... Args>
static void UseDriver(Args&&... args) {
// assert(driver == nullptr && "OS Driver already set");
driver = std::make_unique<T>(std::forward<Args>(args)...);
}
/** Debug Variables */
static void Init() { driver->Init(); }
static void Deinit() { driver->Deinit(); }
static void SetViewPort(const ivec2& vp) { driver->SetViewPort(vp); }
static void SetViewPort(int w, int h) { driver->SetViewPort(w, h); }
static void Reset() { driver->Reset(); }
static void Draw(const Pool<Li::Command>& commands) {
driver->Draw(commands);
}
static TextureID LoadTexture(const std::vector<PD::u8>& pixels, int w, int h,
TextureFormat type = TextureFormat::RGBA32,
TextureFilter filter = TextureFilter::Linear) {
return driver->LoadTexture(pixels, w, h, type, filter);
}
static const char* GetDriverName() { return driver->GetName(); }
private:
static std::unique_ptr<GfxDriver> driver;
// Optional Index counter
u32 IndexCounter;
// Optional Vertex counter
u32 VertexCounter;
// Optional Frame Counter
u64 FrameCounter;
// Draw calls counter
u32 DrawCalls;
// Command Counter
u32 DrawCommands;
};
} // namespace PD

267
include/pd/drivers/hid.hpp Executable file
View File

@@ -0,0 +1,267 @@
#pragma once
/*
MIT License
Copyright (c) 2024 - 2026 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPHidE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <pd/core/core.hpp>
#include <pd/drivers/types.hpp>
namespace PD {
/** Did not found a better solution yet sadly */
namespace HidKb {
// Lets use u128 here
using KbKey = u128;
constexpr static KbKey Kb_No = 0;
constexpr static KbKey Kb_Escape = KbKey::Flag(0);
constexpr static KbKey Kb_Q = KbKey::Flag(1);
constexpr static KbKey Kb_W = KbKey::Flag(2);
constexpr static KbKey Kb_E = KbKey::Flag(3);
constexpr static KbKey Kb_R = KbKey::Flag(4);
constexpr static KbKey Kb_T = KbKey::Flag(5);
constexpr static KbKey Kb_Z = KbKey::Flag(6);
constexpr static KbKey Kb_U = KbKey::Flag(7);
constexpr static KbKey Kb_I = KbKey::Flag(8);
constexpr static KbKey Kb_O = KbKey::Flag(9);
constexpr static KbKey Kb_P = KbKey::Flag(10);
constexpr static KbKey Kb_A = KbKey::Flag(11);
constexpr static KbKey Kb_S = KbKey::Flag(12);
constexpr static KbKey Kb_D = KbKey::Flag(13);
constexpr static KbKey Kb_F = KbKey::Flag(14);
constexpr static KbKey Kb_G = KbKey::Flag(15);
constexpr static KbKey Kb_H = KbKey::Flag(16);
constexpr static KbKey Kb_J = KbKey::Flag(17);
constexpr static KbKey Kb_K = KbKey::Flag(18);
constexpr static KbKey Kb_L = KbKey::Flag(19);
constexpr static KbKey Kb_Y = KbKey::Flag(20);
constexpr static KbKey Kb_X = KbKey::Flag(21);
constexpr static KbKey Kb_C = KbKey::Flag(22);
constexpr static KbKey Kb_V = KbKey::Flag(23);
constexpr static KbKey Kb_B = KbKey::Flag(24);
constexpr static KbKey Kb_N = KbKey::Flag(25);
constexpr static KbKey Kb_M = KbKey::Flag(26);
constexpr static KbKey Kb_1 = KbKey::Flag(27);
constexpr static KbKey Kb_2 = KbKey::Flag(28);
constexpr static KbKey Kb_3 = KbKey::Flag(29);
constexpr static KbKey Kb_4 = KbKey::Flag(30);
constexpr static KbKey Kb_5 = KbKey::Flag(31);
constexpr static KbKey Kb_6 = KbKey::Flag(32);
constexpr static KbKey Kb_7 = KbKey::Flag(33);
constexpr static KbKey Kb_8 = KbKey::Flag(34);
constexpr static KbKey Kb_9 = KbKey::Flag(35);
constexpr static KbKey Kb_0 = KbKey::Flag(36);
constexpr static KbKey Kb_F1 = KbKey::Flag(37);
constexpr static KbKey Kb_F2 = KbKey::Flag(38);
constexpr static KbKey Kb_F3 = KbKey::Flag(39);
constexpr static KbKey Kb_F4 = KbKey::Flag(40);
constexpr static KbKey Kb_F5 = KbKey::Flag(41);
constexpr static KbKey Kb_F6 = KbKey::Flag(42);
constexpr static KbKey Kb_F7 = KbKey::Flag(43);
constexpr static KbKey Kb_F8 = KbKey::Flag(44);
constexpr static KbKey Kb_F9 = KbKey::Flag(45);
constexpr static KbKey Kb_F10 = KbKey::Flag(46);
constexpr static KbKey Kb_F11 = KbKey::Flag(47);
constexpr static KbKey Kb_F12 = KbKey::Flag(48);
constexpr static KbKey Kb_MouseLeft = KbKey::Flag(120);
} // namespace HidKb
class PD_API HidDriver {
public:
enum Flags : u32 {
Flags_None = 0,
FLags_HasGamepad = 1 << 0,
Flags_HasKeyboard = 1 << 1,
Flags_HasTouch = 1 << 2,
Flags_HasMouse = 1 << 3,
};
// Todo: Name to GpKey (GamepadKey)
/** Key [Controller] */
enum Key : u32 {
No = 0, ///< No Key
A = 1 << 0, ///< A
B = 1 << 1, ///< B
X = 1 << 2, ///< X
Y = 1 << 3, ///< Y
Start = 1 << 4, ///< Start
Select = 1 << 5, ///< Select
L = 1 << 6, ///< L
R = 1 << 7, ///< R
DUp = 1 << 8, ///< Dpad Up
DDown = 1 << 9, ///< Dpad down
DLeft = 1 << 10, ///< Dpad left
DRight = 1 << 11, ///< Dpad right
CPUp = 1 << 12, ///< Cpad up
CPDown = 1 << 13, ///< cpad down
CPLeft = 1 << 14, ///< cpad left
CPRight = 1 << 15, ///< Cpad right
CSUp = 1 << 16, ///< Cstick up
CSDown = 1 << 17, ///< cstick down
CSLeft = 1 << 18, ///< cstick left
CSRight = 1 << 19, ///< cstick right
ZL = 1 << 20, ///< ZL
ZR = 1 << 21, ///< ZR
Touch = 1 << 22, ///< Touch
Up = DUp | CPUp, ///< DPad or CPad Up
Down = DDown | CPDown, ///< DPad or CPad Down
Left = DLeft | CPLeft, ///< DPad or CPad Left
Right = DRight | CPRight, ///< DPad or CPad Right
};
using KbKey = HidKb::KbKey;
/** Event */
enum Event {
Event_Null,
Event_Down, ///< Key Pressed
Event_Held, ///< Key Held
Event_Up, ///< Key released
};
HidDriver(const std::string& name = "NullHid") : pName(name) {}
HidDriver(PDDriverData data) : pName("NullHid") {}
virtual ~HidDriver() = default;
PD_SHARED(HidDriver);
/**
* Get Mouse Position
* @return Mouse pos
*/
fvec2 MousePos() const {
return pMouse[0];
} /**
* Get Last Mouse Position (from last frame)
* @return Mouse pos
*/
fvec2 MousePosLast() const { return pMouse[1]; }
/**
* Check for a Button Event
* @param e Event Type
* @param keys Keys to check for
* @return if key(s) doing the requiested event
*/
bool IsEvent(Event e, Key keys);
bool IsEvent(Event e, KbKey key);
/**
* Check for Key Press Event
* @param keys set of keys
* @return true if key is pressed
*/
bool IsDown(Key keys) const { return KeyEvents[0].at(Event_Down) & keys; }
/**
* Check for Key Held Event
* @param keys set of keys
* @return true if key is held
*/
bool IsHeld(Key keys) const { return KeyEvents[0].at(Event_Held) & keys; }
/**
* Check for Key Release Event
* @param keys set of keys
* @return true if key is released
*/
bool IsUp(Key keys) const { return KeyEvents[0].at(Event_Up) & keys; }
/**
* Sett all keyevents to 0
*/
void Clear() {
for (int i = 0; i < 2; i++) {
KeyEvents[i][Event_Down] = 0;
KeyEvents[i][Event_Up] = 0;
KeyEvents[i][Event_Held] = 0;
}
}
/**
* Lock input driver
* @param v lock or not lock
*/
void Lock(bool v) {
if (v != pLocked) {
SwapTab();
}
pLocked = v;
}
/**
* Check if Driver is locked
* @return true if locked
*/
bool Locked() const { return pLocked; }
/**
* Lock Input Driver
*/
void Lock() {
if (!pLocked) {
SwapTab();
}
pLocked = true;
}
/**
* Unlock Input Driver
*/
void Unlock() {
if (pLocked) {
SwapTab();
}
pLocked = false;
}
/**
* Template Update Function for a device specific driver
*/
virtual void Update();
/**
* Get Text from Keyboard
*/
virtual void GetInputStr(std::string& str) {}
const std::string& GetName() const { return pName; }
/** Data Section */
/** Backend Identification Name */
const std::string pName;
/** Flags */
u32 Flags = 0;
/** Key Binds Map */
std::unordered_map<u32, u32> pBinds;
std::unordered_map<u128, u128> pKbBinds;
/** Swap Tabe Function */
void SwapTab();
/** Using 2 Positions for Current and Last */
fvec2 pMouse[2];
/** Lock State */
bool pLocked = false;
/** Key Event Table Setup */
std::unordered_map<Event, u32> KeyEvents[2];
/** Keyboard Key Event Table Setup */
std::unordered_map<Event, u128> KbKeyEvents[2];
};
namespace Hid {
using Event = HidDriver::Event;
using Key = HidDriver::Key;
} // namespace Hid
} // namespace PD

View File

@@ -1,16 +0,0 @@
#pragma once
#include <pd/common.hpp>
namespace PD {
class DriverInterface {
public:
DriverInterface(const std::string_view& name) : pName(name) {}
virtual ~DriverInterface() {}
const char* GetName() const { return pName.data(); }
private:
std::string_view pName;
};
} // namespace PD

View File

@@ -1,32 +1,53 @@
#pragma once
#include <pd/drivers/interface.hpp>
/*
MIT License
Copyright (c) 2024 - 2026 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <pd/core/common.hpp>
#include <pd/core/timetrace.hpp>
#include <pd/drivers/types.hpp>
#include <pd/pd_p_api.hpp>
namespace PD {
class PD_API OsDriver : public DriverInterface {
using TraceMap = std::map<std::string, TT::Res::Ref>;
class PD_API OsDriver {
public:
OsDriver(std::string_view name = "Default") : DriverInterface(name) {}
virtual ~OsDriver() {}
OsDriver(const std::string& name = "StdPd") : pName(name) {}
OsDriver(PDDriverData data) : pName("StdPd") {}
virtual ~OsDriver() = default;
PD_SHARED(OsDriver);
virtual u64 GetTime() const;
virtual u64 GetTimeNano() const;
};
virtual u64 GetTime();
virtual u64 GetNanoTime();
TraceMap& GetTraceMap();
TT::Res::Ref& GetTraceRef(const std::string& id);
bool TraceExist(const std::string& id);
const std::string& GetName() const { return pName; }
class PD_API Os {
public:
Os() = default;
~Os() = default;
template <typename T, typename... Args>
static void UseDriver(Args&&... args) {
// assert(driver == nullptr && "OS Driver already set");
driver = std::make_unique<T>(std::forward<Args>(args)...);
}
static u64 GetTime() { return driver->GetTime(); }
static u64 GetTimeNano() { return driver->GetTimeNano(); }
TraceMap pTraces;
private:
static std::unique_ptr<OsDriver> driver;
const std::string pName = "StdPd";
};
} // namespace PD

View File

@@ -0,0 +1,45 @@
#pragma once
/*
MIT License
Copyright (c) 2024 - 2026 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <pd/core/core.hpp>
namespace PD {
class Snd {
public:
Snd(const std::string& name = "NullSnd") : pName(name) {};
~Snd() = default;
PD_SHARED(Snd);
virtual void Init() {}
virtual void Deinit() {}
virtual bool Done(int buf_idx) {}
virtual void Update(int buf_idx, void* data) {}
virtual bool IsChannelPlaying(int chn) {}
virtual bool IsChannelPaused(int chn) {}
const std::string pName = "NullSnd";
};
} // namespace PD

View File

@@ -0,0 +1,3 @@
#pragma once
using PDDriverData = void*;

24962
include/pd/external/json.hpp vendored Executable file

File diff suppressed because it is too large Load Diff

7988
include/pd/external/stb_image.hpp vendored Executable file

File diff suppressed because it is too large Load Diff

5079
include/pd/external/stb_truetype.hpp vendored Executable file

File diff suppressed because it is too large Load Diff

88
include/pd/image/image.hpp Executable file
View File

@@ -0,0 +1,88 @@
#pragma once
/*
MIT License
Copyright (c) 2024 - 2026 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <pd/core/core.hpp>
#include <pd/pd_p_api.hpp>
namespace PD {
class PD_API Image {
public:
enum Format {
RGBA, // bpp == 4
RGB, // bpp == 3
RGB565, // bpp == 2 (not supported in laoding)
BGR, // bpp == 3
ABGR, // bpp == 4
BGRA, // bpp == 4
};
Image() = default;
Image(const std::string& path) { this->Load(path); }
Image(const std::vector<u8>& buf) { this->Load(buf); }
Image(const std::vector<u8>& buf, int w, int h, int bpp = 4) {
this->Copy(buf, w, h, bpp);
}
~Image() = default;
PD_SHARED(Image)
void Load(const std::string& path);
void Load(const std::vector<u8>& buf);
void Copy(const std::vector<u8>& buf, int w, int h, int bpp = 4);
std::vector<PD::u8>& GetBuffer() { return pBuffer; }
std::vector<PD::u8> GetBuffer() const { return pBuffer; }
int Width() const { return pWidth; }
int Height() const { return pHeight; }
ivec2 Size() const { return ivec2(pWidth, pHeight); }
Format Fmt() const { return pFmt; }
void FlipVertical();
void FlipHorizontal();
u8& operator[](int idx) { return pBuffer[idx]; }
u8 operator[](int idx) const { return pBuffer[idx]; }
// Probably these make th eabove ones useless
operator std::vector<PD::u8>&() { return pBuffer; }
operator std::vector<PD::u8>() const { return pBuffer; }
static void Convert(Image::Ref img, Image::Format dst);
static void ReTile(Image::Ref img,
std::function<u32(int x, int y, int w)> src,
std::function<u32(int x, int y, int w)> dst);
static int Fmt2Bpp(Format fmt);
std::vector<PD::u8> pBuffer;
int pWidth;
int pHeight;
Format pFmt = Format::RGBA;
private:
/** Leftover variable used for stbi_load */
int fmt = 0;
};
} // namespace PD

View File

@@ -0,0 +1,71 @@
#pragma once
/*
MIT License
Copyright (c) 2024 - 2026 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <pd/core/core.hpp>
#include <pd/pd_p_api.hpp>
namespace PD {
/**
* Namepace containing functions to blur images
*/
namespace ImgBlur {
/**
* Function to create Gaussian Kernel List
* @param radius Rasius to use
* @param si sigma value to use
* @return list of kernel values
*/
PD_API std::vector<float> GaussianKernel(int radius, float si);
/**
* Gaussian Blur for basic Image Buffer
* @param buf Image Buffer (unsigned char)
* @param w width of the image
* @param h width of the image
* @param radius Blur radius
* @param si Blur sigma
* @param idxfn Indexing function
*/
PD_API void GaussianBlur(
std::vector<u8>& buf, int w, int h, float radius, float si,
std::function<int(int, int, int)> idxfn = [](int x, int y, int w) -> int {
return y * w + x;
});
/**
* Advanced func to access memory directly
* @param buf Referenvce to the buffer
* @param w width of the image
* @param h width of the image
* @param bpp Bytes per Pixels (RGB[A], RGB565, etc)
* @param radius Blur radius
* @param si Blur sigma
* @param idxfn Indexing function
*/
PD_API void GaussianBlur(
void* buf, int w, int h, int bpp, float radius, float si,
std::function<int(int, int, int)> idxfn = [](int x, int y, int w) -> int {
return y * w + x;
});
} // namespace ImgBlur
} // namespace PD

View File

@@ -0,0 +1,58 @@
#pragma once
/*
MIT License
Copyright (c) 2024 - 2026 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <pd/core/core.hpp>
#include <pd/image/image.hpp>
#include <pd/pd_p_api.hpp>
namespace PD {
/**
* Namespace containing function to convert images
*/
namespace ImgConvert {
/**
* Convert RGB24 to RGBA32 by adding a 4th alpha value set to 255
* to every pixel
* @param out Result List
* @param in Input Buffer List (rgb24)
* @param w width of the image
* @param h height of the image
*/
PD_API
void RGB24toRGBA32(std::vector<PD::u8>& out, const std::vector<u8>& in,
const int& w, const int& h);
PD_API
void RGB32toRGBA24(std::vector<u8>& out, const std::vector<u8>& in,
const int& w, const int& h);
/**
* Reverse 32 (RGBA -> ABGR || ABGR -> RGBA)
* @param buf Buffer to convert
* @param w width
* @param h height
*/
PD_API void Reverse32(std::vector<u8>& buf, const int& w, const int& h);
PD_API void ReverseBuf(std::vector<u8>& buf, size_t bpp, int w, int h);
} // namespace ImgConvert
} // namespace PD

View File

@@ -1,61 +1,103 @@
#pragma once
#include <cstddef>
#include <pd/core/pool.hpp>
#include <pd/lithium/pools.hpp>
/*
MIT License
Copyright (c) 2024 - 2026 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <pd/core/core.hpp>
#include <pd/lithium/texture.hpp>
#include <pd/lithium/vertex.hpp>
#include <pd/pd_p_api.hpp>
namespace PD {
namespace Li {
class Command {
public:
Command() { Reset(); }
~Command() {}
Command() = default;
~Command() = default;
void Reserve(size_t vtx, size_t idx) {
if (!FirstVertex)
FirstVertex = AllocateVertices(vtx);
else
AllocateVertices(vtx);
if (!FirstIndex)
FirstIndex = AllocateIndices(idx);
else
AllocateIndices(idx);
PD_RAW(Command);
Command& AddIdx(const u16& idx) {
IndexBuffer.push_back(VertexBuffer.size() + idx);
return *this;
}
Command& AddIdxs(const u16& a, const u16& b, const u16& c) {
IndexBuffer.push_back(VertexBuffer.size() + a);
IndexBuffer.push_back(VertexBuffer.size() + b);
IndexBuffer.push_back(VertexBuffer.size() + c);
return *this;
}
void Reset() {
Command& AddVtx(const Vertex& v) {
VertexBuffer.push_back(std::move(v));
return *this;
}
void Clear() {
VertexBuffer.clear();
IndexBuffer.clear();
Index = 0;
Layer = 0;
Tex = 0;
FirstIndex = nullptr;
FirstVertex = nullptr;
IndexCount = 0;
VertexCount = 0;
}
Command& Add(const Vertex& vtx) {
FirstVertex[VertexCount++] = vtx;
return *this;
}
Command& Add(u16 idx) {
FirstIndex[IndexCount++] = VertexCount + idx;
return *this;
}
Command& Add(u16 a, u16 b, u16 c) {
FirstIndex[IndexCount++] = VertexCount + a;
FirstIndex[IndexCount++] = VertexCount + b;
FirstIndex[IndexCount++] = VertexCount + c;
return *this;
ScissorOn = false;
ScissorRect = ivec4();
}
std::vector<Vertex> VertexBuffer;
std::vector<u16> IndexBuffer;
ivec4 ScissorRect;
bool ScissorOn = false;
int Layer = 0;
int Index = 0;
TexAddress Tex;
};
class PD_API CmdPool {
public:
CmdPool() {}
~CmdPool() {}
Command::Ref NewCmd();
void Init(size_t initial_size);
void Deinit();
void Resize(size_t nulen);
void Reset();
Command::Ref GetCmd(size_t idx) const;
Command::Ref GetCmd(size_t idx);
size_t Size() const;
size_t Cap() const;
void Merge(CmdPool& p);
void Copy(CmdPool& p);
void Sort();
private:
static bool pTheOrder(const Command::Ref& a, const Command::Ref& b);
friend class DrawList;
Command::Ref* begin() { return &pPool[0]; }
Command::Ref* end() { return &pPool[pPoolIdx - 1]; }
std::vector<Command::Ref> pPool;
u32 pPoolIdx = 0;
int Layer = 0;
ptr Tex = 0;
Vertex* FirstVertex = nullptr;
u16* FirstIndex = nullptr;
size_t VertexCount = 0;
size_t IndexCount = 0;
// Todo: implement
size_t VertexCountMax = 0;
size_t IndexCountMax = 0;
};
} // namespace Li
} // namespace PD

224
include/pd/lithium/drawlist.hpp Executable file
View File

@@ -0,0 +1,224 @@
#pragma once
/*
MIT License
Copyright (c) 2024 - 2026 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <pd/lithium/command.hpp>
#include <pd/lithium/font.hpp>
#include <pd/pd_p_api.hpp>
/** Path Rect Flags */
using LiPathRectFlags = PD::u32;
/** Setup for everything (oder so) */
enum LiPathRectFlags_ : PD::u32 {
LiPathRectFlags_None = 0,
LiPathRectFlags_KeepTopLeft = PD_BIT(0),
LiPathRectFlags_KeepTopRight = PD_BIT(1),
LiPathRectFlags_KeepBotRight = PD_BIT(2),
LiPathRectFlags_KeepBotLeft = PD_BIT(3),
LiPathRectFlags_KeepTop = PD_BIT(0) | PD_BIT(1),
LiPathRectFlags_KeepBot = PD_BIT(2) | PD_BIT(3),
LiPathRectFlags_KeepLeft = PD_BIT(0) | PD_BIT(3),
LiPathRectFlags_KeepRight = PD_BIT(1) | PD_BIT(2),
};
namespace PD {
namespace Li {
class PD_API DrawList {
public:
DrawList(Context& ctx, int initial_size = 64);
~DrawList();
/** Require Copy and Move Constructors */
DrawList(const DrawList&) = delete;
DrawList& operator=(const DrawList&) = delete;
DrawList(DrawList&&) noexcept = default;
DrawList& operator=(DrawList&&) noexcept = default;
PD_SHARED(DrawList);
/**
* Append an input drawlist on top of this one
* This Function will clear the Input list to make sure
* That the moved memory blocks don't get used
* @param list DrawList to move into current
*/
void Merge(DrawList::Ref list);
/**
* Copy another drawlist to this drawist.
* This is important for static prerendered Drawlists
* @param list DrawList Reference to copy from
*/
void Copy(DrawList::Ref list);
/**
* Optimize a Drawlist to a more or less perfect order
* to reduce drawcall overhead... This function also uses
* the Layersystem to keep specific stuff in the correct order
*/
void Optimize();
Command::Ref GetNewCmd();
void Clear();
void Layer(int l) { this->pPool.Layer = l; }
int Layer() { return this->pPool.Layer; }
void LayerUp() { this->pPool.Layer++; }
void LayerDown() { this->pPool.Layer--; }
void SetFont(Font::Ref font) { pCurrentFont = font; }
void SetFontScale(float scale) { pFontScale = scale; }
void DrawSolid();
void DrawTexture(Texture::Ref tex);
// SECTION: Draw API //
void DrawRect(const fvec2& pos, const fvec2& size, u32 color,
int thickness = 1);
void DrawRectFilled(const fvec2& pos, const fvec2& size, u32 color);
void DrawTriangle(const fvec2& a, const fvec2& b, const fvec2& c, u32 color,
int thickness = 1);
void DrawTriangleFilled(const fvec2& a, const fvec2& b, const fvec2& c,
u32 color);
void DrawCircle(const fvec2& center, float rad, u32 color, int num_segments,
int thickness = 1);
void DrawCircleFilled(const fvec2& center, float rad, u32 color,
int num_segments);
void DrawText(const fvec2& p, const std::string& text, u32 color);
/**
* Extended Draw Text Function
*/
void DrawTextEx(const fvec2& p, const std::string& text, u32 color,
LiTextFlags flags, const fvec2& box = fvec2(0.f));
void DrawLine(const fvec2& a, const fvec2& b, u32 color, int t = 1);
/**
* Take list of points and display it as a line on screen
* @param points List of Positions
* @param clr Color of the Line
* @param flags Additional Flags (Close for go back to starting point)
* @param thickness Thickness of the Line
*/
void DrawPolyLine(const std::vector<fvec2>& points, u32 clr, u32 flags = 0,
int thickness = 1);
/**
* Take a List ofpoints and display it as Filled Shape
* @note Keep in mind to setup the list of points clockwise
* @param points List of Points
* @param clr Color of the shape
*/
void DrawConvexPolyFilled(const std::vector<fvec2>& points, u32 clr);
// SECTION: PATH API //
/**
* Function to reserve Memory to prevent overhead on
* pusing a lot of points with PathNext
* @param num_points Number of Positions you want to add
*/
void PathReserve(size_t num_points) {
pPath.reserve(pPath.size() + num_points);
}
/**
* Clear current Path
* @note PathStroke and PathFill will automatically clear
*/
void PathClear() { pPath.clear(); }
/**
* Add a Point to the Path
* @note Keep in mind that this function is used for
* setting the starting point
* @param v Position to add
*/
void PathAdd(const fvec2& v) { pPath.push_back(v); }
/**
* Add a Point to the Path
* @note Keep in mind that this function is used for
* setting the starting point
* @param x X Position to add
* @param y Y Position to add
*/
void PathAdd(float x, float y) { pPath.push_back(fvec2(x, y)); }
/**
* Path Stroke Create Line from point to point
* @note For Primitives like Rect or Triangle mak sure to use
* UI7DrawFlags_Close to add a line back to the starting point
* @param clr Color od the line
* @param thickness Thickness of the line
* @param flags Additional Drawflags
*/
void PathStroke(u32 clr, int thickness = 1, u32 flags = 0) {
DrawPolyLine(pPath, clr, flags, thickness);
pPath.clear();
}
/**
* Fill a Path with a Color
* @note **IMPORTANT: ** Paths need to be setup clockwise
* to be rendered correctly
* @param clr Fill Color
*/
void PathFill(u32 clr) {
DrawConvexPolyFilled(pPath, clr);
pPath.clear();
}
void PathArcToN(const fvec2& c, float radius, float a_min, float a_max,
int segments);
void PathFastArcToN(const fvec2& c, float r, float amin, float amax, int s);
/// @brief Create a Path Rect (uses to Positions instead of Pos/Size)
/// @param a Top Left Position
/// @param b Bottom Right Position
/// @param rounding rounding
void PathRect(fvec2 a, fvec2 b, float rounding = 0.f);
/// @brief Create a Path Rect (uses to Positions instead of Pos/Size)
/// @param a Top Left Position
/// @param b Bottom Right Position
/// @param rounding rounding
/// @param flags DrawFlags (for special rounding rules)
void PathRectEx(fvec2 a, fvec2 b, float rounding = 0.f, u32 flags = 0);
void PushClipRect(const fvec4& cr) { pClipRects.push(cr); }
void PopClipRect() {
if (pClipRects.empty()) {
return;
}
pClipRects.pop();
}
const CmdPool& Data() const { return pPool; }
/** One linear Clip rect Setup */
void pClipCmd(Command* cmd);
/** Data Section */
std::stack<fvec4> pClipRects;
float pFontScale = 0.7f;
Font::Ref pCurrentFont;
Texture::Ref CurrentTex;
CmdPool pPool;
std::vector<fvec2> pPath;
u32 pNumIndices = 0;
u32 pNumVertices = 0;
Context* pCtx = nullptr;
};
} // namespace Li
} // namespace PD

136
include/pd/lithium/font.hpp Normal file
View File

@@ -0,0 +1,136 @@
#pragma once
/*
MIT License
Copyright (c) 2024 - 2026 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <pd/core/core.hpp>
#include <pd/lithium/command.hpp>
#include <pd/lithium/rect.hpp>
#include <pd/lithium/texture.hpp>
#include <pd/pd_p_api.hpp>
using LiTextFlags = PD::u32;
enum LiTextFlags_ {
LiTextFlags_None = 0, ///< Do nothing
LiTextFlags_AlignRight = 1 << 0, ///< Align Right of position
LiTextFlags_AlignMid = 1 << 1, ///< Align in the middle of pos and box
LiTextFlags_Shaddow = 1 << 2, ///< Draws the text twice to create shaddow
LiTextFlags_Wrap = 1 << 3, ///< Wrap Text: May be runs better with TMS
LiTextFlags_Short = 1 << 4, ///< Short Text: May be runs better with TMS
LiTextFlags_Scroll = 1 << 5, ///< Not implemented [scoll text if to long]
LiTextFlags_NoOOS = 1 << 6, ///< No Out of Screen Rendering
};
namespace PD {
class Context;
namespace Li {
class DrawList;
class PD_API Font {
public:
/** Codepoint Data holder */
struct Codepoint {
u32 pCodepoint = 0;
fvec4 SimpleUV;
Texture::Ref Tex;
fvec2 Size;
float Offset = 0.f;
bool pInvalid = false;
};
/** Constructore doesnt need Backand anymore */
Font(Context& ctx) : pCtx(ctx) {}
~Font() = default;
PD_SHARED(Font);
/**
* Load a TTF File
* @param path Path to the TTF file
* @param px_height Pixelheight of the codepoints (limit by 64)
*/
void LoadTTF(const std::string& path, int px_height = 32);
/**
* Load a TTF File from Memory
* @param data File data
* @param px_height Pixelheight of the codepoints (limit by 64)
*/
void LoadTTF(const std::vector<u8>& data, int px_height = 32);
/**
* Function that loads a default integrated font...
* This will only work if PD_LI_INCLUDE_FONTS was set
* on lithium build cause otherwise the font data is not included
*/
void LoadDefaultFont(int id = 0, int pixel_height = 32);
/**
* Getter for Codepoint reference
* @return codepoint dataholder reference
*/
Codepoint& GetCodepoint(u32 c);
/**
* Get Text Bounding Box
*/
fvec2 GetTextBounds(const std::string& text, float scale);
/**
* Extended Draw Text Function that vreates a Command List
*/
void CmdTextEx(DrawList& dl, const fvec2& pos, u32 color, float scale,
const std::string& text, LiTextFlags flags = 0,
const fvec2& box = 0);
/**
* Garbage collection for TextMapSystem
*/
void CleanupTMS();
/**
* Utility function to create a font atlas
* During TTF loading (Internal and should not be called)
*/
void pMakeAtlas(bool final, std::vector<u8>& font_tex, int texszs,
PD::Li::Texture::Ref tex);
std::string pWrapText(const std::string& txt, float scale,
const PD::fvec2& max, PD::fvec2& dim);
std::string pShortText(const std::string& txt, float scale,
const PD::fvec2& max, PD::fvec2& dim);
/** Data Section */
int PixelHeight;
int DefaultPixelHeight = 24;
std::vector<Texture::Ref> Textures;
/**
* 32Bit Codepoint Dataholder reference map
* **Now using unordered map**
*/
std::unordered_map<u32, Codepoint> CodeMap;
/** TMS */
struct TMELEM {
PD::u32 ID;
PD::fvec2 Size;
std::string Text;
u64 TimeStamp;
};
std::unordered_map<u32, TMELEM> pTMS;
Context& pCtx;
};
} // namespace Li
} // namespace PD

View File

@@ -23,38 +23,19 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <array>
#include <chrono>
#include <cmath>
#include <cstddef>
#include <exception>
#include <format>
#include <fstream>
#include <numbers>
#include <pd/pd_p_api.hpp>
#include <string>
#include <vector>
#ifdef PD_LI_INCLUDE_FONTS
#include <pd/core/common.hpp>
/** Generated with pdfm */
namespace PD {
[[noreturn]] inline void Throw(const std::string& msg) {
throw std::runtime_error(msg);
}
using u8 = unsigned char;
using u16 = unsigned short;
using u32 = unsigned int;
using u64 = unsigned long long;
using ptr = uintptr_t;
PD_API void Log(const std::string& txt);
template <typename... Args>
void Log(std::format_string<Args...> fmt, Args&&... args) {
std::string msg = std::format(fmt, std::forward<Args>(args)...);
Log(msg);
}
struct FontFileData {
std::string Name;
u32 StartOff;
u32 Size;
};
extern FontFileData pFontData[];
extern size_t pNumFonts;
extern PD::u8 pFontsDataRaw[];
} // namespace PD
#ifdef PD_DEBUG
#define PDLOG(fmt, ...) \
PD::Log("[{}:{}]: " fmt, __FILE__, __LINE__, ##__VA_ARGS__)
#else
#define PDLOG(fmt, ...)
#endif

30
include/pd/lithium/lithium.hpp Normal file → Executable file
View File

@@ -1,5 +1,31 @@
#pragma once
/*
MIT License
Copyright (c) 2024 - 2026 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <pd/lithium/command.hpp>
#include <pd/lithium/pools.hpp>
#include <pd/lithium/vertex.hpp>
#include <pd/lithium/drawlist.hpp>
#include <pd/lithium/font.hpp>
#include <pd/lithium/rect.hpp>
#include <pd/lithium/renderer.hpp>
#include <pd/lithium/texture.hpp>

View File

@@ -1,12 +0,0 @@
#pragma once
#include <cstddef>
#include <pd/lithium/vertex.hpp>
namespace PD {
namespace Li {
PD_API void InitPools(size_t max_vertices = 32768);
PD_API Vertex* AllocateVertices(size_t count);
PD_API u16* AllocateIndices(size_t count);
} // namespace Li
} // namespace PD

149
include/pd/lithium/rect.hpp Executable file
View File

@@ -0,0 +1,149 @@
#pragma once
/*
MIT License
Copyright (c) 2024 - 2026 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <pd/core/core.hpp>
namespace PD {
namespace Li {
class Rect {
public:
Rect() : Top(0), Bot(0) {}
~Rect() = default;
/**
* Constructor that initializes the rectangle using top and bottom positions.
* @param t Top left and right corner positions.
* @param b Bottom left and right corner positions.
*/
Rect(const fvec4& t, const fvec4& b) {
Top = t;
Bot = b;
}
/**
* Constructor that initializes the rectangle using individual corner
* positions.
* @param tl Top left corner position.
* @param tr Top right corner position.
* @param bl Bottom left corner position.
* @param br Bottom right corner position.
*/
Rect(const fvec2& tl, const fvec2& tr, const fvec2& bl, const fvec2& br) {
Top = fvec4(tl, tr);
Bot = fvec4(bl, br);
}
/**
* Constructor that initializes the rectangle using a UV mapping vector.
*
* - The old API used vec4 for UV mapping.
* - Spritesheets have rotated images, so this was updated to use Rect for UV.
*
* @param uv Vec4 UV map.
*/
Rect(const fvec4& uv) {
Top = vec4(uv.x, uv.y, uv.z, uv.y);
Bot = vec4(uv.x, uv.w, uv.z, uv.w);
}
/**
* Get the top-left corner position.
* @return Top-left position as vec2.
*/
fvec2 TopLeft() const { return fvec2(Top.x, Top.y); }
/**
* Get the top-right corner position.
* @return Top-right position as vec2.
*/
fvec2 TopRight() const { return fvec2(Top.z, Top.w); }
/**
* Get the bottom-left corner position.
* @return Bottom-left position as vec2.
*/
fvec2 BotLeft() const { return fvec2(Bot.x, Bot.y); }
/**
* Get the bottom-right corner position.
* @return Bottom-right position as vec2.
*/
fvec2 BotRight() const { return fvec2(Bot.z, Bot.w); }
/**
* Set the top-left corner position.
* @param v New top-left position.
* @return Reference to the updated Rect.
*/
Rect& TopLeft(const fvec2& v) {
Top.x = v.x;
Top.y = v.y;
return *this;
}
/**
* Set the top-right corner position.
* @param v New top-right position.
* @return Reference to the updated Rect.
*/
Rect& TopRight(const fvec2& v) {
Top.z = v.x;
Top.w = v.y;
return *this;
}
/**
* Set the bottom-left corner position.
* @param v New bottom-left position.
* @return Reference to the updated Rect.
*/
Rect& BotLeft(const fvec2& v) {
Bot.x = v.x;
Bot.y = v.y;
return *this;
}
/**
* Set the bottom-right corner position.
* @param v New bottom-right position.
* @return Reference to the updated Rect.
*/
Rect& BotRight(const fvec2& v) {
Bot.z = v.x;
Bot.w = v.y;
return *this;
}
bool operator==(const Rect& r) const { return Top == r.Top && Bot == r.Bot; }
void SwapVec2XY() {
Top.SwapXY();
Top.SwapZW();
Bot.SwapXY();
Bot.SwapZW();
}
/** Data Section */
fvec4 Top;
fvec4 Bot;
};
} // namespace Li
} // namespace PD

View File

@@ -0,0 +1,62 @@
#pragma once
/*
MIT License
Copyright (c) 2024 - 2026 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <pd/drivers/drivers.hpp>
#include <pd/lithium/rect.hpp>
#include <pd/pd_p_api.hpp>
namespace PD {
namespace Li {
/**
* Static Class Render Setup Functions
*/
class PD_API Renderer {
public:
Renderer() = default;
~Renderer() = default;
// SECTION: Open Command and Object creation API
static void RotateCorner(fvec2& pos, float s, float c);
static Rect PrimRect(const fvec2& pos, const fvec2& size, float angle = 0.f);
static Rect PrimLine(const fvec2& a, const fvec2& b, int thickness = 1);
static void CmdQuad(Command* cmd, const Rect& quad, const Rect& uv,
u32 color);
static void CmdTriangle(Command* cmd, const fvec2 a, const fvec2 b,
const fvec2 c, u32 clr);
static void CmdPolyLine(const std::vector<fvec2>& points, u32 clr,
u32 flags = 0, int thickness = 1);
static void CmdConvexPolyFilled(Command* cmd,
const std::vector<fvec2>& points, u32 clr,
Texture::Ref tex);
// SECTION: InBounds Checks
static bool InBox(const fvec2& pos, const fvec2& size, const fvec4& area);
static bool InBox(const fvec2& pos, const fvec4& area);
static bool InBox(const fvec2& a, const fvec2& b, const fvec2& c,
const fvec4& area);
};
} // namespace Li
} // namespace PD

75
include/pd/lithium/texture.hpp Executable file
View File

@@ -0,0 +1,75 @@
#pragma once
/*
MIT License
Copyright (c) 2024 - 2026 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <pd/core/core.hpp>
#include <pd/lithium/rect.hpp>
namespace PD {
namespace Li {
/** Use so address type for TexAddress */
using TexAddress = uintptr_t;
class Texture {
public:
/** Texture Types */
enum Type {
RGBA32, ///< Rgba 32Bit
RGB24, ///< Rgb 24 Bit
A8, ///< A8 8Bit alpha
};
/** Texture Filters */
enum Filter {
NEAREST, ///< Nearest
LINEAR, ///< Linear
};
/** Constructor */
Texture() : Address(0), Size(0), UV(fvec4(0.f, 0.f, 1.f, 1.f)) {}
Texture(TexAddress addr, ivec2 size,
Li::Rect uv = fvec4(0.f, 0.f, 1.f, 1.f)) {
Address = addr;
Size = size;
UV = uv;
}
PD_SHARED(Texture);
void CopyFrom(Texture::Ref tex) {
Address = tex->Address;
Size = tex->Size;
UV = tex->UV;
}
/** Left in Code getter (should be remoevd) */
ivec2 GetSize() const { return Size; }
Li::Rect GetUV() const { return UV; }
operator ivec2() const { return Size; }
operator Li::Rect() const { return UV; }
TexAddress Address;
ivec2 Size;
Li::Rect UV;
};
} // namespace Li
} // namespace PD

View File

@@ -1,19 +1,52 @@
#pragma once
#include <pd/core/vec.hpp>
/*
MIT License
Copyright (c) 2024 - 2026 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <pd/core/core.hpp>
namespace PD {
namespace Li {
class Vertex {
public:
Vertex() {}
Vertex(const fvec2& pos, const fvec2& uv, u32 color)
: pos(pos), uv(uv), color(color) {}
Vertex(const fvec2& p, const fvec2& u, u32 c) {
Pos.x = p.x;
Pos.y = p.y;
UV = u;
Color = c;
}
~Vertex() {}
fvec2 pos;
fvec2 uv;
u32 color = 0x00000000;
PD_RAW(Vertex);
// private:
/** Open Access Data Section */
fvec2 Pos;
fvec2 UV;
u32 Color;
};
} // namespace Li
} // namespace PD

View File

@@ -0,0 +1,76 @@
#pragma once
/*
MIT License
Copyright (c) 2024 - 2026 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <pd/ui7/container/container.hpp>
#include <pd/ui7/io.hpp>
namespace PD {
namespace UI7 {
/**
* Button Object
* @note Button Press is delayed by 1 frame
* (but the visual reaction is done in the same frame)
* This only means that InPressed is responding the info in
* the next frame
*/
class PD_API Button : public Container {
public:
/**
* Button Object constructor
* @param label Label of the Button
* @param pos Base Position
* @param lr Reference to the Renderer
*/
Button(const std::string& label, UI7::IO::Ref io) {
this->label = label;
this->tdim = io->Font->GetTextBounds(label, io->FontScale);
}
~Button() = default;
PD_SHARED(Button);
/** Return true if butten is pressed*/
bool IsPressed() { return pressed; }
/**
* Override for the Input Handler
* @note This function is usally called by Menu::Update
*/
void HandleInput() override;
/**
* Override for the Rendering Handler
* @note This function is usally called by Menu::Update
* */
void Draw() override;
/** Function to Update Size if framepadding changes */
void Update() override;
private:
fvec2 tdim; ///< Text size
UI7Color color = UI7Color_Button; ///< current button color
std::string label; ///< Label of the Button
bool pressed = false; ///< ispressed value
};
} // namespace UI7
} // namespace PD

View File

@@ -0,0 +1,73 @@
#pragma once
/*
MIT License
Copyright (c) 2024 - 2026 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <pd/ui7/container/container.hpp>
namespace PD {
namespace UI7 {
/**
* Checkbox Object
* @note The Updated input is available after
* Context::Update while the visual update is done
* during the Update
*/
class PD_API Checkbox : public Container {
public:
/**
* Constructor for Checkbox Object
* @param label Label of the Checkbox
* @param usr_ref Reference to the bool value to update
* @param io IO Reference
*/
Checkbox(const std::string& label, bool& usr_ref, UI7::IO::Ref io)
: usr_ref(usr_ref) {
this->label = label;
this->tdim = io->Font->GetTextBounds(label, io->FontScale);
}
~Checkbox() = default;
PD_SHARED(Checkbox);
/**
* Override for the Input Handler
* @note This function is usally called by Menu::Update
*/
void HandleInput() override;
/**
* Override for the Rendering Handler
* @note This function is usally called by Menu::Update
* */
void Draw() override;
/** Update Size if framepadding changed */
void Update() override;
private:
fvec2 tdim; ///< Text Size
fvec2 cbs = fvec2(18); ///< Checkbox size
UI7Color color = UI7Color_FrameBackground; ///< Checkbox background Color
std::string label; ///< Checkbox Label
bool& usr_ref; ///< User bool reference
};
} // namespace UI7
} // namespace PD

View File

@@ -0,0 +1,76 @@
#pragma once
/*
MIT License
Copyright (c) 2024 - 2026 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <pd/ui7/container/container.hpp>
#include <pd/ui7/io.hpp>
#include <pd/ui7/layout.hpp>
namespace PD {
namespace UI7 {
/**
* Color Editor (Creating a PopUP when clicking)
*/
class PD_API ColorEdit : public Container {
public:
/**
* Constructor
* @param label Label of the Button
* @param pos Base Position
* @param lr Reference to the Renderer
*/
ColorEdit(const std::string& label, u32* color, UI7::IO::Ref io) {
// PD::Assert(color != nullptr, "Input Color Address is null!");
this->label = label;
this->color_ref = color;
this->initial_color = *color;
this->tdim = io->Font->GetTextBounds(label, io->FontScale);
}
~ColorEdit() = default;
PD_SHARED(ColorEdit);
/**
* Override for the Input Handler
* @note This function is usally called by Menu::Update
*/
void HandleInput() override;
/**
* Override for the Rendering Handler
* @note This function is usally called by Menu::Update
* */
void Draw() override;
/** Function to Update Size if framepadding changes */
void Update() override;
private:
fvec2 tdim; ///< Text size
u32* color_ref = nullptr; ///< Color Reference
u32 initial_color; ///< Initial Color
std::string label; ///< Label of the Button
Layout::Ref layout; ///< Layout to open
bool is_shown = false; ///< AHow Layout Editor
};
} // namespace UI7
} // namespace PD

View File

@@ -0,0 +1,180 @@
#pragma once
/*
MIT License
Copyright (c) 2024 - 2026 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <pd/core/core.hpp>
#include <pd/pd_p_api.hpp>
#include <pd/ui7/io.hpp>
namespace PD {
namespace UI7 {
/**
* Container base class all Objects are based on
* @note this class can be used to create custom Objects as well
*/
class PD_API Container {
public:
Container() = default;
/**
* Constructor with pos and Size
* @param pos Container Position
* @param size Container Size
*/
Container(const fvec2& pos, const fvec2& size) : pos(pos), size(size) {}
/**
* Constructor by a vec4 box
* @param box Box containing top left and bottom right coords
*/
Container(const fvec4& box)
: pos(fvec2(box.x, box.y)), size(fvec2(box.z - box.x, box.w - box.y)) {}
~Container() = default;
PD_SHARED(Container);
/**
* Init Function Required by every Object that uses
* Render or Input functions
* @param io IO Reference
* @param l DrawList Reference
*/
void Init(UI7::IO::Ref io, Li::DrawList::Ref l) {
list = l;
this->io = io;
// this->screen = io->Ren->CurrentScreen();
}
void SetClipRect(fvec4 clip) {
pClipRect = clip;
pCLipRectUsed = true;
}
/** Setter for Position */
void SetPos(const fvec2& pos) { this->pos = pos; }
/** Setter for Size */
void SetSize(const fvec2& size) { this->size = size; }
/** Getter for Position */
fvec2 GetPos() { return pos; }
/** Getter for Size */
fvec2 GetSize() { return size; }
/**
* Get the Containers Final Position
* for Rendering and Input (if it has a parent Object)
*/
fvec2 FinalPos() {
vec2 res = pos;
if (parent) {
/// Probably should use parant->FinalPos here
res += parent->GetPos();
}
return res;
}
/** Setter for Parent Container */
void SetParent(Container::Ref v) { parent = v; }
/** Getter for Parent Container */
Container::Ref GetParent() { return parent; }
/** Check if Rendering can be skipped */
bool Skippable() const { return skippable; }
/** Check if the Object got a timeout (ID OBJ Relevant) */
bool Removable() const { return rem; }
/**
* Handles Scrolling by scrolling pos as well as
* Time for Remove for ID Objects
* @param scrolling Scrolling Position
* @param viewport Viewport to check if the Object is skippable
*/
void HandleScrolling(fvec2 scrolling, fvec4 viewport);
/** Template function for Input Handling */
virtual void HandleInput() {}
/** Tamplate function for Object rendering */
virtual void Draw() {}
/** Template function to update internal data (if needed) */
virtual void Update() {}
/** Internal function */
void PreDraw();
/** Internal function */
void PostDraw();
/** Internal Input Handler */
void HandleInternalInput();
/**
* Function to unlock Input after Rendering is done in
* Menu::Update
* @note This is used if the Object got Input Handled directly after creation
* to not check for Inputs twice
*/
void UnlockInput() { inp_done = false; }
/** Get the Objects ID (if it is an ID object)*/
u32 GetID() const { return id; }
/**
* Set ID for ID Objects
* @param id Object ID (hashed prefix+objname+prefixed_counter)
*/
void SetID(u32 id) { this->id = id; }
/** Get a reference to IO */
UI7::IO::Ref GetIO() { return io; }
protected:
/** used to skip Input/Render preocessing ot not*/
bool skippable = false;
/** value to check if an ID Object goes out of lifetime*/
bool rem = false;
/** Time of the last use (set by HandleScrolling)*/
u64 last_use = 0;
/** Input done or not for current frame*/
bool inp_done = false;
/** Reference to the Screen to draw the Object on*/
// Screen::Ref screen;
/** Container Position*/
fvec2 pos;
/** Container Size*/
fvec2 size;
/** Reference to the Drawlist to Draw to*/
Li::DrawList::Ref list;
/** IO Reference for Renderer and Theme */
UI7::IO::Ref io;
/** Reference to the parent container*/
Container::Ref parent;
/** Object ID (0 if unused)*/
u32 id = 0;
/** Internal Flags */
u32 pFlags = 0;
/** Is Selected? */
bool pSelected = false;
/** Was Pressed */
bool pPressed = false;
/** Was Pressed Twice */
bool pPressedTwice = false;
/** ClipRect */
fvec4 pClipRect;
/** Clip Rect used */
bool pCLipRectUsed = false;
};
} // namespace UI7
} // namespace PD

View File

@@ -0,0 +1,92 @@
#pragma once
/*
MIT License
Copyright (c) 2024 - 2026 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <pd/ui7/container/container.hpp>
#include <pd/ui7/io.hpp>
namespace PD {
namespace UI7 {
/**
* DragData Object can take a datatype or a list
* and modifys these by moving left or right when dragging
*/
template <typename T>
class PD_API DragData : public Container {
public:
/**
* Constructor
* @param label Label of the Button
* @param data Data reference (Supported types can be seen in dragdata.cpp)
* @param num_elms Number of Array elements (for exaple with use ofvec4)
* @param io IO Reference
* @param min minimum number using Minimum limit
* @param max Maximum number set by max limit by default
* @param step To set the modifier for drag movement
* @param precision for float and double to set precision
*/
DragData(const std::string& label, T* data, size_t num_elms, UI7::IO::Ref io,
T min = std::numeric_limits<T>::min(),
T max = std::numeric_limits<T>::max(), T step = 1,
int precision = 1) {
// PD::Assert(data != nullptr, "Input Data Address is null!");
this->label = label;
this->data = data;
this->elm_count = num_elms;
this->min = min;
this->max = max;
this->step = step;
this->precision = precision;
this->tdim = io->Font->GetTextBounds(label, io->FontScale);
}
~DragData() = default;
/** Als ob das funktioniert... */
PD_SHARED(DragData<T>);
/**
* Override for the Input Handler
* @note This function is usally called by Menu::Update
*/
void HandleInput() override;
/**
* Override for the Rendering Handler
* @note This function is usally called by Menu::Update
* */
void Draw() override;
/** Function to Update Size if framepadding changes */
void Update() override;
private:
fvec2 tdim; ///< Text size
std::string label; ///< Label of the Button
T* data;
size_t elm_count = 0;
T min;
T max;
T step;
int precision = 1;
};
} // namespace UI7
} // namespace PD

View File

@@ -0,0 +1,81 @@
#pragma once
/*
MIT License
Copyright (c) 2024 - 2026 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <pd/ui7/container/container.hpp>
#include <pd/ui7/io.hpp>
namespace PD {
namespace UI7 {
/**
* Button Object
* @note Button Press is delayed by 1 frame
* (but the visual reaction is done in the same frame)
* This only means that InPressed is responding the info in
* the next frame
*/
class PD_API DynObj : public Container {
public:
/**
* Button Object constructor
* @param label Label of the Button
* @param pos Base Position
* @param lr Reference to the Renderer
*/
DynObj(std::function<void(UI7::IO::Ref, Li::DrawList::Ref, Container*)>
RenderFunc) {
pRenFun = RenderFunc;
}
~DynObj() = default;
PD_SHARED(DynObj);
void AddInputHandler(std::function<void(UI7::IO::Ref, Container*)> inp) {
pInp = inp;
}
/** Return true if butten is pressed*/
bool IsPressed() { return pressed; }
/**
* Override for the Input Handler
* @note This function is usally called by Menu::Update
*/
void HandleInput() override;
/**
* Override for the Rendering Handler
* @note This function is usally called by Menu::Update
* */
void Draw() override;
/** Function to Update Size if framepadding changes */
void Update() override;
private:
UI7Color color = UI7Color_Button; ///< current button color
bool pressed = false; ///< ispressed value
std::function<void(UI7::IO::Ref, Li::DrawList::Ref, Container*)> pRenFun;
std::function<void(UI7::IO::Ref, Container*)> pInp;
};
} // namespace UI7
} // namespace PD

View File

@@ -0,0 +1,69 @@
#pragma once
/*
MIT License
Copyright (c) 2024 - 2026 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <pd/ui7/container/container.hpp>
namespace PD {
namespace UI7 {
/**
* Image Object
*/
class PD_API Image : public Container {
public:
/**
* Constructor for the Image Object
* @param img Image Texture Reference
* @param size Custom Size of the Image
*/
Image(Li::Texture::Ref img, fvec2 size = 0.f, Li::Rect uv = fvec4(0.f)) {
if (!img) return;
this->img = img;
if (size == fvec2(0.f)) {
size = img->GetSize();
}
if (uv == Li::Rect(fvec4(0.f))) {
uv = img->GetUV();
}
this->cuv = uv;
this->newsize = size;
SetSize(size);
}
~Image() = default;
PD_SHARED(Image);
/**
* Override for the Rendering Handler
* @note This function is usally called by Menu::Update
* */
void Draw() override;
private:
Li::Texture::Ref img = nullptr; ///< Texture reference to the Image
fvec2 newsize = 0.f; ///< New Size
Li::Rect cuv; ///< Custom UV
};
} // namespace UI7
} // namespace PD

View File

@@ -0,0 +1,65 @@
#pragma once
/*
MIT License
Copyright (c) 2024 - 2026 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <pd/ui7/container/container.hpp>
namespace PD {
namespace UI7 {
/**
* Label [Text] Object
*/
class PD_API Label : public Container {
public:
/**
* Constructor for Label Object
* @param label Label [Text] to Draw
* @param lr Renderer Reference
*/
Label(const std::string& label, IO::Ref io) {
this->label = label;
this->tdim = io->Font->GetTextBounds(label, io->FontScale);
this->SetSize(tdim);
}
~Label() = default;
PD_SHARED(Label);
/**
* Override for the Rendering Handler
* @note This function is usally called by Menu::Update
* */
void Draw() override;
/**
* Override Update func to support Text modifications
*/
void Update() override;
private:
fvec2 tdim; ///< Text Size
UI7Color color = UI7Color_Text; ///< Color
std::string label; ///< Text to Render
};
} // namespace UI7
} // namespace PD

View File

@@ -0,0 +1,91 @@
#pragma once
/*
MIT License
Copyright (c) 2024 - 2026 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <pd/ui7/container/container.hpp>
#include <pd/ui7/io.hpp>
namespace PD {
namespace UI7 {
/**
* Slider Object can take a datatype or a list
* and modifys these by moving left or right when dragging
*/
template <typename T>
class PD_API Slider : public Container {
public:
/**
* Constructor
* @param label Label of the Button
* @param data Data reference (Supported types can be seen in Slider.cpp)
* @param num_elms Number of Array elements (for exaple with use ofvec4)
* @param io IO Reference
* @param min minimum number using Minimum limit
* @param max Maximum number set by max limit by default
* @param step To set the modifier for drag movement
* @param precision for float and double to set precision
*/
Slider(const std::string& label, T* data, UI7::IO::Ref io,
T min = std::numeric_limits<T>::min(),
T max = std::numeric_limits<T>::max(), int precision = 1) {
// PD::Assert(data != nullptr, "Input Data Address is null!");
this->label = label;
this->data = data;
this->min = min;
this->max = max;
this->precision = precision;
this->width = io->CurrentViewPort.z * 0.3f;
this->tdim = io->Font->GetTextBounds(label, io->FontScale);
}
~Slider() = default;
/** Als ob das funktioniert... */
PD_SHARED(Slider<T>);
/**
* Override for the Input Handler
* @note This function is usally called by Menu::Update
*/
void HandleInput() override;
/**
* Override for the Rendering Handler
* @note This function is usally called by Menu::Update
* */
void Draw() override;
/** Function to Update Size if framepadding changes */
void Update() override;
private:
fvec2 tdim; ///< Text size
float width; ///< Size
std::string label; ///< Label of the Button
T* data;
T min;
T max;
int precision = 1;
float slw = 0.f; // silider drag width (calculated in update)
float slp = 0.f; // silider drag pos (calculated in update)
};
} // namespace UI7
} // namespace PD

33
include/pd/ui7/containers.hpp Executable file
View File

@@ -0,0 +1,33 @@
#pragma once
/*
MIT License
Copyright (c) 2024 - 2026 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <pd/ui7/container/button.hpp>
#include <pd/ui7/container/checkbox.hpp>
#include <pd/ui7/container/coloredit.hpp>
#include <pd/ui7/container/dragdata.hpp>
#include <pd/ui7/container/dynobj.hpp>
#include <pd/ui7/container/image.hpp>
#include <pd/ui7/container/label.hpp>
#include <pd/ui7/container/slider.hpp>

100
include/pd/ui7/flags.hpp Executable file
View File

@@ -0,0 +1,100 @@
#pragma once
/*
MIT License
Copyright (c) 2024 - 2026 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/** 32Bit Value to Stpre Menu Flags */
using UI7MenuFlags = unsigned int;
/** 32Bit Value to store Alignment Flags */
using UI7Align = unsigned int;
/** 32Bit Value to store Context (IO) flags */
using UI7IOFlags = unsigned int;
/** 32Bit Value for Layout Flags */
using UI7LayoutFlags = unsigned int;
/** Menu Flags */
enum UI7MenuFlags_ {
UI7MenuFlags_None = 0, ///< No Flags (Default)
UI7MenuFlags_NoTitlebar = 1 << 0, ///< Dont Show Titlebar
UI7MenuFlags_CenterTitle = 1 << 1, ///< Center the Menu Title in Titlebar
UI7MenuFlags_HzScrolling = 1 << 2, ///< Enable Horizontal Scrolling
UI7MenuFlags_VtScrolling = 1 << 3, ///< Enable Vertical Scrolling
UI7MenuFlags_NoBackground = 1 << 4, ///< Dont Render Menu Background
UI7MenuFlags_NoClipRect = 1 << 5, ///< Disable clip render area of the Menu
UI7MenuFlags_NoCollapse = 1 << 6, ///< Disable Menu Collapse
UI7MenuFlags_NoMove = 1 << 7, ///< Disable Menu Movement
UI7MenuFlags_NoResize = 1 << 8, ///< Disable Menu Resize
UI7MenuFlags_NoClose = 1 << 9, ///< Disable Close Button
UI7MenuFlags_NoScrollbar = 1 << 10, ///< Hide the Scrollbar
// POC
UI7MenuFlags_Maximize = 1 << 11, ///< Add a Maximize Button
UI7MenuFlags_Minimize = 1 << 12, ///< Add a Minimize Button
UI7MenuFlags_AlwaysAutoSize = 1 << 13, ///< Always Auto Resize Menu
// Enable Horizontal and Vertical Scrolling
UI7MenuFlags_Scrolling = UI7MenuFlags_HzScrolling | UI7MenuFlags_VtScrolling,
};
/** UI7 Layout Flags */
enum UI7LayoutFlags_ {
UI7LayoutFlags_None = 0, ///< No Flags used
UI7LayoutFlags_UseClipRect = 1 << 0, ///< Enable ClipRect
};
/** UI7 Context Flags */
enum UI7IOFlags_ {
UI7IOFlags_None = 0, ///< No Additional Config available
UI7IOFlags_HasTouch = 1 << 0, ///< Enable touch support [future]
UI7IOFlags_HasMouseCursor = 1 << 1, ///< Enable Mouse support [future]
};
/** Probably need to update this */
enum UI7Align_ {
UI7Align_Left = 1 << 0, ///< [Hz Op] Align Left (Default)
UI7Align_Center = 1 << 1, ///< [Hz Op] Align Center
UI7Align_Right = 1 << 2, ///< [Hz Op] Align Right
UI7Align_Top = 1 << 3, ///< [Vt Op] Align Top (Default)
UI7Align_Mid = 1 << 4, ///< [Vt Op] Align Mid
UI7Align_Bottom = 1 << 5, ///< [Vt Op] Align Bottom
// Default Horizontal and Vertical Alignment
UI7Align_Default = UI7Align_Left | UI7Align_Top,
};
/** Special flags for Layout::AddObjectEx */
enum UI7LytAdd_ {
UI7LytAdd_None = 0, ///< Also known as default or ->AddObject
UI7LytAdd_NoCursorUpdate = 1 << 0, ///< Add without cursor alignment
UI7LytAdd_NoScrollHandle = 1 << 1, ///< Skip HandleScrolling
UI7LytAdd_Front = 1 << 2, ///< Add in front of the list
};
/**
* Todo: Look at this
* Maybe proof of concept ???
* Didnt remember that this exists
*/
enum UI7ContainerFlags_ {
UI7ContainerFlags_None = 0,
UI7ContainerFlags_EnableInternalInput = 1 << 0,
UI7ContainerFlags_Selectable = 1 << 1,
UI7ContainerFlags_OutlineSelected = 1 << 2,
};

74
include/pd/ui7/id.hpp Executable file
View File

@@ -0,0 +1,74 @@
#pragma once
/*
MIT License
Copyright (c) 2024 - 2026 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <pd/core/core.hpp>
namespace PD {
namespace UI7 {
/**
* ID Class (Generating an ID by String)
*/
class ID {
public:
/**
* Constructor to Generate ID by input string
* @param text Input String
*/
ID(const std::string& text) {
pID = PD::Strings::FastHash(text);
// pStrName = text;
pName = text;
}
/**
* Constructor used for const char* which is automatically
* used when directly placing a string istead of using ID("")
* @param text Input String
*/
constexpr ID(const char* text) {
pID = PD::FNV1A32(text);
pName = text;
}
/**
* Use an ID as Input
*/
ID(u32 id) { pID = id; }
~ID() = default;
/** Get The ID Initial Name */
// constexpr const std::string_view& GetNameView() const { return pName; }
const std::string GetName() const { return std::string(pName); }
/** Getter for the raw 32bit int id */
constexpr const u32& RawID() const { return pID; }
/** Return the ID when casting to u32 */
constexpr operator u32() const { return pID; }
u32 pID; ///< Hash of the name
std::string pName; ///< Name
// std::string pStrName; ///< Keep this stringview alive
};
} // namespace UI7
} // namespace PD

Some files were not shown because too many files have changed in this diff Show More