From cc567b098598215b44b1eea64df48bc957e1dd0d Mon Sep 17 00:00:00 2001 From: Harrison <53527582+HTV04@users.noreply.github.com> Date: Mon, 22 Aug 2022 12:10:23 -0400 Subject: [PATCH] Add GRRLIB_matrix and transformation functions --- GRRLIB/GRRLIB/GRRLIB_core.c | 7 +- GRRLIB/GRRLIB/GRRLIB_matrix.c | 142 +++++++++++++++++++++++++++++ GRRLIB/GRRLIB/GRRLIB_render.c | 13 ++- GRRLIB/GRRLIB/grrlib.h | 8 ++ GRRLIB/GRRLIB/grrlib/GRRLIB__lib.h | 11 +++ 5 files changed, 172 insertions(+), 9 deletions(-) create mode 100644 GRRLIB/GRRLIB/GRRLIB_matrix.c diff --git a/GRRLIB/GRRLIB/GRRLIB_core.c b/GRRLIB/GRRLIB/GRRLIB_core.c index 0b9c246..48b7f70 100644 --- a/GRRLIB/GRRLIB/GRRLIB_core.c +++ b/GRRLIB/GRRLIB/GRRLIB_core.c @@ -1,5 +1,5 @@ /*------------------------------------------------------------------------------ -Copyright (c) 2009-2021 The GRRLIB Team +Copyright (c) 2009-2022 The GRRLIB Team Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -36,6 +36,7 @@ THE SOFTWARE. GRRLIB_drawSettings GRRLIB_Settings; Mtx GXmodelView2D; +guVector axis2D = (guVector){0, 0, 1}; static void *gp_fifo = NULL; @@ -177,8 +178,10 @@ int GRRLIB_Init (void) { GX_SetTevOrder(GX_TEVSTAGE0, GX_TEXCOORD0, GX_TEXMAP0, GX_COLOR0A0); GX_SetTexCoordGen(GX_TEXCOORD0, GX_TG_MTX2x4, GX_TG_TEX0, GX_IDENTITY); + // Default matrix settings, reflected in GRRLIB_Origin() guMtxIdentity(GXmodelView2D); - guMtxTransApply(GXmodelView2D, GXmodelView2D, 0.0F, 0.0F, -100.0F); + guMtxTransApply(GXmodelView2D, GXmodelView2D, 0.0f, 0.0f, -100.0f); + GX_LoadPosMtxImm(GXmodelView2D, GX_PNMTX0); guOrtho(perspective, 0.0f, rmode->efbHeight, 0.0f, rmode->fbWidth, 0.0f, 1000.0f); diff --git a/GRRLIB/GRRLIB/GRRLIB_matrix.c b/GRRLIB/GRRLIB/GRRLIB_matrix.c new file mode 100644 index 0000000..9f491fe --- /dev/null +++ b/GRRLIB/GRRLIB/GRRLIB_matrix.c @@ -0,0 +1,142 @@ +/*------------------------------------------------------------------------------ +Copyright (c) 2009-2022 The GRRLIB Team + +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 + +extern GRRLIB_drawSettings GRRLIB_Settings; +extern Mtx GXmodelView2D; +extern guVector axis2D; + +/** + * Get the current matrix as a new matrix object. + * @return A handle to the matrix object. + * @see GRRLIB_matrix + */ +GRRLIB_matrix GRRLIB_GetMatrix (void) { + GRRLIB_matrix matrixObject; + + guMtxCopy(GXmodelView2D, matrixObject.matrix); + + return matrixObject; +} + +/** + * Set the current matrix using a matrix object. + * @param matrixObject The matrix object to set the matrix with. + */ +void GRRLIB_SetMatrix (GRRLIB_matrix *matrixObject) { + guMtxCopy(matrixObject->matrix, GXmodelView2D); +} + +/** + * Scale the current matrix. + * @param scaleX The scaling in the direction of the x-axis. + * @param scaleY The scaling in the direction of the y-axis. + */ +void GRRLIB_Scale (f32 scaleX, f32 scaleY) { + Mtx m; + + guMtxIdentity (m); + guMtxScaleApply(m, m, scaleX, scaleY, 1.0f); + + guMtxConcat (GXmodelView2D, m, GXmodelView2D); + GX_LoadPosMtxImm(GXmodelView2D, GX_PNMTX0); +} + +/** + * Rotate the current matrix. + * @param degrees The amount to rotate the current matrix in degrees. + */ +void GRRLIB_Rotate (f32 degrees) { + Mtx m; + + guMtxRotAxisDeg(m, &axis2D, degrees); + + guMtxConcat (GXmodelView2D, m, GXmodelView2D); + GX_LoadPosMtxImm(GXmodelView2D, GX_PNMTX0); +} + +/** + * Translate the current matrix. + * @param posX The translation relative to the x-axis. + * @param posY The translation relative to the y-axis. + */ +void GRRLIB_Translate (f32 posX, f32 posY) { + Mtx m; + + guMtxIdentity (m); + guMtxTransApply(m, m, posX, posY, 0.0f); + + guMtxConcat (GXmodelView2D, m, GXmodelView2D); + GX_LoadPosMtxImm(GXmodelView2D, GX_PNMTX0); +} + +/** + * Transform the current matrix (scale, rotate, then translate). + * @param scaleX The scaling in the direction of the x-axis. + * @param scaleY The scaling in the direction of the y-axis. + * @param degrees The amount to rotate the current matrix in degrees. + * @param posX The translation relative to the x-axis. + * @param posY The translation relative to the y-axis. + */ +void GRRLIB_Transform (f32 scaleX, f32 scaleY, f32 degrees, f32 posX, f32 posY) { + Mtx m1, m2, m; + + guMtxIdentity (m1); + guMtxScaleApply(m1, m1, scaleX, scaleY, 1.0f); + guMtxRotAxisDeg(m2, &axis2D, degrees); + guMtxConcat (m1, m2, m); + guMtxTransApply(m, m, posX, posY, 0.0f); + + guMtxConcat (GXmodelView2D, m, GXmodelView2D); + GX_LoadPosMtxImm(GXmodelView2D, GX_PNMTX0); +} + +/** + * Transform the current matrix (scale, translate, then rotate). + * @param scaleX The scaling in the direction of the x-axis. + * @param scaleY The scaling in the direction of the y-axis. + * @param posX The translation relative to the x-axis. + * @param posY The translation relative to the y-axis. + * @param degrees The amount to rotate the current matrix in degrees. + */ +void GRRLIB_TransformInv (f32 scaleX, f32 scaleY, f32 posX, f32 posY, f32 degrees) { + Mtx m1, m2, m; + + guMtxIdentity (m1); + guMtxScaleApply(m1, m1, scaleX, scaleY, 1.0f); + guMtxTransApply(m2, m2, posX, posY, 0.0f); + guMtxConcat (m1, m2, m); + guMtxRotAxisDeg(m, &axis2D, degrees); + + guMtxConcat (GXmodelView2D, m, GXmodelView2D); + GX_LoadPosMtxImm(GXmodelView2D, GX_PNMTX0); +} + +/** + * Reset the current matrix. + */ +void GRRLIB_Origin (void) { + guMtxIdentity (GXmodelView2D); + guMtxTransApply (GXmodelView2D, GXmodelView2D, 0.0f, 0.0f, -100.0f); + GX_LoadPosMtxImm(GXmodelView2D, GX_PNMTX0); +} diff --git a/GRRLIB/GRRLIB/GRRLIB_render.c b/GRRLIB/GRRLIB/GRRLIB_render.c index 3630ec8..c487f28 100644 --- a/GRRLIB/GRRLIB/GRRLIB_render.c +++ b/GRRLIB/GRRLIB/GRRLIB_render.c @@ -26,8 +26,7 @@ THE SOFTWARE. extern GRRLIB_drawSettings GRRLIB_Settings; extern Mtx GXmodelView2D; - -static guVector axis = (guVector){0, 0, 1}; +extern guVector axis2D; /** * Draw a texture. @@ -64,7 +63,7 @@ void GRRLIB_DrawImg (const f32 xpos, const f32 ypos, const GRRLIB_texImg *tex, guMtxIdentity (m1); guMtxScaleApply(m1, m1, scaleX, scaleY, 1.0); - guMtxRotAxisDeg(m2, &axis, degrees); + guMtxRotAxisDeg(m2, &axis2D, degrees); guMtxConcat (m2, m1, m); const u32 width = tex->w * 0.5; @@ -135,7 +134,7 @@ void GRRLIB_DrawImgQuad (const guVector pos[4], GRRLIB_texImg *tex, const u32 c guMtxIdentity (m1); guMtxScaleApply(m1, m1, 1, 1, 1.0); - guMtxRotAxisDeg(m2, &axis, 0); + guMtxRotAxisDeg(m2, &axis2D, 0); guMtxConcat (m2, m1, m); guMtxConcat (GXmodelView2D, m, mv); @@ -209,7 +208,7 @@ void GRRLIB_DrawTile (const f32 xpos, const f32 ypos, const GRRLIB_texImg *tex, guMtxIdentity (m1); guMtxScaleApply(m1, m1, scaleX, scaleY, 1.0f); - guMtxRotAxisDeg(m2, &axis, degrees); + guMtxRotAxisDeg(m2, &axis2D, degrees); guMtxConcat (m2, m1, m); guMtxTransApply(m, m, @@ -296,7 +295,7 @@ void GRRLIB_DrawPart (const f32 xpos, const f32 ypos, const f32 partx, const f3 guMtxIdentity (m1); guMtxScaleApply(m1, m1, scaleX, scaleY, 1.0f); - guMtxRotAxisDeg(m2, &axis, degrees); + guMtxRotAxisDeg(m2, &axis2D, degrees); guMtxConcat (m2, m1, m); guMtxTransApply(m, m, @@ -373,7 +372,7 @@ void GRRLIB_DrawTileQuad (const guVector pos[4], GRRLIB_texImg *tex, const u32 guMtxIdentity (m1); guMtxScaleApply(m1, m1, 1, 1, 1.0f); - guMtxRotAxisDeg(m2, &axis, 0); + guMtxRotAxisDeg(m2, &axis2D, 0); guMtxConcat (m2, m1, m); guMtxConcat (GXmodelView2D, m, mv); diff --git a/GRRLIB/GRRLIB/grrlib.h b/GRRLIB/GRRLIB/grrlib.h index de8c4d1..dbf5bdf 100644 --- a/GRRLIB/GRRLIB/grrlib.h +++ b/GRRLIB/GRRLIB/grrlib.h @@ -160,6 +160,14 @@ typedef struct GRRLIB_Font { bool kerning; /**< true whenever a face object contains kerning data that can be accessed with FT_Get_Kerning. */ } GRRLIB_ttfFont; +//------------------------------------------------------------------------------ +/** + * Structure to hold the matrix information. + */ +typedef struct GRRLIB_matrix { + Mtx matrix; +} GRRLIB_matrix; + //============================================================================== // Allow general access to screen and frame information //============================================================================== diff --git a/GRRLIB/GRRLIB/grrlib/GRRLIB__lib.h b/GRRLIB/GRRLIB/grrlib/GRRLIB__lib.h index 7584981..a063613 100644 --- a/GRRLIB/GRRLIB/grrlib/GRRLIB__lib.h +++ b/GRRLIB/GRRLIB/grrlib/GRRLIB__lib.h @@ -125,6 +125,17 @@ void GRRLIB_DrawTileQuad (const guVector pos[4], GRRLIB_texImg *tex, const u32 void GRRLIB_Render (void); +//------------------------------------------------------------------------------ +// GRRLIB_matrix.c - Matrix functions +GRRLIB_matrix GRRLIB_GetMatrix (void); +void GRRLIB_SetMatrix (GRRLIB_matrix *matrixObject); +void GRRLIB_Scale (f32 scaleX, f32 scaleY); +void GRRLIB_Rotate (f32 degrees); +void GRRLIB_Translate (f32 posX, f32 posY); +void GRRLIB_Transform (f32 scaleX, f32 scaleY, f32 degrees, f32 posX, f32 posY); +void GRRLIB_TransformInv (f32 scaleX, f32 scaleY, f32 posX, f32 posY, f32 degrees); +void GRRLIB_Origin (void); + //------------------------------------------------------------------------------ // GRRLIB_snapshot.c - Create a texture containing a snapshot of a part of the framebuffer void GRRLIB_Screen2Texture (int posx, int posy, GRRLIB_texImg *tex, bool clear);