diff --git a/backends/source/gfx_citro3d.cpp b/backends/source/gfx_citro3d.cpp index 0d16260..10de327 100644 --- a/backends/source/gfx_citro3d.cpp +++ b/backends/source/gfx_citro3d.cpp @@ -174,6 +174,13 @@ Li::Texture GfxCitro3D::LoadTexture(const std::vector& pixels, int w, int h, TextureFormat type, TextureFilter filter) { if (!impl || w > 1024 || h > 1024) return Li::Texture(); + if (pixels.size() != + static_cast(w * h * Li::TextureFormat2Bpp(type))) { + PDERR("Failed to load Texture due to Size mismatch: {} != {}", + pixels.size(), + static_cast(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); diff --git a/backends/source/gfx_directx9.cpp b/backends/source/gfx_directx9.cpp index 260ea29..2e0144a 100644 --- a/backends/source/gfx_directx9.cpp +++ b/backends/source/gfx_directx9.cpp @@ -209,6 +209,13 @@ Li::Texture GfxDirectX9::LoadTexture(const std::vector& pixels, int w, int h, TextureFormat type, TextureFilter filter) { if (!impl || !impl->Device) return Li::Texture(); + if (pixels.size() != + static_cast(w * h * Li::TextureFormat2Bpp(type))) { + PDERR("Failed to load Texture due to Size mismatch: {} != {}", + pixels.size(), + static_cast(w * h * Li::TextureFormat2Bpp(type))); + return Li::Texture(); + } IDirect3DTexture9* tex = nullptr; D3DFORMAT fmt = D3DFMT_A8R8G8B8; if (type == TextureFormat::RGB24) diff --git a/backends/source/gfx_opengl2.cpp b/backends/source/gfx_opengl2.cpp index 630d5b1..9b60f6d 100644 --- a/backends/source/gfx_opengl2.cpp +++ b/backends/source/gfx_opengl2.cpp @@ -129,6 +129,13 @@ void GfxOpenGL2::SysReset() { Li::Texture GfxOpenGL2::LoadTexture(const std::vector& pixels, int w, int h, TextureFormat type, TextureFilter filter) { + if (pixels.size() != + static_cast(w * h * Li::TextureFormat2Bpp(type))) { + PDERR("Failed to load Texture due to Size mismatch: {} != {}", + pixels.size(), + static_cast(w * h * Li::TextureFormat2Bpp(type))); + return Li::Texture(); + } GLuint texID; glGenTextures(1, &texID); glBindTexture(GL_TEXTURE_2D, texID); diff --git a/backends/source/gfx_opengl3.cpp b/backends/source/gfx_opengl3.cpp index 48e1b96..1ede14e 100644 --- a/backends/source/gfx_opengl3.cpp +++ b/backends/source/gfx_opengl3.cpp @@ -99,6 +99,13 @@ void GfxOpenGL3::SysReset() { Li::Texture GfxOpenGL3::LoadTexture(const std::vector& pixels, int w, int h, TextureFormat type, TextureFilter filter) { + if (pixels.size() != + static_cast(w * h * Li::TextureFormat2Bpp(type))) { + PDERR("Failed to load Texture due to Size mismatch: {} != {}", + pixels.size(), + static_cast(w * h * Li::TextureFormat2Bpp(type))); + return Li::Texture(); + } GLuint texID; glGenTextures(1, &texID); glBindTexture(GL_TEXTURE_2D, texID); diff --git a/include/pd/lithium/texture.hpp b/include/pd/lithium/texture.hpp index ebb0499..9f25d3e 100644 --- a/include/pd/lithium/texture.hpp +++ b/include/pd/lithium/texture.hpp @@ -16,6 +16,18 @@ enum class TextureFormat { A8, }; namespace Li { +static 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; + } +} class Texture { public: using Ptr = Texture*; diff --git a/source/lithium/drawlist.cpp b/source/lithium/drawlist.cpp index d76417a..c79e862 100644 --- a/source/lithium/drawlist.cpp +++ b/source/lithium/drawlist.cpp @@ -75,7 +75,8 @@ PD_API void Drawlist::PathRect(const fvec2& tl, const fvec2& br, float r) { PathAdd(br); PathAdd(vec2(tl.x, br.y)); } else { - float r = std::min({r, (br.x - tl.x) * 0.5f, (br.y - tl.y) * 0.5f}); + float r = 0.f; + r = std::min({r, (br.x - tl.x) * 0.5f, (br.y - tl.y) * 0.5f}); /** Calculate Optimal segment count automatically */ float corner = M_PI * 0.5f; int segments = std::max(3, int(std::ceil(corner / (6.0f * M_PI / 180.0f)))); diff --git a/tests/gfx/include/os-ctx.hpp b/tests/gfx/include/os-ctx.hpp index 89afaba..97c648f 100644 --- a/tests/gfx/include/os-ctx.hpp +++ b/tests/gfx/include/os-ctx.hpp @@ -21,10 +21,14 @@ class OsCtx { virtual bool Mainloop() { return false; } virtual void ClearViewPort() {} virtual void SwapBuffers() {} - PD::fvec2 SizeTranslate(PD::fvec2 in) { + PD::fvec2 PositionTranslate(PD::fvec2 in) { return fvec2(in.x * pViewPort.x, in.y * pViewPort.y); } + PD::fvec2 SizeTranslate(PD::fvec2 in) { + return fvec2(in.x * pViewPort.y, in.y * pViewPort.y); + } + protected: PD::ivec2 pViewPort; const Driver pDriver; diff --git a/tests/gfx/source/main.cpp b/tests/gfx/source/main.cpp index 110550c..e43de3c 100644 --- a/tests/gfx/source/main.cpp +++ b/tests/gfx/source/main.cpp @@ -42,11 +42,12 @@ int main(int argc, char** argv) { while (pOs->Mainloop()) { pOs->ClearViewPort(); PD::Li::ResetPools(); - pList.PathRect(pOs->SizeTranslate(0.05), pOs->SizeTranslate(0.4f), 10.f); + pList.PathRect(pOs->PositionTranslate(0.05), pOs->PositionTranslate(0.4f), + 10.f); pList.PathFill(0xff00ffff); pList.BindTexture(pTex); - pList.DrawRectFilled(pOs->SizeTranslate(PD::fvec2(0.02f, 0.5f)), - pOs->SizeTranslate(PD::fvec2(0.14, 0.3)), 0xffffffff); + pList.DrawRectFilled(pOs->PositionTranslate(PD::fvec2(0.02f, 0.5f)), + pOs->SizeTranslate(PD::fvec2(0.3)), 0xffffffff); PD::Gfx::Reset(); PD::Gfx::Draw(pList); pList.Clear(); diff --git a/tests/gfx/source/os/desktopos.cpp b/tests/gfx/source/os/desktopos.cpp index 6ccab4e..6a451da 100644 --- a/tests/gfx/source/os/desktopos.cpp +++ b/tests/gfx/source/os/desktopos.cpp @@ -1,4 +1,5 @@ #include +#include #if !defined(__SWITCH__) && !defined(__3DS__) #define GLFW_INCLUDE_NONE @@ -11,8 +12,6 @@ #include #endif -#include - namespace PD { struct DesktopOS::Impl { GLFWwindow* win = nullptr; @@ -62,9 +61,9 @@ void DesktopOS::Init() { d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.hDeviceWindow = hwnd; - HRESULT hr = impl->d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, - D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, - &impl->dx9_device); + HRESULT hr = impl->d3d->CreateDevice( + D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, + D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &impl->dx9_device); if (FAILED(hr)) { MessageBoxW(nullptr, L"Failed to create D3D9 device", L"Error", MB_OK); std::abort(); @@ -91,9 +90,19 @@ bool DesktopOS::Mainloop() { void DesktopOS::ClearViewPort() { PD::Gfx::SetViewPort(pViewPort); - glClearColor(0.1, 0.1, 0.1, 0.1); - glClear(GL_COLOR_BUFFER_BIT); - glViewport(0, 0, pViewPort.x, pViewPort.y); + if (pDriver == Driver::OpenGL3 || pDriver == Driver::OpenGL2) { + glClearColor(0.1, 0.1, 0.1, 0.1); + glClear(GL_COLOR_BUFFER_BIT); + glViewport(0, 0, pViewPort.x, pViewPort.y); + } else if (pDriver == Driver::DirectX9) { +#ifdef _WIN32 + if (impl->dx9_device) { + impl->dx9_device->Clear(0, nullptr, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, + D3DCOLOR_XRGB(25, 25, 25), 1.0f, 0); + impl->dx9_device->BeginScene(); + } +#endif + } } void DesktopOS::SwapBuffers() { glfwSwapBuffers(impl->win); }