Pārlūkot izejas kodu

Enforce init makros

Kajetan Johannes Hammerle 2 nedēļas atpakaļ
vecāks
revīzija
878b975652

+ 1 - 0
include/core/Box.h

@@ -8,6 +8,7 @@ typedef struct {
     CoreVector3 max;
 } CoreBox;
 
+#define CORE_BOX ((CoreBox){0})
 CoreBox* coreSetBox(CoreBox* box, const CoreVector3* size);
 CoreBox* coreOffsetBox(CoreBox* box, const CoreVector3* offset);
 bool coreCollidesWithBox(const CoreBox* box, const CoreBox* other);

+ 7 - 0
include/core/Vector.h

@@ -53,4 +53,11 @@ CORE_DEFINE_VECTOR_CONVERSION(CoreVector2, V2, CoreIntVector2, IV2);
 CORE_DEFINE_VECTOR_CONVERSION(CoreVector3, V3, CoreIntVector3, IV3);
 CORE_DEFINE_VECTOR_CONVERSION(CoreVector4, V4, CoreIntVector4, IV4);
 
+#define CORE_VECTOR2 ((CoreVector2){0})
+#define CORE_VECTOR3 ((CoreVector3){0})
+#define CORE_VECTOR4 ((CoreVector4){0})
+#define CORE_INT_VECTOR2 ((CoreIntVector2){0})
+#define CORE_INT_VECTOR3 ((CoreIntVector3){0})
+#define CORE_INT_VECTOR4 ((CoreIntVector4){0})
+
 #endif

+ 3 - 3
src/Box.c

