From f61c92a59fe6f5026cec9bd516bc3b06750751a8 Mon Sep 17 00:00:00 2001 From: SwareJonge <41187958+SwareJonge@users.noreply.github.com> Date: Tue, 17 Jun 2025 00:38:54 +0200 Subject: [PATCH] match J2D --- configure.py | 4 +- include/MSL_C/math.h | 2 + src/static/JSystem/J2DGraph/.gitkeep | 0 .../JSystem/J2DGraph/J2DGrafContext.cpp | 201 ++++++++++++++++++ src/static/JSystem/J2DGraph/J2DOrthoGraph.cpp | 97 +++++++++ 5 files changed, 302 insertions(+), 2 deletions(-) delete mode 100644 src/static/JSystem/J2DGraph/.gitkeep create mode 100644 src/static/JSystem/J2DGraph/J2DGrafContext.cpp create mode 100644 src/static/JSystem/J2DGraph/J2DOrthoGraph.cpp diff --git a/configure.py b/configure.py index 9e9fd86c..06f195db 100644 --- a/configure.py +++ b/configure.py @@ -611,8 +611,8 @@ config.libs = [ JSystemLib( "J2DGraph", [ - Object(NonMatching, "JSystem/J2DGraph/J2DGrafContext.cpp"), - Object(NonMatching, "JSystem/J2DGraph/J2DOrthoGraph.cpp"), + Object(Matching, "JSystem/J2DGraph/J2DGrafContext.cpp"), + Object(Matching, "JSystem/J2DGraph/J2DOrthoGraph.cpp"), ], ), JSystemLib( diff --git a/include/MSL_C/math.h b/include/MSL_C/math.h index 3fc61d26..a90549c7 100644 --- a/include/MSL_C/math.h +++ b/include/MSL_C/math.h @@ -21,6 +21,8 @@ extern double sin(double deg); extern double cos(double deg); extern double tan(double deg); +extern double ceil(double); + #ifdef __cplusplus } #endif diff --git a/src/static/JSystem/J2DGraph/.gitkeep b/src/static/JSystem/J2DGraph/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/src/static/JSystem/J2DGraph/J2DGrafContext.cpp b/src/static/JSystem/J2DGraph/J2DGrafContext.cpp new file mode 100644 index 00000000..a672729b --- /dev/null +++ b/src/static/JSystem/J2DGraph/J2DGrafContext.cpp @@ -0,0 +1,201 @@ +#include "JSystem/J2D/J2DGrafContext.h" +#include "MSL_C/math.h" + +J2DGrafContext::J2DGrafContext(f32 left, f32 top, f32 right, f32 bottom) + : mBounds(left, top, left + right, top + bottom) + , mScissorBounds(left, top, left + right, top + bottom) +{ + JUtility::TColor color(-1); + setColor(color); + setLineWidth(6); +} + +void J2DGrafContext::setPort() +{ + setScissor(); + setup2D(); + + GXSetViewport(mBounds.i.x, mBounds.i.y, mBounds.f.x - mBounds.i.x, mBounds.f.y - mBounds.i.y, 0.0f, 1.0f); +} + +void J2DGrafContext::setup2D() +{ + GXSetNumIndStages(0); + for (int i = 0; i < 8; i++) { + GXSetTevDirect((GXTevStageID)i); + } + + GXSetAlphaCompare(GX_ALWAYS, 0, GX_AOP_AND, GX_ALWAYS, 0); + GXSetZMode(0, GX_LEQUAL, 0); + GXSetTevOp(GX_TEVSTAGE0, GX_PASSCLR); + + GXSetNumChans(1); + GXSetNumTevStages(1); + GXSetNumTexGens(0); + GXSetTevOrder(GX_TEVSTAGE0, GX_TEXCOORD_NULL, GX_TEXMAP_NULL, GX_COLOR0A0); + GXSetCullMode(GX_CULL_NONE); + + GXLoadPosMtxImm(mPosMtx, 0); + GC_Mtx m; + PSMTXIdentity(m); + GXLoadTexMtxImm(m, 60, GX_MTX3x4); + + GXSetChanCtrl(GX_COLOR0A0, 0, GX_SRC_REG, GX_SRC_VTX, GX_LIGHT_NULL, GX_DF_NONE, GX_AF_NONE); + GXSetChanCtrl(GX_COLOR1A1, 0, GX_SRC_REG, GX_SRC_REG, GX_LIGHT_NULL, GX_DF_NONE, GX_AF_NONE); + + GXSetCurrentMtx(0); + GXSetTexCoordGen2(GX_TEXCOORD0, GX_TG_MTX2x4, GX_TG_TEX0, 60, 0, 125); + + GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_CLR0, GX_POS_XYZ, GX_RGBA8, 0); + GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_POS, GX_POS_XYZ, GX_RGBA4, 0); + GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_TEX0, GX_POS_XYZ, GX_RGBX8, 0xF); + GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_TEX1, GX_POS_XYZ, GX_RGBX8, 0xF); + + GXSetLineWidth(mLineWidth, GX_TO_ZERO); + + GXClearVtxDesc(); + GXSetVtxDesc(GX_VA_POS, GX_DIRECT); + GXSetVtxDesc(GX_VA_CLR0, GX_DIRECT); + GXSetVtxDesc(GX_VA_TEX0, GX_NONE); +} + +void J2DGrafContext::setScissor() +{ + JGeometry::TBox2f hardBounds(0, 0, 1024, 1000); + JGeometry::TBox2f newBounds(mScissorBounds); + + mScissorBounds.intersect(hardBounds); + newBounds.absolute(); + newBounds.addPos(0.0f, -1.0f); + + if (newBounds.intersect(hardBounds)) { + GXSetScissor(newBounds.i.x, newBounds.i.y, newBounds.getWidth(), newBounds.getHeight()); + } else { + GXSetScissor(0, 0, 0, 0); + } +} + +void J2DGrafContext::scissor(const JGeometry::TBox2f& bounds) { mScissorBounds = bounds; } + +void J2DGrafContext::place(const JGeometry::TBox2f& bounds) +{ + mBounds = bounds; + mScissorBounds = bounds; +} + +void J2DGrafContext::setColor(JUtility::TColor colorTL, JUtility::TColor colorTR, JUtility::TColor colorBR, JUtility::TColor colorBL) +{ + mColorTL = colorTL; + mColorTR = colorTR; + mColorBR = colorBR; + mColorBL = colorBL; + + _B0.mType = 1; + _B0.mSrcFactor = 4; + _B0.mDestFactor = 5; + + mLinePart.mType = 1; + mLinePart.mSrcFactor = 4; + mLinePart.mDestFactor = 5; + + mBoxPart.mType = 1; + mBoxPart.mSrcFactor = 4; + mBoxPart.mDestFactor = 5; + + if ((u8)u32(mColorTL) != 0xFF) { + return; + } + + _B0.mType = 0; + _B0.mSrcFactor = 1; + _B0.mDestFactor = 0; + + if ((u8)u32(mColorBR) != 0xFF) { + return; + } + + mLinePart.mType = 0; + mLinePart.mSrcFactor = 1; + mLinePart.mDestFactor = 0; + + if ((u8)u32(mColorTR) != 0xFF) { + return; + } + if ((u8)u32(mColorBL) != 0xFF) { + return; + } + + mBoxPart.mType = 0; + mBoxPart.mSrcFactor = 1; + mBoxPart.mDestFactor = 0; +} + +void J2DGrafContext::setLineWidth(u8 width) +{ + mLineWidth = width; + GXSetLineWidth(mLineWidth, GX_TO_ZERO); +} + +void J2DGrafContext::fillBox(const JGeometry::TBox2f& box) +{ + GXSetBlendMode((GXBlendMode)mBoxPart.mType, (GXBlendFactor)mBoxPart.mSrcFactor, (GXBlendFactor)mBoxPart.mDestFactor, GX_LO_SET); + GXLoadPosMtxImm(mPosMtx, 0); + GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_POS, GX_CLR_RGBA, GX_F32, 0); + + GXBegin(GX_QUADS, GX_VTXFMT0, 4); + GXPosition3f32(box.i.x, box.i.y, 0.0f); + GXColor1u32(mColorTL); + GXPosition3f32(box.f.x, box.i.y, 0.0f); + GXColor1u32(mColorTR); + GXPosition3f32(box.f.x, box.f.y, 0.0f); + GXColor1u32(mColorBL); + GXPosition3f32(box.i.x, box.f.y, 0.0f); + GXColor1u32(mColorBR); + GXEnd(); + + GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_POS, GX_CLR_RGBA, GX_RGBA4, 0); +} + +void J2DGrafContext::drawFrame(const JGeometry::TBox2f& box) +{ + GXSetBlendMode((GXBlendMode)mBoxPart.mType, (GXBlendFactor)mBoxPart.mSrcFactor, (GXBlendFactor)mBoxPart.mDestFactor, GX_LO_SET); + GXLoadPosMtxImm(mPosMtx, 0); + GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_POS, GX_CLR_RGBA, GX_F32, 0); + + GXBegin(GX_LINESTRIP, GX_VTXFMT0, 5); + GXPosition3f32(box.i.x, box.i.y, 0.0f); + GXColor1u32(mColorTL); + GXPosition3f32(box.f.x, box.i.y, 0.0f); + GXColor1u32(mColorTR); + GXPosition3f32(box.f.x, box.f.y, 0.0f); + GXColor1u32(mColorBL); + GXPosition3f32(box.i.x, box.f.y, 0.0f); + GXColor1u32(mColorBR); + GXPosition3f32(box.i.x, box.i.y, 0.0f); + GXColor1u32(mColorTL); + GXEnd(); + + GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_POS, GX_CLR_RGBA, GX_RGBA4, 0); +} + +void J2DGrafContext::line(JGeometry::TVec2f start, JGeometry::TVec2f end) +{ + GXSetBlendMode((GXBlendMode)mLinePart.mType, (GXBlendFactor)mLinePart.mSrcFactor, (GXBlendFactor)mLinePart.mDestFactor, GX_LO_SET); + GXLoadPosMtxImm(mPosMtx, 0); + GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_POS, GX_CLR_RGBA, GX_F32, 0); + + GXBegin(GX_LINES, GX_VTXFMT0, 2); + GXPosition3f32(start.x, start.y, 0.0f); + GXColor1u32(mColorTL); + GXPosition3f32(end.x, end.y, 0.0f); + GXColor1u32(mColorBR); + GXEnd(); + + GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_POS, GX_CLR_RGBA, GX_RGBA4, 0); +} + +void J2DGrafContext::lineTo(JGeometry::TVec2f pos) +{ + line(mPrevPos, pos); + mPrevPos = pos; +} \ No newline at end of file diff --git a/src/static/JSystem/J2DGraph/J2DOrthoGraph.cpp b/src/static/JSystem/J2DGraph/J2DOrthoGraph.cpp new file mode 100644 index 00000000..7d7ec7b7 --- /dev/null +++ b/src/static/JSystem/J2DGraph/J2DOrthoGraph.cpp @@ -0,0 +1,97 @@ +#include "JSystem/J2D/J2DGrafContext.h" + +J2DOrthoGraph::J2DOrthoGraph() + : J2DGrafContext(0.0f, 0.0f, 0.0f, 0.0f) +{ + setLookat(); +} + +J2DOrthoGraph::J2DOrthoGraph(f32 left, f32 top, f32 right, f32 bottom, f32 near, f32 far) + : J2DGrafContext(left, top, right, bottom) +{ + mOrtho = JGeometry::TBox2f(0, 0, right, bottom); + mNear = near; + mFar = far; + setLookat(); +} + +void J2DOrthoGraph::setPort() +{ + J2DGrafContext::setPort(); + C_MTXOrtho(mMtx44, mOrtho.i.y, 0.5f + mOrtho.f.y, mOrtho.i.x, mOrtho.f.x, mNear, mFar); + GXSetProjection(mMtx44, GX_ORTHOGRAPHIC); +} + + +void J2DOrthoGraph::setOrtho(const JGeometry::TBox2f& bounds, f32 far, f32 near) +{ + mOrtho = bounds; + mNear = -near; + mFar = -far; +} + +void J2DOrthoGraph::setLookat() +{ + PSMTXIdentity(mPosMtx); + GXLoadPosMtxImm(mPosMtx, 0); +} + +void J2DOrthoGraph::scissorBounds(JGeometry::TBox2f* out, const JGeometry::TBox2f* src) +{ + f32 widthPower = this->getWidthPower(); + f32 heightPower = this->getHeightPower(); + f32 ix = mBounds.i.x >= 0 ? mBounds.i.x : 0; + f32 iy = mBounds.i.y >= 0 ? mBounds.i.y : 0; + f32 f0 = ix + widthPower * (src->i.x - mOrtho.i.x); + f32 f2 = ix + widthPower * (src->f.x - mOrtho.i.x); + f32 f1 = iy + heightPower * (src->i.y - mOrtho.i.y); + f32 f3 = iy + heightPower * (src->f.y - mOrtho.i.y); + out->set(f0, f1, f2, f3); + out->intersect(mScissorBounds); +} + +void J2DDrawLine(f32 x1, f32 y1, f32 x2, f32 y2, JUtility::TColor color, int line_width) +{ + J2DOrthoGraph oGrph; + oGrph.setLineWidth(line_width); + oGrph.setColor(color); + oGrph.moveTo(x1, y1); + oGrph.lineTo(x2, y2); +} + +void J2DFillBox(f32 l, f32 b, f32 x, f32 y, JUtility::TColor color) +{ + J2DFillBox(JGeometry::TBox2f(l, b, l + x, b + y), color); +} + +void J2DFillBox(const JGeometry::TBox2f& box, JUtility::TColor color) +{ + J2DOrthoGraph oGrph; + oGrph.setColor(color); + oGrph.fillBox(box); +} + +void J2DFillBox(f32 l, f32 b, f32 x, f32 y, JUtility::TColor c1, JUtility::TColor c2, JUtility::TColor c3, JUtility::TColor c4) +{ + J2DFillBox(JGeometry::TBox2f(l, b, l + x, b + y), c1, c2, c3, c4); +} + +void J2DFillBox(const JGeometry::TBox2f &box, JUtility::TColor c1, JUtility::TColor c2, JUtility::TColor c3, JUtility::TColor c4) +{ + J2DOrthoGraph oGrph; + oGrph.setColor(c1, c2, c3, c4); + oGrph.fillBox(box); +} + +void J2DDrawFrame(f32 l, f32 b, f32 x, f32 y, JUtility::TColor color, u8 line_width) +{ + J2DDrawFrame(JGeometry::TBox2f(l, b, l + x, b + y), color, line_width); +} + +void J2DDrawFrame(const JGeometry::TBox2f& box, JUtility::TColor color, u8 line_width) +{ + J2DOrthoGraph oGrph; + oGrph.setColor(color); + oGrph.setLineWidth(line_width); + oGrph.drawFrame(box); +}