Correct logic of BSP back/front tests
[neverball] / share / binary.c
index 683f59e..1ada5ed 100644 (file)
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 
-#include <SDL.h>
-#include <SDL_byteorder.h>
+#include <SDL_endian.h>
 
 /*---------------------------------------------------------------------------*/
 
 void put_float(FILE *fout, const float *f)
 {
-    unsigned char *p = (unsigned char *) f;
+    const unsigned char *p = (const unsigned char *) f;
 
 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
     fputc((int) p[3], fout);
@@ -39,7 +39,7 @@ void put_float(FILE *fout, const float *f)
 
 void put_index(FILE *fout, const int *i)
 {
-    unsigned char *p = (unsigned char *) i;
+    const unsigned char *p = (const unsigned char *) i;
 
 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
     fputc((int) p[3], fout);
@@ -54,6 +54,19 @@ void put_index(FILE *fout, const int *i)
 #endif
 }
 
+void put_short(FILE *fout, const short *s)
+{
+    const unsigned char *p = (const unsigned char *) s;
+
+#if SDL_BYTEORDER == SDL_BIG_ENDIAN
+    fputc((int) p[1], fout);
+    fputc((int) p[0], fout);
+#else
+    fputc((int) p[0], fout);
+    fputc((int) p[1], fout);
+#endif
+}
+
 void put_array(FILE *fout, const float *v, size_t n)
 {
     size_t i;
@@ -98,6 +111,19 @@ void get_index(FILE *fin, int *i)
 #endif
 }
 
+void get_short(FILE *fin, short *s)
+{
+    unsigned char *p = (unsigned char *) s;
+
+#if SDL_BYTEORDER == SDL_BIG_ENDIAN
+    p[1] = (unsigned char) fgetc(fin);
+    p[0] = (unsigned char) fgetc(fin);
+#else
+    p[0] = (unsigned char) fgetc(fin);
+    p[1] = (unsigned char) fgetc(fin);
+#endif
+}
+
 void get_array(FILE *fin, float *v, size_t n)
 {
     size_t i;
@@ -107,3 +133,21 @@ void get_array(FILE *fin, float *v, size_t n)
 }
 
 /*---------------------------------------------------------------------------*/
+
+void put_string(FILE *fout, const char *s)
+{
+    fputs(s, fout);
+    fputc('\0', fout);
+}
+
+void get_string(FILE *fin, char *s, int max)
+{
+    do
+        *s = (char) fgetc(fin);
+    while (*s++ != '\0' && max-- > 0);
+
+    if(*(s - 1) != '\0')
+        *(s - 1) = '\0';
+}
+
+/*---------------------------------------------------------------------------*/