mojoshader_common.c
changeset 1150 02c0f0afb39a
parent 1146 31337471b909
child 1153 663c9541e21f
--- a/mojoshader_common.c	Tue Oct 13 12:08:55 2015 -0400
+++ b/mojoshader_common.c	Fri Jan 01 02:12:19 2016 -0500
@@ -3,8 +3,8 @@
 
 // Convenience functions for allocators...
 #if !MOJOSHADER_FORCE_ALLOCATOR
-void *MOJOSHADER_internal_malloc(int bytes, void *d) { return malloc(bytes); }
-void MOJOSHADER_internal_free(void *ptr, void *d) { free(ptr); }
+void * MOJOSHADERCALL MOJOSHADER_internal_malloc(int bytes, void *d) { return malloc(bytes); }
+void MOJOSHADERCALL MOJOSHADER_internal_free(void *ptr, void *d) { free(ptr); }
 #endif
 
 MOJOSHADER_error MOJOSHADER_out_of_mem_error = {
@@ -78,7 +78,7 @@
 int hash_iter(const HashTable *table, const void *key,
               const void **_value, void **iter)
 {
-    HashItem *item = *iter;
+    HashItem *item = (HashItem *) *iter;
     if (item == NULL)
         item = table->table[calc_hash(table, key)];
     else
@@ -103,7 +103,7 @@
 
 int hash_iter_keys(const HashTable *table, const void **_key, void **iter)
 {
-    HashItem *item = *iter;
+    HashItem *item = (HashItem *) *iter;
     int idx = 0;
 
     if (item != NULL)
@@ -1011,5 +1011,73 @@
     return -1;  // no match found.
 } // buffer_find
 
+
+// Based on SDL_string.c's SDL_PrintFloat function
+size_t MOJOSHADER_printFloat(char *text, size_t maxlen, float arg)
+{
+    size_t len;
+    size_t left = maxlen;
+    char *textstart = text;
+
+    int precision = 9;
+
+    if (arg)
+    {
+        /* This isn't especially accurate, but hey, it's easy. :) */
+        unsigned long value;
+
+        if (arg < 0)
+        {
+            if (left > 1)
+            {
+                *text = '-';
+                --left;
+            } // if
+            ++text;
+            arg = -arg;
+        } // if
+        value = (unsigned long) arg;
+        len = snprintf(text, left, "%lu", value);
+        text += len;
+        if (len >= left)
+            left = (left < 1) ? left : 1;
+        else
+            left -= len;
+        arg -= value;
+
+        int mult = 10;
+        if (left > 1)
+        {
+            *text = '.';
+            --left;
+        } // if
+        ++text;
+        while (precision-- > 0)
+        {
+            value = (unsigned long) (arg * mult);
+            len = snprintf(text, left, "%lu", value);
+            text += len;
+            if (len >= left)
+                left = (left < 1) ? left : 1;
+            else
+                left -= len;
+            arg -= (double) value / mult;
+            if (arg < 0) arg = -arg; // Sometimes that bit gets flipped...
+            mult *= 10;
+        } // while
+    } // if
+    else
+    {
+        if (left > 3)
+        {
+            snprintf(text, left, "0.0");
+            left -= 3;
+        } // if
+        text += 3;
+    } // else
+
+    return (text - textstart);
+} // MOJOSHADER_printFloat
+
 // end of mojoshader_common.c ...