@@ -42,11 +42,11 @@ CoreBox* coreExpandBox(CoreBox* box, const CoreVector3* offset) {
 }
 
 CoreBox* coreGrowBox(CoreBox* box, const CoreVector3* growth) {
-    CoreVector3 half = {0};
+    CoreVector3 half = CORE_VECTOR3;
     coreMulV3F(&half, growth, 0.5f);
-    CoreVector3 nMin = {0};
+    CoreVector3 nMin = CORE_VECTOR3;
     coreSubV3(&nMin, &box->min, &half);
-    CoreVector3 nMax = {0};
+    CoreVector3 nMax = CORE_VECTOR3;
     coreAddV3(&nMax, &box->max, &half);
     for(size_t i = 0; i < 3; i++) {
         if(nMin.data[i] > nMax.data[i]) {

+ 2 - 2
src/Frustum.c

@@ -5,13 +5,13 @@
 
 #include "core/Utility.h"
 
-#define CV30 (&(CoreVector3){0})
+#define CV30 (&CORE_VECTOR3)
 #define CV4(a, b, c, d) ((CoreVector4){{a, b, c, d}})
 
 CoreFrustum* coreInitFrustum(CoreFrustum* f, float fieldOfView, float nearClip,
                              float farClip) {
     for(size_t i = 0; i < CORE_ARRAY_LENGTH(f->planes); i++) {
-        f->planes[i] = (CorePlane){0};
+        f->planes[i] = CORE_PLANE;
     }
     f->tan = tanf(coreDegreeToRadian(fieldOfView) * 0.5f);
     f->nearClip = nearClip;

+ 3 - 3
src/Matrix.c

@@ -6,7 +6,7 @@
 #include "core/Utility.h"
 
 CoreMatrix* coreTransposeMatrix(CoreMatrix* m) {
-    CoreMatrix c = {0};
+    CoreMatrix c = CORE_ZERO_MATRIX;
     for(size_t x = 0; x < 4; x++) {
         for(size_t y = 0; y < 4; y++) {
             c.data[x].data[y] = m->data[y].data[x];
@@ -16,11 +16,11 @@ CoreMatrix* coreTransposeMatrix(CoreMatrix* m) {
     return m;
 }
 
-#define CV40 (&(CoreVector4){0})
+#define CV40 (&CORE_VECTOR4)
 
 CoreMatrix* coreMulSetMatrix(CoreMatrix* m, const CoreMatrix* a) {
     for(int i = 0; i < 4; i++) {
-        CoreVector4 d = {0};
+        CoreVector4 d = CORE_VECTOR4;
         coreAddSetV4(&d, coreMulV4F(CV40, a->data + 0, m->data[i].data[0]));
         coreAddSetV4(&d, coreMulV4F(CV40, a->data + 1, m->data[i].data[1]));
         coreAddSetV4(&d, coreMulV4F(CV40, a->data + 2, m->data[i].data[2]));

+ 1 - 1
src/Plane.c

@@ -2,7 +2,7 @@
 
 #include <stdio.h>
 
-#define CV30 (&(CoreVector3){0})
+#define CV30 (&CORE_VECTOR3)
 
 CorePlane* coreInitPlane(CorePlane* p, const CoreVector3* a,
                          const CoreVector3* b, const CoreVector3* c) {

+ 1 - 1
src/Quaternion.c

@@ -5,7 +5,7 @@
 
 #include "core/Utility.h"
 
-#define CV30 (&(CoreVector3){0})
+#define CV30 (&CORE_VECTOR3)
 
 CoreQuaternion* coreAxisAngleQ(CoreQuaternion* q, const CoreVector3* axis,
                                float angle) {

+ 1 - 1
src/SpinLock.c

@@ -12,7 +12,7 @@ void coreLockSpinLock(CoreSpinLock* l) {
         if(atomic_compare_exchange_weak(&l->lock, &expected, true)) {
             break;
         }
-        struct timespec s = {0};
+        struct timespec s = {.tv_nsec = 0, .tv_sec = 0};
         thrd_sleep(&s, nullptr);
     }
 }

+ 7 - 7
test/modules/BoxTests.c

@@ -4,7 +4,7 @@
 #define CV3(a, b, c) (&(CoreVector3){{a, b, c}})
 
 static void testInit() {
-    CoreBox box = {0};
+    CoreBox box = CORE_BOX;
     coreSetBox(&box, CV3(1.0f, 2.0f, 3.0f));
     char buffer[128];
     coreToStringBox(&box, buffer, sizeof(buffer));
@@ -26,7 +26,7 @@ static void testInit() {
 }
 
 static void testOffset() {
-    CoreBox box = {0};
+    CoreBox box = CORE_BOX;
     coreSetBox(&box, CV3(1.0f, 2.0f, 3.0f));
     coreOffsetBox(&box, CV3(7.0f, -4.0f, 6.0f));
     char buffer[128];
@@ -36,11 +36,11 @@ static void testOffset() {
 }
 
 static void testCollidesWith() {
-    CoreBox boxA = {0};
+    CoreBox boxA = CORE_BOX;
     coreSetBox(&boxA, CV3(1.0f, 2.0f, 3.0f));
-    CoreBox boxB = {0};
+    CoreBox boxB = CORE_BOX;
     coreSetBox(&boxB, CV3(-1.0f, -2.0f, -3.0f));
-    CoreBox boxC = {0};
+    CoreBox boxC = CORE_BOX;
     coreSetBox(&boxC, CV3(2.0f, 2.0f, 2.0f));
     coreOffsetBox(&boxC, CV3(-1.0f, -1.0f, -1.0f));
 
@@ -53,7 +53,7 @@ static void testCollidesWith() {
 }
 
 static void testExpand() {
-    CoreBox box = {0};
+    CoreBox box = CORE_BOX;
     coreSetBox(&box, CV3(1.0f, 2.0f, 3.0f));
     coreExpandBox(&box, CV3(7.0f, -4.0f, 6.0f));
 
@@ -70,7 +70,7 @@ static void testExpand() {
 }
 
 static void testGrow() {
-    CoreBox box = {0};
+    CoreBox box = CORE_BOX;
     coreSetBox(&box, CV3(1.0f, 2.0f, 3.0f));
     coreGrowBox(&box, CV3(4.0f, 2.0f, 6.0f));
     char buffer[128];

+ 4 - 4
test/modules/FrustumTests.c

@@ -4,7 +4,7 @@
 #define CV3(a, b, c) (&(CoreVector3){{a, b, c}})
 
 static void testToString() {
-    CoreFrustum f = {0};
+    CoreFrustum f;
     coreInitFrustum(&f, 60.0f, 0.1f, 1000.0f);
     char buffer[128];
     coreToStringFrustum(&f, buffer, sizeof(buffer));
@@ -14,7 +14,7 @@ static void testToString() {
 
 static void testPointIsInside() {
     CoreIntVector2 size = {{200, 100}};
-    CoreFrustum f = {0};
+    CoreFrustum f;
     coreInitFrustum(&f, 60.0f, 0.1f, 1000.0f);
     coreUpdateFrustumPlanes(&f, CV3(0, 0, 0), CV3(1, 0, 0), CV3(0, 1, 0),
                             CV3(0, 0, 1), &size);
@@ -30,7 +30,7 @@ static void testPointIsInside() {
 
 static void testSphereIsInside() {
     CoreIntVector2 size = {{200, 100}};
-    CoreFrustum f = {0};
+    CoreFrustum f;
     coreInitFrustum(&f, 60.0f, 0.1f, 1000.0f);
     coreUpdateFrustumPlanes(&f, CV3(0, 0, 0), CV3(1, 0, 0), CV3(0, 1, 0),
                             CV3(0, 0, 1), &size);
@@ -53,7 +53,7 @@ static void testSphereIsInside() {
 }
 
 static void testUpdateProjection() {
-    CoreFrustum f = {0};
+    CoreFrustum f;
     coreInitFrustum(&f, 60.0f, 0.1f, 1000.0f);
     const CoreMatrix* m =
         coreUpdateProjection(&f, &(CoreIntVector2){{400, 300}});

+ 2 - 2
test/modules/SpinLockTests.c

@@ -23,7 +23,7 @@ static int incrementMutexCounter(void* p) {
 static void testMutex() {
     i64 n = -coreNanos();
 
-    MutexCounter mc = {0};
+    MutexCounter mc = {.counter = 0};
     mtx_init(&mc.m, mtx_plain);
     thrd_t t[2];
     thrd_create(t + 0, incrementMutexCounter, &mc);
@@ -54,7 +54,7 @@ static int incrementSpinLockCounter(void* p) {
 static void testSpinLock() {
     i64 n = -coreNanos();
 
-    SpinLockCounter sc = {0};
+    SpinLockCounter sc = {.counter = 0};
     coreInitSpinLock(&sc.s);
     thrd_t t[2];
     thrd_create(t + 0, incrementSpinLockCounter, &sc);

+ 2 - 2
test/modules/VectorTests.c

@@ -81,7 +81,7 @@ static void testCross() {
 static void testSetAdd() {
 #define X(T)                                                                   \
     {                                                                          \
-        T v = {0};                                                             \
+        T v = *C##T(0, 0, 0, 0);                                               \
         coreAddSet##T(&v, C##T(1, 2, 3, 4));                                   \
         CORE_TEST_##T(C##T(1, 2, 3, 4), &v);                                   \
         coreAddSet##T(&v, C##T(2, 3, 4, 5));                                   \
@@ -99,7 +99,7 @@ static void testAdd() {
 static void testSetSub() {
 #define X(T)                                                                   \
     {                                                                          \
-        T v = {0};                                                             \
+        T v = *C##T(0, 0, 0, 0);                                               \
         coreSubSet##T(&v, C##T(1, 2, 3, 4));                                   \
         CORE_TEST_##T(C##T(-1, -2, -3, -4), &v);                               \
         coreSubSet##T(&v, C##T(2, 3, 4, 5));                                   \

+ 3 - 3
test/modules/ViewTests.c

@@ -4,7 +4,7 @@
 #define CV3(a, b, c) (&(CoreVector3){{a, b, c}})
 
 static void testFromAngles() {
-    CoreView v = {0};
+    CoreView v = CORE_VIEW;
     coreUpdateDirections(&v, 0.0f, 0.0f);
     CORE_TEST_V3(CV3(0.0f, 1.0f, 0.0f), &v.up);
     CORE_TEST_V3(CV3(0.0f, -1.0f, 0.0f), &v.down);
@@ -15,7 +15,7 @@ static void testFromAngles() {
 }
 
 static void testFromQuaternion() {
-    CoreView v = {0};
+    CoreView v = CORE_VIEW;
     coreUpdateDirectionsQ(&v, &CORE_UNIT_QUATERNION);
     CORE_TEST_V3(CV3(0.0f, 1.0f, 0.0f), &v.up);
     CORE_TEST_V3(CV3(0.0f, -1.0f, 0.0f), &v.down);
@@ -26,7 +26,7 @@ static void testFromQuaternion() {
 }
 
 static void testUpdateMatrix() {
-    CoreView v = {0};
+    CoreView v = CORE_VIEW;
     CoreMatrix* m = coreUpdateMatrix(&v, CV3(1.0f, 2.0f, 3.0f));
 
     char buffer[128];