119 Commits
Author SHA1 Message Date
tobid7 6547f64d13 fix resize sym 2026-09-18 22:11:08 +02:00
tobid7 2b37204763 remove Font::LoadDefaultFont 2026-09-17 21:35:19 +02:00
tobid7 8c2bcedc93 Testing around 2026-09-16 21:20:45 +02:00
tobid7 01d3eb771e remove the 3ds ifdefs out of the core mat lib
need to find a way to handle mtx stuff in the gfx driver or another way
2026-09-15 07:20:00 +02:00
tobid7 6de3683818 3ds: move ui7 to bottom screne 2026-09-13 14:10:07 +02:00
tobid7 ec49f70c0b Add more gfx backend flags (available features) 2026-09-13 01:24:55 +02:00
tobid7 6284ff885d citro3d: clipping 2026-09-13 01:06:23 +02:00
tobid7 91c245c1a6 dx9 clipping 2026-09-12 17:29:48 +02:00
tobid7 c828166ce4 refactor UI7 clipping 2026-09-10 07:55:37 +02:00
tobid7 c7816ea170 fix close sym 2026-09-09 22:03:19 +02:00
tobid7 0dbf430c0c Implement clipping 2026-09-08 21:15:21 +02:00
tobid7 aec9083ebb small fixes 2026-09-07 22:05:51 +02:00
tobid7 9e6c6fd84d Merge dev-0.7-rewrite into stable 2026-09-06 16:23:08 +02:00
tobid7 7e0f3a6bf6 reset layers and give menus a base layer to gather perfect ordered rendering 2026-09-06 15:10:37 +02:00
tobid7 1ae4d0cd46 Add FontStack to UI7 and uncomment layer lines 2026-09-06 15:03:45 +02:00
tobid7 420edd0677 move and fix scrolling to layout
Scrolling was hardcoded to 3ds sCrEeNhEiGhT wTh
2026-09-06 14:46:22 +02:00
tobid7 e8a9055090 Layers are back 2026-09-06 14:39:28 +02:00
tobid7 9e27dfb0eb hotfix 2026-09-06 14:25:12 +02:00
tobid7 42c669de55 Add Hid3DS 2026-09-06 14:21:30 +02:00
tobid7 4488425a94 Bring back global lithium pools
why?
cause sorting around the global driver pool would basically use more cpu power then just copy data sequences into the right order
And sorting data references costs 0 performance at all
2026-09-05 22:52:59 +02:00
tobid7 eafe5c9be5 Add Drawlist Vertex and Indexcount geteter (for debugging usage only) 2026-09-05 21:28:09 +02:00
tobid7 d18e397eaf Fix The rest of UI7 Mem leaks
Throw OneFrame living objs into a pool
2026-09-05 18:06:59 +02:00
tobid7 fc5a357caa stop leaking idobj memory (and coloredit layout mem) 2026-09-05 18:06:12 +02:00
tobid7 a1cb3bf290 dx9: update device size on window size change 2026-09-05 17:18:21 +02:00
tobid7 8c1063640c format documents 2026-09-05 17:04:28 +02:00
tobid7 85f50df154 unload debugfont and add Gfx debug menu 2026-09-05 17:03:17 +02:00
tobid7 32c94e0776 fix random garbage ptr issues 2026-09-05 17:02:58 +02:00
tobid7 6b5f30b334 Fix Indexorder issues and patchfixsomehow dx9 tex filtering 2026-09-05 17:02:37 +02:00
tobid7 42cb7ce2ed No big update without msvc compiler errors :/ 2026-09-05 16:29:53 +02:00
tobid7 26a8c230c6 More timings 2026-09-05 15:10:48 +02:00
tobid7 0a5a86459b UI7 is BACK!
took me about a full week to get something to render again withour shared_ptrs
as of now this is very unstable, untested and lacking a lot of features due to lithium is lacking the base for that
Added JetBrainsMono font as debug font for the metrics
2026-09-05 14:48:03 +02:00
tobid7 0f25a70901 Add missing GetDriverName to Os 2026-09-05 14:47:41 +02:00
tobid7 6a22b26a3d Draw the line (oder so) 2026-09-05 14:46:51 +02:00
tobid7 245cc0f314 Load the font async for now 2026-09-05 11:56:41 +02:00
tobid7 cb437d41a0 n3ds-boost 2026-09-05 11:56:26 +02:00
tobid7 322ce785b9 SDF & MONOSPACE FONT RENDERING 2026-09-05 11:56:13 +02:00
tobid7 b29e298d55 save 75% of font memory 2026-09-03 09:36:41 +02:00
tobid7 2801b876e6 Upload pools once per Gfx::Draw and FIX DX9 2026-09-03 09:18:49 +02:00
tobid7 f1540f0098 reset pools on newframe 2026-09-03 08:12:34 +02:00
tobid7 54584728c3 move global pools into gfx driver
prevents double copy problems
saves memory
(needs to be optimized...)
2026-09-02 08:04:50 +02:00
tobid7 daaf473af7 faster rect command creation 2026-09-01 21:21:40 +02:00
tobid7 9ed3ed233d cache ascii chars directly 2026-09-01 21:20:52 +02:00
tobid7 dd97aab749 insane performance boost (on 3ds) 2026-09-01 21:16:23 +02:00
tobid7 0746295013 time trace is back 2026-09-01 10:02:54 +02:00
tobid7 2984354884 implement Image::Convert and Image::Flip 2026-09-01 08:15:03 +02:00
tobid7 4d2470b0c6 pool append move and copy 2026-08-31 08:59:41 +02:00
tobid7 cfb6a9b3dd # small fixes
- Fix Os::GetTimeNano()
- only build tests on non vendored usage
2026-07-19 14:27:48 +02:00
tobid7 799e779fe6 Fix 2026-04-30 12:38:35 +02:00
tobid7 7f708a565e Fix 3ds build and update libpicasso 2026-04-25 00:01:06 +02:00
tobid7 280ce524bf remove poc func 2026-04-18 14:34:45 +02:00
tobid7 b99fc39444 Add support for rotated gradients 2026-04-18 14:33:31 +02:00
tobid7 6dbf5a4812 Add stick and trigger input support 2026-04-09 21:51:38 +02:00
tobid7 ff3b241dd2 Fix clang warning 2026-04-05 04:31:50 +02:00
tobid7 7a1751589e fix preset names for Selection Views 2026-04-05 04:31:22 +02:00
tobid7 8a4b3c119d Add NX Hid Driver Template
- Add WIP HidNX Driver (clangd not working with devkitpro for switch on windows)
- Add default-release as default search path for compile_commands.json
- remove mingw preset (casue its exactly the default target)
- Move Mouse pos cycle into HidDriver::Update
- Test around with HidGlfw on Nintendo switch
2026-04-05 04:20:32 +02:00
tobid7 1f7d96a455 Add proper Gamepad support (WIP) 2026-04-05 00:26:00 +02:00
tobid7 c9768010f2 allo wPD::Color in Drawlist 2026-04-04 17:44:05 +02:00
tobid7 679de3ae94 Add Input functionality to Ultra
- Add point InSpace check to PD::Li::Math
- Add Universal AlignmentCenter flag for Horizontal and and Vertical Alignment
-  Add Fallbackfont to Layout (if you dont want to set font per object)
- Add Button Object WIP
- Rename OnHover to OnFocus and add OnUnfocus
- Move font and FontScale to ElementBase (for fallback logic etc)
- Add UpdateInput func to ElementBase
- Corectly Set fontScale in Text Rendering
- Update ecample
2026-04-03 15:17:43 +02:00
tobid7 af7fc026df Allow implicit usage of "#ffffffff" etc 2026-04-03 14:22:51 +02:00
tobid7 5bc8046ebe Add HidDriver base and small HidGLFW driver 2026-04-03 14:12:42 +02:00
tobid7 8215baac99 rename PDBackendFlags to PDGfxBackendFlags 2026-04-03 12:52:30 +02:00
tobid7 a776addf11 Implement VCanvas and Update TextElem
- Text now requires a font and is able to take an individual scale
- Container requires &ref for Elements to make sure they always exist
- SetViewport now sets the canvas as well
- SetBaseViewport Sets the Virtual Canvas Size
- Layout Now requires a Drawlist reference in Render function
- main.cpp: updated the template
2026-04-03 12:43:49 +02:00
tobid7 1cf3b6f8e6 Actually implement ultra-rendering 2026-04-02 23:29:53 +02:00
tobid7 b5321ca1a1 Rework Project Ultra
- Added container
- Made container the base of Layout
- Set Containers theirself as pParent for Elements on Push
2026-03-29 04:28:45 +02:00
tobid7 4498c2c0fb Fix path rects 2026-03-29 04:27:37 +02:00
tobid7 818b086f08 Fixing riunded rects 2026-03-29 01:08:02 +01:00
tobid7 e560496512 Add a default preset (for linux/macos default builds) 2026-03-28 13:59:42 +01:00
tobid7 7b6e611f12 Add cmake presets
- MSVC requires cmake for Windows
- mingw only builds when GLSLANG is excluded due to wrong header generation
2026-03-28 13:53:55 +01:00
tobid7 72deaad28d build 3dsx and nro in ALL stage 2026-03-28 13:52:59 +01:00
tobid7 2e652a3c24 More stuff done at Project Ultra
- Added renderspace
- Revision System (to not always update pRenderspace
- added some test scene
2026-03-28 13:52:36 +01:00
tobid7 784421fa6c WIP (Codename Ultra) UI lib 2026-03-26 21:02:10 +01:00
tobid7 bc06a3fee8 implement font deletion 2026-03-26 21:01:43 +01:00
tobid7 d2ab60c824 Fix font bugs 2026-03-25 22:24:54 +01:00
tobid7 a8fe58ef0f Add debug getters (and somehow text is not rendering :/) 2026-03-25 20:27:23 +01:00
tobid7 eea0e9844f Fix Vertex Alloc usage at Index Alloc 2026-03-24 22:20:41 +01:00
tobid7 4777c85f9a Readd font support (not workign yet) 2026-03-22 21:50:53 +01:00
tobid7 b49c0bd3dc Update lazyvec / vec api as well as rect are mostly constexpr now 2026-03-22 00:15:14 +01:00
tobid7 4db5d98cb2 Always disable glslang build for NintendoSwitch on Windows 2026-03-21 19:28:59 +01:00
tobid7 794e06b890 Present DX9 Frames ... 2026-03-21 16:47:16 +01:00
tobid7 d8efcd41b1 Add a PositionTranslate func and add Errors for TextureSize mismatch 2026-03-21 16:39:55 +01:00
tobid7 4dab1987d1 Add PD::Image and add a setter for logfilter 2026-03-21 15:52:09 +01:00
tobid7 6fab4a07a9 Forgot about dx9 (UNTESTED!!!) 2026-03-21 14:48:48 +01:00
tobid7 baad2ce15c Add Os Bridge into test framework 2026-03-21 14:43:16 +01:00
tobid7 a86c13b9a3 Refactor PD::Li Pools system, Upgrade PD::Pool and add better logging
- Add ResetFast to Pool using c++20 concept
- Add Put function to Pool
- Use Pool Index instead of pointers in Command
- Add accessor to Li Pools to prevent wrong command usage
- Add Command Formatter
- Use ResetFast for all Pools
- Add Reset func to Li::Vertex (for ResetFast)
- Add Pool Expandor and Put funcs to Lithium pools
- Add getters for GfxDriverBase to PD::Li Pools
2026-03-21 13:35:16 +01:00
tobid7 3b0b103eb3 Add nintendo switch support 2026-03-21 10:18:01 +01:00
tobid7 6beef97cdf Remove Gfx Driver Vertex and IndexBuffer size config 2026-03-21 10:11:10 +01:00
tobid7 afe30a5dbd Add Drawlist
- Add Pool iterator support
- Add Pool Expandability
- Add Pool::Push
- Add Lithium Maths API
- Remove InitPools
- update spirv-helper
2026-03-19 22:06:58 +01:00
tobid7 e6cd8b7d24 Merge branch 'dev-0.7-rewrite' of ssh://dev.npid7.de:222/tobid7/palladium into dev-0.7-rewrite 2026-03-19 16:26:07 +01:00
tobid7 66602a79e0 Replace spirv-helper stuff with a library that combines everything 2026-03-19 15:54:01 +01:00
tobid7 a28b7318d2 Add the shaders 2026-03-19 09:22:46 +01:00
tobid7 0501a4f87c Make UI7::IO::CurrentViewPort a Reference (finally) 2026-03-18 21:33:35 +01:00
tobid7 71563e8979 Add pdsg (palladium spirv-generator)
- remove glslang from pd-system
- use spirv blobs instead of glsl 460
- Use Legacy shaders for OpenGL2 backend
- add devnotes
2026-03-18 21:21:00 +01:00
tobid7 7d89ab1c47 Add SpirvHelper (Shader cross compilation)
- Make OpenGL2 and OpenGL3 use the same base shaders (using SpirvHelper)
- Add Transpose func to Mat4
2026-03-18 15:17:48 +01:00
tobid7 186fce803e Add missing sources 2026-03-18 11:37:44 +01:00
tobid7 66412ca8e0 Full 3ds support and fix dx9 2026-03-18 11:34:36 +01:00
tobid7 e04046720b Work at 3ds support and backend upgrades
- Track textures (not sure if this is done tbh)
- Add lithium formatters and move TextureID, TextureFormat and TextureFilter to lithium
- Only include gl-helper if any glDriver is included
- Add Li::Rect for UV stuff
- Add Li::Texture as Info holder (still thinking of making them to ptrs
- Add Check if textures are still loaded on exit
2026-03-18 09:31:47 +01:00
tobid7 d4c59e5b61 Add backends
- Renamed GfxOpenGL to GfxOPenGL2
- Added GfxOpenGL3 backend for OpenGL 3.3+
- Added WIP DirectX9 backend
- Added structure for Citro3D
- Added linear Allocator
2026-03-17 16:47:19 +01:00
tobid7 3d8b57ead6 add .gitattributes file 2026-03-16 20:28:30 +01:00
tobid7 fe9194b907 More work to drivers
- Add gfx_test
- add texture loading to GfxOpenGL
- add full submit code
- add debug logging
- add construct and destroy functionality to Pool
- add command functionality
- add vertex and index pools to lithium (static and not threadsafe yet)
- Update GfxDriver Matrix with SetViewPort
- Add glfw (only dependency of gfx_test) maybe later required for input driver
2026-03-16 17:33:46 +01:00
tobid7 4924d86bc0 # Work at gfx driver system
- Update pool to use template allocator directly instead of std::vector
- Add a GfxDriver config Template to be able to modify settings like allocators / Types for specific Drivers
- Add glad
2026-03-16 15:19:12 +01:00
tobid7 41b612ec0a 0.7.0 rewrite dev
- remove everyting
- keep core
-rename bit_utils to bits
- add formatter for  color
- add float getters to color
- start with new drivers api
2026-03-16 06:37:51 +01:00
tobid7 ac37ce9e12 Fix test 2026-03-14 21:15:40 +01:00
tobid7 ff1b574276 [pd-ui7] add getters for ui7 menu pos and size 2026-03-13 20:55:40 +01:00
tobid7 1b47ed843f [pd-drivers]: Ad more debug values
[pd-3ds]: Use new debug values
[pd-lithium]: Add functionality to DrawList::Optimize
[pd-ui7]: Add setters for Menu pos ans size
2026-03-12 20:18:27 +01:00
tobid7 02d2200edd Fix Render discard issue [pd-3ds]
- Clip Rects do now not discard when they move out of screen (cause the maths ends in < 0 which is not working with u32)
- Fixing ui7 crash wehn im input was nullptr
2026-03-10 18:57:29 +01:00
tobid7 97efca5173 Merge branch 'stable' of https://github.com/tobid7/palladium into stable 2026-03-09 20:47:53 +01:00
tobid7 68cc809555 Fix Drawlist::Clear / Add HashID / FNV Type traits 2026-03-09 20:46:41 +01:00
tobid7 ca490df915 Add U8Iterator and Pool base class 2026-03-05 20:24:47 +01:00
tobid7 da0f7320c8 Add stuff for cmake find_package
- Add build* to gitignore
- Fix year in license file
- other changes are result of clang-format
2026-03-01 21:41:53 +01:00
tobid7 8ee7006d2c Export OS correctly 2026-01-30 20:19:13 +01:00
tobid7 683c226ce0 Fix MSVC dll building 2026-01-29 20:17:04 +01:00
tobid7 1ec06a26cc Fix GL Scissor in Desktop Backend 2026-01-28 07:40:34 +01:00
tobid7 21b45f5855 WIP: Add CLipRects to UI7 Layouts
- Added Scissor Support to Font Rendering
2026-01-28 07:11:56 +01:00
tobid7 090656b30c Fix close sym input logic 2026-01-27 08:06:52 +01:00
tobid7 e8072a064c WIP Backend System Redesign Step 1
- Created 1 Context for Backend Management and Sharing
- Made every class that used a static Backend require the Context or specific Backend
- Bring Back 3ds support
2026-01-26 20:46:27 +01:00
tobid7 892f8ce0c4 Update License Header Text 2026-01-25 21:12:16 +01:00
tobid7 c373b15bab remove drivers pd_p_api header 2026-01-25 21:00:50 +01:00
tobid7 fb46f4d36a Let's just use 1 PD_API header 2026-01-25 20:57:14 +01:00
tobid7 337c016824 Unfiy all sub projects back into 1 libpalladium 2026-01-25 20:44:52 +01:00
242 changed files with 35075 additions and 53769 deletions
+6
View File
@@ -0,0 +1,6 @@
CompileFlags:
CompilationDatabase: build/default-release
Add: []
Completion:
HeaderInsertion: Never
+1
View File
@@ -0,0 +1 @@
vendor/* linguist-vendored
+2 -1
View File
@@ -1,3 +1,4 @@
build/ build*/
.cache .cache
.vscode .vscode
.DS_Store
+12 -6
View File
@@ -1,6 +1,12 @@
[submodule "backends/desktop/glfw"] [submodule "vendor/stb"]
path = backends/desktop/glfw path = vendor/stb
url = https://github.com/glfw/glfw url = https://github.com/nothings/stb.git
[submodule "backends/3ds/libpicasso"] [submodule "vendor/glfw"]
path = backends/3ds/libpicasso path = vendor/glfw
url = https://github.com/npid7/libpicasso url = https://github.com/glfw/glfw.git
[submodule "vendor/libpicasso"]
path = vendor/libpicasso
url = https://github.com/tobid7/libpicasso
[submodule "vendor/spirv-helper"]
path = vendor/spirv-helper
url = https://github.com/tobid7/spirv-helper.git
+163 -15
View File
@@ -1,36 +1,184 @@
cmake_minimum_required(VERSION 3.22) cmake_minimum_required(VERSION 3.22)
include(GNUInstallDirs)
# Set Project # Set Project
project(palladium LANGUAGES C CXX VERSION 0.6.0) project(palladium LANGUAGES C CXX VERSION 0.7.0)
# Required to add this Variable # Required to add this Variable
set(PD_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include) set(PD_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include)
include(cmake/palladium.cmake) include(cmake/palladium.cmake)
option(PD_BUILD_TESTS "Sets if TestApp and TestBench get build" OFF) option(PD_BUILD_TESTS "Sets if TestApp and TestBench get build" OFF)
option(PD_BUILD_SHARED "Build Shared Libraries" OFF) option(PD_BUILD_SHARED "Build Shared Library" OFF)
option(PD_BUILD_TOOLS "Build Palladium Tools" OFF) option(PD_BUILD_TOOLS "Build Palladium Tools" OFF)
option(PD_INCLUDE_STB_IMAGE "Inlude stb image symbols in palladium" ON)
if(${CMAKE_SYSTEM_NAME} STREQUAL "Nintendo3DS")
add_compile_options(-Wno-psabi)
endif()
if(${PD_BUILD_TOOLS}) if(${PD_BUILD_TOOLS})
add_subdirectory(tools) add_subdirectory(tools)
endif() endif()
## Include Library Source add_subdirectory(vendor)
add_subdirectory(pd/drivers) # # Include Library Source
add_subdirectory(pd/core) set(PD_SOURCES
add_subdirectory(pd/image) # Common
add_subdirectory(pd/external) source/common.cpp
add_subdirectory(pd/lithium)
add_subdirectory(pd/ui7)
add_library(palladium INTERFACE) # Core
target_link_libraries(palladium INTERFACE source/core/bits.cpp
pd-core pd-image pd-external pd-lithium pd-ui7 #pd-net source/core/color.cpp
source/core/io.cpp
source/core/mat.cpp
source/core/strings.cpp
source/core/timer.cpp
source/core/timetrace.cpp
# Image
source/image/convert.cpp
source/image/image.cpp
# Drivers
source/drivers/os.cpp
source/drivers/gfx.cpp
source/drivers/hid.cpp
# Lithium
source/lithium/command.cpp
source/lithium/drawlist.cpp
source/lithium/font.cpp
source/lithium/math.cpp
source/lithium/pools.cpp
# UI7
source/ui7/io.cpp
source/ui7/layout.cpp
source/ui7/menu.cpp
source/ui7/theme.cpp
source/ui7/ui7.cpp
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
# Ultra
source/ultra/canvas.cpp
source/ultra/layout.cpp
source/ultra/elems/element.cpp
source/ultra/elems/rect.cpp
source/ultra/elems/text.cpp
source/ultra/elems/image.cpp
source/ultra/elems/button.cpp
) )
add_dependencies(palladium if(${PD_BUILD_SHARED})
pd-drivers pd-core pd-image pd-external pd-lithium pd-ui7 #pd-net add_library(palladium SHARED ${PD_SOURCES})
target_compile_definitions(palladium PRIVATE PD_BUILD_SHARED)
else()
add_library(palladium STATIC ${PD_SOURCES})
target_compile_definitions(palladium PUBLIC PD_BUILD_STATIC)
endif()
target_link_libraries(palladium
PUBLIC stb
) )
install(DIRECTORY include DESTINATION ".") target_compile_definitions(palladium
PUBLIC
PD_DEBUG
$<$<OR:$<BOOL:${PD_INCLUDE_STB_IMAGE}>,$<BOOL:${PD_BUILD_SHARED}>>:PD_INCLUDE_STB_IMAGE> # Always on in shared build
)
target_compile_options(palladium
PUBLIC $<$<CXX_COMPILER_ID:GNU,Clang>:
-fmacro-prefix-map=${CMAKE_CURRENT_SOURCE_DIR}/source=source
-fmacro-prefix-map=${CMAKE_CURRENT_SOURCE_DIR}/include=include
>
)
add_library(palladium::palladium ALIAS palladium)
target_include_directories(palladium
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<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>
$<$<AND:$<CONFIG:Debug>,$<CXX_COMPILER_ID:MSVC>>:/Od /Zi>
$<$<AND:$<CONFIG:Release>,$<CXX_COMPILER_ID:MSVC>>:/O2>
)
install(
TARGETS palladium
EXPORT palladiumTargets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
install(
DIRECTORY include/
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)
install(EXPORT palladiumTargets
FILE palladiumTargets.cmake
NAMESPACE palladium::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/palladium
)
include(CMakePackageConfigHelpers)
configure_package_config_file(
cmake/palladiumConfig.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/palladiumConfig.cmake
INSTALL_DESTINATION
${CMAKE_INSTALL_LIBDIR}/cmake/palladium
)
write_basic_package_version_file(
${CMAKE_CURRENT_BINARY_DIR}/palladiumConfigVersion.cmake
VERSION
${PROJECT_VERSION}
COMPATIBILITY
SameMajorVersion
)
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
${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp
${CMAKE_CURRENT_SOURCE_DIR}/include/*.hpp
${CMAKE_CURRENT_SOURCE_DIR}/backends/source/*.cpp
${CMAKE_CURRENT_SOURCE_DIR}/backends/include/*.hpp
${CMAKE_CURRENT_SOURCE_DIR}/tests/core/source/*.cpp
${CMAKE_CURRENT_SOURCE_DIR}/tests/gfx/source/*.cpp
)
add_custom_target(pd-clang-format
COMMAND ${CLANG_FORMAT} --style=file -i ${PD_FMTFILES}
COMMENT "Formatting Project Sources"
)
add_subdirectory(backends)
if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/tests)
endif()
+14
View File
@@ -0,0 +1,14 @@
{
"version": 10,
"cmakeMinimumRequired": {
"major": 3,
"minor": 22,
"patch": 0
},
"include": [
"cmake/presets/default.json",
"cmake/presets/3ds.json",
"cmake/presets/switch.json",
"cmake/presets/msvc.json"
]
}
+1 -1
View File
@@ -1,6 +1,6 @@
MIT License MIT License
Copyright (c) 2024 - 2025 René Amthor tobid7 Copyright (c) 2024 - 2026 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
-13
View File
@@ -1,13 +0,0 @@
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)
pd_add_lib(pd-3ds SRC_FILES ${SRC})
target_include_directories(pd-3ds PUBLIC include)
target_link_libraries(pd-3ds PUBLIC m palladium ctru citro3d pica::pica)
-33
View File
@@ -1,33 +0,0 @@
#pragma once
/*
MIT License
Copyright (c) 2024 - 2025 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();
void Init(void* data = nullptr);
} // namespace PD
-61
View File
@@ -1,61 +0,0 @@
#pragma once
/*
MIT License
Copyright (c) 2024 - 2025 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() : 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;
};
} // namespace PD
-270
View File
@@ -1,270 +0,0 @@
/*
MIT License
Copyright (c) 2024 - 2025 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 = CurrentVertex;
IndexCounter = CurrentIndex;
}
void GfxC3D::BindTex(PD::Li::TexAddress addr) {
C3D_TexBind(0, reinterpret_cast<C3D_Tex*>(addr));
}
void GfxC3D::RenderDrawData(const PD::Li::CmdPool& Commands) {
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::Texture::Ref 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);
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,
ViewPort.y - (ScissorRect.y + ScissorRect.w),
ViewPort.x - (ScissorRect.x + ScissorRect.z),
ViewPort.y - ScissorRect.y, ViewPort.x - ScissorRect.x);
} else {
C3D_SetScissor(GPU_SCISSOR_DISABLE, 0, 0, 0, 0);
}
FragCfg(((C3D_Tex*)Tex->Address)->fmt);
BindTex(Tex->Address);
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);
}
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
-87
View File
@@ -1,87 +0,0 @@
/*
MIT License
Copyright (c) 2024 - 2025 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() : 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
-132
View File
@@ -1,132 +0,0 @@
/*
MIT License
Copyright (c) 2024 - 2025 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() {
TT::Scope st("LI_SystemFont"); // Trace loading time
Li::Font::Ref ret = Li::Font::New();
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
+90
View File
@@ -0,0 +1,90 @@
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)
option(PD_ENABLE_HID_GLFW "Enable GLFW Input Driver" ON)
option(PD_ENABLE_HID_NX "Enable NX Input Driver" OFF)
option(PD_ENABLE_HID3DS "Enable 3ds Input Driver" OFF)
if(NOT WIN32) # cause we are not on windows...
set(PD_ENABLE_DIRECTX9 OFF)
endif()
if(${CMAKE_SYSTEM_NAME} STREQUAL "Nintendo3DS")
set(PD_ENABLE_OPENGL2 OFF)
set(PD_ENABLE_OPENGL3 OFF)
set(PD_ENABLE_VULKAN OFF)
set(PD_ENABLE_CITRO3D ON)
set(PD_ENABLE_HID_GLFW OFF)
set(PD_ENABLE_HID_NX OFF)
set(PD_ENABLE_HID3DS ON)
elseif(${CMAKE_SYSTEM_NAME} STREQUAL "NintendoSwitch")
set(PD_ENABLE_OPENGL2 OFF)
set(PD_ENABLE_OPENGL3 ON)
set(PD_ENABLE_VULKAN OFF)
set(PD_ENABLE_CITRO3D OFF)
set(PD_ENABLE_HID_NX ON) # Best case for Nintendo Switch
set(PD_ENABLE_HID_GLFW ON) # Technically supported but not recommended
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
${CMAKE_CURRENT_SOURCE_DIR}/source/hid_3ds.cpp
${CMAKE_CURRENT_SOURCE_DIR}/source/hid_glfw.cpp
${CMAKE_CURRENT_SOURCE_DIR}/source/hid_nx.cpp
)
target_include_directories(pd-system
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
# Why is this not a default include (same problem as with the __SWITCH__ define)
$<$<STREQUAL:${CMAKE_SYSTEM_NAME},NintendoSwitch>:${DEVKITPRO}/portlibs/switch/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>
$<$<BOOL:${PD_ENABLE_CITRO3D}>:PD_ENABLE_CITRO3D>
$<$<BOOL:${PD_ENABLE_HID_GLFW}>:PD_ENABLE_HID_GLFW>
$<$<BOOL:${PD_ENABLE_HID_NX}>:PD_ENABLE_HID_NX>
$<$<BOOL:${PD_ENABLE_HID3DS}>:PD_ENABLE_HID3DS>
)
# Palladium
target_link_libraries(pd-system PUBLIC palladium::palladium)
# Depandant Lib includes (i love this cmake feature)
target_link_libraries(pd-system PUBLIC
$<$<BOOL:${PD_ENABLE_DIRECTX9}>:d3d9 d3dcompiler> # DirectX9
$<$<BOOL:${PD_ENABLE_CITRO3D}>:pica::pica citro3d ctru> # 3ds
$<$<BOOL:${PD_ENABLE_OPENGL3}>:spirv-helper> # OpenGL3
# Include Glad if we have any OpenGL Usage
$<$<OR:$<BOOL:${PD_ENABLE_OPENGL2}>,$<BOOL:${PD_ENABLE_OPENGL3}>>:glad>
$<$<BOOL:${PD_ENABLE_HID_NX}>:nx> # Hid NX requirement
# Hid GLFW requirement
$<$<BOOL:${PD_ENABLE_HID_GLFW}>:
$<$<STREQUAL:${CMAKE_SYSTEM_NAME},NintendoSwitch>:glfw3>
$<$<NOT:$<STREQUAL:${CMAKE_SYSTEM_NAME},NintendoSwitch>>:glfw>>
)
+24
View File
@@ -0,0 +1,24 @@
# Backends
- [Graphics](#graphics)
- [OpenGL2](#opengl2-legacy)
- [OpenGL3](#opengl3-modern-opengl)
- [DirectX9](#directx9-could-potentially-cover-more-then-opengl2)
- [Citro3D](#citro3d-3ds)
## Graphics
### OpenGL2 (Legacy)
> [!NOTE]
> As SPIRV to GLSL 120 translation doesn't work, your'e forced to write separate shaders for this backend
### OpenGL3+ (Modern OpenGL)
### DirectX9 (Could potentially cover more than OpenGL2)
### Citro3D (3DS)
> [!NOTE]
> This backend requires shaders as binary file, directly in the code or as string for libpicasso
> The Fragment Stage is special and is still wip
-28
View File
@@ -1,28 +0,0 @@
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
)
pd_add_lib(pd-desktop SRC_FILES ${SRC})
target_include_directories(pd-desktop PUBLIC include)
target_link_libraries(pd-desktop PUBLIC palladium glad glfw)
target_compile_definitions(pd-desktop
PRIVATE PD_OPENGL=${PD_GL_VERSION}
)
-4
View File
@@ -1,4 +0,0 @@
cmake_minimum_required(VERSION 3.22)
add_library(glad source/glad.c)
target_include_directories(glad PUBLIC include)
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
-32
View File
@@ -1,32 +0,0 @@
#pragma once
/*
MIT License
Copyright (c) 2024 - 2025 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);
}
@@ -1,90 +0,0 @@
#pragma once
/*
MIT License
Copyright (c) 2024 - 2025 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() : 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
@@ -1,73 +0,0 @@
#pragma once
/*
MIT License
Copyright (c) 2024 - 2025 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(GLFWwindow* win);
~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 (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] = 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
-500
View File
@@ -1,500 +0,0 @@
/*
MIT License
Copyright (c) 2024 - 2025 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
}
}
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));
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
-176
View File
@@ -1,176 +0,0 @@
/*
MIT License
Copyright (c) 2024 - 2025 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(GLFWwindow* win) : HidDriver("HidGLFW") {
Window = win;
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
-43
View File
@@ -1,43 +0,0 @@
/*
MIT License
Copyright (c) 2024 - 2025 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
@@ -1,8 +1,12 @@
#pragma once #pragma once
#ifdef __3DS__
#include <3ds.h> #include <3ds.h>
#endif
#include <pd/core/common.hpp> #include <limits>
#include <memory>
#include <stdexcept>
// Custom C++ Allocator class to interface with libctru linear heap memory // Custom C++ Allocator class to interface with libctru linear heap memory
// based on this guide: // based on this guide:
@@ -21,9 +25,17 @@ class LinearAllocator {
if (n > max_size()) { if (n > max_size()) {
throw std::runtime_error("[PD] LinearAllocator: Bad alloc!"); throw std::runtime_error("[PD] LinearAllocator: Bad alloc!");
} }
#ifdef __3DS__
return static_cast<T*>(linearAlloc(n * sizeof(T))); 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); } 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> template <class U, class... Args>
void construct(U* p, Args&&... args) { void construct(U* p, Args&&... args) {
@@ -42,8 +54,14 @@ class LinearAllocator {
return false; return false;
} }
#ifdef __3DS__
// Use linearSpace free as max_size to not allocate out of bounds // Use linearSpace free as max_size to not allocate out of bounds
// or to b eable to see a crash report screen. // or to b eable to see a crash report screen.
size_t max_size() const noexcept { return linearSpaceFree(); } 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 } // namespace PD
@@ -0,0 +1,38 @@
#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>;
};
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;
Li::Texture LoadTexture(
const std::vector<PD::u8>& pixels, int w, int h,
TextureFormat type = TextureFormat::RGBA32,
TextureFilter filter = TextureFilter::Linear) override;
void DeleteTexture(const Li::Texture& tex) override;
void UploadPools() override;
void ClipRect() override;
private:
struct Impl;
Impl* impl = nullptr;
};
} // namespace PD
@@ -0,0 +1,39 @@
#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>;
};
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;
Li::Texture LoadTexture(
const std::vector<PD::u8>& pixels, int w, int h,
TextureFormat type = TextureFormat::RGBA32,
TextureFilter filter = TextureFilter::Linear) override;
void DeleteTexture(const Li::Texture& tex) override;
void UploadPools() override;
void ClipRect() override;
private:
struct Impl;
Impl* impl = nullptr;
void* pDevice = nullptr;
};
} // namespace PD
@@ -0,0 +1,45 @@
#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>;
};
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;
Li::Texture LoadTexture(
const std::vector<PD::u8>& pixels, int w, int h,
TextureFormat type = TextureFormat::RGBA32,
TextureFilter filter = TextureFilter::Linear) override;
void DeleteTexture(const Li::Texture& tex) override;
void UploadPools() override;
void ClipRect() override;
private:
void pSetupShaderAttribs(u32 shader);
u32 pShader = 0;
u32 VBO = 0;
u32 IBO = 0;
int pLocTex = 0;
int pLocAlfa = 0;
int pLocIsSDF = 0;
int pLocProjection = 0;
static const char* pVertCode;
static const char* pFragCode;
};
} // namespace PD
@@ -0,0 +1,43 @@
#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>;
};
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;
Li::Texture LoadTexture(
const std::vector<PD::u8>& pixels, int w, int h,
TextureFormat type = TextureFormat::RGBA32,
TextureFilter filter = TextureFilter::Linear) override;
void DeleteTexture(const Li::Texture& tex) override;
void UploadPools() override;
void ClipRect() override;
private:
u32 pShader = 0;
u32 VBO = 0;
u32 IBO = 0;
u32 VAO = 0;
int pLocTex = 0;
int pLocAlfa = 0;
int pLocIsSDF = 0;
int pLocProjection = 0;
};
} // namespace PD
+7
View File
@@ -0,0 +1,7 @@
#pragma once
#include <pd/common.hpp>
namespace PD {
u32 CreateShaderProgram(const char* vert, const char* frag);
}
+17
View File
@@ -0,0 +1,17 @@
#pragma once
#include <pd/drivers/hid.hpp>
namespace PD {
class Hid3DS : public HidDriver {
public:
Hid3DS();
~Hid3DS();
void Update() override;
private:
struct Impl;
Impl* impl;
};
} // namespace PD
+21
View File
@@ -0,0 +1,21 @@
#pragma once
#include <pd/drivers/hid.hpp>
typedef struct GLFWwindow GLFWwindow;
typedef struct GLFWgamepadstate GLFWgamepadstate;
namespace PD {
class HidGlfw : public HidDriver {
public:
HidGlfw(GLFWwindow* window);
~HidGlfw();
void Update() override;
private:
void HandleAxisKey(GLFWgamepadstate s, int iK, int eA, bool negative);
struct Impl;
Impl* impl;
};
} // namespace PD
+17
View File
@@ -0,0 +1,17 @@
#pragma once
#include <pd/drivers/hid.hpp>
namespace PD {
class HidNX : public HidDriver {
public:
HidNX();
~HidNX();
void Update() override;
private:
struct Impl;
Impl* impl;
};
} // namespace PD
+17
View File
@@ -0,0 +1,17 @@
#pragma once
namespace PD {
/**
* Shader blobs
* Required for OpenGL3, and maybe later for vulkan
* Shaders can be found in /tools/pdsg/shaders
*/
namespace Shaders {
// clang-format off
const unsigned int VertexShader[] = { 0x7230203, 0x10000, 0x8000B, 0x2B, 0x0, 0x20011, 0x1, 0x6000B, 0x1, 0x4C534C47, 0x6474732E, 0x3035342E, 0x0, 0x3000E, 0x0, 0x1, 0xB000F, 0x0, 0x4, 0x6E69616D, 0x0, 0xD, 0x19, 0x24, 0x25, 0x27, 0x29, 0x30003, 0x2, 0x1CC, 0x40005, 0x4, 0x6E69616D, 0x0, 0x60005, 0xB, 0x505F6C67, 0x65567265, 0x78657472, 0x0, 0x60006, 0xB, 0x0, 0x505F6C67, 0x7469736F, 0x6E6F69, 0x70006, 0xB, 0x1, 0x505F6C67, 0x746E696F, 0x657A6953, 0x0, 0x70006, 0xB, 0x2, 0x435F6C67, 0x4470696C, 0x61747369, 0x65636E, 0x70006, 0xB, 0x3, 0x435F6C67, 0x446C6C75, 0x61747369, 0x65636E, 0x30005, 0xD, 0x0, 0x30005, 0x11, 0x4F4255, 0x60006, 0x11, 0x0, 0x6A6F7270, 0x69746365, 0x6E6F, 0x30005, 0x13, 0x6F6275, 0x30005, 0x19, 0x736F70, 0x30005, 0x24, 0x56556F, 0x30005, 0x25, 0x7675, 0x40005, 0x27, 0x6C6F436F, 0x726F, 0x40005, 0x29, 0x6F6C6F63, 0x72, 0x30047, 0xB, 0x2, 0x50048, 0xB, 0x0, 0xB, 0x0, 0x50048, 0xB, 0x1, 0xB, 0x1, 0x50048, 0xB, 0x2, 0xB, 0x3, 0x50048, 0xB, 0x3, 0xB, 0x4, 0x30047, 0x11, 0x2, 0x40048, 0x11, 0x0, 0x5, 0x50048, 0x11, 0x0, 0x7, 0x10, 0x50048, 0x11, 0x0, 0x23, 0x0, 0x40047, 0x13, 0x21, 0x0, 0x40047, 0x13, 0x22, 0x0, 0x40047, 0x19, 0x1E, 0x0, 0x40047, 0x24, 0x1E, 0x0, 0x40047, 0x25, 0x1E, 0x1, 0x40047, 0x27, 0x1E, 0x1, 0x40047, 0x29, 0x1E, 0x2, 0x20013, 0x2, 0x30021, 0x3, 0x2, 0x30016, 0x6, 0x20, 0x40017, 0x7, 0x6, 0x4, 0x40015, 0x8, 0x20, 0x0, 0x4002B, 0x8, 0x9, 0x1, 0x4001C, 0xA, 0x6, 0x9, 0x6001E, 0xB, 0x7, 0x6, 0xA, 0xA, 0x40020, 0xC, 0x3, 0xB, 0x4003B, 0xC, 0xD, 0x3, 0x40015, 0xE, 0x20, 0x1, 0x4002B, 0xE, 0xF, 0x0, 0x40018, 0x10, 0x7, 0x4, 0x3001E, 0x11, 0x10, 0x40020, 0x12, 0x2, 0x11, 0x4003B, 0x12, 0x13, 0x2, 0x40020, 0x14, 0x2, 0x10, 0x40017, 0x17, 0x6, 0x2, 0x40020, 0x18, 0x1, 0x17, 0x4003B, 0x18, 0x19, 0x1, 0x4002B, 0x6, 0x1B, 0x0, 0x4002B, 0x6, 0x1C, 0x3F800000, 0x40020, 0x21, 0x3, 0x7, 0x40020, 0x23, 0x3, 0x17, 0x4003B, 0x23, 0x24, 0x3, 0x4003B, 0x18, 0x25, 0x1, 0x4003B, 0x21, 0x27, 0x3, 0x40020, 0x28, 0x1, 0x7, 0x4003B, 0x28, 0x29, 0x1, 0x50036, 0x2, 0x4, 0x0, 0x3, 0x200F8, 0x5, 0x50041, 0x14, 0x15, 0x13, 0xF, 0x4003D, 0x10, 0x16, 0x15, 0x4003D, 0x17, 0x1A, 0x19, 0x50051, 0x6, 0x1D, 0x1A, 0x0, 0x50051, 0x6, 0x1E, 0x1A, 0x1, 0x70050, 0x7, 0x1F, 0x1D, 0x1E, 0x1B, 0x1C, 0x50091, 0x7, 0x20, 0x16, 0x1F, 0x50041, 0x21, 0x22, 0xD, 0xF, 0x3003E, 0x22, 0x20, 0x4003D, 0x17, 0x26, 0x25, 0x3003E, 0x24, 0x26, 0x4003D, 0x7, 0x2A, 0x29, 0x3003E, 0x27, 0x2A, 0x100FD, 0x10038, };
// clang-format on
// clang-format off
const unsigned int FragmentShader[] = { 0x7230203, 0x10000, 0x8000B, 0x50, 0x0, 0x20011, 0x1, 0x6000B, 0x1, 0x4C534C47, 0x6474732E, 0x3035342E, 0x0, 0x3000E, 0x0, 0x1, 0x8000F, 0x0, 0x4, 0x6E69616D, 0x0, 0x11, 0x2C, 0x2E, 0x30003, 0x2, 0x1CC, 0x40005, 0x4, 0x6E69616D, 0x0, 0x30005, 0x9, 0x6374, 0x30005, 0xD, 0x786574, 0x30005, 0x11, 0x56556F, 0x50005, 0x16, 0x68737550, 0x61746144, 0x0, 0x50006, 0x16, 0x0, 0x61666C61, 0x0, 0x50006, 0x16, 0x1, 0x735F7369, 0x6664, 0x40005, 0x18, 0x68737570, 0x0, 0x40005, 0x23, 0x68706C61, 0x61, 0x50005, 0x2C, 0x67617246, 0x6F6C6F43, 0x72, 0x40005, 0x2E, 0x6C6F436F, 0x726F, 0x40047, 0xD, 0x21, 0x0, 0x40047, 0xD, 0x22, 0x0, 0x40047, 0x11, 0x1E, 0x0, 0x30047, 0x16, 0x2, 0x50048, 0x16, 0x0, 0x23, 0x0, 0x50048, 0x16, 0x1, 0x23, 0x4, 0x40047, 0x2E, 0x1E, 0x1, 0x20013, 0x2, 0x30021, 0x3, 0x2, 0x30016, 0x6, 0x20, 0x40017, 0x7, 0x6, 0x4, 0x40020, 0x8, 0x7, 0x7, 0x90019, 0xA, 0x6, 0x1, 0x0, 0x0, 0x0, 0x1, 0x0, 0x3001B, 0xB, 0xA, 0x40020, 0xC, 0x0, 0xB, 0x4003B, 0xC, 0xD, 0x0, 0x40017, 0xF, 0x6, 0x2, 0x40020, 0x10, 0x1, 0xF, 0x4003B, 0x10, 0x11, 0x1, 0x4002B, 0x6, 0x13, 0x0, 0x40015, 0x15, 0x20, 0x1, 0x4001E, 0x16, 0x15, 0x15, 0x40020, 0x17, 0x9, 0x16, 0x4003B, 0x17, 0x18, 0x9, 0x4002B, 0x15, 0x19, 0x1, 0x40020, 0x1A, 0x9, 0x15, 0x4002B, 0x15, 0x1D, 0x0, 0x20014, 0x1E, 0x40020, 0x22, 0x7, 0x6, 0x4002B, 0x6, 0x24, 0x3EE66666, 0x4002B, 0x6, 0x25, 0x3F0CCCCD, 0x40015, 0x26, 0x20, 0x0, 0x4002B, 0x26, 0x27, 0x3, 0x40020, 0x2B, 0x3, 0x7, 0x4003B, 0x2B, 0x2C, 0x3, 0x40020, 0x2D, 0x1, 0x7, 0x4003B, 0x2D, 0x2E, 0x1, 0x40017, 0x2F, 0x6, 0x3, 0x40020, 0x33, 0x1, 0x6, 0x50036, 0x2, 0x4, 0x0, 0x3, 0x200F8, 0x5, 0x4003B, 0x8, 0x9, 0x7, 0x4003B, 0x22, 0x23, 0x7, 0x4003D, 0xB, 0xE, 0xD, 0x4003D, 0xF, 0x12, 0x11, 0x70058, 0x7, 0x14, 0xE, 0x12, 0x2, 0x13, 0x3003E, 0x9, 0x14, 0x50041, 0x1A, 0x1B, 0x18, 0x19, 0x4003D, 0x15, 0x1C, 0x1B, 0x500AB, 0x1E, 0x1F, 0x1C, 0x1D, 0x300F7, 0x21, 0x0, 0x400FA, 0x1F, 0x20, 0x3B, 0x200F8, 0x20, 0x50041, 0x22, 0x28, 0x9, 0x27, 0x4003D, 0x6, 0x29, 0x28, 0x8000C, 0x6, 0x2A, 0x1, 0x31, 0x24, 0x25, 0x29, 0x3003E, 0x23, 0x2A, 0x4003D, 0x7, 0x30, 0x2E, 0x8004F, 0x2F, 0x31, 0x30, 0x30, 0x0, 0x1, 0x2, 0x4003D, 0x6, 0x32, 0x23, 0x50041, 0x33, 0x34, 0x2E, 0x27, 0x4003D, 0x6, 0x35, 0x34, 0x50085, 0x6, 0x36, 0x32, 0x35, 0x50051, 0x6, 0x37, 0x31, 0x0, 0x50051, 0x6, 0x38, 0x31, 0x1, 0x50051, 0x6, 0x39, 0x31, 0x2, 0x70050, 0x7, 0x3A, 0x37, 0x38, 0x39, 0x36, 0x3003E, 0x2C, 0x3A, 0x200F9, 0x21, 0x200F8, 0x3B, 0x50041, 0x1A, 0x3C, 0x18, 0x1D, 0x4003D, 0x15, 0x3D, 0x3C, 0x500AB, 0x1E, 0x3E, 0x3D, 0x1D, 0x300F7, 0x40, 0x0, 0x400FA, 0x3E, 0x3F, 0x4C, 0x200F8, 0x3F, 0x4003D, 0x7, 0x41, 0x2E, 0x8004F, 0x2F, 0x42, 0x41, 0x41, 0x0, 0x1, 0x2, 0x50041, 0x22, 0x43, 0x9, 0x27, 0x4003D, 0x6, 0x44, 0x43, 0x50041, 0x33, 0x45, 0x2E, 0x27, 0x4003D, 0x6, 0x46, 0x45, 0x50085, 0x6, 0x47, 0x44, 0x46, 0x50051, 0x6, 0x48, 0x42, 0x0, 0x50051, 0x6, 0x49, 0x42, 0x1, 0x50051, 0x6, 0x4A, 0x42, 0x2, 0x70050, 0x7, 0x4B, 0x48, 0x49, 0x4A, 0x47, 0x3003E, 0x2C, 0x4B, 0x200F9, 0x40, 0x200F8, 0x4C, 0x4003D, 0x7, 0x4D, 0x9, 0x4003D, 0x7, 0x4E, 0x2E, 0x50085, 0x7, 0x4F, 0x4D, 0x4E, 0x3003E, 0x2C, 0x4F, 0x200F9, 0x40, 0x200F8, 0x40, 0x200F9, 0x21, 0x200F8, 0x21, 0x100FD, 0x10038, };
// clang-format on
} // namespace Shaders
} // namespace PD
+11
View File
@@ -0,0 +1,11 @@
#pragma once
// Gfx
#include <pd_system/gfx_citro3d.hpp>
#include <pd_system/gfx_directx9.hpp>
#include <pd_system/gfx_opengl2.hpp>
#include <pd_system/gfx_opengl3.hpp>
// Hid
#include <pd_system/hid_3ds.hpp>
#include <pd_system/hid_glfw.hpp>
+299
View File
@@ -0,0 +1,299 @@
// Well yes, i finally try it
#include <pd/core/core.hpp>
#include <pd/lithium/formatters.hpp>
#include <pd_system/gfx_citro3d.hpp>
#if defined(PD_ENABLE_CITRO3D) && defined(__3DS__)
#include <3ds.h>
#include <citro3d.h>
#include <pd/drivers/drivers.hpp>
#include <pica.hpp>
namespace PD {
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
)";
struct GfxCitro3D::Impl {
C3D_AttrInfo pAttr;
shaderProgram_s pShader;
DVLB_s* pCode;
int uLocProjection = 0;
C3D_Tex* CurrentTex = nullptr;
std::vector<u8> pShaderRaw;
GPU_TEXCOLOR TextureTranslateFormat(TextureFormat fmt) {
switch (fmt) {
case PD::TextureFormat::A8:
return GPU_A8;
case PD::TextureFormat::RGB24:
return GPU_RGB8;
case PD::TextureFormat::RGBA32:
return GPU_RGBA8;
default:
return GPU_RGBA8;
}
}
int TextureFormat2Bpp(TextureFormat fmt) {
switch (fmt) {
case PD::TextureFormat::A8:
return 1;
case PD::TextureFormat::RGB24:
return 3;
case PD::TextureFormat::RGBA32:
return 4;
default:
return 0;
}
}
void SetupPixelStage(bool sdf) {
shaderProgramUse(&pShader);
C3D_BindProgram(&pShader);
C3D_SetAttrInfo(&pAttr);
C3D_DepthTest(false, GPU_GREATER, GPU_WRITE_ALL);
if (!CurrentTex) return;
C3D_TexEnv* env = C3D_GetTexEnv(0);
C3D_TexEnvInit(env);
C3D_TexEnv* env1 = C3D_GetTexEnv(1);
C3D_TexEnvInit(env1);
if (sdf) {
C3D_TexEnvSrc(env, C3D_RGB, GPU_PRIMARY_COLOR);
C3D_TexEnvFunc(env, C3D_RGB, GPU_REPLACE);
C3D_TexEnvColor(env, 0x60606060);
C3D_TexEnvSrc(env, C3D_Alpha, GPU_TEXTURE0, GPU_CONSTANT);
C3D_TexEnvOpAlpha(env, GPU_TEVOP_A_SRC_ALPHA, GPU_TEVOP_A_SRC_ALPHA);
C3D_TexEnvFunc(env, C3D_Alpha, GPU_SUBTRACT);
C3D_TexEnvScale(env, C3D_Alpha, GPU_TEVSCALE_4);
C3D_TexEnvSrc(env1, C3D_RGB, GPU_PREVIOUS);
C3D_TexEnvFunc(env1, C3D_RGB, GPU_REPLACE);
C3D_TexEnvSrc(env1, C3D_Alpha, GPU_PREVIOUS, GPU_PRIMARY_COLOR);
C3D_TexEnvOpAlpha(env, GPU_TEVOP_A_SRC_ALPHA, GPU_TEVOP_A_SRC_ALPHA);
C3D_TexEnvFunc(env1, C3D_Alpha, GPU_MODULATE);
} else {
switch (CurrentTex->fmt) {
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 GfxCitro3D::SysInit() {
if (impl) return;
PDLOG("GfxCitro3D::SysInit();");
impl = new Impl();
Flags |= PDGfxBackendFlags_FlipUV_Y;
Flags |= PDGfxBackendFlags_HasClipRect;
Flags |= PDGfxBackendFlags_ReqPow2;
Flags |= PDGfxBackendFlags_ApproxSDF;
impl->pShaderRaw = Pica::AssembleCode(LIShaderCTR);
impl->pCode = DVLB_ParseFile((uint32_t*)impl->pShaderRaw.data(),
impl->pShaderRaw.size());
shaderProgramInit(&impl->pShader);
shaderProgramSetVsh(&impl->pShader, &impl->pCode->DVLE[0]);
impl->uLocProjection = shaderInstanceGetUniformLocation(
impl->pShader.vertexShader, "projection");
AttrInfo_Init(&impl->pAttr);
AttrInfo_AddLoader(&impl->pAttr, 0, GPU_FLOAT, 2);
AttrInfo_AddLoader(&impl->pAttr, 1, GPU_FLOAT, 2);
AttrInfo_AddLoader(&impl->pAttr, 2, GPU_UNSIGNED_BYTE, 4);
}
void GfxCitro3D::SysDeinit() {
if (!impl) return;
shaderProgramFree(&impl->pShader);
DVLB_Free(impl->pCode);
delete impl;
impl = nullptr;
PDLOG("GfxCitro3D::SysDeinit()");
}
void GfxCitro3D::Submit(size_t count, size_t start) {
if (!impl) return;
BindTexture(CurrentTex);
impl->SetupPixelStage(CurrentTexIsSDF); // needs to be called after
C3D_Mtx proj;
Mtx_OrthoTilt(&proj, 0.f, ViewPort.x, ViewPort.y, 0.f, 1.f, -1.f, false);
C3D_FVUnifMtx4x4(GPU_VERTEX_SHADER, impl->uLocProjection, &proj);
// C3D_FVUnifMtx4x4(GPU_VERTEX_SHADER, impl->uLocProjection,
// (C3D_Mtx*)&Projection);
C3D_DrawElements(GPU_TRIANGLES, count, C3D_UNSIGNED_SHORT,
GetIndexBufPtr(start));
impl->CurrentTex = nullptr;
}
void GfxCitro3D::BindTexture(TextureID id) {
if (!impl || !id) return;
impl->CurrentTex = (C3D_Tex*)id;
C3D_TexBind(0, (C3D_Tex*)id);
}
void GfxCitro3D::SysReset() {
if (!impl) return;
C3D_CullFace(GPU_CULL_NONE);
}
Li::Texture GfxCitro3D::LoadTexture(const std::vector<PD::u8>& pixels, int w,
int h, TextureFormat type,
TextureFilter filter) {
if (!impl || w > 1024 || h > 1024) return Li::Texture();
if (pixels.size() !=
static_cast<size_t>(w * h * Li::TextureFormat2Bpp(type))) {
PDERR("Failed to load Texture due to Size mismatch: {} != {}",
pixels.size(),
static_cast<size_t>(w * h * Li::TextureFormat2Bpp(type)));
return Li::Texture();
}
// Don't check here as check done before
PD::Li::Texture res;
int bpp = impl->TextureFormat2Bpp(type);
ivec2 tex_size(w, h);
// Pow2
if (!PD::Bits::IsSingleBit(w)) {
tex_size.x = PD::Bits::GetPow2((unsigned int)w);
}
if (!PD::Bits::IsSingleBit(h)) {
tex_size.y = PD::Bits::GetPow2((unsigned int)h);
}
res.SetSize(w, h);
res.SetUV(0.f, 1.f, ((float)w / (float)tex_size.x),
1.0 - ((float)h / (float)tex_size.y));
// Texture Setup
auto fltr = (filter == TextureFilter::Linear ? GPU_NEAREST : GPU_LINEAR);
auto tex_fmt = impl->TextureTranslateFormat(type);
auto tex = new C3D_Tex;
C3D_TexInit(tex, (u16)tex_size.x, (u16)tex_size.y, tex_fmt);
C3D_TexSetFilter(tex, fltr, fltr);
std::memset((PD::u8*)tex->data, 0x0, tex->size);
/// 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.SetID((TextureID)tex);
RegisterTexture(res);
PDLOG("GfxCitro3D::LoadTexture -> {{ {} }}, [{}, {}]", res, type, filter);
return res;
}
void GfxCitro3D::DeleteTexture(const Li::Texture& tex) {
if (!tex.GetID()) return;
UnregisterTexture(tex);
C3D_Tex* t = reinterpret_cast<C3D_Tex*>(tex.GetID());
C3D_TexDelete(t);
delete t;
}
void GfxCitro3D::UploadPools() {
auto buf = C3D_GetBufInfo();
BufInfo_Init(buf);
BufInfo_Add(buf, GetVertexBufPtr(0), sizeof(Li::Vertex), 3, 0x210);
}
void GfxCitro3D::ClipRect() {
// This is so cursed but it works
C3D_SetScissor(CurrentHasClip ? GPU_SCISSOR_NORMAL : GPU_SCISSOR_DISABLE,
ViewPort.y - CurrentClip.w - CurrentClip.y,
ViewPort.x - CurrentClip.z - CurrentClip.x,
ViewPort.y - CurrentClip.y, ViewPort.x - CurrentClip.x);
}
} // namespace PD
#else
namespace PD {
void GfxCitro3D::SysInit() {
PDERR(
"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() {}
Li::Texture GfxCitro3D::LoadTexture(const std::vector<PD::u8>& pixels, int w,
int h, TextureFormat type,
TextureFilter filter) {
return Li::Texture();
}
void GfxCitro3D::DeleteTexture(const Li::Texture& tex) {}
void GfxCitro3D::UploadPools() {}
void GfxCitro3D::ClipRect() {}
} // namespace PD
#endif
+325
View File
@@ -0,0 +1,325 @@
// Well yes, i finally try it
#include <pd/lithium/formatters.hpp>
#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.bgra;
return o;
}
)";
static const char* g_psCode = R"(
sampler2D tex : register(s0);
float alfa : register(c1);
float is_sdf : register(c2);
struct PS_IN {
float2 uv : TEXCOORD0;
float4 col : COLOR0;
};
float4 main(PS_IN input) : COLOR0 {
float4 tc = tex2D(tex, input.uv);
if (is_sdf > 0.5) {
float alpha = smoothstep(0.45, 0.55, tc.a);
return float4(input.col.rgb, alpha * input.col.a);
} else if (alfa > 0.5)
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;
size_t VertexBufferSize = 0;
size_t IndexBufferSize = 0;
};
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);
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)) {
PDERR("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();
Flags |= PDGfxBackendFlags_HasClipRect;
} else {
PDERR(
"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;
BindTexture(CurrentTex);
impl->Device->SetVertexShaderConstantF(0, Projection.Ptr(), 4);
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,
GetVertexPoolSize(), 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);
float sdf = CurrentTexIsSDF ? 1.0f : 0.0f;
impl->Device->SetPixelShaderConstantF(1, &sdf, 1);
}
void GfxDirectX9::SysReset() {
if (!impl || !impl->Device) return;
impl->Device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
impl->Device->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
impl->Device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
impl->Device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
impl->Device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
impl->Device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
impl->Device->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR);
}
Li::Texture GfxDirectX9::LoadTexture(const std::vector<PD::u8>& pixels, int w,
int h, TextureFormat type,
TextureFilter filter) {
if (!impl || !impl->Device) return Li::Texture();
if (pixels.size() !=
static_cast<size_t>(w * h * Li::TextureFormat2Bpp(type))) {
PDERR("Failed to load Texture due to Size mismatch: {} != {}",
pixels.size(),
static_cast<size_t>(w * h * Li::TextureFormat2Bpp(type)));
return Li::Texture();
}
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 Li::Texture();
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;
}
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;
}
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);
Li::Texture res;
res.SetID((TextureID)tex);
res.SetSize(w, h);
res.SetUV(0.f, 0.f, 1.f, 1.f);
RegisterTexture(res);
PDLOG("GfxDirectX9::LoadTexture -> {{ {} }}, [{}, {}]", res, type, filter);
return res;
}
void GfxDirectX9::DeleteTexture(const Li::Texture& tex) {
if (!tex.GetID()) return;
UnregisterTexture(tex);
IDirect3DTexture9* t = (IDirect3DTexture9*)tex.GetID();
t->Release();
}
void GfxDirectX9::UploadPools() {
if (!impl || !impl->Device) return;
if (!impl->VBO || impl->VertexBufferSize != GetVertexPoolSize()) {
if (impl->VBO) impl->VBO->Release();
impl->Device->CreateVertexBuffer(GetVertexPoolSize() * sizeof(Li::Vertex),
D3DUSAGE_WRITEONLY, 0, D3DPOOL_MANAGED,
&impl->VBO, nullptr);
impl->VertexBufferSize = GetVertexPoolSize();
}
if (!impl->IBO || impl->IndexBufferSize != GetIndexPoolSize()) {
if (impl->IBO) impl->IBO->Release();
impl->Device->CreateIndexBuffer(GetIndexPoolSize() * sizeof(u16),
D3DUSAGE_WRITEONLY, D3DFMT_INDEX16,
D3DPOOL_MANAGED, &impl->IBO, nullptr);
impl->IndexBufferSize = GetIndexPoolSize();
}
void* vptr;
impl->VBO->Lock(0, 0, &vptr, 0);
memcpy(vptr, GetVertexBufPtr(0),
GetVertexPoolSize() * sizeof(PD::Li::Vertex));
impl->VBO->Unlock();
void* iptr;
impl->IBO->Lock(0, 0, &iptr, 0);
memcpy(iptr, GetIndexBufPtr(0), GetIndexPoolSize() * sizeof(u16));
impl->IBO->Unlock();
}
void GfxDirectX9::ClipRect() {
if (CurrentHasClip) {
RECT r;
r.left = CurrentClip.x;
r.top = CurrentClip.y;
r.right = CurrentClip.x + CurrentClip.z;
r.bottom = CurrentClip.y + CurrentClip.w;
impl->Device->SetScissorRect(&r);
impl->Device->SetRenderState(D3DRS_SCISSORTESTENABLE, TRUE);
} else {
impl->Device->SetRenderState(D3DRS_SCISSORTESTENABLE, FALSE);
}
}
} // namespace PD
#else
namespace PD {
void GfxDirectX9::SysInit() {
PDERR(
"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() {}
Li::Texture GfxDirectX9::LoadTexture(const std::vector<PD::u8>& pixels, int w,
int h, TextureFormat type,
TextureFilter filter) {
return Li::Texture();
}
void GfxDirectX9::DeleteTexture(const Li::Texture& tex) {}
void GfxDirectX9::UploadPools() {}
void GfxDirectX9::ClipRect() {}
} // namespace PD
#endif
+219
View File
@@ -0,0 +1,219 @@
#include <pd/lithium/formatters.hpp>
#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;
uniform bool is_sdf;
void main() {
vec4 tc = texture2D(tex, oUV);
if (is_sdf) {
float dist = tc.a;
float alpha = smoothstep(0.45, 0.55, dist);
gl_FragColor = vec4(oColor.rgb, alpha * oColor.a);
} else 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() {
Flags |= PDGfxBackendFlags_HasClipRect;
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");
pLocIsSDF = glGetUniformLocation(pShader, "is_sdf");
pLocProjection = glGetUniformLocation(pShader, "projection");
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
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);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
pSetupShaderAttribs(pShader);
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);
glUniform1i(pLocIsSDF, CurrentTexIsSDF);
}
void GfxOpenGL2::SysReset() {
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
Li::Texture GfxOpenGL2::LoadTexture(const std::vector<PD::u8>& pixels, int w,
int h, TextureFormat type,
TextureFilter filter) {
if (pixels.size() !=
static_cast<size_t>(w * h * Li::TextureFormat2Bpp(type))) {
PDERR("Failed to load Texture due to Size mismatch: {} != {}",
pixels.size(),
static_cast<size_t>(w * h * Li::TextureFormat2Bpp(type)));
return Li::Texture();
}
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);
Li::Texture res;
res.SetID(texID);
res.SetSize(w, h);
res.SetUV(0.f, 0.f, 1.f, 1.f);
RegisterTexture(res);
PDLOG("GfxOpenGL2::LoadTexture -> {{ {} }}, [{}, {}]", res, type, filter);
return res;
}
void GfxOpenGL2::DeleteTexture(const Li::Texture& tex) {
UnregisterTexture(tex);
GLuint tex_ = tex.GetID();
glDeleteTextures(1, &tex_);
}
void GfxOpenGL2::UploadPools() {
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, GetVertexPoolSize() * sizeof(PD::Li::Vertex),
GetVertexBufPtr(0), GL_DYNAMIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, GetIndexPoolSize() * sizeof(u16),
GetIndexBufPtr(0), GL_DYNAMIC_DRAW);
}
void GfxOpenGL2::ClipRect() {
if (CurrentHasClip) {
glEnable(GL_SCISSOR_TEST);
glScissor(CurrentClip.x, ViewPort.y - (CurrentClip.y + CurrentClip.w),
CurrentClip.z, CurrentClip.w);
} else {
glDisable(GL_SCISSOR_TEST);
}
}
} // namespace PD
#else
namespace PD {
void GfxOpenGL2::SysInit() {
PDERR(
"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() {}
Li::Texture GfxOpenGL2::LoadTexture(const std::vector<PD::u8>& pixels, int w,
int h, TextureFormat type,
TextureFilter filter) {
return Li::Texture();
}
void GfxOpenGL2::DeleteTexture(const Li::Texture& tex) {}
void GfxOpenGL2::pSetupShaderAttribs(u32 shader) {}
void GfxOpenGL2::UploadPools() {}
void GfxOpenGL2::ClipRect() {}
} // namespace PD
#endif
+194
View File
@@ -0,0 +1,194 @@
#include <pd/lithium/formatters.hpp>
#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>
#include <pd_system/shaders.hpp>
#include <spirv-helper.hpp>
namespace PD {
void GfxOpenGL3::SysInit() {
Flags |= PDGfxBackendFlags_HasClipRect;
std::string vcode = SpirvHelper::SPV2GLSL(
std::vector<unsigned int>(
Shaders::VertexShader,
Shaders::VertexShader +
(sizeof(Shaders::VertexShader) / sizeof(unsigned int))),
330, false);
std::string fcode = SpirvHelper::SPV2GLSL(
std::vector<unsigned int>(
Shaders::FragmentShader,
Shaders::FragmentShader +
(sizeof(Shaders::FragmentShader) / sizeof(unsigned int))),
330, false);
PDLOG("Vertex: \n{}", vcode);
PDLOG("Fragment: \n{}", fcode);
pShader = CreateShaderProgram(vcode.c_str(), fcode.c_str());
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, "push.alfa");
pLocIsSDF = glGetUniformLocation(pShader, "push.is_sdf");
pLocProjection = glGetUniformLocation(pShader, "ubo.projection");
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
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);
glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_SHORT,
reinterpret_cast<void*>(start * sizeof(u16)));
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_R8);
glUniform1i(pLocIsSDF, CurrentTexIsSDF);
}
void GfxOpenGL3::SysReset() {
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
Li::Texture GfxOpenGL3::LoadTexture(const std::vector<PD::u8>& pixels, int w,
int h, TextureFormat type,
TextureFilter filter) {
if (pixels.size() !=
static_cast<size_t>(w * h * Li::TextureFormat2Bpp(type))) {
PDERR("Failed to load Texture due to Size mismatch: {} != {}",
pixels.size(),
static_cast<size_t>(w * h * Li::TextureFormat2Bpp(type)));
return Li::Texture();
}
GLuint texID;
glGenTextures(1, &texID);
glBindTexture(GL_TEXTURE_2D, texID);
// Set base format (Always using RGBA as base)
GLenum fmt = GL_RGBA;
GLenum ifmt = GL_RGBA8;
if (type == TextureFormat::RGB24) {
ifmt = GL_RGB8;
fmt = GL_RGB;
} else if (type == TextureFormat::A8) {
ifmt = GL_R8;
fmt = GL_RED;
}
glTexImage2D(GL_TEXTURE_2D, 0, ifmt, w, h, 0, fmt, GL_UNSIGNED_BYTE,
pixels.data());
if (type == TextureFormat::A8) {
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, GL_RED);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, GL_ONE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_G, GL_ONE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, GL_ONE);
}
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);
Li::Texture res;
res.SetID(texID);
res.SetSize(w, h);
res.SetUV(0.f, 0.f, 1.f, 1.f);
RegisterTexture(res);
PDLOG("GfxOpenGL3::LoadTexture -> {{ {} }}, [{}, {}]", res, type, filter);
return res;
}
void GfxOpenGL3::DeleteTexture(const Li::Texture& tex) {
UnregisterTexture(tex);
GLuint tex_ = tex.GetID();
glDeleteTextures(1, &tex_);
}
void GfxOpenGL3::UploadPools() {
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, GetVertexPoolSize() * sizeof(PD::Li::Vertex),
GetVertexBufPtr(0), GL_DYNAMIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, GetIndexPoolSize() * sizeof(u16),
GetIndexBufPtr(0), GL_DYNAMIC_DRAW);
glBindVertexArray(0);
}
void GfxOpenGL3::ClipRect() {
if (CurrentHasClip) {
glEnable(GL_SCISSOR_TEST);
glScissor(CurrentClip.x, ViewPort.y - (CurrentClip.y + CurrentClip.w),
CurrentClip.z, CurrentClip.w);
} else {
glDisable(GL_SCISSOR_TEST);
}
}
} // namespace PD
#else
namespace PD {
void GfxOpenGL3::SysInit() {
PDERR(
"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() {}
Li::Texture GfxOpenGL3::LoadTexture(const std::vector<PD::u8>& pixels, int w,
int h, TextureFormat type,
TextureFilter filter) {
return Li::Texture();
}
void GfxOpenGL3::DeleteTexture(const Li::Texture& tex) {}
void GfxOpenGL3::UploadPools() {}
void GfxOpenGL3::ClipRect() {}
} // namespace PD
#endif
+48
View File
@@ -0,0 +1,48 @@
#if defined(PD_ENABLE_OPENGL2) || defined(PD_ENABLE_OPENGL3)
#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
#endif
+82
View File
@@ -0,0 +1,82 @@
#include <pd_system/hid_3ds.hpp>
#if defined(PD_ENABLE_HID3DS) && defined(__3DS__)
#include <3ds.h>
namespace PD {
struct Hid3DS::Impl {
std::string* Text;
bool InTextMode = false;
};
Hid3DS::Hid3DS() : HidDriver("Hid3DS") {
impl = new Impl;
pFlags |= PDHidBackendFlags_HasGamepad;
pFlags |= PDHidBackendFlags_HasTouch;
pGamepad[KEY_A] = HidInternal::Gamepad::A;
pGamepad[KEY_B] = HidInternal::Gamepad::B;
pGamepad[KEY_X] = HidInternal::Gamepad::X;
pGamepad[KEY_Y] = HidInternal::Gamepad::Y;
pGamepad[KEY_START] = HidInternal::Gamepad::Start;
pGamepad[KEY_SELECT] = HidInternal::Gamepad::Select;
pGamepad[KEY_L] = HidInternal::Gamepad::L;
pGamepad[KEY_R] = HidInternal::Gamepad::R;
pGamepad[KEY_DUP] = HidInternal::Gamepad::DUp;
pGamepad[KEY_DDOWN] = HidInternal::Gamepad::DDown;
pGamepad[KEY_DLEFT] = HidInternal::Gamepad::DLeft;
pGamepad[KEY_DRIGHT] = HidInternal::Gamepad::DRight;
pGamepad[KEY_CPAD_UP] = HidInternal::Gamepad::CPUp;
pGamepad[KEY_CPAD_DOWN] = HidInternal::Gamepad::CPDown;
pGamepad[KEY_CPAD_LEFT] = HidInternal::Gamepad::CPLeft;
pGamepad[KEY_CPAD_RIGHT] = HidInternal::Gamepad::CPRight;
pGamepad[KEY_CSTICK_UP] = HidInternal::Gamepad::CSUp;
pGamepad[KEY_CSTICK_DOWN] = HidInternal::Gamepad::CSDown;
pGamepad[KEY_CSTICK_LEFT] = HidInternal::Gamepad::CSLeft;
pGamepad[KEY_CSTICK_RIGHT] = HidInternal::Gamepad::CSRight;
pGamepad[KEY_ZL] = HidInternal::Gamepad::ZL;
pGamepad[KEY_ZR] = HidInternal::Gamepad::ZR;
pGamepad[KEY_TOUCH] = HidInternal::Gamepad::Touch;
}
Hid3DS::~Hid3DS() { delete impl; }
void Hid3DS::Update() {
HidDriver::Update(); // clear stats
hidScanInput();
u32 kd = hidKeysDown();
u32 kh = hidKeysHeld();
u32 ku = hidKeysUp();
for (const auto& b : pGamepad) {
if (b.first & kd) {
pGamepadEvents[0][Event::Down] |= b.second;
}
if (b.first & kh) {
pGamepadEvents[0][Event::Held] |= b.second;
}
if (b.first & ku) {
pGamepadEvents[0][Event::Up] |= b.second;
}
}
/*if (pLocked) {
SwapTab();
}*/
touchPosition t;
hidTouchRead(&t);
pMouse[0] = fvec2(t.px, t.py);
}
} // namespace PD
#else
struct GLFWgamepadstate {};
namespace PD {
Hid3DS::Hid3DS() : HidDriver("Hid3DS") {}
Hid3DS::~Hid3DS() {}
void Hid3DS::Update() {
HidDriver::Update(); // clear stats
}
} // namespace PD
#endif
+156
View File
@@ -0,0 +1,156 @@
#include <pd_system/hid_glfw.hpp>
#ifdef PD_ENABLE_HID_GLFW
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
namespace PD {
struct HidGlfw::Impl {
GLFWwindow* Win;
int PrevState;
std::unordered_map<int, int> PrevStates;
std::unordered_map<int, int> GPPrevStates;
GLFWcharfun OldTextCB;
std::string* Text;
bool InTextMode = false;
};
constexpr int KEY_BASE = GLFW_GAMEPAD_BUTTON_LAST;
constexpr int KEY_LSTICK_LEFT = KEY_BASE + 1;
constexpr int KEY_LSTICK_RIGHT = KEY_BASE + 2;
constexpr int KEY_LSTICK_UP = KEY_BASE + 3;
constexpr int KEY_LSTICK_DOWN = KEY_BASE + 4;
constexpr int KEY_RSTICK_LEFT = KEY_BASE + 5;
constexpr int KEY_RSTICK_RIGHT = KEY_BASE + 6;
constexpr int KEY_RSTICK_UP = KEY_BASE + 7;
constexpr int KEY_RSTICK_DOWN = KEY_BASE + 8;
constexpr int KEY_LEFT_TRIGGER = KEY_BASE + 9;
constexpr int KEY_RIGHT_TRIGGER = KEY_BASE + 10;
constexpr int KEY_LAST = KEY_RIGHT_TRIGGER;
HidGlfw::HidGlfw(GLFWwindow* win) : HidDriver("HidGlfw") {
impl = new Impl;
impl->Win = win;
pFlags |= PDHidBackendFlags_HasMouse;
pFlags |= PDHidBackendFlags_HasKeyboard;
if (glfwJoystickPresent(GLFW_JOYSTICK_1)) {
pFlags |= PDHidBackendFlags_HasGamepad;
}
pGamepad[GLFW_GAMEPAD_BUTTON_A] = HidInternal::Gamepad::A;
pGamepad[GLFW_GAMEPAD_BUTTON_B] = HidInternal::Gamepad::B;
pGamepad[GLFW_GAMEPAD_BUTTON_X] = HidInternal::Gamepad::X;
pGamepad[GLFW_GAMEPAD_BUTTON_Y] = HidInternal::Gamepad::Y;
pGamepad[GLFW_GAMEPAD_BUTTON_START] = HidInternal::Gamepad::Start;
pGamepad[GLFW_GAMEPAD_BUTTON_BACK] = HidInternal::Gamepad::Select;
pGamepad[GLFW_GAMEPAD_BUTTON_DPAD_LEFT] = HidInternal::Gamepad::DLeft;
pGamepad[GLFW_GAMEPAD_BUTTON_DPAD_RIGHT] = HidInternal::Gamepad::DRight;
pGamepad[GLFW_GAMEPAD_BUTTON_DPAD_UP] = HidInternal::Gamepad::DUp;
pGamepad[GLFW_GAMEPAD_BUTTON_DPAD_DOWN] = HidInternal::Gamepad::DDown;
pGamepad[GLFW_GAMEPAD_BUTTON_LEFT_BUMPER] = HidInternal::Gamepad::L;
pGamepad[GLFW_GAMEPAD_BUTTON_RIGHT_BUMPER] = HidInternal::Gamepad::R;
pGamepad[GLFW_GAMEPAD_BUTTON_LEFT_THUMB] = HidInternal::Gamepad::LStick;
pGamepad[GLFW_GAMEPAD_BUTTON_RIGHT_THUMB] = HidInternal::Gamepad::RStick;
pGamepad[KEY_LEFT_TRIGGER] = HidInternal::Gamepad::ZL;
pGamepad[KEY_RIGHT_TRIGGER] = HidInternal::Gamepad::ZR;
pGamepad[KEY_LSTICK_LEFT] = HidInternal::Gamepad::CPLeft;
pGamepad[KEY_LSTICK_RIGHT] = HidInternal::Gamepad::CPRight;
pGamepad[KEY_LSTICK_UP] = HidInternal::Gamepad::CPUp;
pGamepad[KEY_LSTICK_DOWN] = HidInternal::Gamepad::CPDown;
pGamepad[KEY_RSTICK_LEFT] = HidInternal::Gamepad::CSLeft;
pGamepad[KEY_RSTICK_RIGHT] = HidInternal::Gamepad::CSRight;
pGamepad[KEY_RSTICK_UP] = HidInternal::Gamepad::CSUp;
pGamepad[KEY_RSTICK_DOWN] = HidInternal::Gamepad::CSDown;
for (int i = 0; i <= KEY_LAST; i++) {
impl->GPPrevStates[i] = 0;
}
}
HidGlfw::~HidGlfw() { delete impl; }
void HidGlfw::Update() {
HidDriver::Update(); // clear stats
GLFWgamepadstate gpstate;
int gps = glfwGetGamepadState(GLFW_JOYSTICK_1, &gpstate);
if (gps == GLFW_TRUE) {
HandleAxisKey(gpstate, KEY_LEFT_TRIGGER, GLFW_GAMEPAD_AXIS_LEFT_TRIGGER,
false);
HandleAxisKey(gpstate, KEY_RIGHT_TRIGGER, GLFW_GAMEPAD_AXIS_RIGHT_TRIGGER,
false);
HandleAxisKey(gpstate, KEY_LSTICK_LEFT, GLFW_GAMEPAD_AXIS_LEFT_X, true);
HandleAxisKey(gpstate, KEY_LSTICK_RIGHT, GLFW_GAMEPAD_AXIS_LEFT_X, false);
HandleAxisKey(gpstate, KEY_LSTICK_UP, GLFW_GAMEPAD_AXIS_LEFT_Y, true);
HandleAxisKey(gpstate, KEY_LSTICK_DOWN, GLFW_GAMEPAD_AXIS_LEFT_Y, false);
HandleAxisKey(gpstate, KEY_RSTICK_LEFT, GLFW_GAMEPAD_AXIS_RIGHT_X, true);
HandleAxisKey(gpstate, KEY_RSTICK_RIGHT, GLFW_GAMEPAD_AXIS_RIGHT_X, false);
HandleAxisKey(gpstate, KEY_RSTICK_UP, GLFW_GAMEPAD_AXIS_RIGHT_Y, true);
HandleAxisKey(gpstate, KEY_RSTICK_DOWN, GLFW_GAMEPAD_AXIS_RIGHT_Y, false);
pLStick[0].x = gpstate.axes[GLFW_GAMEPAD_AXIS_LEFT_X];
pLStick[0].y = gpstate.axes[GLFW_GAMEPAD_AXIS_LEFT_Y];
pRStick[0].x = gpstate.axes[GLFW_GAMEPAD_AXIS_RIGHT_X];
pRStick[0].y = gpstate.axes[GLFW_GAMEPAD_AXIS_RIGHT_Y];
for (int i = 0; i <= GLFW_GAMEPAD_BUTTON_LAST; i++) {
if (gpstate.buttons[i] == GLFW_PRESS) {
if (impl->GPPrevStates[i] == GLFW_RELEASE) {
pGamepadEvents[0][Event::Down] |= pGamepad[i];
}
pGamepadEvents[0][Event::Held] |= pGamepad[i];
} else if (gpstate.buttons[i] == GLFW_RELEASE &&
impl->GPPrevStates[i] == GLFW_PRESS) {
pGamepadEvents[0][Event::Up] |= pGamepad[i];
}
impl->GPPrevStates[i] = gpstate.buttons[i];
}
}
int state = glfwGetMouseButton(impl->Win, GLFW_MOUSE_BUTTON_LEFT);
if (state == GLFW_PRESS) {
if (impl->PrevState == GLFW_RELEASE) {
pGamepadEvents[0][Event::Down] |= HidInternal::Touch;
}
pGamepadEvents[0][Event::Held] |= HidInternal::Touch;
} else if (state == GLFW_RELEASE && impl->PrevState == GLFW_PRESS) {
pGamepadEvents[0][Event::Up] |= HidInternal::Touch;
}
impl->PrevState = state;
// if (pLocked) {
// SwapTab();
// }
double x, y;
glfwGetCursorPos(impl->Win, &x, &y);
pMouse[0] = fvec2(x, y);
}
void HidGlfw::HandleAxisKey(GLFWgamepadstate s, int iK, int eA, bool negative) {
if (s.axes[eA] <= 1.f && s.axes[eA] >= -1.f) {
if (s.axes[eA] > 0.05f && !negative) {
if (impl->GPPrevStates[iK] == GLFW_RELEASE) {
pGamepadEvents[0][Event::Down] |= pGamepad[iK];
}
pGamepadEvents[0][Event::Held] |= pGamepad[iK];
}
if (s.axes[eA] < -0.05f && negative) {
if (impl->GPPrevStates[iK] == GLFW_RELEASE) {
pGamepadEvents[0][Event::Down] |= pGamepad[iK];
}
pGamepadEvents[0][Event::Held] |= pGamepad[iK];
}
}
impl->GPPrevStates[iK] = (s.axes[eA] <= -0.05 && s.axes[eA] >= 0.05 &&
s.axes[eA] >= -1.f && s.axes[eA] <= 1);
}
} // namespace PD
#else
struct GLFWgamepadstate {};
namespace PD {
HidGlfw::HidGlfw(GLFWwindow* win) : HidDriver("HidGlfw") {}
HidGlfw::~HidGlfw() {}
void HidGlfw::Update() {
HidDriver::Update(); // clear stats
}
void HidGlfw::HandleAxisKey(GLFWgamepadstate s, int iK, int eA, bool negative) {
}
} // namespace PD
#endif
+31
View File
@@ -0,0 +1,31 @@
#include <pd_system/hid_nx.hpp>
#ifdef PD_ENABLE_HID_NX
#include <switch.h>
namespace PD {
struct HidNX::Impl {
PadState Pad;
};
HidNX::HidNX() : HidDriver("HidNX") { impl = new Impl; }
HidNX::~HidNX() { delete impl; }
void HidNX::Update() {
HidDriver::Update(); // clear stats
}
} // namespace PD
#else
namespace PD {
HidNX::HidNX() : HidDriver("HidNX") {}
HidNX::~HidNX() {}
void HidNX::Update() {
HidDriver::Update(); // clear stats
}
} // namespace PD
#endif
-45
View File
@@ -11,51 +11,6 @@ set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
# Enable Compile Command Export # Enable Compile Command Export
set(CMAKE_EXPORT_COMPILE_COMMANDS ON) 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 ### ### GIT HELPER ###
function(pd_git_get_hash ret) function(pd_git_get_hash ret)
+6
View File
@@ -0,0 +1,6 @@
@PACKAGE_INIT@
include("${CMAKE_CURRENT_LIST_DIR}/palladiumTargets.cmake")
if(NOT TARGET palladium::pd-desktop AND PALLADIUM_FIND_DESKTOP)
find_dependency(palladium::pd-desktop OPTIONAL)
endif()
+35
View File
@@ -0,0 +1,35 @@
{
"version": 10,
"configurePresets": [
{
"name": "3ds-debug",
"generator": "Ninja",
"displayName": "Nintendo 3DS Debug",
"binaryDir": "${sourceDir}/build/3ds-debug",
"toolchainFile": "/opt/devkitPro/cmake/3DS.cmake",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug"
}
},
{
"name": "3ds-release",
"generator": "Ninja",
"displayName": "Nintendo 3DS Release",
"binaryDir": "${sourceDir}/build/3ds-release",
"toolchainFile": "/opt/devkitPro/cmake/3DS.cmake",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release"
}
}
],
"buildPresets": [
{
"name": "3ds-release",
"configurePreset": "3ds-release"
},
{
"name": "3ds-debug",
"configurePreset": "3ds-debug"
}
]
}
+33
View File
@@ -0,0 +1,33 @@
{
"version": 10,
"configurePresets": [
{
"name": "default-debug",
"generator": "Ninja",
"displayName": "Default Debug",
"binaryDir": "${sourceDir}/build/default-debug",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug"
}
},
{
"name": "default-release",
"generator": "Ninja",
"displayName": "Default Release",
"binaryDir": "${sourceDir}/build/default-release",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release"
}
}
],
"buildPresets": [
{
"name": "default-release",
"configurePreset": "default-release"
},
{
"name": "default-debug",
"configurePreset": "default-debug"
}
]
}
+24
View File
@@ -0,0 +1,24 @@
{
"version": 10,
"configurePresets": [
{
"name": "msvc",
"cmakeExecutable": "C:\\Program Files\\CMake\\bin\\cmake.exe",
"displayName": "MSVC",
"generator": "Visual Studio 17 2022",
"binaryDir": "${sourceDir}/build/msvc-build"
}
],
"buildPresets": [
{
"name": "msvc-release",
"configuration": "Release",
"configurePreset": "msvc"
},
{
"name": "msvc-debug",
"configuration": "Debug",
"configurePreset": "msvc"
}
]
}
+37
View File
@@ -0,0 +1,37 @@
{
"version": 10,
"configurePresets": [
{
"name": "switch-debug",
"generator": "Ninja",
"displayName": "Nintendo Switch Debug",
"binaryDir": "${sourceDir}/build/switch-debug",
"toolchainFile": "/opt/devkitPro/cmake/Switch.cmake",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Debug",
"SPV_EXCLUDE_GLSLANG": "ON"
}
},
{
"name": "switch-release",
"generator": "Ninja",
"displayName": "Nintendo Switch Release",
"binaryDir": "${sourceDir}/build/switch-release",
"toolchainFile": "/opt/devkitPro/cmake/Switch.cmake",
"cacheVariables": {
"CMAKE_BUILD_TYPE": "Release",
"SPV_EXCLUDE_GLSLANG": "ON"
}
}
],
"buildPresets": [
{
"name": "switch-release",
"configurePreset": "switch-release"
},
{
"name": "switch-debug",
"configurePreset": "switch-debug"
}
]
}
+23 -1
View File
@@ -1,8 +1,30 @@
#pragma once #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/core/core.hpp>
#include <pd/drivers/drivers.hpp> #include <pd/drivers/drivers.hpp>
#include <pd/external/json.hpp>
#include <pd/image/image.hpp> #include <pd/image/image.hpp>
#include <pd/lithium/lithium.hpp> #include <pd/lithium/lithium.hpp>
#include <pd/ui7/ui7.hpp> #include <pd/ui7/ui7.hpp>
+109
View File
@@ -0,0 +1,109 @@
#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.
*/
#if defined(__GNUG__) && !defined(_MSC_VER)
#include <cxxabi.h>
#endif
#include <array>
#include <chrono>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <exception>
#include <format>
#include <fstream>
#include <numbers>
#include <pd/pd_p_api.hpp>
#include <string>
#include <unordered_map>
#include <vector>
namespace PD {
enum class LogLevel {
None,
Info,
Warning,
Error,
};
[[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 LogFilter(LogLevel minimum);
PD_API void Log(const std::string& txt, LogLevel lvl = LogLevel::Info);
template <typename... Args>
void Log(std::format_string<Args...> fmt, Args&&... args) {
std::string msg = std::format(fmt, std::forward<Args>(args)...);
Log(msg, LogLevel::Info);
}
template <typename... Args>
void Log(LogLevel lvl, std::format_string<Args...> fmt, Args&&... args) {
std::string msg = std::format(fmt, std::forward<Args>(args)...);
Log(msg, lvl);
}
template <typename T>
std::string TypeName() {
#ifdef __RTTI
#if defined(__GNUG__) && !defined(_MSC_VER)
int res = 0;
std::unique_ptr<char, void (*)(void*)> up{
abi::__cxa_demangle(typeid(T).name(), nullptr, nullptr, &res), std::free};
return (res == 0) ? up.get() : typeid(T).name();
#else
return typeid(T).name(); // no demangler available :/
#endif
#else
return "";
#endif
}
} // namespace PD
#if defined(__GNUC__) || defined(__clang__)
#define PDPRETTYFUNC __PRETTY_FUNCTION__
#elif defined(_MSC_VER)
#define PDPRETTYFUNC __FUNCSIG__
#else
#define PDPRETTYFUNC __FUNCTION__
#endif
#ifdef PD_DEBUG
#define PDLOG(fmt, ...) \
PD::Log("[{}:{}]: " fmt, __FILE__, __LINE__, ##__VA_ARGS__)
#define PDWARN(fmt, ...) \
PD::Log(PD::LogLevel::Warning, "[{}:{}]: " fmt, __FILE__, __LINE__, \
##__VA_ARGS__)
#define PDERR(fmt, ...) \
PD::Log(PD::LogLevel::Error, "[{}:{}]: " fmt, __FILE__, __LINE__, \
##__VA_ARGS__)
#else
#define PDLOG(fmt, ...)
#define PDWARN(fmt, ...)
#define PDERR(fmt, ...)
#endif
+6 -10
View File
@@ -2,8 +2,7 @@
/* /*
MIT License MIT License
Copyright (c) 2024 - 2026 René Amthor (tobid7)
Copyright (c) 2024 - 2025 tobid7
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
@@ -24,24 +23,21 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. SOFTWARE.
*/ */
#include <pd/core/common.hpp> #include <pd/common.hpp>
namespace PD { namespace PD {
/** namespace Bits {
* Binary Utillity Functions
*/
namespace BitUtil {
/** /**
* Check if a 32 Bit number only set a sigle bit to 1 * Check if a 32 Bit number only set a sigle bit to 1
* @param v 32 bit unsigned int * @param v 32 bit unsigned int
* @return true if its a single bit number * @return true if its a single bit number
*/ */
PD_CORE_API bool IsSingleBit(u32 v); PD_API bool IsSingleBit(u32 v);
/** /**
* Get the Next Power of two Number * Get the Next Power of two Number
* @param v Current Number * @param v Current Number
* @return Next Number thats a Pow of 2 * @return Next Number thats a Pow of 2
*/ */
PD_CORE_API u32 GetPow2(u32 v); PD_API u32 GetPow2(u32 v);
} // namespace BitUtil } // namespace Bits
} // namespace PD } // namespace PD
+29 -4
View File
@@ -2,7 +2,7 @@
/* /*
MIT License MIT License
Copyright (c) 2024 - 2025 René Amthor (tobid7) Copyright (c) 2024 - 2026 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
@@ -23,11 +23,11 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. SOFTWARE.
*/ */
#include <pd/core/common.hpp> #include <pd/common.hpp>
#include <pd/core/strings.hpp> #include <pd/core/strings.hpp>
namespace PD { namespace PD {
class PD_CORE_API Color { class PD_API Color {
public: public:
/** /**
* Default Constructor (all variables are set to 0) * Default Constructor (all variables are set to 0)
@@ -71,6 +71,12 @@ class PD_CORE_API Color {
*/ */
constexpr Color(const std::string_view& hex) { Hex(hex); } constexpr Color(const std::string_view& hex) { Hex(hex); }
/**
* Constructor for Hex Input (is abel to run at compile time xD)
* @param hex Hex String in `#ffffff` or `#ffffffff` format
*/
constexpr Color(const char* hex) { Hex(std::string_view{hex}); }
/** /**
* Create Color Object by Hex String (at compile time btw) * Create Color Object by Hex String (at compile time btw)
* @param hex Hex String in `#ffffff` or `#ffffffff` format * @param hex Hex String in `#ffffff` or `#ffffffff` format
@@ -78,7 +84,7 @@ class PD_CORE_API Color {
*/ */
constexpr Color& Hex(const std::string_view& hex) { constexpr Color& Hex(const std::string_view& hex) {
if (!(hex.length() == 7 || hex.length() == 9)) { if (!(hex.length() == 7 || hex.length() == 9)) {
throw "[PD] Color: hex string is not rgb or rgba!"; PD::Throw("[PD] Color: hex string is not rgb or rgba!");
} }
r = PD::Strings::HexChar2Int(hex[1]) * 16 + r = PD::Strings::HexChar2Int(hex[1]) * 16 +
PD::Strings::HexChar2Int(hex[2]); PD::Strings::HexChar2Int(hex[2]);
@@ -115,6 +121,20 @@ class PD_CORE_API Color {
return *this; return *this;
} }
/**
* Lerp
* @param v Target color
* @param t interpolation factor
* @return Class Reference
*/
constexpr Color& Lerp(const Color& v, float t) {
a = static_cast<u8>(a + (v.a - a) * t);
b = static_cast<u8>(b + (v.b - b) * t);
g = static_cast<u8>(g + (v.g - g) * t);
r = static_cast<u8>(r + (v.r - r) * t);
return *this;
}
/** /**
* Get 32Bit Color Value * Get 32Bit Color Value
* @return 32Bit Color Value (ABGR iirc) * @return 32Bit Color Value (ABGR iirc)
@@ -140,6 +160,11 @@ class PD_CORE_API Color {
*/ */
constexpr operator u32() const { return Get(); } 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 */ /** Public Access Data section */
u8 r; u8 r;
u8 g; u8 g;
-84
View File
@@ -1,84 +0,0 @@
#pragma once
/*
MIT License
Copyright (c) 2024 - 2025 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/core/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) {
throw std::runtime_error("[PD] " + str);
}
/** Types */
using u8 = unsigned char;
using u16 = unsigned short;
using u32 = unsigned int;
using u64 = unsigned long long;
} // namespace PD
+6 -2
View File
@@ -2,7 +2,7 @@
/* /*
MIT License MIT License
Copyright (c) 2024 - 2025 René Amthor (tobid7) Copyright (c) 2024 - 2026 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
@@ -23,11 +23,15 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. SOFTWARE.
*/ */
#include <pd/core/bit_util.hpp> #include <pd/core/bits.hpp>
#include <pd/core/color.hpp> #include <pd/core/color.hpp>
#include <pd/core/fnv.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/io.hpp>
#include <pd/core/mat.hpp> #include <pd/core/mat.hpp>
#include <pd/core/pool.hpp>
#include <pd/core/strings.hpp> #include <pd/core/strings.hpp>
#include <pd/core/timer.hpp> #include <pd/core/timer.hpp>
#include <pd/core/timetrace.hpp> #include <pd/core/timetrace.hpp>
+19 -2
View File
@@ -2,7 +2,7 @@
/* /*
MIT License MIT License
Copyright (c) 2024 - 2025 René Amthor (tobid7) Copyright (c) 2024 - 2026 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
@@ -23,7 +23,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. SOFTWARE.
*/ */
#include <pd/core/common.hpp> #include <pd/common.hpp>
namespace PD { namespace PD {
/** /**
@@ -50,4 +50,21 @@ constexpr u64 FNV1A64(std::string_view str) {
} }
return ret; return ret;
} }
namespace Detail {
template <typename T>
struct FNV1A {};
template <>
struct FNV1A<u32> {
static constexpr u32 Hash(std::string_view str) { return FNV1A32(str); }
static constexpr u32 Hash(const std::string& str) { return FNV1A32(str); }
};
template <>
struct FNV1A<u64> {
static constexpr u64 Hash(std::string_view str) { return FNV1A64(str); }
static constexpr u64 Hash(const std::string& str) { return FNV1A64(str); }
};
} // namespace Detail
} // namespace PD } // namespace PD
+98
View File
@@ -0,0 +1,98 @@
#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...
}
};
+2 -2
View File
@@ -2,7 +2,7 @@
/* /*
MIT License MIT License
Copyright (c) 2024 - 2025 René Amthor (tobid7) Copyright (c) 2024 - 2026 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
@@ -25,7 +25,7 @@ SOFTWARE.
// This file is based on fvec4 // This file is based on fvec4
#include <pd/core/common.hpp> #include <pd/common.hpp>
#include <pd/core/vec4.hpp> #include <pd/core/vec4.hpp>
namespace PD { namespace PD {
+50
View File
@@ -0,0 +1,50 @@
#pragma once
#include <pd/core/fnv.hpp>
#include <pd/core/strings.hpp>
namespace PD {
template <typename T>
class HashID {
public:
using __Type = T;
constexpr HashID() {};
constexpr HashID(T id) { pID = id; }
HashID(const std::string& name) {
pID = Detail::FNV1A<T>::Hash(name);
#ifdef PD_HASHID_KEEP_STR
pName = name;
#endif
}
constexpr HashID(const char* name) {
pID = Detail::FNV1A<T>::Hash(std::string_view(name));
#ifdef PD_HASHID_KEEP_STR
pName = name;
#endif
}
~HashID() {}
constexpr T Get() { return pID; }
std::string GetName() const {
#ifdef PD_HASHID_KEEP_STR
return pName;
#else
return std::format("hash({:#08x})", pID);
#endif
}
operator T() const { return pID; }
// operator std::string() const { return GetName(); }
private:
T pID;
#ifdef PD_HASHID_KEEP_STR
str pName;
#endif
};
using HashID32 = HashID<u32>;
using HashID64 = HashID<u64>;
} // namespace PD
+7 -7
View File
@@ -2,7 +2,7 @@
/* /*
MIT License MIT License
Copyright (c) 2024 - 2025 René Amthor (tobid7) Copyright (c) 2024 - 2026 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
@@ -23,7 +23,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. SOFTWARE.
*/ */
#include <pd/core/common.hpp> #include <pd/common.hpp>
namespace PD { namespace PD {
/** /**
@@ -35,28 +35,28 @@ namespace IO {
* @param path Path to the File * @param path Path to the File
* @return 8Bit FileBuffer * @return 8Bit FileBuffer
*/ */
PD_CORE_API std::vector<u8> LoadFile2Mem(const std::string& path); PD_API std::vector<u8> LoadFile2Mem(const std::string& path);
/** /**
* Load a File into a std::string * Load a File into a std::string
* @param path Path to the File * @param path Path to the File
* @return std::string file content * @return std::string file content
*/ */
PD_CORE_API std::string LoadFile2Str(const std::string& path); PD_API std::string LoadFile2Str(const std::string& path);
/** /**
* Hash a 8Bit Memory Buffer * Hash a 8Bit Memory Buffer
* @param data 8Bit input Buffer * @param data 8Bit input Buffer
* @return 32Bit Hash * @return 32Bit Hash
*/ */
PD_CORE_API u32 HashMemory(const std::vector<u8>& data); PD_API u32 HashMemory(const std::vector<u8>& data);
/** /**
* Function to decrompress RLE buffer * Function to decrompress RLE buffer
* @param data Data buffer to decompress * @param data Data buffer to decompress
*/ */
PD_CORE_API void DecompressRLE(std::vector<u8>& data); PD_API void DecompressRLE(std::vector<u8>& data);
/** /**
* Function to compress data with RLE Algorithm * Function to compress data with RLE Algorithm
* @param data Data buf * @param data Data buf
*/ */
PD_CORE_API void CompressRLE(std::vector<u8>& data); PD_API void CompressRLE(std::vector<u8>& data);
} // namespace IO } // namespace IO
} // namespace PD } // namespace PD
+14 -26
View File
@@ -2,8 +2,7 @@
/* /*
MIT License MIT License
Copyright (c) 2024 - 2026 René Amthor (tobid7)
Copyright (c) 2024 - 2025 tobid7
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
@@ -24,7 +23,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. SOFTWARE.
*/ */
#include <pd/core/common.hpp> #include <pd/common.hpp>
#include <pd/core/vec3.hpp> #include <pd/core/vec3.hpp>
namespace PD { namespace PD {
@@ -41,7 +40,7 @@ constexpr float Radians(float v) { return v * (Numbers::Tau / 360.0f); }
* @note That this is not a full Matrix Library * @note That this is not a full Matrix Library
*/ */
struct PD_CORE_API Mat4 { struct PD_API Mat4 {
std::array<float, 16> m; std::array<float, 16> m;
constexpr Mat4() : m{} {} constexpr Mat4() : m{} {}
constexpr static Mat4 Diagonal(float x, float y, float z, float w) { constexpr static Mat4 Diagonal(float x, float y, float z, float w) {
@@ -57,21 +56,9 @@ struct PD_CORE_API Mat4 {
constexpr float* Ptr() { return m.data(); } constexpr float* Ptr() { return m.data(); }
constexpr const float* Ptr() const { return m.data(); } constexpr const float* Ptr() const { return m.data(); }
constexpr float& operator()(int row, int col) { constexpr float& operator()(int row, int col) { return m[col * 4 + row]; }
#ifdef __3DS__
// 3ds is full reverse order iirc
return m[row * 4 + (3 - col)];
#else
return m[col * 4 + row];
#endif
}
constexpr float operator()(int row, int col) const { constexpr float operator()(int row, int col) const {
#ifdef __3DS__
// 3ds is full reverse order iirc
return m[row * 4 + (3 - col)];
#else
return m[col * 4 + row]; return m[col * 4 + row];
#endif
} }
constexpr Mat4 operator*(const Mat4& v) const { constexpr Mat4 operator*(const Mat4& v) const {
@@ -110,24 +97,25 @@ struct PD_CORE_API Mat4 {
return ret; return ret;
} }
constexpr Mat4 Transpose() const {
Mat4 ret;
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
ret(i, j) = (*this)(j, i);
}
}
return ret;
}
constexpr static Mat4 Ortho(float l, float r, float b, float t, float n, constexpr static Mat4 Ortho(float l, float r, float b, float t, float n,
float f) { float f) {
Mat4 ret; Mat4 ret;
#ifdef __3DS__ // Patch to rotate the Matrix correctly
ret(0, 1) = 2.f / (t - b);
ret(0, 3) = (b + t) / (b - t);
ret(1, 0) = 2.f / (l - r);
ret(1, 3) = (l + r) / (r - l);
ret(2, 2) = 1.f / (n - f);
ret(2, 3) = 0.5f * (n + f) / (n - f) - 0.5f;
#else
ret(0, 0) = 2.0f / (r - l); ret(0, 0) = 2.0f / (r - l);
ret(0, 3) = -(r + l) / (r - l); ret(0, 3) = -(r + l) / (r - l);
ret(1, 1) = 2.0f / (t - b); ret(1, 1) = 2.0f / (t - b);
ret(1, 3) = -(t + b) / (t - b); ret(1, 3) = -(t + b) / (t - b);
ret(2, 2) = -2.0f / (f - n); ret(2, 2) = -2.0f / (f - n);
ret(2, 3) = -(f + n) / (f - n); ret(2, 3) = -(f + n) / (f - n);
#endif
ret(3, 3) = 1.f; ret(3, 3) = 1.f;
return ret; return ret;
} }
-51
View File
@@ -1,51 +0,0 @@
#pragma once
/*
MIT License
Copyright (c) 2024 - 2025 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.
*/
/** Generated with ppam */
#ifdef _WIN32 // Windows (MSVC Tested)
#ifdef PD_CORE_BUILD_SHARED
#define PD_CORE_API __declspec(dllexport)
#else
#define PD_CORE_API __declspec(dllimport)
#endif
#elif defined(__APPLE__) // macOS (untested yet)
#ifdef PD_CORE_BUILD_SHARED
#define PD_CORE_API __attribute__((visibility("default")))
#else
#define PD_CORE_API
#endif
#elif defined(__linux__) // Linux (untested yet)
#ifdef PD_CORE_BUILD_SHARED
#define PD_CORE_API __attribute__((visibility("default")))
#else
#define PD_CORE_API
#endif
#elif defined(__3DS__) // 3ds Specific
// Only Static supported
#define PD_CORE_API
#else
#define PD_CORE_API
#endif
+166
View File
@@ -0,0 +1,166 @@
#pragma once
#include <memory>
#include <pd/common.hpp>
namespace PD {
// lets take use of c++ 20 concepts
// https://en.cppreference.com/w/cpp/language/constraints.html
template <typename T>
concept Resettable = requires(T& v) { v.Reset(); };
template <typename T, typename Alloc = std::allocator<T>>
class Pool {
public:
using value_type = T;
using iterator = T*;
using const_iterator = const T*;
Pool() = default;
~Pool() {
if (pData) {
for (size_t i = 0; i < pCap; i++) {
std::allocator_traits<Alloc>::destroy(pAlloc, &pData[i]);
}
pAlloc.deallocate(pData, pCap);
pData = nullptr;
}
}
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);
}
Pool(const Pool&) = delete;
Pool(Pool&&) = delete;
Pool& operator=(const Pool&) = delete;
Pool& operator=(Pool&&) = delete;
T* Allocate(size_t num = 1) {
ExpandIf(num);
T* ret = &pData[pPos];
pPos += num;
return ret;
}
void Push(const T& elem) {
T* e = Allocate(1);
*e = elem;
}
void Put(size_t idx, const T& elem) {
if (idx >= pCap) ExpandIf(idx);
pData[idx] = elem;
}
void ExpandIf(size_t req) {
if ((pPos + req) <= pCap) return;
size_t ncap = std::max(pCap * 2, pPos + req);
T* nu = pAlloc.allocate(ncap);
if (pData) {
for (size_t i = 0; i < pPos; i++) {
std::allocator_traits<Alloc>::construct(
pAlloc, &nu[i], std::move_if_noexcept(pData[i]));
}
for (size_t i = 0; i < pCap; i++) {
std::allocator_traits<Alloc>::destroy(pAlloc, &pData[i]);
}
pAlloc.deallocate(pData, pCap);
}
for (size_t i = pPos; i < ncap; i++) {
std::allocator_traits<Alloc>::construct(pAlloc, &nu[i]);
}
PDLOG("Pool::ExpandIf({}): {} -> {}", req, pCap, ncap);
pData = nu;
pCap = ncap;
}
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]);
}
}
void ResetFast() {
if constexpr (Resettable<T>) {
for (size_t i = 0; i < pPos; i++) {
pData[i].Reset();
}
} else if constexpr (!std::is_trivially_destructible_v<T>) {
PDWARN(
"ResetFast should only be executed with a non destructible type or a "
"class/struct that has a Reset func! {} is not "
"trivially_destructible and has no reset func.",
TypeName<T>());
}
pPos = 0;
}
/** THE ILLEGAL RESET FUNC */
void NoReset() { pPos = 0; }
/**
* Copy the data of another pool
*/
void AppendCopy(const Pool& v) {
if (!v.size()) return;
ExpandIf(v.size());
for (size_t i = 0; i < v.size(); i++) {
pData[pPos + i] = v.pData[i];
}
pPos += v.size();
}
/**
* Move the data of another pool
*/
void AppendMove(Pool& v) {
if (!v.size()) return;
ExpandIf(v.size());
for (size_t i = 0; i < v.size(); i++) {
pData[pPos + i] = std::move(v.pData[i]);
}
pPos += v.size();
v.ResetFast();
}
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]; }
iterator begin() { return pData; }
iterator end() { return pData + pPos; }
const_iterator begin() const { return pData; }
const_iterator end() const { return pData + pPos; }
T& operator[](size_t idx) { return at(idx); }
const T& operator[](size_t idx) const { return at(idx); }
private:
Pool(size_t size) : pCap(size), pPos(0) {
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);
}
size_t pCap = 0;
size_t pPos = 0;
Alloc pAlloc;
T* pData = nullptr;
};
} // namespace PD
Executable → Regular
+68 -28
View File
@@ -2,7 +2,7 @@
/* /*
MIT License MIT License
Copyright (c) 2024 - 2025 René Amthor (tobid7) Copyright (c) 2024 - 2026 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
@@ -23,7 +23,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. SOFTWARE.
*/ */
#include <pd/core/common.hpp> #include <pd/common.hpp>
namespace PD { namespace PD {
/** /**
@@ -43,47 +43,47 @@ constexpr int HexChar2Int(char c) {
* @param exts List of Extensions to check for * @param exts List of Extensions to check for
* @return true if one of the extensions is found in the String * @return true if one of the extensions is found in the String
*/ */
PD_CORE_API bool StringEndsWith(const std::string& str, PD_API bool StringEndsWith(const std::string& str,
const std::vector<std::string>& exts); const std::vector<std::string>& exts);
/** /**
* Function to Create a wstring of a string * Function to Create a wstring of a string
* @param s Input String to Convert * @param s Input String to Convert
* @return Result wstring * @return Result wstring
* @note Returns Empty if it has an error * @note Returns Empty if it has an error
*/ */
PD_CORE_API std::wstring MakeWstring(const std::string& s); PD_API std::wstring MakeWstring(const std::string& s);
/** /**
* Generate a Formatted String by an Nanoseconds Input * Generate a Formatted String by an Nanoseconds Input
* @param nanos Nanoseconds Input * @param nanos Nanoseconds Input
* @return Result String * @return Result String
*/ */
PD_CORE_API const std::string FormatNanos(unsigned long long nanos); PD_API const std::string FormatNanos(unsigned long long nanos);
/** /**
* Generate a Formatted String by an Milliseconds Input * Generate a Formatted String by an Milliseconds Input
* @param millis Milliseconds Input * @param millis Milliseconds Input
* @return Result String * @return Result String
*/ */
PD_CORE_API const std::string FormatMillis(unsigned long long millis); PD_API const std::string FormatMillis(unsigned long long millis);
/** /**
* Create a formatted String by an input bytes value * Create a formatted String by an input bytes value
* @param bytes value in bytes * @param bytes value in bytes
* @result Formatted String for example `2.5MB` * @result Formatted String for example `2.5MB`
*/ */
PD_CORE_API const std::string FormatBytes(unsigned long long bytes); PD_API const std::string FormatBytes(unsigned long long bytes);
/** /**
* Extract the Filename out of a Path * Extract the Filename out of a Path
* @param path Path to extract from * @param path Path to extract from
* @param saperators Path Split Chars * @param saperators Path Split Chars
* @return extracted filename * @return extracted filename
*/ */
PD_CORE_API const std::string GetFileName( PD_API const std::string GetFileName(const std::string& path,
const std::string& path, const std::string& saperators = "/\\"); const std::string& saperators = "/\\");
/** /**
* Remove Extension from a Path / Filename * Remove Extension from a Path / Filename
* @param path Input Path * @param path Input Path
* @return Path without Extension * @return Path without Extension
*/ */
PD_CORE_API const std::string PathRemoveExtension(const std::string& path); PD_API const std::string PathRemoveExtension(const std::string& path);
/** /**
* Function to Convert a Type to a hex value * Function to Convert a Type to a hex value
* @tparam T Type * @tparam T Type
@@ -92,39 +92,79 @@ PD_CORE_API const std::string PathRemoveExtension(const std::string& path);
*/ */
template <typename T> template <typename T>
inline const std::string ToHex(const T& v) { inline const std::string ToHex(const T& v) {
std::stringstream s; return std::format("{0:0{1}X}", v, sizeof(v) * 2);
s << "0x" << std::setfill('0') << std::setw(sizeof(v) * 2) << std::hex << v;
return s.str();
} }
/** /**
* Generate a Hash out of a string * Generate a Hash out of a string
* @param s String to hash * @param s String to hash
* @return 32Bit Hash * @return 32Bit Hash
*/ */
PD_CORE_API u32 FastHash(const std::string& s); PD_API u32 FastHash(const std::string& s);
/** /**
* Function to Generate a Compiler Name and Version String * Function to Generate a Compiler Name and Version String
* Based on their Macros * Based on their Macros
* @return CompilerName: Version * @return CompilerName: Version
*/ */
inline const std::string GetCompilerVersion() { inline const char* GetCompilerVersion() {
/// As the function looks like this Project is meant to /// As the function looks like this Project is meant to
/// Be ported to other systems as well /// Be ported to other systems as well
std::stringstream res; #define __mks(x) #x
#define mkstring(x) __mks(x)
#ifdef __clang__ // Check clang first #ifdef __clang__ // Check clang first
res << "Clang: " << __clang_major__ << "."; return "Clang: " mkstring(__clang_major__) "." mkstring(
res << __clang_minor__ << "."; __clang_minor__) "." mkstring(__clang_patchlevel__);
res << __clang_patchlevel__;
#elif __GNUC__ #elif __GNUC__
res << "GCC: " << __GNUC__; return "GCC: " mkstring(__GNUC__) "." mkstring(__GNUC_MINOR__) "." mkstring(
res << "." << __GNUC_MINOR__ << "."; __GNUC_PATCHLEVEL__);
res << __GNUC_PATCHLEVEL__;
#elif _MSC_VER #elif _MSC_VER
res << "MSVC: " << _MSC_VER; return "MSVC; " mkstring(_MSC_VER);
#else #else
res << "Unknown Compiler"; return "Unknown Compiler";
#endif #endif
return res.str();
} }
} // namespace Strings } // namespace Strings
class U8Iterator {
public:
explicit U8Iterator(const char* s) : ptr(reinterpret_cast<const u8*>(s)) {}
~U8Iterator() = default;
bool Decode32(u32& ret) {
if (ptr == nullptr || *ptr == 0) return false;
u8 c = *ptr;
if (c < 0x80) {
ret = c;
ptr += 1;
} else if ((c >> 5) == 0x6) {
ret = ((c & 0x1F) << 6) | (ptr[1] & 0x3F);
ptr += 2;
} else if ((c >> 4) == 0xE) {
ret = ((c & 0x0F) << 12) | ((ptr[1] & 0x3F) << 6) | (ptr[2] & 0x3F);
ptr += 3;
} else {
ret = ((c & 0x07) << 18) | ((ptr[1] & 0x3F) << 12) |
((ptr[2] & 0x3F) << 6) | (ptr[3] & 0x3F);
ptr += 4;
}
return true;
}
bool PeekNext32(u32& ret) {
if (ptr == nullptr || *(ptr + 1) == 0) return false;
u8 c = *ptr;
if (c < 0x80) {
ret = c;
} else if ((c >> 5) == 0x6) {
ret = ((c & 0x1F) << 6) | (ptr[1] & 0x3F);
} else if ((c >> 4) == 0xE) {
ret = ((c & 0x0F) << 12) | ((ptr[1] & 0x3F) << 6) | (ptr[2] & 0x3F);
} else {
ret = ((c & 0x07) << 18) | ((ptr[1] & 0x3F) << 12) |
((ptr[2] & 0x3F) << 6) | (ptr[3] & 0x3F);
}
return true;
}
private:
const u8* ptr = nullptr;
};
} // namespace PD } // namespace PD
+3 -5
View File
@@ -2,7 +2,7 @@
/* /*
MIT License MIT License
Copyright (c) 2024 - 2025 René Amthor (tobid7) Copyright (c) 2024 - 2026 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
@@ -23,13 +23,13 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. SOFTWARE.
*/ */
#include <pd/core/common.hpp> #include <pd/common.hpp>
namespace PD { namespace PD {
/** /**
* Timer class * Timer class
*/ */
class PD_CORE_API Timer { class PD_API Timer {
public: public:
/** /**
* Constructor * Constructor
@@ -41,8 +41,6 @@ class PD_CORE_API Timer {
*/ */
~Timer() {} ~Timer() {}
PD_SHARED(Timer);
/** /**
* Resume Timer if Paused * Resume Timer if Paused
*/ */
Executable → Regular
+20 -19
View File
@@ -2,7 +2,7 @@
/* /*
MIT License MIT License
Copyright (c) 2024 - 2025 René Amthor (tobid7) Copyright (c) 2024 - 2026 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
@@ -23,7 +23,8 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. SOFTWARE.
*/ */
#include <pd/core/common.hpp> #include <map>
#include <pd/common.hpp>
namespace PD { namespace PD {
/** /**
@@ -38,8 +39,6 @@ class TimeStats {
TimeStats(int l) : len(l), val(l, 0) {} TimeStats(int l) : len(l), val(l, 0) {}
~TimeStats() = default; ~TimeStats() = default;
PD_SHARED(TimeStats);
/** /**
* Add a New Value to the list * Add a New Value to the list
* @param v value to add * @param v value to add
@@ -102,12 +101,12 @@ class TimeStats {
* Get Data Buffer * Get Data Buffer
* @return data bufer (not edidable) * @return data bufer (not edidable)
*/ */
const std::vector<u64> &GetData() { return val; } const std::vector<u64>& GetData() { return val; }
/** /**
* Access an element in the list [not edidable] * Access an element in the list [not edidable]
* @return value to access * @return value to access
*/ */
const u64 &operator[](int i) { return val[smart_idx(i)]; } const u64& operator[](int i) { return val[smart_idx(i)]; }
/** /**
* Get List Lengh * Get List Lengh
* @return Lengh * @return Lengh
@@ -151,16 +150,14 @@ namespace TT {
class Res { class Res {
public: public:
/** Constructore that Inits a protocol at size of 60 frames */ /** Constructore that Inits a protocol at size of 60 frames */
Res() : start(0), end(0) { protocol = TimeStats::New(60); } Res() : start(0), end(0), protocol(60) {}
~Res() = default; ~Res() = default;
PD_SHARED(Res);
/** /**
* Setter for the ID (Name) * Setter for the ID (Name)
* @param v ID of the Trace * @param v ID of the Trace
*/ */
void SetID(const std::string &v) { id = v; } void SetID(const std::string& v) { id = v; }
/** /**
* Getter for the traces ID * Getter for the traces ID
* @return Trace ID * @return Trace ID
@@ -183,7 +180,7 @@ class Res {
void SetEnd(u64 v) { void SetEnd(u64 v) {
end = v; end = v;
diff = end - start; diff = end - start;
protocol->Add(GetLastDiff()); protocol.Add(GetLastDiff());
} }
/** /**
* Getter for the End Time * Getter for the End Time
@@ -200,30 +197,34 @@ class Res {
* Get Protcol Reference * Get Protcol Reference
* @return Protocol Ref * @return Protocol Ref
*/ */
TimeStats::Ref GetProtocol() { return protocol; } TimeStats& GetProtocol() { return protocol; }
private: private:
/** Trace ID */ /** Trace ID */
std::string id; std::string id;
/** Start time */ /** Start time */
u64 start; u64 start = 0;
/** End Time */ /** End Time */
u64 end; u64 end = 0;
/** Last Diff */ /** Last Diff */
u64 diff; u64 diff = 0;
/** Protocol */ /** Protocol */
TimeStats::Ref protocol; TimeStats protocol;
}; };
using TraceMap = std::map<std::string, TT::Res>;
/** /**
* Begin a Trace * Begin a Trace
* @param id Name of the Trace * @param id Name of the Trace
*/ */
PD_CORE_API void Beg(const std::string &id); PD_API void Beg(const std::string& id);
/** /**
* End a Trace * End a Trace
* @param id Name of the Trace * @param id Name of the Trace
*/ */
PD_CORE_API void End(const std::string &id); PD_API void End(const std::string& id);
PD_API TraceMap& GetTraceMap();
PD_API TT::Res& GetTraceRef(const std::string& id);
PD_API bool TraceExist(const std::string& id);
/** /**
* Collect Start end end of the trace by tracking * Collect Start end end of the trace by tracking
* when the Scope object goes out of scope * when the Scope object goes out of scope
@@ -245,7 +246,7 @@ class Scope {
* Constructor requiring a Name for the Trace * Constructor requiring a Name for the Trace
* @param id Name of the Trace * @param id Name of the Trace
*/ */
Scope(const std::string &id) { Scope(const std::string& id) {
this->ID = id; this->ID = id;
Beg(id); Beg(id);
} }
+2 -2
View File
@@ -2,7 +2,7 @@
/* /*
MIT License MIT License
Copyright (c) 2024 - 2025 René Amthor (tobid7) Copyright (c) 2024 - 2026 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
@@ -23,7 +23,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. SOFTWARE.
*/ */
#include <pd/core/common.hpp> #include <pd/common.hpp>
#ifndef M_PI #ifndef M_PI
#define M_PI 3.14159265358979323846 #define M_PI 3.14159265358979323846
+1 -1
View File
@@ -1,6 +1,6 @@
#pragma once #pragma once
#include <pd/core/common.hpp> #include <pd/common.hpp>
namespace PD { namespace PD {
/** /**
+2 -3
View File
@@ -2,8 +2,7 @@
/* /*
MIT License MIT License
Copyright (c) 2024 - 2026 René Amthor (tobid7)
Copyright (c) 2024 - 2025 tobid7
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
@@ -24,7 +23,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. SOFTWARE.
*/ */
#include <pd/core/common.hpp> #include <pd/common.hpp>
#include <pd/core/vec2.hpp> #include <pd/core/vec2.hpp>
#include <pd/core/vec3.hpp> #include <pd/core/vec3.hpp>
#include <pd/core/vec4.hpp> #include <pd/core/vec4.hpp>
+25 -25
View File
@@ -2,7 +2,7 @@
/* /*
MIT License MIT License
Copyright (c) 2024 - 2025 René Amthor (tobid7) Copyright (c) 2024 - 2026 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
@@ -23,9 +23,9 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. SOFTWARE.
*/ */
// This file is generated by lazyvec 2.0.0 // This file is generated by lazyvec 2.1.0
#include <pd/core/common.hpp> #include <pd/common.hpp>
namespace PD { namespace PD {
template <typename T> template <typename T>
@@ -54,117 +54,117 @@ class vec2 {
// Operations // Operations
template <typename T1> template <typename T1>
vec2<T>& operator+=(T1 v) { constexpr vec2<T>& operator+=(T1 v) {
x += (T)v; x += (T)v;
y += (T)v; y += (T)v;
return *this; return *this;
} }
template <typename T1> template <typename T1>
vec2<T>& operator+=(const vec2<T1>& v) { constexpr vec2<T>& operator+=(const vec2<T1>& v) {
x += (T)v.x; x += (T)v.x;
y += (T)v.y; y += (T)v.y;
return *this; return *this;
} }
template <typename T1> template <typename T1>
vec2<T> operator+(T1 v) const { constexpr vec2<T> operator+(T1 v) const {
return vec2<T>(x + (T)v, y + (T)v); return vec2<T>(x + (T)v, y + (T)v);
} }
template <typename T1> template <typename T1>
vec2<T> operator+(const vec2<T1>& v) const { constexpr vec2<T> operator+(const vec2<T1>& v) const {
return vec2<T>(x + (T)v.x, y + (T)v.y); return vec2<T>(x + (T)v.x, y + (T)v.y);
} }
template <typename T1> template <typename T1>
vec2<T>& operator-=(T1 v) { constexpr vec2<T>& operator-=(T1 v) {
x -= (T)v; x -= (T)v;
y -= (T)v; y -= (T)v;
return *this; return *this;
} }
template <typename T1> template <typename T1>
vec2<T>& operator-=(const vec2<T1>& v) { constexpr vec2<T>& operator-=(const vec2<T1>& v) {
x -= (T)v.x; x -= (T)v.x;
y -= (T)v.y; y -= (T)v.y;
return *this; return *this;
} }
template <typename T1> template <typename T1>
vec2<T> operator-(T1 v) const { constexpr vec2<T> operator-(T1 v) const {
return vec2<T>(x - (T)v, y - (T)v); return vec2<T>(x - (T)v, y - (T)v);
} }
template <typename T1> template <typename T1>
vec2<T> operator-(const vec2<T1>& v) const { constexpr vec2<T> operator-(const vec2<T1>& v) const {
return vec2<T>(x - (T)v.x, y - (T)v.y); return vec2<T>(x - (T)v.x, y - (T)v.y);
} }
template <typename T1> template <typename T1>
vec2<T>& operator*=(T1 v) { constexpr vec2<T>& operator*=(T1 v) {
x *= (T)v; x *= (T)v;
y *= (T)v; y *= (T)v;
return *this; return *this;
} }
template <typename T1> template <typename T1>
vec2<T>& operator*=(const vec2<T1>& v) { constexpr vec2<T>& operator*=(const vec2<T1>& v) {
x *= (T)v.x; x *= (T)v.x;
y *= (T)v.y; y *= (T)v.y;
return *this; return *this;
} }
template <typename T1> template <typename T1>
vec2<T> operator*(T1 v) const { constexpr vec2<T> operator*(T1 v) const {
return vec2<T>(x * (T)v, y * (T)v); return vec2<T>(x * (T)v, y * (T)v);
} }
template <typename T1> template <typename T1>
vec2<T> operator*(const vec2<T1>& v) const { constexpr vec2<T> operator*(const vec2<T1>& v) const {
return vec2<T>(x * (T)v.x, y * (T)v.y); return vec2<T>(x * (T)v.x, y * (T)v.y);
} }
template <typename T1> template <typename T1>
vec2<T>& operator/=(T1 v) { constexpr vec2<T>& operator/=(T1 v) {
x /= (T)v; x /= (T)v;
y /= (T)v; y /= (T)v;
return *this; return *this;
} }
template <typename T1> template <typename T1>
vec2<T>& operator/=(const vec2<T1>& v) { constexpr vec2<T>& operator/=(const vec2<T1>& v) {
x /= (T)v.x; x /= (T)v.x;
y /= (T)v.y; y /= (T)v.y;
return *this; return *this;
} }
template <typename T1> template <typename T1>
vec2<T> operator/(T1 v) const { constexpr vec2<T> operator/(T1 v) const {
return vec2<T>(x / (T)v, y / (T)v); return vec2<T>(x / (T)v, y / (T)v);
} }
template <typename T1> template <typename T1>
vec2<T> operator/(const vec2<T1>& v) const { constexpr vec2<T> operator/(const vec2<T1>& v) const {
return vec2<T>(x / (T)v.x, y / (T)v.y); return vec2<T>(x / (T)v.x, y / (T)v.y);
} }
// Generic Operations // Generic Operations
vec2 operator-() const { return vec2(-x, -y); } constexpr vec2 operator-() const { return vec2(-x, -y); }
template <typename T1> template <typename T1>
bool operator==(const vec2<T1>& v) const { constexpr bool operator==(const vec2<T1>& v) const {
return x == (T)v.x && y == (T)v.y; return x == (T)v.x && y == (T)v.y;
} }
template <typename T1> template <typename T1>
bool operator!=(const vec2<T1>& v) const { constexpr bool operator!=(const vec2<T1>& v) const {
return !(*this == v); return !(*this == v);
} }
// Functions // Functions
double Len() const { return std::sqrt(SqLen()); } double Len() const { return std::sqrt(SqLen()); }
double SqLen() const { return x * x + y * y; } constexpr double SqLen() const { return x * x + y * y; }
template <typename T1> template <typename T1>
double Distance(const vec2<T1>& v) const { double Distance(const vec2<T1>& v) const {
@@ -180,12 +180,12 @@ class vec2 {
} }
template <typename T1> template <typename T1>
T Dot(const vec2<T1>& v) const { constexpr T Dot(const vec2<T1>& v) const {
return x * (T)v.x + y * (T)v.y; return x * (T)v.x + y * (T)v.y;
} }
// Swap Functions // Swap Functions
void SwapXY() { constexpr void SwapXY() {
T t = x; T t = x;
x = y; x = y;
y = t; y = t;
+28 -28
View File
@@ -2,7 +2,7 @@
/* /*
MIT License MIT License
Copyright (c) 2024 - 2025 René Amthor (tobid7) Copyright (c) 2024 - 2026 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
@@ -23,9 +23,9 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. SOFTWARE.
*/ */
// This file is generated by lazyvec 2.0.0 // This file is generated by lazyvec 2.1.0
#include <pd/core/common.hpp> #include <pd/common.hpp>
// Extended includes (rename if you use other filenames/paths) // Extended includes (rename if you use other filenames/paths)
#include <pd/core/vec2.hpp> #include <pd/core/vec2.hpp>
@@ -69,7 +69,7 @@ class vec3 {
// Operations // Operations
template <typename T1> template <typename T1>
vec3<T>& operator+=(T1 v) { constexpr vec3<T>& operator+=(T1 v) {
x += (T)v; x += (T)v;
y += (T)v; y += (T)v;
z += (T)v; z += (T)v;
@@ -77,7 +77,7 @@ class vec3 {
} }
template <typename T1> template <typename T1>
vec3<T>& operator+=(const vec3<T1>& v) { constexpr vec3<T>& operator+=(const vec3<T1>& v) {
x += (T)v.x; x += (T)v.x;
y += (T)v.y; y += (T)v.y;
z += (T)v.z; z += (T)v.z;
@@ -85,17 +85,17 @@ class vec3 {
} }
template <typename T1> template <typename T1>
vec3<T> operator+(T1 v) const { constexpr vec3<T> operator+(T1 v) const {
return vec3<T>(x + (T)v, y + (T)v, z + (T)v); return vec3<T>(x + (T)v, y + (T)v, z + (T)v);
} }
template <typename T1> template <typename T1>
vec3<T> operator+(const vec3<T1>& v) const { constexpr vec3<T> operator+(const vec3<T1>& v) const {
return vec3<T>(x + (T)v.x, y + (T)v.y, z + (T)v.z); return vec3<T>(x + (T)v.x, y + (T)v.y, z + (T)v.z);
} }
template <typename T1> template <typename T1>
vec3<T>& operator-=(T1 v) { constexpr vec3<T>& operator-=(T1 v) {
x -= (T)v; x -= (T)v;
y -= (T)v; y -= (T)v;
z -= (T)v; z -= (T)v;
@@ -103,7 +103,7 @@ class vec3 {
} }
template <typename T1> template <typename T1>
vec3<T>& operator-=(const vec3<T1>& v) { constexpr vec3<T>& operator-=(const vec3<T1>& v) {
x -= (T)v.x; x -= (T)v.x;
y -= (T)v.y; y -= (T)v.y;
z -= (T)v.z; z -= (T)v.z;
@@ -111,17 +111,17 @@ class vec3 {
} }
template <typename T1> template <typename T1>
vec3<T> operator-(T1 v) const { constexpr vec3<T> operator-(T1 v) const {
return vec3<T>(x - (T)v, y - (T)v, z - (T)v); return vec3<T>(x - (T)v, y - (T)v, z - (T)v);
} }
template <typename T1> template <typename T1>
vec3<T> operator-(const vec3<T1>& v) const { constexpr vec3<T> operator-(const vec3<T1>& v) const {
return vec3<T>(x - (T)v.x, y - (T)v.y, z - (T)v.z); return vec3<T>(x - (T)v.x, y - (T)v.y, z - (T)v.z);
} }
template <typename T1> template <typename T1>
vec3<T>& operator*=(T1 v) { constexpr vec3<T>& operator*=(T1 v) {
x *= (T)v; x *= (T)v;
y *= (T)v; y *= (T)v;
z *= (T)v; z *= (T)v;
@@ -129,7 +129,7 @@ class vec3 {
} }
template <typename T1> template <typename T1>
vec3<T>& operator*=(const vec3<T1>& v) { constexpr vec3<T>& operator*=(const vec3<T1>& v) {
x *= (T)v.x; x *= (T)v.x;
y *= (T)v.y; y *= (T)v.y;
z *= (T)v.z; z *= (T)v.z;
@@ -137,17 +137,17 @@ class vec3 {
} }
template <typename T1> template <typename T1>
vec3<T> operator*(T1 v) const { constexpr vec3<T> operator*(T1 v) const {
return vec3<T>(x * (T)v, y * (T)v, z * (T)v); return vec3<T>(x * (T)v, y * (T)v, z * (T)v);
} }
template <typename T1> template <typename T1>
vec3<T> operator*(const vec3<T1>& v) const { constexpr vec3<T> operator*(const vec3<T1>& v) const {
return vec3<T>(x * (T)v.x, y * (T)v.y, z * (T)v.z); return vec3<T>(x * (T)v.x, y * (T)v.y, z * (T)v.z);
} }
template <typename T1> template <typename T1>
vec3<T>& operator/=(T1 v) { constexpr vec3<T>& operator/=(T1 v) {
x /= (T)v; x /= (T)v;
y /= (T)v; y /= (T)v;
z /= (T)v; z /= (T)v;
@@ -155,7 +155,7 @@ class vec3 {
} }
template <typename T1> template <typename T1>
vec3<T>& operator/=(const vec3<T1>& v) { constexpr vec3<T>& operator/=(const vec3<T1>& v) {
x /= (T)v.x; x /= (T)v.x;
y /= (T)v.y; y /= (T)v.y;
z /= (T)v.z; z /= (T)v.z;
@@ -163,31 +163,31 @@ class vec3 {
} }
template <typename T1> template <typename T1>
vec3<T> operator/(T1 v) const { constexpr vec3<T> operator/(T1 v) const {
return vec3<T>(x / (T)v, y / (T)v, z / (T)v); return vec3<T>(x / (T)v, y / (T)v, z / (T)v);
} }
template <typename T1> template <typename T1>
vec3<T> operator/(const vec3<T1>& v) const { constexpr vec3<T> operator/(const vec3<T1>& v) const {
return vec3<T>(x / (T)v.x, y / (T)v.y, z / (T)v.z); return vec3<T>(x / (T)v.x, y / (T)v.y, z / (T)v.z);
} }
// Generic Operations // Generic Operations
vec3 operator-() const { return vec3(-x, -y, -z); } constexpr vec3 operator-() const { return vec3(-x, -y, -z); }
template <typename T1> template <typename T1>
bool operator==(const vec3<T1>& v) const { constexpr bool operator==(const vec3<T1>& v) const {
return x == (T)v.x && y == (T)v.y && z == (T)v.z; return x == (T)v.x && y == (T)v.y && z == (T)v.z;
} }
template <typename T1> template <typename T1>
bool operator!=(const vec3<T1>& v) const { constexpr bool operator!=(const vec3<T1>& v) const {
return !(*this == v); return !(*this == v);
} }
// Functions // Functions
double Len() const { return std::sqrt(SqLen()); } double Len() const { return std::sqrt(SqLen()); }
double SqLen() const { return x * x + y * y + z * z; } constexpr double SqLen() const { return x * x + y * y + z * z; }
template <typename T1> template <typename T1>
double Distance(const vec3<T1>& v) const { double Distance(const vec3<T1>& v) const {
@@ -203,27 +203,27 @@ class vec3 {
} }
template <typename T1> template <typename T1>
T Dot(const vec3<T1>& v) const { constexpr T Dot(const vec3<T1>& v) const {
return x * (T)v.x + y * (T)v.y + z * (T)v.z; return x * (T)v.x + y * (T)v.y + z * (T)v.z;
} }
template <typename T1> template <typename T1>
vec3<T> Cross(const vec3<T1>& v) const { constexpr vec3<T> Cross(const vec3<T1>& v) const {
return vec3<T>(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x); return vec3<T>(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x);
} }
// Swap Functions // Swap Functions
void SwapXY() { constexpr void SwapXY() {
T t = x; T t = x;
x = y; x = y;
y = t; y = t;
} }
void SwapXZ() { constexpr void SwapXZ() {
T t = x; T t = x;
x = z; x = z;
z = t; z = t;
} }
void SwapYZ() { constexpr void SwapYZ() {
T t = y; T t = y;
y = z; y = z;
z = t; z = t;
+30 -30
View File
@@ -2,7 +2,7 @@
/* /*
MIT License MIT License
Copyright (c) 2024 - 2025 René Amthor (tobid7) Copyright (c) 2024 - 2026 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
@@ -23,9 +23,9 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE. SOFTWARE.
*/ */
// This file is generated by lazyvec 2.0.0 // This file is generated by lazyvec 2.1.0
#include <pd/core/common.hpp> #include <pd/common.hpp>
// Extended includes (rename if you use other filenames/paths) // Extended includes (rename if you use other filenames/paths)
#include <pd/core/vec2.hpp> #include <pd/core/vec2.hpp>
#include <pd/core/vec3.hpp> #include <pd/core/vec3.hpp>
@@ -84,7 +84,7 @@ class vec4 {
// Operations // Operations
template <typename T1> template <typename T1>
vec4<T>& operator+=(T1 v) { constexpr vec4<T>& operator+=(T1 v) {
x += (T)v; x += (T)v;
y += (T)v; y += (T)v;
z += (T)v; z += (T)v;
@@ -93,7 +93,7 @@ class vec4 {
} }
template <typename T1> template <typename T1>
vec4<T>& operator+=(const vec4<T1>& v) { constexpr vec4<T>& operator+=(const vec4<T1>& v) {
x += (T)v.x; x += (T)v.x;
y += (T)v.y; y += (T)v.y;
z += (T)v.z; z += (T)v.z;
@@ -102,17 +102,17 @@ class vec4 {
} }
template <typename T1> template <typename T1>
vec4<T> operator+(T1 v) const { constexpr vec4<T> operator+(T1 v) const {
return vec4<T>(x + (T)v, y + (T)v, z + (T)v, w + (T)v); return vec4<T>(x + (T)v, y + (T)v, z + (T)v, w + (T)v);
} }
template <typename T1> template <typename T1>
vec4<T> operator+(const vec4<T1>& v) const { constexpr vec4<T> operator+(const vec4<T1>& v) const {
return vec4<T>(x + (T)v.x, y + (T)v.y, z + (T)v.z, w + (T)v.w); return vec4<T>(x + (T)v.x, y + (T)v.y, z + (T)v.z, w + (T)v.w);
} }
template <typename T1> template <typename T1>
vec4<T>& operator-=(T1 v) { constexpr vec4<T>& operator-=(T1 v) {
x -= (T)v; x -= (T)v;
y -= (T)v; y -= (T)v;
z -= (T)v; z -= (T)v;
@@ -121,7 +121,7 @@ class vec4 {
} }
template <typename T1> template <typename T1>
vec4<T>& operator-=(const vec4<T1>& v) { constexpr vec4<T>& operator-=(const vec4<T1>& v) {
x -= (T)v.x; x -= (T)v.x;
y -= (T)v.y; y -= (T)v.y;
z -= (T)v.z; z -= (T)v.z;
@@ -130,17 +130,17 @@ class vec4 {
} }
template <typename T1> template <typename T1>
vec4<T> operator-(T1 v) const { constexpr vec4<T> operator-(T1 v) const {
return vec4<T>(x - (T)v, y - (T)v, z - (T)v, w - (T)v); return vec4<T>(x - (T)v, y - (T)v, z - (T)v, w - (T)v);
} }
template <typename T1> template <typename T1>
vec4<T> operator-(const vec4<T1>& v) const { constexpr vec4<T> operator-(const vec4<T1>& v) const {
return vec4<T>(x - (T)v.x, y - (T)v.y, z - (T)v.z, w - (T)v.w); return vec4<T>(x - (T)v.x, y - (T)v.y, z - (T)v.z, w - (T)v.w);
} }
template <typename T1> template <typename T1>
vec4<T>& operator*=(T1 v) { constexpr vec4<T>& operator*=(T1 v) {
x *= (T)v; x *= (T)v;
y *= (T)v; y *= (T)v;
z *= (T)v; z *= (T)v;
@@ -149,7 +149,7 @@ class vec4 {
} }
template <typename T1> template <typename T1>
vec4<T>& operator*=(const vec4<T1>& v) { constexpr vec4<T>& operator*=(const vec4<T1>& v) {
x *= (T)v.x; x *= (T)v.x;
y *= (T)v.y; y *= (T)v.y;
z *= (T)v.z; z *= (T)v.z;
@@ -158,17 +158,17 @@ class vec4 {
} }
template <typename T1> template <typename T1>
vec4<T> operator*(T1 v) const { constexpr vec4<T> operator*(T1 v) const {
return vec4<T>(x * (T)v, y * (T)v, z * (T)v, w * (T)v); return vec4<T>(x * (T)v, y * (T)v, z * (T)v, w * (T)v);
} }
template <typename T1> template <typename T1>
vec4<T> operator*(const vec4<T1>& v) const { constexpr vec4<T> operator*(const vec4<T1>& v) const {
return vec4<T>(x * (T)v.x, y * (T)v.y, z * (T)v.z, w * (T)v.w); return vec4<T>(x * (T)v.x, y * (T)v.y, z * (T)v.z, w * (T)v.w);
} }
template <typename T1> template <typename T1>
vec4<T>& operator/=(T1 v) { constexpr vec4<T>& operator/=(T1 v) {
x /= (T)v; x /= (T)v;
y /= (T)v; y /= (T)v;
z /= (T)v; z /= (T)v;
@@ -177,7 +177,7 @@ class vec4 {
} }
template <typename T1> template <typename T1>
vec4<T>& operator/=(const vec4<T1>& v) { constexpr vec4<T>& operator/=(const vec4<T1>& v) {
x /= (T)v.x; x /= (T)v.x;
y /= (T)v.y; y /= (T)v.y;
z /= (T)v.z; z /= (T)v.z;
@@ -186,31 +186,31 @@ class vec4 {
} }
template <typename T1> template <typename T1>
vec4<T> operator/(T1 v) const { constexpr vec4<T> operator/(T1 v) const {
return vec4<T>(x / (T)v, y / (T)v, z / (T)v, w / (T)v); return vec4<T>(x / (T)v, y / (T)v, z / (T)v, w / (T)v);
} }
template <typename T1> template <typename T1>
vec4<T> operator/(const vec4<T1>& v) const { constexpr vec4<T> operator/(const vec4<T1>& v) const {
return vec4<T>(x / (T)v.x, y / (T)v.y, z / (T)v.z, w / (T)v.w); return vec4<T>(x / (T)v.x, y / (T)v.y, z / (T)v.z, w / (T)v.w);
} }
// Generic Operations // Generic Operations
vec4 operator-() const { return vec4(-x, -y, -z, -w); } constexpr vec4 operator-() const { return vec4(-x, -y, -z, -w); }
template <typename T1> template <typename T1>
bool operator==(const vec4<T1>& v) const { constexpr bool operator==(const vec4<T1>& v) const {
return x == (T)v.x && y == (T)v.y && z == (T)v.z && w == (T)v.w; return x == (T)v.x && y == (T)v.y && z == (T)v.z && w == (T)v.w;
} }
template <typename T1> template <typename T1>
bool operator!=(const vec4<T1>& v) const { constexpr bool operator!=(const vec4<T1>& v) const {
return !(*this == v); return !(*this == v);
} }
// Functions // Functions
double Len() const { return std::sqrt(SqLen()); } double Len() const { return std::sqrt(SqLen()); }
double SqLen() const { return x * x + y * y + z * z + w * w; } constexpr double SqLen() const { return x * x + y * y + z * z + w * w; }
template <typename T1> template <typename T1>
double Distance(const vec4<T1>& v) const { double Distance(const vec4<T1>& v) const {
@@ -226,37 +226,37 @@ class vec4 {
} }
template <typename T1> template <typename T1>
T Dot(const vec4<T1>& v) const { constexpr T Dot(const vec4<T1>& v) const {
return x * (T)v.x + y * (T)v.y + z * (T)v.z + w * (T)v.w; return x * (T)v.x + y * (T)v.y + z * (T)v.z + w * (T)v.w;
} }
// Swap Functions // Swap Functions
void SwapXY() { constexpr void SwapXY() {
T t = x; T t = x;
x = y; x = y;
y = t; y = t;
} }
void SwapXZ() { constexpr void SwapXZ() {
T t = x; T t = x;
x = z; x = z;
z = t; z = t;
} }
void SwapXW() { constexpr void SwapXW() {
T t = x; T t = x;
x = w; x = w;
w = t; w = t;
} }
void SwapYZ() { constexpr void SwapYZ() {
T t = y; T t = y;
y = z; y = z;
z = t; z = t;
} }
void SwapYW() { constexpr void SwapYW() {
T t = y; T t = y;
y = w; y = w;
w = t; w = t;
} }
void SwapZW() { constexpr void SwapZW() {
T t = z; T t = z;
z = w; z = w;
w = t; w = t;
-23
View File
@@ -1,28 +1,5 @@
#pragma once #pragma once
/*
MIT License
Copyright (c) 2024 - 2025 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/gfx.hpp> #include <pd/drivers/gfx.hpp>
#include <pd/drivers/hid.hpp> #include <pd/drivers/hid.hpp>
#include <pd/drivers/os.hpp> #include <pd/drivers/os.hpp>
+285 -105
View File
@@ -1,144 +1,324 @@
#pragma once #pragma once
/* #include <pd/core/mat.hpp>
MIT License #include <pd/drivers/interface.hpp>
Copyright (c) 2024 - 2025 René Amthor (tobid7) #include <pd/lithium/drawlist.hpp>
#include <pd/lithium/pools.hpp>
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/texture.hpp> #include <pd/lithium/texture.hpp>
using LiBackendFlags = PD::u32; using PDGfxBackendFlags = PD::u32;
enum LiBackendFlags_ { enum PDGfxBackendFlags_ {
LiBackendFlags_None = 0, PDGfxBackendFlags_None = 0,
LiBackendFlags_FlipUV_Y = PD_BIT(0), // Essential for font loading PDGfxBackendFlags_FlipUV_Y = 1 << 0, // Essential for font loading
PDGfxBackendFlags_WindingCW = 1 << 1, // Use CW instead of CCW winding
PDGfxBackendFlags_HasClipRect = 1 << 2, // Clip rects supported
PDGfxBackendFlags_ReqPow2 = 1 << 3, // Tex size must be pow of 2
PDGfxBackendFlags_ApproxSDF = 1 << 4, // Fake SDF for non smoothstep systems
}; };
namespace PD { namespace PD {
// The backend api nobody asked for
class GfxDriver2 { // Pre interface class
class PD_API GfxDriver : public DriverInterface {
public: public:
GfxDriver2(const std::string& name = "NullGfx") : pName(name) {}; GfxDriver(std::string_view name);
~GfxDriver2() = default; virtual ~GfxDriver();
virtual void Init() {} virtual void Init() {}
virtual void Deinit() {} virtual void Deinit() {
virtual void Draw(const PD::Li::CmdPool& pool) {} DeleteTexture(pWhite);
virtual void TexBind(PD::Li::TexAddress addr) {} SysDeinit();
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 TexDelete(PD::Li::TexAddress tex) {}
void SetViewPort(const ivec2& size);
void SetViewPort(int x, int y);
virtual void BindTexture(TextureID id) {}
void Reset();
virtual Li::Texture LoadTexture(
const std::vector<PD::u8>& pixels, int w, int h,
TextureFormat type = TextureFormat::RGBA32,
TextureFilter filter = TextureFilter::Linear) {
return Li::Texture();
}
virtual void DeleteTexture(const Li::Texture& tex) {}
virtual void Draw(const Li::Drawlist& commands) {}
Li::Texture::Ptr GetWhiteTexture() { return &pWhite; }
PDGfxBackendFlags GetFlags() { return Flags; }
size_t GetNumVertices() const { return CountVertices; }
size_t GetNumIndices() const { return CountIndices; }
size_t GetNumDrawcalls() const { return CountDrawcalls; }
size_t GetNumCommands() const { return CountCommands; }
// Global Pool Interface
virtual size_t AllocateVertices(size_t count, PD::ptr accessor) = 0;
virtual size_t AllocateIndices(size_t count, PD::ptr accessor) = 0;
virtual bool ExpandVertices(size_t count, PD::ptr accessor) = 0;
virtual bool ExpandIndices(size_t count, PD::ptr accessor) = 0;
virtual void PutVertex(size_t loc, const Li::Vertex& vtx,
PD::ptr accessor) = 0;
virtual void PutIndex(size_t loc, u16 idx, PD::ptr accessor) = 0;
virtual const Li::Vertex& GetVertex(size_t loc) const = 0;
virtual const u16& GetIndex(size_t loc) const = 0;
virtual void ResetPools() = 0;
protected: protected:
const std::string pName; virtual void SysDeinit() {}
LiBackendFlags pFlags = 0; virtual void SysInit() {}
virtual void SysReset() {}
virtual void Submit(size_t count, size_t start) {}
virtual void UploadPools() {} // not every driver requires it
virtual void ClipRect() {}
void RegisterTexture(const Li::Texture& tex);
void UnregisterTexture(const Li::Texture& tex);
// Counters
size_t CountDrawcalls = 0;
size_t CountCommands = 0;
size_t CountVertices = 0;
size_t CountIndices = 0;
size_t CurrentIndex = 0; size_t CurrentIndex = 0;
size_t CurrentVertex = 0; size_t CurrentVertex = 0;
size_t pCountDrawcalls = 0;
size_t pCountCommands = 0;
// State Variables oder so
TextureID CurrentTex = 0;
bool CurrentTexIsSDF = false;
bool CurrentHasClip = false;
fvec4 CurrentClip = 0;
Mat4 Projection;
ivec2 ViewPort; ivec2 ViewPort;
std::unordered_map<TextureID, Li::Texture> pTextureRegestry;
Li::Texture pWhite;
PDGfxBackendFlags Flags = 0;
}; };
class GfxDriver {
struct DefaultGfxConfig {
// Vertex Allocator
template <typename T>
using VertexAlloc = std::allocator<T>;
// Index Allocator
template <typename T>
using IndexAlloc = std::allocator<T>;
};
template <typename Config = DefaultGfxConfig>
class GfxDriverBase : public GfxDriver {
public: public:
GfxDriver(const std::string& name = "NullGfx") : pName(name) {}; using VtxPool =
~GfxDriver() = default; 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() {}
PD_SHARED(GfxDriver); void Init() override {
SysInit();
void PostInit(); std::vector<u8> img(16 * 16 * 4, 0xff);
pWhite = LoadTexture(img, 16, 16);
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; }
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;
} }
virtual void DestroyTex(PD::Li::Texture::Ref tex) {} void Draw(const Li::Drawlist& dl) override {
const auto& commands = dl.Data();
if (commands.size() == 0) return;
pCountCommands += commands.size();
Li::Texture::Ref GetSolidTex() { return pSolid; } size_t vtotal = dl.GetNumVertices();
size_t itotal = dl.GetNumIndices();
const std::string pName = "NullGfx"; size_t start_vtx = pVtxPool.size();
LiBackendFlags Flags = 0; pVtxPool.Allocate(vtotal);
ivec2 ViewPort; CurrentVertex += vtotal;
Mat4 Projection;
Li::Texture::Ref pSolid;
size_t CurrentVertex = 0;
size_t CurrentIndex = 0;
/** Debug Variables */ size_t start_idx = pIdxPool.size();
pIdxPool.Allocate(itotal);
CurrentIndex += itotal;
// Optional Index counter size_t current_vtx = start_vtx;
u32 IndexCounter; size_t current_idx = start_idx;
// Optional Vertex counter
u32 VertexCounter; const auto& vpool = Li::GetVertexPool();
// Optional Frame Counter const auto& ipool = Li::GetIndexPool();
u64 FrameCounter; /** Build Pools */
for (size_t i = 0; i < commands.size(); i++) {
const auto& cmd = commands[i];
if (cmd.VertexCount > 0) {
std::memcpy(reinterpret_cast<void*>(pVtxPool.begin() + current_vtx),
vpool.begin() + cmd.FirstVertex,
cmd.VertexCount * sizeof(Li::Vertex));
}
for (size_t idx = 0; idx < cmd.IndexCount; idx++) {
u16 local_idx = ipool[cmd.FirstIndex + idx];
pIdxPool[current_idx + idx] = static_cast<u16>(current_vtx + local_idx);
}
current_vtx += cmd.VertexCount;
current_idx += cmd.IndexCount;
}
UploadPools();
size_t index = 0;
size_t ioff = start_idx;
while (index < commands.size()) {
CurrentTex = commands[index].Tex;
CurrentTexIsSDF = commands[index].SDF;
CurrentHasClip = commands[index].ClipRectUsed;
CurrentClip = commands[index].ClipRect;
if (!CurrentTex) {
CurrentTex = pWhite.GetID();
}
size_t num_indices = 0;
ClipRect();
while (index < commands.size() &&
CurrentTexIsSDF == commands[index].SDF &&
(CurrentTex == commands[index].Tex ||
(CurrentTex == pWhite.GetID() && commands[index].Tex == 0)) &&
CurrentClip == commands[index].ClipRect &&
CurrentHasClip == commands[index].ClipRectUsed) {
num_indices += commands[index].IndexCount;
index++;
}
if (num_indices > 0) {
Submit(num_indices, ioff);
ioff += num_indices;
pCountDrawcalls++;
}
}
}
size_t AllocateVertices(size_t count, PD::ptr accessor) override {
pVertexAccessor = accessor;
size_t loc = pVtxPool.size();
pVtxPool.Allocate(count);
return loc;
}
size_t AllocateIndices(size_t count, PD::ptr accessor) override {
pIndexAccessor = accessor;
size_t loc = pIdxPool.size();
pIdxPool.Allocate(count);
return loc;
}
bool ExpandVertices(size_t count, PD::ptr accessor) override {
if (pVertexAccessor != accessor) return false;
pVtxPool.Allocate(count);
return true;
}
bool ExpandIndices(size_t count, PD::ptr accessor) override {
if (pIndexAccessor != accessor) return false;
pIdxPool.Allocate(count);
return true;
}
void PutVertex(size_t loc, const Li::Vertex& vtx, PD::ptr accessor) override {
if (pVertexAccessor != accessor) return;
pVtxPool.Put(loc, vtx);
}
void PutIndex(size_t loc, u16 idx, PD::ptr accessor) override {
if (pIndexAccessor != accessor) return;
pIdxPool.Put(loc, idx);
}
const Li::Vertex& GetVertex(size_t loc) const override {
return pVtxPool[loc];
}
const u16& GetIndex(size_t loc) const override { return pIdxPool[loc]; }
void ResetPools() override {
pVtxPool.NoReset();
pIdxPool.NoReset();
}
protected:
u16* GetIndexBufPtr(size_t start) { return &pIdxPool[start]; }
Li::Vertex* GetVertexBufPtr(size_t start) { return &pVtxPool[start]; }
size_t GetVertexPoolSize() const { return pVtxPool.size(); }
size_t GetIndexPoolSize() const { return pIdxPool.size(); }
private:
VtxPool pVtxPool;
IdxPool pIdxPool;
PD::ptr pVertexAccessor = 0;
PD::ptr pIndexAccessor = 0;
}; };
/** Static Gfx Controller */ class PD_API Gfx {
class Gfx {
public: public:
Gfx() = default; Gfx() = default;
~Gfx() = default; ~Gfx() = default;
static void Init(GfxDriver::Ref d); template <typename T, typename... Args>
static void UseDriver(Args&&... args) {
static void Deinit() { pGfx->Deinit(); } // assert(driver == nullptr && "OS Driver already set");
static void NewFrame() { pGfx->NewFrame(); } driver = std::make_unique<T>(std::forward<Args>(args)...);
static void BindTex(Li::TexAddress addr) { pGfx->BindTex(addr); }
static void SetViewPort(const ivec2& vp) { pGfx->SetViewPort(vp); }
static void SetViewPort(int w, int h) { pGfx->SetViewPort(PD::ivec2(w, h)); }
static void RenderDrawData(const Li::CmdPool& Commands) {
pGfx->RenderDrawData(Commands);
} }
static LiBackendFlags Flags() { return pGfx->Flags; } static void Init() { driver->Init(); }
static Li::Texture::Ref LoadTex( static void Deinit() { driver->Deinit(); }
const std::vector<u8>& pixels, int w, int h, static void SetViewPort(const ivec2& vp) { driver->SetViewPort(vp); }
Li::Texture::Type type = Li::Texture::Type::RGBA32, static void SetViewPort(int w, int h) { driver->SetViewPort(w, h); }
Li::Texture::Filter filter = Li::Texture::Filter::LINEAR) { static void Reset() { driver->Reset(); }
return pGfx->LoadTex(pixels, w, h, type, filter); static void Draw(const Li::Drawlist& dl) { driver->Draw(dl); }
static Li::Texture 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 void DeleteTexture(const Li::Texture& tex) {
driver->DeleteTexture(tex);
}
static Li::Texture::Ptr GetWhiteTexture() {
return driver->GetWhiteTexture();
} }
static void DestroyTex(Li::Texture::Ref tex) { pGfx->DestroyTex(tex); } static PDGfxBackendFlags GetFlags() { return driver->GetFlags(); }
static Li::Texture::Ref GetSolidTex() { return pGfx->GetSolidTex(); } static const char* GetDriverName() { return driver->GetName(); }
static GfxDriver::Ref pGfx; static size_t GetNumVertices() { return driver->GetNumVertices(); }
static size_t GetNumIndices() { return driver->GetNumIndices(); }
static size_t GetNumDrawcalls() { return driver->GetNumDrawcalls(); }
static size_t GetNumCommands() { return driver->GetNumCommands(); }
// Gloabal Pool Interface
static size_t AllocateVertices(size_t count, PD::ptr accessor) {
return driver->AllocateVertices(count, accessor);
}
static size_t AllocateIndices(size_t count, PD::ptr accessor) {
return driver->AllocateIndices(count, accessor);
}
static bool ExpandVertices(size_t count, PD::ptr accessor) {
return driver->ExpandVertices(count, accessor);
}
static bool ExpandIndices(size_t count, PD::ptr accessor) {
return driver->ExpandIndices(count, accessor);
}
static void PutVertex(size_t loc, const Li::Vertex& vtx, PD::ptr accessor) {
driver->PutVertex(loc, vtx, accessor);
}
static void PutIndex(size_t loc, u16 idx, PD::ptr accessor) {
driver->PutIndex(loc, idx, accessor);
}
static const Li::Vertex& GetVertex(size_t loc) {
return driver->GetVertex(loc);
}
static const u16& GetIndex(size_t loc) { return driver->GetIndex(loc); }
static void NewFrame() { driver->ResetPools(); }
private:
static std::unique_ptr<GfxDriver> driver;
}; };
} // namespace PD } // namespace PD
Executable → Regular
+168 -281
View File
@@ -1,296 +1,183 @@
#pragma once #pragma once
/*
MIT License
Copyright (c) 2024 - 2025 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/core/core.hpp>
#include <pd/drivers/interface.hpp>
namespace PD { using PDHidBackendFlags = PD::u32;
/** Did not found a better solution yet sadly */ enum PDHidBackendFlags_ {
namespace HidKb { PDHidBackendFlags_None = 0,
// Lets use u128 here PDHidBackendFlags_HasTouch = 1 << 1,
using KbKey = u128; PDHidBackendFlags_HasGamepad = 1 << 2,
constexpr static KbKey Kb_No = 0; PDHidBackendFlags_HasMouse = 1 << 3,
constexpr static KbKey Kb_Escape = KbKey::Flag(0); PDHidBackendFlags_HasKeyboard = 1 << 4,
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 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) {};
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) {}
/** 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];
}; };
/** Static Hid Controller */ namespace PD {
class Hid { namespace HidInternal {
class Keyboard {
public: public:
Keyboard() = default;
virtual ~Keyboard() = default;
using Key = u128;
constexpr static Key No = 0;
constexpr static Key Escape = Key::Flag(0);
constexpr static Key Q = Key::Flag(1);
constexpr static Key W = Key::Flag(2);
constexpr static Key E = Key::Flag(3);
constexpr static Key R = Key::Flag(4);
constexpr static Key T = Key::Flag(5);
constexpr static Key Z = Key::Flag(6);
constexpr static Key U = Key::Flag(7);
constexpr static Key I = Key::Flag(8);
constexpr static Key O = Key::Flag(9);
constexpr static Key P = Key::Flag(10);
constexpr static Key A = Key::Flag(11);
constexpr static Key S = Key::Flag(12);
constexpr static Key D = Key::Flag(13);
constexpr static Key F = Key::Flag(14);
constexpr static Key G = Key::Flag(15);
constexpr static Key H = Key::Flag(16);
constexpr static Key J = Key::Flag(17);
constexpr static Key K = Key::Flag(18);
constexpr static Key L = Key::Flag(19);
constexpr static Key Y = Key::Flag(20);
constexpr static Key X = Key::Flag(21);
constexpr static Key C = Key::Flag(22);
constexpr static Key V = Key::Flag(23);
constexpr static Key B = Key::Flag(24);
constexpr static Key N = Key::Flag(25);
constexpr static Key M = Key::Flag(26);
constexpr static Key _1 = Key::Flag(27);
constexpr static Key _2 = Key::Flag(28);
constexpr static Key _3 = Key::Flag(29);
constexpr static Key _4 = Key::Flag(30);
constexpr static Key _5 = Key::Flag(31);
constexpr static Key _6 = Key::Flag(32);
constexpr static Key _7 = Key::Flag(33);
constexpr static Key _8 = Key::Flag(34);
constexpr static Key _9 = Key::Flag(35);
constexpr static Key _0 = Key::Flag(36);
constexpr static Key F1 = Key::Flag(37);
constexpr static Key F2 = Key::Flag(38);
constexpr static Key F3 = Key::Flag(39);
constexpr static Key F4 = Key::Flag(40);
constexpr static Key F5 = Key::Flag(41);
constexpr static Key F6 = Key::Flag(42);
constexpr static Key F7 = Key::Flag(43);
constexpr static Key F8 = Key::Flag(44);
constexpr static Key F9 = Key::Flag(45);
constexpr static Key F10 = Key::Flag(46);
constexpr static Key F11 = Key::Flag(47);
constexpr static Key F12 = Key::Flag(48);
constexpr static Key MouseLeft = Key::Flag(120);
};
using GamepadKey = u32;
enum Gamepad : GamepadKey {
None = 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
LStick = 1 << 23, ///< Left Stick
RStick = 1 << 24, ///< Right Stick
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
};
} // namespace HidInternal
// Pre interface class
class PD_API HidDriver : public DriverInterface {
public:
enum class Event {
Null, ///< Nothing happended
Down, ///< Key Pressed
Held, ///< Key held
Up, ///< Key released
};
HidDriver(std::string_view name = "HidNull");
virtual ~HidDriver();
virtual void Init() {}
virtual void Deinit() {}
virtual const fvec2& MousePos() const { return pMouse[0]; }
virtual const fvec2& MousePosLast() const { return pMouse[1]; }
virtual const fvec2& TouchPos() const { return pMouse[0]; }
virtual const fvec2& TouchPosLast() const { return pMouse[1]; }
virtual void Update();
virtual bool IsEvent(Event e, HidInternal::GamepadKey keys);
virtual bool IsEvent(Event e, HidInternal::Keyboard::Key keys);
virtual const fvec2& GetLeftStick() const { return pLStick[0]; }
virtual const fvec2& GetRightStick() const { return pRStick[0]; }
PDHidBackendFlags GetFlags() const { return pFlags; }
protected:
void SwapTab();
PDHidBackendFlags pFlags = PDHidBackendFlags_None;
fvec2 pMouse[2]; // Current And last pos
fvec2 pLStick[2];
fvec2 pRStick[2];
std::unordered_map<u32, u32> pGamepad;
std::unordered_map<u128, u128> pKeyboard;
std::unordered_map<Event, u32> pGamepadEvents[2];
std::unordered_map<Event, u128> pKeyboardEvents[2];
};
class PD_API Hid {
public:
using Gamepad = HidInternal::Gamepad;
using Keyboard = HidInternal::Keyboard;
using Event = HidDriver::Event;
Hid() = default; Hid() = default;
~Hid() = default; ~Hid() = default;
/** Referenec to Drivers enums */ template <typename T, typename... Args>
using Key = HidDriver::Key; static void UseDriver(Args&&... args) {
using KbKey = HidKb::KbKey; // assert(driver == nullptr && "OS Driver already set");
using Event = HidDriver::Event; driver = std::make_unique<T>(std::forward<Args>(args)...);
static void Init(HidDriver::Ref v = nullptr) {
if (v) {
pHid = v;
} else {
pHid = HidDriver::New();
}
} }
static bool IsEvent(Event e, Key keys) { return pHid->IsEvent(e, keys); } static void Init() { driver->Init(); }
static bool IsEvent(Event e, KbKey key) { return pHid->IsEvent(e, key); } static void Deinit() { driver->Deinit(); }
static bool IsDown(Key keys) { return pHid->IsDown(keys); } static const fvec2& MousePos() { return driver->MousePos(); }
static bool IsUp(Key keys) { return pHid->IsUp(keys); } static const fvec2& MousePosLast() { return driver->MousePosLast(); }
static bool IsHeld(Key keys) { return pHid->IsHeld(keys); } static const fvec2& TouchPos() { return driver->TouchPos(); }
static fvec2 MousePos() { return pHid->MousePos(); } static const fvec2& TouchPosLast() { return driver->TouchPosLast(); }
static fvec2 MousePosLast() { return pHid->MousePosLast(); } static void Update() { driver->Update(); }
static void Clear() { pHid->Clear(); } static bool IsEvent(Event e, HidInternal::GamepadKey keys) {
static void Lock() { pHid->Lock(); } return driver->IsEvent(e, keys);
static void Lock(bool v) { pHid->Lock(v); } }
static void Unlock() { pHid->Unlock(); } static bool IsEvent(Event e, HidInternal::Keyboard::Key keys) {
static bool Locked() { return pHid->Locked(); } return driver->IsEvent(e, keys);
static void Update() { pHid->Update(); } }
static u32 GetFlags() { return pHid->Flags; } static PDHidBackendFlags GetFlags() { return driver->GetFlags(); }
static void GetStrInput(std::string& str) { pHid->GetInputStr(str); } static const fvec2& GetLeftStick() { return driver->GetLeftStick(); }
static const fvec2& GetRightStick() { return driver->GetRightStick(); }
static HidDriver::Ref pHid; static const char* GetDriverName() { return driver->GetName(); }
private:
static std::unique_ptr<HidDriver> driver;
}; };
} // namespace PD } // namespace PD
+16
View File
@@ -0,0 +1,16 @@
#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
+19 -56
View File
@@ -1,71 +1,34 @@
#pragma once #pragma once
/* #include <pd/drivers/interface.hpp>
MIT License
Copyright (c) 2024 - 2025 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>
namespace PD { namespace PD {
using TraceMap = std::map<std::string, TT::Res::Ref>; class PD_API OsDriver : public DriverInterface {
class OsDriver {
public: public:
OsDriver() = default; OsDriver(std::string_view name = "Default") : DriverInterface(name) {}
virtual ~OsDriver() = default; virtual ~OsDriver() {}
PD_SHARED(OsDriver);
virtual u64 GetTime(); virtual u64 GetTime() const;
virtual u64 GetNanoTime(); virtual u64 GetTimeNano() const;
TraceMap& GetTraceMap();
TT::Res::Ref& GetTraceRef(const std::string& id);
bool TraceExist(const std::string& id);
TraceMap pTraces;
}; };
/** Static Os Controller */ class PD_API Os {
class OS {
public: public:
OS() = default; Os() = default;
~OS() = default; ~Os() = default;
static void Init(OsDriver::Ref v = nullptr) { template <typename T, typename... Args>
if (v) { static void UseDriver(Args&&... args) {
pOs = v; // assert(driver == nullptr && "OS Driver already set");
} else { driver = std::make_unique<T>(std::forward<Args>(args)...);
pOs = OsDriver::New();
}
} }
static u64 GetTime() { return pOs->GetTime(); } static u64 GetTime() { return driver->GetTime(); }
static u64 GetNanoTime() { return pOs->GetNanoTime(); } static u64 GetTimeNano() { return driver->GetTimeNano(); }
static TraceMap& GetTraceMap() { return pOs->GetTraceMap(); }
static TT::Res::Ref& GetTraceRef(const std::string& id) {
return pOs->GetTraceRef(id);
}
static bool TraceExist(const std::string& id) { return pOs->TraceExist(id); }
static OsDriver::Ref pOs; static const char* GetDriverName() { return driver->GetName(); }
private:
static std::unique_ptr<OsDriver> driver;
}; };
} // namespace PD } // namespace PD
-26
View File
@@ -1,26 +0,0 @@
#pragma once
/*
MIT License
Copyright (c) 2024 - 2025 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.
*/
#define PD_DEF_EXP(x, y) x y = nullptr
-45
View File
@@ -1,45 +0,0 @@
#pragma once
/*
MIT License
Copyright (c) 2024 - 2025 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
-24962
View File
File diff suppressed because it is too large Load Diff
-7988
View File
File diff suppressed because it is too large Load Diff
-5079
View File
File diff suppressed because it is too large Load Diff
+10 -11
View File
@@ -2,8 +2,7 @@
/* /*
MIT License MIT License
Copyright (c) 2024 - 2026 René Amthor (tobid7)
Copyright (c) 2024 - 2025 tobid7
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
@@ -26,7 +25,7 @@ SOFTWARE.
#include <pd/core/core.hpp> #include <pd/core/core.hpp>
#include <pd/image/image.hpp> #include <pd/image/image.hpp>
#include <pd/image/pd_p_api.hpp> #include <pd/pd_p_api.hpp>
namespace PD { namespace PD {
/** /**
@@ -41,19 +40,19 @@ namespace ImgConvert {
* @param w width of the image * @param w width of the image
* @param h height of the image * @param h height of the image
*/ */
PD_IMAGE_API PD_API
void RGB24toRGBA32(std::vector<PD::u8> &out, const std::vector<u8> &in, void RGB24toRGBA32(std::vector<PD::u8>& out, const std::vector<u8>& in,
const int &w, const int &h); const int& w, const int& h);
PD_IMAGE_API PD_API
void RGB32toRGBA24(std::vector<u8> &out, const std::vector<u8> &in, void RGB32toRGBA24(std::vector<u8>& out, const std::vector<u8>& in,
const int &w, const int &h); const int& w, const int& h);
/** /**
* Reverse 32 (RGBA -> ABGR || ABGR -> RGBA) * Reverse 32 (RGBA -> ABGR || ABGR -> RGBA)
* @param buf Buffer to convert * @param buf Buffer to convert
* @param w width * @param w width
* @param h height * @param h height
*/ */
PD_IMAGE_API void Reverse32(std::vector<u8> &buf, const int &w, const int &h); PD_API void Reverse32(std::vector<u8>& buf, const int& w, const int& h);
PD_IMAGE_API void ReverseBuf(std::vector<u8> &buf, size_t bpp, int w, int h); PD_API void ReverseBuf(std::vector<u8>& buf, size_t bpp, int w, int h);
} // namespace ImgConvert } // namespace ImgConvert
} // namespace PD } // namespace PD
Executable → Regular
+25 -64
View File
@@ -1,36 +1,11 @@
#pragma once #pragma once
/*
MIT License
Copyright (c) 2024 - 2025 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/core/core.hpp>
#include <pd/image/pd_p_api.hpp>
namespace PD { namespace PD {
class PD_IMAGE_API Image { class PD_API Image {
public: public:
enum Format { enum class Format {
RGBA, // bpp == 4 RGBA, // bpp == 4
RGB, // bpp == 3 RGB, // bpp == 3
RGB565, // bpp == 2 (not supported in laoding) RGB565, // bpp == 2 (not supported in laoding)
@@ -38,52 +13,38 @@ class PD_IMAGE_API Image {
ABGR, // bpp == 4 ABGR, // bpp == 4
BGRA, // bpp == 4 BGRA, // bpp == 4
}; };
Image() = default; Image();
Image(const std::string& path) { this->Load(path); } Image(const std::string& path);
Image(const std::vector<u8>& buf) { this->Load(buf); } Image(const std::vector<u8>& buf);
Image(const std::vector<u8>& buf, int w, int h, int bpp = 4) { Image(const std::vector<u8>& pixels, int w, int h, int bpp = 4);
this->Copy(buf, w, h, bpp); ~Image();
}
~Image() = default;
PD_SHARED(Image)
void Load(const std::string& path); void Load(const std::string& path);
void Load(const std::vector<u8>& buf); void Load(const std::vector<u8>& buf);
void Copy(const std::vector<u8>& buf, int w, int h, int bpp = 4); void Load(const u8* buf, size_t size);
void Copy(const std::vector<u8>& pixels, int w, int h, int bpp = 4);
std::vector<PD::u8>& GetBuffer() { return pBuffer; } const int& Width() const { return pSize.x; }
std::vector<PD::u8> GetBuffer() const { return pBuffer; } const int& Height() const { return pSize.y; }
const ivec2& Size() const { return pSize; }
int Width() const { return pWidth; } const std::vector<u8>& data() const { return pData; }
int Height() const { return pHeight; } std::vector<u8>& data() { return pData; }
ivec2 Size() const { return ivec2(pWidth, pHeight); } operator const std::vector<u8>&() const { return pData; }
Format Fmt() const { return pFmt; } operator std::vector<u8>&() { return pData; }
operator const ivec2&() const { return pSize; }
operator const Format&() const { return pFormat; }
void FlipVertical(); void Flip(bool horizontal = true, bool vertical = true);
void FlipHorizontal();
u8& operator[](int idx) { return pBuffer[idx]; } static int Format2Bpp(Format fmt);
u8 operator[](int idx) const { return pBuffer[idx]; } static Format GuessFmtFromBpp(int bpp);
// Probably these make th eabove ones useless void Convert(Format dst);
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: private:
/** Leftover variable used for stbi_load */ std::vector<u8> pData;
int fmt = 0; ivec2 pSize;
Format pFormat = Format::RGBA;
}; };
} // namespace PD } // namespace PD
-72
View File
@@ -1,72 +0,0 @@
#pragma once
/*
MIT License
Copyright (c) 2024 - 2025 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/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_IMAGE_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_IMAGE_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_IMAGE_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
-51
View File
@@ -1,51 +0,0 @@
#pragma once
/*
MIT License
Copyright (c) 2024 - 2025 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.
*/
/** Generated with ppam */
#ifdef _WIN32 // Windows (MSVC Tested)
#ifdef PD_IMAGE_BUILD_SHARED
#define PD_IMAGE_API __declspec(dllexport)
#else
#define PD_IMAGE_API __declspec(dllimport)
#endif
#elif defined(__APPLE__) // macOS (untested yet)
#ifdef PD_IMAGE_BUILD_SHARED
#define PD_IMAGE_API __attribute__((visibility("default")))
#else
#define PD_IMAGE_API
#endif
#elif defined(__linux__) // Linux (untested yet)
#ifdef PD_IMAGE_BUILD_SHARED
#define PD_IMAGE_API __attribute__((visibility("default")))
#else
#define PD_IMAGE_API
#endif
#elif defined(__3DS__) // 3ds Specific
// Only Static supported
#define PD_IMAGE_API
#else
#define PD_IMAGE_API
#endif
+25
View File
@@ -0,0 +1,25 @@
#pragma once
#include <pd/core/core.hpp>
#include <pd/lithium/texture.hpp>
namespace PD {
namespace Li {
enum class AtlasState {
Invalid,
ReqCreate,
ReqUpdate,
ReqDestroy,
};
class Atlas {
public:
Atlas() {}
~Atlas() {}
private:
AtlasState pState = AtlasState::Invalid;
TextureID pID;
};
} // namespace Li
} // namespace PD
Executable → Regular
+21 -88
View File
@@ -1,103 +1,36 @@
#pragma once #pragma once
/* #include <cstddef>
MIT License #include <pd/core/pool.hpp>
Copyright (c) 2024 - 2025 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/pd_p_api.hpp>
#include <pd/lithium/texture.hpp>
#include <pd/lithium/vertex.hpp> #include <pd/lithium/vertex.hpp>
namespace PD { namespace PD {
namespace Li { namespace Li {
class Command { class Command {
public: public:
Command() = default; Command() { Reset(); }
~Command() = default; ~Command() {}
PD_RAW(Command); void Reserve(size_t vtx, size_t idx);
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;
}
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;
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_LITHIUM_API CmdPool {
public:
CmdPool() {}
~CmdPool() {}
Command::Ref NewCmd();
void Init(size_t initial_size);
void Deinit();
void Resize(size_t nulen);
void Reset(); 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: Command& Add(const Vertex& vtx);
static bool pTheOrder(const Command::Ref& a, const Command::Ref& b); Command& Add(u16 idx);
friend class DrawList; Command& Add(u16 a, u16 b, u16 c);
Command::Ref* begin() { return &pPool[0]; }
Command::Ref* end() { return &pPool[pPoolIdx - 1]; }
std::vector<Command::Ref> pPool;
u32 pPoolIdx = 0;
int Layer = 0; int Layer = 0;
ptr Tex = 0;
bool SDF = false;
size_t FirstVertex = 0;
size_t FirstIndex = 0;
size_t VertexCount = 0;
size_t IndexCount = 0;
// Todo: implement
size_t VertexCountMax = 0;
size_t IndexCountMax = 0;
// ClipRect
PD::fvec4 ClipRect;
bool ClipRectUsed = false;
}; };
} // namespace Li } // namespace Li
} // namespace PD } // namespace PD
Executable → Regular
+116 -193
View File
@@ -1,224 +1,147 @@
#pragma once #pragma once
/*
MIT License
Copyright (c) 2024 - 2025 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/command.hpp>
#include <pd/lithium/font.hpp> #include <pd/lithium/font.hpp>
#include <pd/lithium/pd_p_api.hpp> #include <pd/lithium/texture.hpp>
/** Path Rect Flags */
using LiPathRectFlags = PD::u32; using LiPathRectFlags = PD::u32;
/** Setup for everything (oder so) */
enum LiPathRectFlags_ : PD::u32 { enum LiPathRectFlags_ : PD::u32 {
LiPathRectFlags_None = 0, LiPathRectFlags_None = 0,
LiPathRectFlags_KeepTopLeft = PD_BIT(0), LiPathRectFlags_KeepTopLeft = 1 << 0,
LiPathRectFlags_KeepTopRight = PD_BIT(1), LiPathRectFlags_KeepTopRight = 1 << 1,
LiPathRectFlags_KeepBotRight = PD_BIT(2), LiPathRectFlags_KeepBotRight = 1 << 2,
LiPathRectFlags_KeepBotLeft = PD_BIT(3), LiPathRectFlags_KeepBotLeft = 1 << 3,
LiPathRectFlags_KeepTop = PD_BIT(0) | PD_BIT(1), LiPathRectFlags_KeepTop =
LiPathRectFlags_KeepBot = PD_BIT(2) | PD_BIT(3), LiPathRectFlags_KeepTopLeft | LiPathRectFlags_KeepTopRight,
LiPathRectFlags_KeepLeft = PD_BIT(0) | PD_BIT(3), LiPathRectFlags_KeepBot =
LiPathRectFlags_KeepRight = PD_BIT(1) | PD_BIT(2), LiPathRectFlags_KeepBotLeft | LiPathRectFlags_KeepBotRight,
LiPathRectFlags_KeepLeft =
LiPathRectFlags_KeepTopLeft | LiPathRectFlags_KeepBotLeft,
LiPathRectFlags_KeepRight =
LiPathRectFlags_KeepTopRight | LiPathRectFlags_KeepBotRight,
};
using LiDrawFlags = PD::u32;
enum LiDrawFlags_ : PD::u32 {
LiDrawFlags_None = 0,
LiDrawFlags_Close = 1 << 0,
LiDrawFlags_AA = 1 << 1,
}; };
namespace PD { namespace PD {
namespace Li { namespace Li {
class PD_LITHIUM_API DrawList { class PD_API Drawlist {
public: public:
DrawList(int initial_size = 64); Drawlist();
~DrawList(); ~Drawlist();
/** Require Copy and Move Constructors */ /** Baisc */
DrawList(const DrawList&) = delete; void Merge(Drawlist& other);
DrawList& operator=(const DrawList&) = delete; void Copy(Drawlist& other);
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(); void Optimize();
Command::Ref GetNewCmd();
void Clear(); void Clear();
void Layer(int l) { this->pPool.Layer = l; } void SetLayer(int layer) { pCurrentLayer = layer; }
int Layer() { return this->pPool.Layer; } void LayerUp() { pCurrentLayer++; }
void LayerUp() { this->pPool.Layer++; } void LayerDown() { pCurrentLayer--; }
void LayerDown() { this->pPool.Layer--; } int GetLayer() const { return pCurrentLayer; }
void SetFont(Font::Ref font) { pCurrentFont = font; } size_t GetNumVertices() const {
void SetFontScale(float scale) { pFontScale = scale; } size_t count = 0;
for (const auto& cmd : pCommands) count += cmd.VertexCount;
void DrawSolid(); return count;
void DrawTexture(Texture::Ref tex) { CurrentTex = 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 size_t GetNumIndices() const {
* @note PathStroke and PathFill will automatically clear size_t count = 0;
*/ for (const auto& cmd : pCommands) count += cmd.IndexCount;
void PathClear() { pPath.clear(); } return count;
/**
* 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();
} }
/** Command Allocation */
Command& NewCommand();
bool HasCommands() { return pCommands.size() > 0; }
Command& GetLastCommand() { return pCommands[pCommands.size() - 1]; }
/** Path API */
void PathAdd(const fvec2& point) { pPath.Push(point); }
void PathAdd(float x, float y) { pPath.Push(fvec2(x, y)); }
void PathClear() { pPath.Reset(); }
/** /**
* Fill a Path with a Color * @brief Reserve memory for the next PathAdd uses
* @note **IMPORTANT: ** Paths need to be setup clockwise * @note As path will autoexpant and keep the size this func is
* to be rendered correctly * only for special use cases
* @param clr Fill Color
*/ */
void PathFill(u32 clr) { void PathReserve(size_t num) { pPath.ExpandIf(num); }
DrawConvexPolyFilled(pPath, clr); void PathStroke(const PD::Color& color, int t = 1,
pPath.clear(); LiDrawFlags flags = LiDrawFlags_None);
} void PathFill(const PD::Color& color);
void PathArcToN(const fvec2& c, float radius, float a_min, float a_max, void PathFillGradient(const PD::Color& a, const PD::Color& b,
int segments); float rad = 0.f);
void PathArcToN(const fvec2& c, float r, float amin, float amax, int s);
void PathFastArcToN(const fvec2& c, float r, float amin, float amax, int s); 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) void PathRect(const fvec2& tl, const fvec2& br, float r = 0.f);
/// @param a Top Left Position void PathRectEx(const fvec2& tl, const fvec2& br, float r = 0.f,
/// @param b Bottom Right Position LiPathRectFlags flags = LiPathRectFlags_None);
/// @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); } /** Texture Handling */
void BindTexture(const Texture& tex);
void UnbindTexture() { pCurrentTexture = Texture(); }
/** Font Handling */
void SetFont(Font* font) { pFont = font; }
void SetFontscale(float fontscale = 1.f) { pFontScale = fontscale; }
const float& GetFontScale() const { return pFontScale; }
/** Data geters */
const Pool<Command>& Data() const { return pCommands; }
operator const Pool<Command>&() const { return pCommands; }
/** Drawing functions */
void DrawRect(const fvec2& pos, const fvec2& size, const PD::Color& color,
int t = 1);
void DrawRectFilled(const fvec2& pos, const fvec2& size,
const PD::Color& color);
void DrawTriangle(const fvec2& a, const fvec2& b, const fvec2& c,
const PD::Color& color, int t = 1);
void DrawTriangleFilled(const fvec2& a, const fvec2& b, const fvec2& c,
const PD::Color& color);
void DrawCircle(const fvec2& center, float rad, const PD::Color& color,
int num_segments, int t = 1);
void DrawCircleFilled(const fvec2& center, float rad, const PD::Color& color,
int num_segments);
void DrawText(const fvec2& p, const char* text, const PD::Color& color);
void DrawTextEx(const fvec2& p, const char* text, const PD::Color& color,
LiTextFlags flags, const fvec2& box = fvec2(0.f));
void DrawLine(const fvec2& a, const fvec2& b, const PD::Color& color,
int thickness = 1);
void DrawPolyLine(const Pool<fvec2>& points, const PD::Color& color,
LiDrawFlags flags = LiDrawFlags_None, int t = 1);
void DrawConvexPolyFilled(const Pool<fvec2>& points, const PD::Color& color);
void DrawConvexPolyFilled(const Pool<fvec2>& points, const PD::Color& a,
const PD::Color b, float rad = 0.f);
void PrimQuad(Command& cmd, const Rect& quad, const Rect& uv,
const PD::Color& color);
void PrimTriangle(Command& cmd, const fvec2& a, const fvec2& b,
const fvec2& c, const PD::Color& color);
void PushClipRect(const fvec4& r) { pClipRects.push_back(r); }
void PopClipRect() { void PopClipRect() {
if (pClipRects.empty()) { if (!pClipRects.empty()) pClipRects.pop_back();
return;
}
pClipRects.pop();
} }
const CmdPool& Data() const { return pPool; } bool HasClipRect() { return !pClipRects.empty(); }
/** One linear Clip rect Setup */
void pClipCmd(Command* cmd);
/** Data Section */ private:
Texture pCurrentTexture;
std::stack<fvec4> pClipRects; int pCurrentLayer = 0;
float pFontScale = 0.7f; Pool<Command> pCommands;
Font::Ref pCurrentFont; Pool<fvec2> pPath;
Texture::Ref CurrentTex; std::vector<fvec4> pClipRects;
CmdPool pPool; Font* pFont = nullptr;
std::vector<fvec2> pPath; float pFontScale = 1.f;
u32 pNumIndices = 0;
u32 pNumVertices = 0;
}; };
} // namespace Li } // namespace Li
} // namespace PD } // namespace PD
Executable → Regular
+40 -55
View File
@@ -1,37 +1,9 @@
#pragma once #pragma once
/*
MIT License
Copyright (c) 2024 - 2025 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/pd_p_api.hpp>
#include <pd/lithium/rect.hpp>
#include <pd/lithium/texture.hpp> #include <pd/lithium/texture.hpp>
using LiTextFlags = PD::u32; using LiTextFlags = PD::u32;
enum LiTextFlags_ { enum LiTextFlags_ : PD::u32 {
LiTextFlags_None = 0, ///< Do nothing LiTextFlags_None = 0, ///< Do nothing
LiTextFlags_AlignRight = 1 << 0, ///< Align Right of position LiTextFlags_AlignRight = 1 << 0, ///< Align Right of position
LiTextFlags_AlignMid = 1 << 1, ///< Align in the middle of pos and box LiTextFlags_AlignMid = 1 << 1, ///< Align in the middle of pos and box
@@ -42,44 +14,48 @@ enum LiTextFlags_ {
LiTextFlags_NoOOS = 1 << 6, ///< No Out of Screen Rendering LiTextFlags_NoOOS = 1 << 6, ///< No Out of Screen Rendering
}; };
using LiFontFlags = PD::u32;
enum LiFontFlags_ : PD::u32 {
LiFontFlags_None = 0, ///< Not special (bitmap font)
LiFontFlags_SDF = 1 << 0, ///< SDF Texture (and mode)
LiFontFlags_Monospace = 1 << 1, ///< Monospace Rendering
};
namespace PD { namespace PD {
namespace Li { namespace Li {
class PD_LITHIUM_API Font { class Drawlist;
class PD_API Font {
public: public:
/** Codepoint Data holder */
struct Codepoint { struct Codepoint {
u32 pCodepoint = 0; u32 pCodepoint = 0;
fvec4 SimpleUV; fvec4 SimpleUV;
Texture::Ref Tex; size_t Tex = 0;
fvec2 Size; fvec2 Size;
float Offset = 0.f; fvec2 Offset;
// float Offset = 0.f;
float AdvanceX = 0.f;
bool pInvalid = false; bool pInvalid = false;
}; };
Font() {}
~Font() {}
/** Constructore doesnt need Backand anymore */ bool IsMonospace() { return pFlags & LiFontFlags_Monospace; }
Font() = default; bool IsSDF() { return pFlags & LiFontFlags_SDF; }
~Font() = default;
PD_SHARED(Font);
/** /**
* Load a TTF File * Load a TTF File
* @param path Path to the TTF file * @param path Path to the TTF file
* @param px_height Pixelheight of the codepoints (limit by 64) * @param px_height Pixelheight of the codepoints (limit by 64)
*/ */
void LoadTTF(const std::string& path, int px_height = 32); void LoadTTF(const std::string& path, int px_height = 32,
LiFontFlags flags = LiFontFlags_None);
/** /**
* Load a TTF File from Memory * Load a TTF File from Memory
* @param data File data * @param data File data
* @param px_height Pixelheight of the codepoints (limit by 64) * @param px_height Pixelheight of the codepoints (limit by 64)
*/ */
void LoadTTF(const std::vector<u8>& data, int px_height = 32); void LoadTTF(const std::vector<u8>& data, int px_height = 32,
/** LiFontFlags flags = LiFontFlags_None);
* 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 * Getter for Codepoint reference
* @return codepoint dataholder reference * @return codepoint dataholder reference
@@ -89,13 +65,17 @@ class PD_LITHIUM_API Font {
/** /**
* Get Text Bounding Box * Get Text Bounding Box
*/ */
fvec2 GetTextBounds(const std::string& text, float scale); fvec2 GetTextBounds(const char* text, float scale);
/** /**
* Extended Draw Text Function that vreates a Command List * Extended Draw Text Function that vreates a Command List
*/ */
void CmdTextEx(CmdPool& cmds, const fvec2& pos, u32 color, float scale, void CmdTextEx(Drawlist& dl, const fvec2& pos, u32 color, float scale,
const std::string& text, LiTextFlags flags = 0, const char* text, LiTextFlags flags = 0, const fvec2& box = 0);
const fvec2& box = 0);
/**
* Cleanup Font and unload Textures
*/
void Delete();
/** /**
* Garbage collection for TextMapSystem * Garbage collection for TextMapSystem
@@ -105,22 +85,23 @@ class PD_LITHIUM_API Font {
* Utility function to create a font atlas * Utility function to create a font atlas
* During TTF loading (Internal and should not be called) * During TTF loading (Internal and should not be called)
*/ */
void pMakeAtlas(bool final, std::vector<u8>& font_tex, int texszs, void BakeAndPush(bool final, std::vector<u8>& font_tex, int texszs);
PD::Li::Texture::Ref tex);
std::string pWrapText(const std::string& txt, float scale, std::string pWrapText(const std::string& txt, float scale,
const PD::fvec2& max, PD::fvec2& dim); const PD::fvec2& max, PD::fvec2& dim);
std::string pShortText(const std::string& txt, float scale, std::string pShortText(const std::string& txt, float scale,
const PD::fvec2& max, PD::fvec2& dim); const PD::fvec2& max, PD::fvec2& dim);
/** Data Section */ /** Data Section */
int PixelHeight; int PixelHeight = 0;
int DefaultPixelHeight = 24; int DefaultPixelHeight = 32;
std::vector<Texture::Ref> Textures; std::vector<TextureID> Textures;
/** /**
* 32Bit Codepoint Dataholder reference map * 32Bit Codepoint Dataholder reference map
* **Now using unordered map** * **Now using unordered map**
*/ */
std::unordered_map<u32, Codepoint> CodeMap; std::unordered_map<u32, Codepoint> CodeMap;
/** need some performance tweaks */
std::array<Codepoint, 128> pAsciiCache;
/** TMS */ /** TMS */
struct TMELEM { struct TMELEM {
PD::u32 ID; PD::u32 ID;
@@ -129,6 +110,10 @@ class PD_LITHIUM_API Font {
u64 TimeStamp; u64 TimeStamp;
}; };
std::unordered_map<u32, TMELEM> pTMS; std::unordered_map<u32, TMELEM> pTMS;
private:
size_t pCurrentTex = 0;
LiFontFlags pFlags = 0;
}; };
} // namespace Li } // namespace Li
} // namespace PD } // namespace PD
-41
View File
@@ -1,41 +0,0 @@
#pragma once
/*
MIT License
Copyright (c) 2024 - 2025 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.
*/
#ifdef PD_LI_INCLUDE_FONTS
#include <pd/core/common.hpp>
/** Generated with pdfm */
namespace PD {
struct FontFileData {
std::string Name;
u32 StartOff;
u32 Size;
};
extern FontFileData pFontData[];
extern size_t pNumFonts;
extern PD::u8 pFontsDataRaw[];
} // namespace PD
#endif
+122
View File
@@ -0,0 +1,122 @@
#pragma once
#include <pd/lithium/atlas.hpp>
#include <pd/lithium/command.hpp>
#include <pd/lithium/rect.hpp>
#include <pd/lithium/texture.hpp>
#include <pd/lithium/vertex.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);
}
};
template <>
struct std::formatter<PD::Li::Rect> : std::formatter<std::string> {
constexpr auto parse(std::format_parse_context& ctx) { return ctx.begin(); }
template <typename FormatContext>
auto format(const PD::Li::Rect& value, FormatContext& ctx) const {
// {l, t, r, t, l, b, r, b}
return std::format_to(ctx.out(), "{}, {}, {}, {}, {}, {}, {}, {}",
value.Top.x, value.Top.y, value.Top.z, value.Top.w,
value.Bot.x, value.Bot.y, value.Bot.z, value.Bot.w);
}
};
template <>
struct std::formatter<PD::Li::Texture> : std::formatter<std::string> {
constexpr auto parse(std::format_parse_context& ctx) { return ctx.begin(); }
template <typename FormatContext>
auto format(const PD::Li::Texture& value, FormatContext& ctx) const {
// {ID, [w, h], [l, t, r, t, l, b, r, b]}
return std::format_to(ctx.out(), "{}, [{}], [{}]", value.GetID(),
value.GetSize(), value.GetUV());
}
};
template <>
struct std::formatter<PD::Li::Vertex> : std::formatter<std::string> {
constexpr auto parse(std::format_parse_context& ctx) { return ctx.begin(); }
template <typename FormatContext>
auto format(const PD::Li::Vertex& value, FormatContext& ctx) const {
// { [x, y], [u, v], colorhex }
return std::format_to(ctx.out(), "[{}], [{}], #{:08X}", value.pos, value.uv,
value.color);
}
};
template <>
struct std::formatter<PD::Li::AtlasState> : std::formatter<std::string> {
constexpr auto parse(std::format_parse_context& ctx) { return ctx.begin(); }
template <typename FormatContext>
auto format(const PD::Li::AtlasState& value, FormatContext& ctx) const {
std::string_view ret = "Unknown";
switch (value) {
case PD::Li::AtlasState::Invalid:
ret = "Invalid";
break;
case PD::Li::AtlasState::ReqCreate:
ret = "RequestCreate";
break;
case PD::Li::AtlasState::ReqUpdate:
ret = "RequestUpdate";
break;
case PD::Li::AtlasState::ReqDestroy:
ret = "RequestDestroy";
break;
}
return std::format_to(ctx.out(), "{}", ret);
}
};
template <>
struct std::formatter<PD::Li::Command> : std::formatter<std::string> {
constexpr auto parse(std::format_parse_context& ctx) { return ctx.begin(); }
template <typename FormatContext>
auto format(const PD::Li::Command& value, FormatContext& ctx) const {
return std::format_to(ctx.out(), "[0x{:X}, 0x{:X}] [{}, {}] 0x{:X}",
(PD::ptr)value.FirstVertex, (PD::ptr)value.FirstIndex,
value.VertexCount, value.IndexCount, value.Tex);
}
};
Executable → Regular
+3 -27
View File
@@ -1,32 +1,8 @@
#pragma once #pragma once
/*
MIT License
Copyright (c) 2024 - 2025 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/command.hpp>
#include <pd/lithium/drawlist.hpp> #include <pd/lithium/drawlist.hpp>
#include <pd/lithium/font.hpp> #include <pd/lithium/font.hpp>
#include <pd/lithium/rect.hpp> #include <pd/lithium/math.hpp>
#include <pd/lithium/renderer.hpp> #include <pd/lithium/pools.hpp>
#include <pd/lithium/texture.hpp> #include <pd/lithium/vertex.hpp>
+19
View File
@@ -0,0 +1,19 @@
#pragma once
#include <pd/core/core.hpp>
#include <pd/lithium/rect.hpp>
namespace PD {
namespace Li {
namespace Math {
PD_API bool InBounds(const fvec2& pos, const fvec2& size, const fvec4& rect);
PD_API bool InBounds(const fvec2& pos, const fvec4& rect);
PD_API bool InBounds(const fvec2& a, const fvec2& b, const fvec2& c,
const fvec4& rect);
PD_API bool InSpace(const PD::fvec2& pos, const Rect& rect);
PD_API void RotateCorner(fvec2& pos, float sinus, float cosinus);
PD_API Rect PrimRect(const fvec2& pos, const fvec2& size, float angle = 0.f);
PD_API Rect PrimLine(const fvec2& a, const fvec2& b, int t = 1);
} // namespace Math
} // namespace Li
} // namespace PD
-51
View File
@@ -1,51 +0,0 @@
#pragma once
/*
MIT License
Copyright (c) 2024 - 2025 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.
*/
/** Generated with ppam */
#ifdef _WIN32 // Windows (MSVC Tested)
#ifdef PD_LITHIUM_BUILD_SHARED
#define PD_LITHIUM_API __declspec(dllexport)
#else
#define PD_LITHIUM_API __declspec(dllimport)
#endif
#elif defined(__APPLE__) // macOS (untested yet)
#ifdef PD_LITHIUM_BUILD_SHARED
#define PD_LITHIUM_API __attribute__((visibility("default")))
#else
#define PD_LITHIUM_API
#endif
#elif defined(__linux__) // Linux (untested yet)
#ifdef PD_LITHIUM_BUILD_SHARED
#define PD_LITHIUM_API __attribute__((visibility("default")))
#else
#define PD_LITHIUM_API
#endif
#elif defined(__3DS__) // 3ds Specific
// Only Static supported
#define PD_LITHIUM_API
#else
#define PD_LITHIUM_API
#endif
+12
View File
@@ -0,0 +1,12 @@
#pragma once
#include <pd/core/core.hpp>
#include <pd/lithium/vertex.hpp>
namespace PD {
namespace Li {
PD_API PD::Pool<Vertex>& GetVertexPool();
PD_API PD::Pool<u16>& GetIndexPool();
PD_API void ResetPools();
} // namespace Li
} // namespace PD
Executable → Regular
+18 -15
View File
@@ -2,7 +2,7 @@
/* /*
MIT License MIT License
Copyright (c) 2024 - 2025 René Amthor (tobid7) Copyright (c) 2024 - 2026 René Amthor (tobid7)
Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal of this software and associated documentation files (the "Software"), to deal
@@ -29,14 +29,14 @@ namespace PD {
namespace Li { namespace Li {
class Rect { class Rect {
public: public:
Rect() : Top(0), Bot(0) {} constexpr Rect() : Top(0), Bot(0) {}
~Rect() = default; ~Rect() = default;
/** /**
* Constructor that initializes the rectangle using top and bottom positions. * Constructor that initializes the rectangle using top and bottom positions.
* @param t Top left and right corner positions. * @param t Top left and right corner positions.
* @param b Bottom left and right corner positions. * @param b Bottom left and right corner positions.
*/ */
Rect(const fvec4& t, const fvec4& b) { constexpr Rect(const fvec4& t, const fvec4& b) {
Top = t; Top = t;
Bot = b; Bot = b;
} }
@@ -48,7 +48,8 @@ class Rect {
* @param bl Bottom left corner position. * @param bl Bottom left corner position.
* @param br Bottom right corner position. * @param br Bottom right corner position.
*/ */
Rect(const fvec2& tl, const fvec2& tr, const fvec2& bl, const fvec2& br) { constexpr Rect(const fvec2& tl, const fvec2& tr, const fvec2& bl,
const fvec2& br) {
Top = fvec4(tl, tr); Top = fvec4(tl, tr);
Bot = fvec4(bl, br); Bot = fvec4(bl, br);
} }
@@ -61,7 +62,7 @@ class Rect {
* *
* @param uv Vec4 UV map. * @param uv Vec4 UV map.
*/ */
Rect(const fvec4& uv) { constexpr Rect(const fvec4& uv) {
Top = vec4(uv.x, uv.y, uv.z, uv.y); Top = vec4(uv.x, uv.y, uv.z, uv.y);
Bot = vec4(uv.x, uv.w, uv.z, uv.w); Bot = vec4(uv.x, uv.w, uv.z, uv.w);
} }
@@ -70,29 +71,29 @@ class Rect {
* Get the top-left corner position. * Get the top-left corner position.
* @return Top-left position as vec2. * @return Top-left position as vec2.
*/ */
fvec2 TopLeft() const { return fvec2(Top.x, Top.y); } constexpr fvec2 TopLeft() const { return fvec2(Top.x, Top.y); }
/** /**
* Get the top-right corner position. * Get the top-right corner position.
* @return Top-right position as vec2. * @return Top-right position as vec2.
*/ */
fvec2 TopRight() const { return fvec2(Top.z, Top.w); } constexpr fvec2 TopRight() const { return fvec2(Top.z, Top.w); }
/** /**
* Get the bottom-left corner position. * Get the bottom-left corner position.
* @return Bottom-left position as vec2. * @return Bottom-left position as vec2.
*/ */
fvec2 BotLeft() const { return fvec2(Bot.x, Bot.y); } constexpr fvec2 BotLeft() const { return fvec2(Bot.x, Bot.y); }
/** /**
* Get the bottom-right corner position. * Get the bottom-right corner position.
* @return Bottom-right position as vec2. * @return Bottom-right position as vec2.
*/ */
fvec2 BotRight() const { return fvec2(Bot.z, Bot.w); } constexpr fvec2 BotRight() const { return fvec2(Bot.z, Bot.w); }
/** /**
* Set the top-left corner position. * Set the top-left corner position.
* @param v New top-left position. * @param v New top-left position.
* @return Reference to the updated Rect. * @return Reference to the updated Rect.
*/ */
Rect& TopLeft(const fvec2& v) { constexpr Rect& TopLeft(const fvec2& v) {
Top.x = v.x; Top.x = v.x;
Top.y = v.y; Top.y = v.y;
return *this; return *this;
@@ -103,7 +104,7 @@ class Rect {
* @param v New top-right position. * @param v New top-right position.
* @return Reference to the updated Rect. * @return Reference to the updated Rect.
*/ */
Rect& TopRight(const fvec2& v) { constexpr Rect& TopRight(const fvec2& v) {
Top.z = v.x; Top.z = v.x;
Top.w = v.y; Top.w = v.y;
return *this; return *this;
@@ -114,7 +115,7 @@ class Rect {
* @param v New bottom-left position. * @param v New bottom-left position.
* @return Reference to the updated Rect. * @return Reference to the updated Rect.
*/ */
Rect& BotLeft(const fvec2& v) { constexpr Rect& BotLeft(const fvec2& v) {
Bot.x = v.x; Bot.x = v.x;
Bot.y = v.y; Bot.y = v.y;
return *this; return *this;
@@ -125,15 +126,17 @@ class Rect {
* @param v New bottom-right position. * @param v New bottom-right position.
* @return Reference to the updated Rect. * @return Reference to the updated Rect.
*/ */
Rect& BotRight(const fvec2& v) { constexpr Rect& BotRight(const fvec2& v) {
Bot.z = v.x; Bot.z = v.x;
Bot.w = v.y; Bot.w = v.y;
return *this; return *this;
} }
bool operator==(Rect& r) { return Top == r.Top && Bot == r.Bot; } constexpr bool operator==(const Rect& r) const {
return Top == r.Top && Bot == r.Bot;
}
void SwapVec2XY() { constexpr void SwapVec2XY() {
Top.SwapXY(); Top.SwapXY();
Top.SwapZW(); Top.SwapZW();
Bot.SwapXY(); Bot.SwapXY();

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