Fix warnings
This commit is contained in:
166
src/Util.c
166
src/Util.c
@@ -1,8 +1,7 @@
|
||||
// Copyright (C) 2011-2021 Valeriano Alfonso Rodriguez (Kableado)
|
||||
// Copyright (C) 2011-2023 Valeriano Alfonso Rodriguez (Kableado)
|
||||
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "Util.h"
|
||||
@@ -11,7 +10,7 @@
|
||||
// Misc
|
||||
//
|
||||
|
||||
float CosineInterpolation(float f) { return (1.0f - cos(f * Pi)) * 0.5f; }
|
||||
float CosineInterpolation(float f) { return (1.0f - cosf(f * Pi)) * 0.5f; }
|
||||
|
||||
int MinimumInt(int i0, int i1) {
|
||||
if (i1 < i0) {
|
||||
@@ -56,24 +55,24 @@ int Rect_PointInsideAny(TRect r[], int rCount, int x, int y) {
|
||||
// SolveQuadratic
|
||||
//
|
||||
// Solves a Quadratic equation using a, b and c coeficients.
|
||||
int SolveQuadratic(float a, float b, float c, float *Rmin, float *Rmax) {
|
||||
int SolveQuadratic(float a, float b, float c, float *RMin, float *RMax) {
|
||||
float root;
|
||||
float divisor;
|
||||
float b2;
|
||||
b2 = b * b;
|
||||
root = b2 - 4.0 * a * c;
|
||||
root = b2 - 4.0f * a * c;
|
||||
if (root < 0) {
|
||||
// Complex
|
||||
return (0);
|
||||
}
|
||||
divisor = (2.0 * a);
|
||||
if (fabs(divisor) == 0.0f) {
|
||||
divisor = (2.0f * a);
|
||||
if (fabsf(divisor) == 0.0f) {
|
||||
// +inf -inf
|
||||
return (0);
|
||||
}
|
||||
root = sqrtf(root);
|
||||
Rmin[0] = (float)((-b - root) / divisor);
|
||||
Rmax[0] = (float)((-b + root) / divisor);
|
||||
RMin[0] = (float)((-b - root) / divisor);
|
||||
RMax[0] = (float)((-b + root) / divisor);
|
||||
return (1);
|
||||
}
|
||||
|
||||
@@ -89,7 +88,7 @@ float vec2_norm(vec2 v) {
|
||||
}
|
||||
|
||||
void vec2_orthogonalize4(vec2 v) {
|
||||
if (fabs(v[0]) > fabs(v[1])) {
|
||||
if (fabsf(v[0]) > fabsf(v[1])) {
|
||||
if (v[0] >= 0) {
|
||||
v[0] = 1.0f;
|
||||
v[1] = 0.0f;
|
||||
@@ -109,9 +108,9 @@ void vec2_orthogonalize4(vec2 v) {
|
||||
}
|
||||
|
||||
void vec2_orthogonalize8(vec2 v) {
|
||||
float diff = fabs(fabs(v[0]) - fabs(v[1]));
|
||||
float diff = fabsf(fabsf(v[0]) - fabsf(v[1]));
|
||||
if (diff > 0.2f) {
|
||||
if (fabs(v[0]) > fabs(v[1])) {
|
||||
if (fabsf(v[0]) > fabsf(v[1])) {
|
||||
if (v[0] >= 0) {
|
||||
v[0] = 1.0f;
|
||||
v[1] = 0.0f;
|
||||
@@ -143,10 +142,10 @@ void vec2_orthogonalize8(vec2 v) {
|
||||
}
|
||||
|
||||
/////////////////////////////
|
||||
// Intersec_RayUnitCircle
|
||||
// Intersect_RayUnitCircle
|
||||
//
|
||||
// Intersection between a ray and a Unit Circle.
|
||||
int Intersec_RayUnitCircle(vec2 orig, vec2 vel, vec2 center, float *t) {
|
||||
int Intersect_RayUnitCircle(const vec2 orig, const vec2 vel, const vec2 center, float *t) {
|
||||
float a, b, c;
|
||||
float Rmin, Rmax;
|
||||
vec2 distv;
|
||||
@@ -155,8 +154,9 @@ int Intersec_RayUnitCircle(vec2 orig, vec2 vel, vec2 center, float *t) {
|
||||
|
||||
// Check if the collision is even posible
|
||||
qlvel = vec2_dot(vel, vel);
|
||||
if (fabs(qlvel) <= 0.0f)
|
||||
if (fabsf(qlvel) <= 0.0f) {
|
||||
return (0);
|
||||
}
|
||||
vec2_minus(distv, orig, center);
|
||||
qdistv = vec2_dot(distv, distv);
|
||||
|
||||
@@ -178,17 +178,17 @@ int Intersec_RayUnitCircle(vec2 orig, vec2 vel, vec2 center, float *t) {
|
||||
}
|
||||
|
||||
/////////////////////////////
|
||||
// Colision_CircleCircle
|
||||
// Collision_CircleCircle
|
||||
//
|
||||
// Colision point of a circle against another circle.
|
||||
int Colision_CircleCircle(vec2 cir1, float rad1, vec2 vel, vec2 cir2, float rad2, float *t, vec2 n) {
|
||||
// Collision point of a circle against another circle.
|
||||
int Collision_CircleCircle(const vec2 cir1, float ra, const vec2 vel, const vec2 cb, float rb, float *t, vec2 n) {
|
||||
vec2 vel_a, orig_a, cen_a, temp;
|
||||
float rads, invrads;
|
||||
float maxx, minx;
|
||||
float maxy, miny;
|
||||
|
||||
// Check if the collision is even posible
|
||||
rads = rad1 + rad2;
|
||||
rads = ra + rb;
|
||||
minx = cir1[0] - rads;
|
||||
maxx = cir1[0] + rads;
|
||||
if (vel[0] > 0) {
|
||||
@@ -196,7 +196,7 @@ int Colision_CircleCircle(vec2 cir1, float rad1, vec2 vel, vec2 cir2, float rad2
|
||||
} else {
|
||||
minx += vel[0];
|
||||
}
|
||||
if (cir2[0] < minx || cir2[0] > maxx)
|
||||
if (cb[0] < minx || cb[0] > maxx)
|
||||
return (0);
|
||||
miny = cir1[1] - rads;
|
||||
maxy = cir1[1] + rads;
|
||||
@@ -205,18 +205,18 @@ int Colision_CircleCircle(vec2 cir1, float rad1, vec2 vel, vec2 cir2, float rad2
|
||||
} else {
|
||||
miny += vel[1];
|
||||
}
|
||||
if (cir2[1] < miny || cir2[1] > maxy)
|
||||
if (cb[1] < miny || cb[1] > maxy)
|
||||
return (0);
|
||||
|
||||
// Convert to a unit circle vs ray
|
||||
invrads = 1.0f / rads;
|
||||
vec2_scale(vel_a, vel, invrads);
|
||||
vec2_scale(orig_a, cir1, invrads);
|
||||
vec2_scale(cen_a, cir2, invrads);
|
||||
if (Intersec_RayUnitCircle(orig_a, vel_a, cen_a, t)) {
|
||||
vec2_scale(cen_a, cb, invrads);
|
||||
if (Intersect_RayUnitCircle(orig_a, vel_a, cen_a, t)) {
|
||||
// Calculate n
|
||||
vec2_scaleadd(temp, cir1, vel, *t);
|
||||
vec2_minus(n, temp, cir2);
|
||||
vec2_minus(n, temp, cb);
|
||||
vec2_scale(n, n, invrads);
|
||||
return (1);
|
||||
}
|
||||
@@ -226,8 +226,8 @@ int Colision_CircleCircle(vec2 cir1, float rad1, vec2 vel, vec2 cir2, float rad2
|
||||
/////////////////////////////
|
||||
// Intersect_RayEdge
|
||||
//
|
||||
// Intersection between a ray and a edge.
|
||||
int Intersect_RayEdge(vec2 pos, vec2 vel, vec2 norm, vec2 edgePos, float len, float *t) {
|
||||
// Intersection between a ray and an edge.
|
||||
int Intersect_RayEdge(const vec2 pos, const vec2 vel, const vec2 norm, const vec2 edgePos, float len, float *t) {
|
||||
vec2 pos2, intersection, perp, edgePos2;
|
||||
float delta, d1, d2, hLen;
|
||||
|
||||
@@ -264,9 +264,9 @@ int Intersect_RayEdge(vec2 pos, vec2 vel, vec2 norm, vec2 edgePos, float len, fl
|
||||
}
|
||||
|
||||
/////////////////////////////
|
||||
// absmod
|
||||
// AbsMod
|
||||
//
|
||||
int absmod(int v, int d) {
|
||||
int AbsMod(int v, int d) {
|
||||
if (v < 0) {
|
||||
v += d * (((v / d) * (-1)) + 1);
|
||||
return (v);
|
||||
@@ -274,7 +274,7 @@ int absmod(int v, int d) {
|
||||
return (v % d);
|
||||
}
|
||||
}
|
||||
float fabsmod(float v, int d) {
|
||||
float AbsModFloat(float v, int d) {
|
||||
if (v < 0) {
|
||||
v += d * ((((int)(v / d)) * (-1)) + 1);
|
||||
return (v);
|
||||
@@ -299,85 +299,89 @@ int IsBigEndian() {
|
||||
// EndsWith
|
||||
//
|
||||
int EndsWith(char *str, char *suffix) {
|
||||
if (!str || !suffix)
|
||||
if (!str || !suffix) {
|
||||
return 0;
|
||||
int lenStr = strlen(str);
|
||||
int lenSuffix = strlen(suffix);
|
||||
if (lenSuffix > lenStr)
|
||||
}
|
||||
size_t lenStr = strlen(str);
|
||||
size_t lenSuffix = strlen(suffix);
|
||||
if (lenSuffix > lenStr) {
|
||||
return 0;
|
||||
}
|
||||
return strncmp(str + lenStr - lenSuffix, suffix, lenSuffix) == 0;
|
||||
}
|
||||
|
||||
/////////////////////////////
|
||||
// Rand
|
||||
//
|
||||
// (LGC++) + cambio de semilla
|
||||
// (LGC++) + Seed change
|
||||
|
||||
#define __seed_n 30
|
||||
#define __seed_a 30
|
||||
#define __seed_b 5
|
||||
#define __seed_c 10
|
||||
#define __seed_d 15
|
||||
// #define __LGC_a 1664525ul
|
||||
// #define __LGC_c 1013904223ul
|
||||
// #define __LGC_m 4294967296ul
|
||||
#define __LGC_a 16807ul
|
||||
#define __LGC_c 2
|
||||
#define __LGC_m 2147483647ul
|
||||
unsigned __seeds[30];
|
||||
int __seed_i = -1;
|
||||
#define g_LGC_Seed_N 30
|
||||
#define g_LGC_Seed_A 30
|
||||
#define g_LGC_Seed_B 5
|
||||
#define g_LGC_Seed_C 10
|
||||
#define g_LGC_Seed_D 15
|
||||
#define g_LGC_A 16807ul
|
||||
#define g_LGC_C 2
|
||||
#define g_LGC_M 2147483647ul
|
||||
unsigned g_LGC_Seeds[30];
|
||||
int g_LGC_Seed_I = -1;
|
||||
|
||||
unsigned __rand_count;
|
||||
unsigned __rand_orig_seed;
|
||||
unsigned g_RandCount;
|
||||
unsigned g_RandOrigSeed;
|
||||
|
||||
void Rand_Seed(unsigned seed) {
|
||||
int i;
|
||||
__seeds[0] = seed;
|
||||
g_LGC_Seeds[0] = seed;
|
||||
for (i = 1; i < 30; i++) {
|
||||
__seeds[i] = (__seeds[i - 1] * __LGC_a + __LGC_c) % __LGC_m;
|
||||
//__seeds[i]=(__seeds[i-1]*__LGC_a+__LGC_c);
|
||||
g_LGC_Seeds[i] = (g_LGC_Seeds[i - 1] * g_LGC_A + g_LGC_C) % g_LGC_M;
|
||||
}
|
||||
__seed_i = 29;
|
||||
g_LGC_Seed_I = 29;
|
||||
|
||||
// Cambio de semilla
|
||||
__rand_count = 0;
|
||||
__rand_orig_seed = seed;
|
||||
g_RandCount = 0;
|
||||
g_RandOrigSeed = seed;
|
||||
}
|
||||
|
||||
unsigned Rand_Get() {
|
||||
unsigned val;
|
||||
int a, b, c, d;
|
||||
|
||||
if (__seed_i == -1) {
|
||||
if (g_LGC_Seed_I == -1) {
|
||||
Rand_Seed(1);
|
||||
}
|
||||
|
||||
a = __seed_i - __seed_a;
|
||||
if (a < 0)
|
||||
a += __seed_n;
|
||||
b = __seed_i - __seed_b;
|
||||
if (b < 0)
|
||||
b += __seed_n;
|
||||
c = __seed_i - __seed_c;
|
||||
if (c < 0)
|
||||
c += __seed_n;
|
||||
d = __seed_i - __seed_d;
|
||||
if (d < 0)
|
||||
d += __seed_n;
|
||||
val = __seeds[a] ^ __seeds[b] ^ __seeds[c] ^ __seeds[d];
|
||||
a = g_LGC_Seed_I - g_LGC_Seed_A;
|
||||
if (a < 0) {
|
||||
a += g_LGC_Seed_N;
|
||||
}
|
||||
b = g_LGC_Seed_I - g_LGC_Seed_B;
|
||||
if (b < 0) {
|
||||
b += g_LGC_Seed_N;
|
||||
}
|
||||
c = g_LGC_Seed_I - g_LGC_Seed_C;
|
||||
if (c < 0) {
|
||||
c += g_LGC_Seed_N;
|
||||
}
|
||||
d = g_LGC_Seed_I - g_LGC_Seed_D;
|
||||
if (d < 0) {
|
||||
d += g_LGC_Seed_N;
|
||||
}
|
||||
val = g_LGC_Seeds[a] ^ g_LGC_Seeds[b] ^ g_LGC_Seeds[c] ^ g_LGC_Seeds[d];
|
||||
|
||||
a = __seed_i - 1;
|
||||
if (a < 0)
|
||||
a = __seed_n - 1;
|
||||
__seeds[__seed_i] = (__seeds[a] * __LGC_a + __LGC_c) % __LGC_m;
|
||||
//__seeds[__seed_i]=(__seeds[a]*__LGC_a+__LGC_c);
|
||||
__seed_i++;
|
||||
if (__seed_i == __seed_n)
|
||||
__seed_i = 0;
|
||||
a = g_LGC_Seed_I - 1;
|
||||
if (a < 0) {
|
||||
a = g_LGC_Seed_N - 1;
|
||||
}
|
||||
g_LGC_Seeds[g_LGC_Seed_I] = (g_LGC_Seeds[a] * g_LGC_A + g_LGC_C) % g_LGC_M;
|
||||
g_LGC_Seed_I++;
|
||||
if (g_LGC_Seed_I == g_LGC_Seed_N) {
|
||||
g_LGC_Seed_I = 0;
|
||||
}
|
||||
|
||||
// Cambio de semilla
|
||||
__rand_count++;
|
||||
if (__rand_count > (1 << 15)) {
|
||||
Rand_Seed(__rand_orig_seed + 1);
|
||||
g_RandCount++;
|
||||
if (g_RandCount > (1 << 15)) {
|
||||
Rand_Seed(g_RandOrigSeed + 1);
|
||||
}
|
||||
|
||||
return (val);
|
||||
@@ -393,7 +397,7 @@ unsigned Rand_GetBetween(int min, int max) {
|
||||
/////////////////////////////
|
||||
// Print
|
||||
//
|
||||
// Prints the formated text
|
||||
// Prints the formatted text
|
||||
int Print(char *fmt, ...) {
|
||||
va_list ap;
|
||||
int n;
|
||||
|
||||
Reference in New Issue
Block a user