From 95c915f5f8243ced66c91953c49cbb9b55098cda Mon Sep 17 00:00:00 2001 From: Crayon2000 Date: Wed, 23 Nov 2022 22:27:38 -0500 Subject: [PATCH] Use proper types and reduce variables scope --- GRRLIB/GRRLIB/GRRLIB_3D.c | 38 +++---- GRRLIB/GRRLIB/GRRLIB_bmfx.c | 117 +++++++++------------ GRRLIB/GRRLIB/grrlib/GRRLIB_texSetup.h | 11 +- examples/3D_sample1/source/main.c | 6 +- examples/gamecube/3D_sample1/source/main.c | 6 +- 5 files changed, 80 insertions(+), 98 deletions(-) diff --git a/GRRLIB/GRRLIB/GRRLIB_3D.c b/GRRLIB/GRRLIB/GRRLIB_3D.c index 67eea8e..207c321 100644 --- a/GRRLIB/GRRLIB/GRRLIB_3D.c +++ b/GRRLIB/GRRLIB/GRRLIB_3D.c @@ -459,16 +459,16 @@ void GRRLIB_DrawSphere(f32 r, int lats, int longs, bool filled, u32 col) { * @param col Color of the cube. */ void GRRLIB_DrawCube(f32 size, bool filled, u32 col) { - static f32 n[6][3] = + static const f32 normal[6][3] = { - {-1.0, 0.0, 0.0}, - {0.0, 1.0, 0.0}, - {1.0, 0.0, 0.0}, - {0.0, -1.0, 0.0}, - {0.0, 0.0, 1.0}, - {0.0, 0.0, -1.0} + {-1.0f, 0.0f, 0.0f}, + {0.0f, 1.0f, 0.0f}, + {1.0f, 0.0f, 0.0f}, + {0.0f, -1.0f, 0.0f}, + {0.0f, 0.0f, 1.0f}, + {0.0f, 0.0f, -1.0f} }; - static int faces[6][4] = + static const u8 faces[6][4] = { {0, 1, 2, 3}, {3, 2, 6, 7}, @@ -479,12 +479,12 @@ void GRRLIB_DrawCube(f32 size, bool filled, u32 col) { }; f32 v[8][3]; - v[0][0] = v[1][0] = v[2][0] = v[3][0] = -size / 2; - v[4][0] = v[5][0] = v[6][0] = v[7][0] = size / 2; - v[0][1] = v[1][1] = v[4][1] = v[5][1] = -size / 2; - v[2][1] = v[3][1] = v[6][1] = v[7][1] = size / 2; - v[0][2] = v[3][2] = v[4][2] = v[7][2] = -size / 2; - v[1][2] = v[2][2] = v[5][2] = v[6][2] = size / 2; + v[0][0] = v[1][0] = v[2][0] = v[3][0] = -size / 2.0f; + v[4][0] = v[5][0] = v[6][0] = v[7][0] = size / 2.0f; + v[0][1] = v[1][1] = v[4][1] = v[5][1] = -size / 2.0f; + v[2][1] = v[3][1] = v[6][1] = v[7][1] = size / 2.0f; + v[0][2] = v[3][2] = v[4][2] = v[7][2] = -size / 2.0f; + v[1][2] = v[2][2] = v[5][2] = v[6][2] = size / 2.0f; for (s8 i = 5; i >= 0; i--) { if(filled == true) { @@ -494,20 +494,20 @@ void GRRLIB_DrawCube(f32 size, bool filled, u32 col) { GX_Begin(GX_LINESTRIP, GX_VTXFMT0, 5); } GX_Position3f32(v[faces[i][0]][0], v[faces[i][0]][1], v[faces[i][0]][2] ); - GX_Normal3f32(n[i][0], n[i][1], n[i][2]); + GX_Normal3f32(normal[i][0], normal[i][1], normal[i][2]); GX_Color1u32(col); GX_Position3f32(v[faces[i][1]][0], v[faces[i][1]][1], v[faces[i][1]][2]); - GX_Normal3f32(n[i][0], n[i][1], n[i][2]); + GX_Normal3f32(normal[i][0], normal[i][1], normal[i][2]); GX_Color1u32(col); GX_Position3f32(v[faces[i][2]][0], v[faces[i][2]][1], v[faces[i][2]][2]); - GX_Normal3f32(n[i][0], n[i][1], n[i][2]); + GX_Normal3f32(normal[i][0], normal[i][1], normal[i][2]); GX_Color1u32(col); GX_Position3f32(v[faces[i][3]][0], v[faces[i][3]][1], v[faces[i][3]][2]); - GX_Normal3f32(n[i][0], n[i][1], n[i][2]); + GX_Normal3f32(normal[i][0], normal[i][1], normal[i][2]); GX_Color1u32(col); if(filled == false) { GX_Position3f32(v[faces[i][0]][0], v[faces[i][0]][1], v[faces[i][0]][2]); - GX_Normal3f32(n[i][0], n[i][1], n[i][2]); + GX_Normal3f32(normal[i][0], normal[i][1], normal[i][2]); GX_Color1u32(col); } GX_End(); diff --git a/GRRLIB/GRRLIB/GRRLIB_bmfx.c b/GRRLIB/GRRLIB/GRRLIB_bmfx.c index 27068ed..493c599 100644 --- a/GRRLIB/GRRLIB/GRRLIB_bmfx.c +++ b/GRRLIB/GRRLIB/GRRLIB_bmfx.c @@ -1,5 +1,5 @@ /*------------------------------------------------------------------------------ -Copyright (c) 2009-2017 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 @@ -31,10 +31,10 @@ THE SOFTWARE. * @param texdest The texture destination. */ void GRRLIB_BMFX_FlipH (const GRRLIB_texImg *texsrc, GRRLIB_texImg *texdest) { - unsigned int x, y, txtWidth = texsrc->w - 1; + const u32 txtWidth = texsrc->w - 1; - for (y = 0; y < texsrc->h; y++) { - for (x = 0; x < texsrc->w; x++) { + for (u32 y = 0; y < texsrc->h; y++) { + for (u32 x = 0; x < texsrc->w; x++) { GRRLIB_SetPixelTotexImg(txtWidth - x, y, texdest, GRRLIB_GetPixelFromtexImg(x, y, texsrc)); } @@ -48,10 +48,10 @@ void GRRLIB_BMFX_FlipH (const GRRLIB_texImg *texsrc, GRRLIB_texImg *texdest) { * @param texdest The texture destination. */ void GRRLIB_BMFX_FlipV (const GRRLIB_texImg *texsrc, GRRLIB_texImg *texdest) { - unsigned int x, y, texHeight = texsrc->h - 1; + const u32 texHeight = texsrc->h - 1; - for (y = 0; y < texsrc->h; y++) { - for (x = 0; x < texsrc->w; x++) { + for (u32 y = 0; y < texsrc->h; y++) { + for (u32 x = 0; x < texsrc->w; x++) { GRRLIB_SetPixelTotexImg(x, texHeight - y, texdest, GRRLIB_GetPixelFromtexImg(x, y, texsrc)); } @@ -66,15 +66,13 @@ void GRRLIB_BMFX_FlipV (const GRRLIB_texImg *texsrc, GRRLIB_texImg *texdest) { */ void GRRLIB_BMFX_Grayscale (const GRRLIB_texImg *texsrc, GRRLIB_texImg *texdest) { - unsigned int x, y; + for (u32 y = 0; y < texsrc->h; y++) { + for (u32 x = 0; x < texsrc->w; x++) { + const u32 color = GRRLIB_GetPixelFromtexImg(x, y, texsrc); - for (y = 0; y < texsrc->h; y++) { - for (x = 0; x < texsrc->w; x++) { - u32 color = GRRLIB_GetPixelFromtexImg(x, y, texsrc); - - u8 gray = ((R(color)* 77 + - G(color)*150 + - B(color)* 28 ) / 255); + const u8 gray = ((R(color) * 77 + + G(color) * 150 + + B(color) * 28 ) / 255); GRRLIB_SetPixelTotexImg(x, y, texdest, (gray << 24) | (gray << 16) | (gray << 8) | (A(color))); @@ -91,18 +89,16 @@ void GRRLIB_BMFX_Grayscale (const GRRLIB_texImg *texsrc, * @author elisherer */ void GRRLIB_BMFX_Sepia (const GRRLIB_texImg *texsrc, GRRLIB_texImg *texdest) { - unsigned int x, y; - u16 sr, sg, sb; - u32 color; - - for (y = 0; y < texsrc->h; y++) { - for (x = 0; x < texsrc->w; x++) { - color = GRRLIB_GetPixelFromtexImg(x, y, texsrc); - sr = R(color)*0.393 + G(color)*0.769 + B(color)*0.189; - sg = R(color)*0.349 + G(color)*0.686 + B(color)*0.168; - sb = R(color)*0.272 + G(color)*0.534 + B(color)*0.131; - if (sr>255) sr=255; - if (sg>255) sg=255; + for (u32 y = 0; y < texsrc->h; y++) { + for (u32 x = 0; x < texsrc->w; x++) { + const u32 color = GRRLIB_GetPixelFromtexImg(x, y, texsrc); + u16 sr = R(color)*0.393 + G(color)*0.769 + B(color)*0.189; + u16 sg = R(color)*0.349 + G(color)*0.686 + B(color)*0.168; + u16 sb = R(color)*0.272 + G(color)*0.534 + B(color)*0.131; + if (sr > 255) + sr = 255; + if (sg > 255) + sg = 255; GRRLIB_SetPixelTotexImg(x, y, texdest, RGBA(sr, sg, sb, A(color))); } @@ -116,12 +112,9 @@ void GRRLIB_BMFX_Sepia (const GRRLIB_texImg *texsrc, GRRLIB_texImg *texdest) { * @param texdest The texture destination. */ void GRRLIB_BMFX_Invert (const GRRLIB_texImg *texsrc, GRRLIB_texImg *texdest) { - unsigned int x, y; - u32 color; - - for (y = 0; y < texsrc->h; y++) { - for (x = 0; x < texsrc->w; x++) { - color = GRRLIB_GetPixelFromtexImg(x, y, texsrc); + for (u32 y = 0; y < texsrc->h; y++) { + for (u32 x = 0; x < texsrc->w; x++) { + const u32 color = GRRLIB_GetPixelFromtexImg(x, y, texsrc); GRRLIB_SetPixelTotexImg(x, y, texdest, ((0xFFFFFF - (color >> 8 & 0xFFFFFF)) << 8) | (color & 0xFF)); } @@ -137,26 +130,21 @@ void GRRLIB_BMFX_Invert (const GRRLIB_texImg *texsrc, GRRLIB_texImg *texdest) { */ void GRRLIB_BMFX_Blur (const GRRLIB_texImg *texsrc, GRRLIB_texImg *texdest, const u32 factor) { - int numba = (1+(factor<<1))*(1+(factor<<1)); - u32 x, y; - s32 k, l; - int tmp; - int newr, newg, newb, newa; + const int numba = (1 + (factor << 1)) * (1 + (factor << 1)); u32 colours[numba]; - u32 thiscol; - for (x = 0; x < texsrc->w; x++) { - for (y = 0; y < texsrc->h; y++) { - newr = 0; - newg = 0; - newb = 0; - newa = 0; + for (u32 x = 0; x < texsrc->w; x++) { + for (u32 y = 0; y < texsrc->h; y++) { + int newr = 0; + int newg = 0; + int newb = 0; + int newa = 0; - tmp = 0; - thiscol = GRRLIB_GetPixelFromtexImg(x, y, texsrc); + int tmp = 0; + const u32 thiscol = GRRLIB_GetPixelFromtexImg(x, y, texsrc); - for (k = x - factor; k <= x + factor; k++) { - for (l = y - factor; l <= y + factor; l++) { + for (s32 k = x - factor; k <= x + factor; k++) { + for (s32 l = y - factor; l <= y + factor; l++) { if (k < 0 || k >= texsrc->w || l < 0 || l >= texsrc->h) { colours[tmp] = thiscol; } @@ -193,21 +181,18 @@ void GRRLIB_BMFX_Blur (const GRRLIB_texImg *texsrc, */ void GRRLIB_BMFX_Scatter (const GRRLIB_texImg *texsrc, GRRLIB_texImg *texdest, const u32 factor) { - unsigned int x, y; - u32 val1, val2; - u32 val3, val4; - int factorx2 = factor*2; + const int factorx2 = factor * 2; - for (y = 0; y < texsrc->h; y++) { - for (x = 0; x < texsrc->w; x++) { - val1 = x + (int) (factorx2 * (rand() / (RAND_MAX + 1.0))) - factor; - val2 = y + (int) (factorx2 * (rand() / (RAND_MAX + 1.0))) - factor; + for (u32 y = 0; y < texsrc->h; y++) { + for (u32 x = 0; x < texsrc->w; x++) { + const u32 val1 = x + (int) (factorx2 * (rand() / (RAND_MAX + 1.0))) - factor; + const u32 val2 = y + (int) (factorx2 * (rand() / (RAND_MAX + 1.0))) - factor; if ((val1 >= texsrc->w) || (val2 >= texsrc->h)) { } else { - val3 = GRRLIB_GetPixelFromtexImg(x, y, texsrc); - val4 = GRRLIB_GetPixelFromtexImg(val1, val2, texsrc); + const u32 val3 = GRRLIB_GetPixelFromtexImg(x, y, texsrc); + const u32 val4 = GRRLIB_GetPixelFromtexImg(val1, val2, texsrc); GRRLIB_SetPixelTotexImg(x, y, texdest, val4); GRRLIB_SetPixelTotexImg(val1, val2, texdest, val3); } @@ -224,15 +209,11 @@ void GRRLIB_BMFX_Scatter (const GRRLIB_texImg *texsrc, */ void GRRLIB_BMFX_Pixelate (const GRRLIB_texImg *texsrc, GRRLIB_texImg *texdest, const u32 factor) { - unsigned int x, y; - unsigned int xx, yy; - u32 rgb; - - for (x = 0; x < texsrc->w - 1 - factor; x += factor) { - for (y = 0; y < texsrc->h - 1 - factor; y +=factor) { - rgb = GRRLIB_GetPixelFromtexImg(x, y, texsrc); - for (xx = x; xx < x + factor; xx++) { - for (yy = y; yy < y + factor; yy++) { + for (u32 x = 0; x < texsrc->w - 1 - factor; x += factor) { + for (u32 y = 0; y < texsrc->h - 1 - factor; y +=factor) { + const u32 rgb = GRRLIB_GetPixelFromtexImg(x, y, texsrc); + for (u32 xx = x; xx < x + factor; xx++) { + for (u32 yy = y; yy < y + factor; yy++) { GRRLIB_SetPixelTotexImg(xx, yy, texdest, rgb); } } diff --git a/GRRLIB/GRRLIB/grrlib/GRRLIB_texSetup.h b/GRRLIB/GRRLIB/grrlib/GRRLIB_texSetup.h index bc10928..e4accc7 100644 --- a/GRRLIB/GRRLIB/grrlib/GRRLIB_texSetup.h +++ b/GRRLIB/GRRLIB/grrlib/GRRLIB_texSetup.h @@ -46,12 +46,13 @@ void GRRLIB_FlushTex (GRRLIB_texImg *tex) { */ INLINE void GRRLIB_FreeTexture (GRRLIB_texImg *tex) { - if(tex != NULL) { - if (tex->data != NULL) { - free(tex->data); - } - free(tex); + if(tex == NULL) { + return; } + if (tex->data != NULL) { + free(tex->data); + } + free(tex); } /** diff --git a/examples/3D_sample1/source/main.c b/examples/3D_sample1/source/main.c index a4f3206..f7e37cb 100644 --- a/examples/3D_sample1/source/main.c +++ b/examples/3D_sample1/source/main.c @@ -12,9 +12,9 @@ #include "font_png.h" int main() { - float a=0; - u32 col[3] = {0xFFFFFFFF, 0xAAAAAAFF, 0x666666FF}; - int cubeZ=0; + float a = 0; + const u32 col[3] = {0xFFFFFFFF, 0xAAAAAAFF, 0x666666FF}; + int cubeZ = 0; GRRLIB_Init(); WPAD_Init(); diff --git a/examples/gamecube/3D_sample1/source/main.c b/examples/gamecube/3D_sample1/source/main.c index 810ea75..2dae1ad 100644 --- a/examples/gamecube/3D_sample1/source/main.c +++ b/examples/gamecube/3D_sample1/source/main.c @@ -12,9 +12,9 @@ #include "font_png.h" int main() { - float a=0; - u32 col[3] = {0xFFFFFFFF, 0xAAAAAAFF, 0x666666FF}; - int cubeZ=0; + float a = 0; + const u32 col[3] = {0xFFFFFFFF, 0xAAAAAAFF, 0x666666FF}; + int cubeZ = 0; GRRLIB_Init(); PAD_Init();