Use proper types and reduce variables scope

This commit is contained in:
Crayon2000 2022-11-23 22:27:38 -05:00
parent 423ff0881f
commit 95c915f5f8
5 changed files with 80 additions and 98 deletions

View file

@ -459,16 +459,16 @@ void GRRLIB_DrawSphere(f32 r, int lats, int longs, bool filled, u32 col) {
* @param col Color of the cube. * @param col Color of the cube.
*/ */
void GRRLIB_DrawCube(f32 size, bool filled, u32 col) { 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}, {-1.0f, 0.0f, 0.0f},
{0.0, 1.0, 0.0}, {0.0f, 1.0f, 0.0f},
{1.0, 0.0, 0.0}, {1.0f, 0.0f, 0.0f},
{0.0, -1.0, 0.0}, {0.0f, -1.0f, 0.0f},
{0.0, 0.0, 1.0}, {0.0f, 0.0f, 1.0f},
{0.0, 0.0, -1.0} {0.0f, 0.0f, -1.0f}
}; };
static int faces[6][4] = static const u8 faces[6][4] =
{ {
{0, 1, 2, 3}, {0, 1, 2, 3},
{3, 2, 6, 7}, {3, 2, 6, 7},
@ -479,12 +479,12 @@ void GRRLIB_DrawCube(f32 size, bool filled, u32 col) {
}; };
f32 v[8][3]; f32 v[8][3];
v[0][0] = v[1][0] = v[2][0] = v[3][0] = -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; 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; 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; 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; 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; v[1][2] = v[2][2] = v[5][2] = v[6][2] = size / 2.0f;
for (s8 i = 5; i >= 0; i--) { for (s8 i = 5; i >= 0; i--) {
if(filled == true) { if(filled == true) {
@ -494,20 +494,20 @@ void GRRLIB_DrawCube(f32 size, bool filled, u32 col) {
GX_Begin(GX_LINESTRIP, GX_VTXFMT0, 5); 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_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_Color1u32(col);
GX_Position3f32(v[faces[i][1]][0], v[faces[i][1]][1], v[faces[i][1]][2]); 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_Color1u32(col);
GX_Position3f32(v[faces[i][2]][0], v[faces[i][2]][1], v[faces[i][2]][2]); 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_Color1u32(col);
GX_Position3f32(v[faces[i][3]][0], v[faces[i][3]][1], v[faces[i][3]][2]); 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); GX_Color1u32(col);
if(filled == false) { if(filled == false) {
GX_Position3f32(v[faces[i][0]][0], v[faces[i][0]][1], v[faces[i][0]][2]); 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_Color1u32(col);
} }
GX_End(); GX_End();

View file

@ -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 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
@ -31,10 +31,10 @@ THE SOFTWARE.
* @param texdest The texture destination. * @param texdest The texture destination.
*/ */
void GRRLIB_BMFX_FlipH (const GRRLIB_texImg *texsrc, GRRLIB_texImg *texdest) { 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 (u32 y = 0; y < texsrc->h; y++) {
for (x = 0; x < texsrc->w; x++) { for (u32 x = 0; x < texsrc->w; x++) {
GRRLIB_SetPixelTotexImg(txtWidth - x, y, texdest, GRRLIB_SetPixelTotexImg(txtWidth - x, y, texdest,
GRRLIB_GetPixelFromtexImg(x, y, texsrc)); 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. * @param texdest The texture destination.
*/ */
void GRRLIB_BMFX_FlipV (const GRRLIB_texImg *texsrc, GRRLIB_texImg *texdest) { 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 (u32 y = 0; y < texsrc->h; y++) {
for (x = 0; x < texsrc->w; x++) { for (u32 x = 0; x < texsrc->w; x++) {
GRRLIB_SetPixelTotexImg(x, texHeight - y, texdest, GRRLIB_SetPixelTotexImg(x, texHeight - y, texdest,
GRRLIB_GetPixelFromtexImg(x, y, texsrc)); 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, void GRRLIB_BMFX_Grayscale (const GRRLIB_texImg *texsrc,
GRRLIB_texImg *texdest) { 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++) { const u8 gray = ((R(color) * 77 +
for (x = 0; x < texsrc->w; x++) { G(color) * 150 +
u32 color = GRRLIB_GetPixelFromtexImg(x, y, texsrc); B(color) * 28 ) / 255);
u8 gray = ((R(color)* 77 +
G(color)*150 +
B(color)* 28 ) / 255);
GRRLIB_SetPixelTotexImg(x, y, texdest, GRRLIB_SetPixelTotexImg(x, y, texdest,
(gray << 24) | (gray << 16) | (gray << 8) | (A(color))); (gray << 24) | (gray << 16) | (gray << 8) | (A(color)));
@ -91,18 +89,16 @@ void GRRLIB_BMFX_Grayscale (const GRRLIB_texImg *texsrc,
* @author elisherer * @author elisherer
*/ */
void GRRLIB_BMFX_Sepia (const GRRLIB_texImg *texsrc, GRRLIB_texImg *texdest) { void GRRLIB_BMFX_Sepia (const GRRLIB_texImg *texsrc, GRRLIB_texImg *texdest) {
unsigned int x, y; for (u32 y = 0; y < texsrc->h; y++) {
u16 sr, sg, sb; for (u32 x = 0; x < texsrc->w; x++) {
u32 color; const u32 color = GRRLIB_GetPixelFromtexImg(x, y, texsrc);
u16 sr = R(color)*0.393 + G(color)*0.769 + B(color)*0.189;
for (y = 0; y < texsrc->h; y++) { u16 sg = R(color)*0.349 + G(color)*0.686 + B(color)*0.168;
for (x = 0; x < texsrc->w; x++) { u16 sb = R(color)*0.272 + G(color)*0.534 + B(color)*0.131;
color = GRRLIB_GetPixelFromtexImg(x, y, texsrc); if (sr > 255)
sr = R(color)*0.393 + G(color)*0.769 + B(color)*0.189; sr = 255;
sg = R(color)*0.349 + G(color)*0.686 + B(color)*0.168; if (sg > 255)
sb = R(color)*0.272 + G(color)*0.534 + B(color)*0.131; sg = 255;
if (sr>255) sr=255;
if (sg>255) sg=255;
GRRLIB_SetPixelTotexImg(x, y, texdest, GRRLIB_SetPixelTotexImg(x, y, texdest,
RGBA(sr, sg, sb, A(color))); 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. * @param texdest The texture destination.
*/ */
void GRRLIB_BMFX_Invert (const GRRLIB_texImg *texsrc, GRRLIB_texImg *texdest) { void GRRLIB_BMFX_Invert (const GRRLIB_texImg *texsrc, GRRLIB_texImg *texdest) {
unsigned int x, y; for (u32 y = 0; y < texsrc->h; y++) {
u32 color; 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++) {
color = GRRLIB_GetPixelFromtexImg(x, y, texsrc);
GRRLIB_SetPixelTotexImg(x, y, texdest, GRRLIB_SetPixelTotexImg(x, y, texdest,
((0xFFFFFF - (color >> 8 & 0xFFFFFF)) << 8) | (color & 0xFF)); ((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, void GRRLIB_BMFX_Blur (const GRRLIB_texImg *texsrc,
GRRLIB_texImg *texdest, const u32 factor) { GRRLIB_texImg *texdest, const u32 factor) {
int numba = (1+(factor<<1))*(1+(factor<<1)); const int numba = (1 + (factor << 1)) * (1 + (factor << 1));
u32 x, y;
s32 k, l;
int tmp;
int newr, newg, newb, newa;
u32 colours[numba]; u32 colours[numba];
u32 thiscol;
for (x = 0; x < texsrc->w; x++) { for (u32 x = 0; x < texsrc->w; x++) {
for (y = 0; y < texsrc->h; y++) { for (u32 y = 0; y < texsrc->h; y++) {
newr = 0; int newr = 0;
newg = 0; int newg = 0;
newb = 0; int newb = 0;
newa = 0; int newa = 0;
tmp = 0; int tmp = 0;
thiscol = GRRLIB_GetPixelFromtexImg(x, y, texsrc); const u32 thiscol = GRRLIB_GetPixelFromtexImg(x, y, texsrc);
for (k = x - factor; k <= x + factor; k++) { for (s32 k = x - factor; k <= x + factor; k++) {
for (l = y - factor; l <= y + factor; l++) { for (s32 l = y - factor; l <= y + factor; l++) {
if (k < 0 || k >= texsrc->w || l < 0 || l >= texsrc->h) { if (k < 0 || k >= texsrc->w || l < 0 || l >= texsrc->h) {
colours[tmp] = thiscol; colours[tmp] = thiscol;
} }
@ -193,21 +181,18 @@ void GRRLIB_BMFX_Blur (const GRRLIB_texImg *texsrc,
*/ */
void GRRLIB_BMFX_Scatter (const GRRLIB_texImg *texsrc, void GRRLIB_BMFX_Scatter (const GRRLIB_texImg *texsrc,
GRRLIB_texImg *texdest, const u32 factor) { GRRLIB_texImg *texdest, const u32 factor) {
unsigned int x, y; const int factorx2 = factor * 2;
u32 val1, val2;
u32 val3, val4;
int factorx2 = factor*2;
for (y = 0; y < texsrc->h; y++) { for (u32 y = 0; y < texsrc->h; y++) {
for (x = 0; x < texsrc->w; x++) { for (u32 x = 0; x < texsrc->w; x++) {
val1 = x + (int) (factorx2 * (rand() / (RAND_MAX + 1.0))) - factor; const u32 val1 = x + (int) (factorx2 * (rand() / (RAND_MAX + 1.0))) - factor;
val2 = y + (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)) { if ((val1 >= texsrc->w) || (val2 >= texsrc->h)) {
} }
else { else {
val3 = GRRLIB_GetPixelFromtexImg(x, y, texsrc); const u32 val3 = GRRLIB_GetPixelFromtexImg(x, y, texsrc);
val4 = GRRLIB_GetPixelFromtexImg(val1, val2, texsrc); const u32 val4 = GRRLIB_GetPixelFromtexImg(val1, val2, texsrc);
GRRLIB_SetPixelTotexImg(x, y, texdest, val4); GRRLIB_SetPixelTotexImg(x, y, texdest, val4);
GRRLIB_SetPixelTotexImg(val1, val2, texdest, val3); 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, void GRRLIB_BMFX_Pixelate (const GRRLIB_texImg *texsrc,
GRRLIB_texImg *texdest, const u32 factor) { GRRLIB_texImg *texdest, const u32 factor) {
unsigned int x, y; for (u32 x = 0; x < texsrc->w - 1 - factor; x += factor) {
unsigned int xx, yy; for (u32 y = 0; y < texsrc->h - 1 - factor; y +=factor) {
u32 rgb; const u32 rgb = GRRLIB_GetPixelFromtexImg(x, y, texsrc);
for (u32 xx = x; xx < x + factor; xx++) {
for (x = 0; x < texsrc->w - 1 - factor; x += factor) { for (u32 yy = y; yy < y + factor; yy++) {
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++) {
GRRLIB_SetPixelTotexImg(xx, yy, texdest, rgb); GRRLIB_SetPixelTotexImg(xx, yy, texdest, rgb);
} }
} }

View file

@ -46,12 +46,13 @@ void GRRLIB_FlushTex (GRRLIB_texImg *tex) {
*/ */
INLINE INLINE
void GRRLIB_FreeTexture (GRRLIB_texImg *tex) { void GRRLIB_FreeTexture (GRRLIB_texImg *tex) {
if(tex != NULL) { if(tex == NULL) {
return;
}
if (tex->data != NULL) { if (tex->data != NULL) {
free(tex->data); free(tex->data);
} }
free(tex); free(tex);
}
} }
/** /**

View file

@ -12,9 +12,9 @@
#include "font_png.h" #include "font_png.h"
int main() { int main() {
float a=0; float a = 0;
u32 col[3] = {0xFFFFFFFF, 0xAAAAAAFF, 0x666666FF}; const u32 col[3] = {0xFFFFFFFF, 0xAAAAAAFF, 0x666666FF};
int cubeZ=0; int cubeZ = 0;
GRRLIB_Init(); GRRLIB_Init();
WPAD_Init(); WPAD_Init();

View file

@ -12,9 +12,9 @@
#include "font_png.h" #include "font_png.h"
int main() { int main() {
float a=0; float a = 0;
u32 col[3] = {0xFFFFFFFF, 0xAAAAAAFF, 0x666666FF}; const u32 col[3] = {0xFFFFFFFF, 0xAAAAAAFF, 0x666666FF};
int cubeZ=0; int cubeZ = 0;
GRRLIB_Init(); GRRLIB_Init();
PAD_Init(); PAD_Init();