Import from neverball-1.3.1.tar.gz
[neverball-archive.git] / share / binary.c
blob33308ff43ba648c16b00af3610acb363a4991f81
1 /*
2 * Copyright (C) 2003 Robert Kooima
4 * NEVERBALL is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published
6 * by the Free Software Foundation; either version 2 of the License,
7 * or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
15 #include <stdio.h>
16 #include <stdlib.h>
18 #include <SDL.h>
19 #include <SDL_byteorder.h>
21 /*---------------------------------------------------------------------------*/
23 void put_float(FILE *fout, const float *f)
25 unsigned char *p = (unsigned char *) f;
27 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
28 fputc((int) p[3], fout);
29 fputc((int) p[2], fout);
30 fputc((int) p[1], fout);
31 fputc((int) p[0], fout);
32 #else
33 fputc((int) p[0], fout);
34 fputc((int) p[1], fout);
35 fputc((int) p[2], fout);
36 fputc((int) p[3], fout);
37 #endif
40 void put_int(FILE *fout, const int *i)
42 unsigned char *p = (unsigned char *) i;
44 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
45 fputc((int) p[3], fout);
46 fputc((int) p[2], fout);
47 fputc((int) p[1], fout);
48 fputc((int) p[0], fout);
49 #else
50 fputc((int) p[0], fout);
51 fputc((int) p[1], fout);
52 fputc((int) p[2], fout);
53 fputc((int) p[3], fout);
54 #endif
57 void put_short(FILE *fout, const short *s)
59 unsigned char *p = (unsigned char *) s;
61 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
62 fputc((int) p[1], fout);
63 fputc((int) p[0], fout);
64 #else
65 fputc((int) p[0], fout);
66 fputc((int) p[1], fout);
67 #endif
70 void put_array(FILE *fout, const float *v, size_t n)
72 size_t i;
74 for (i = 0; i < n; i++)
75 put_float(fout, v + i);
78 /*---------------------------------------------------------------------------*/
80 void get_float(FILE *fin, float *f)
82 unsigned char *p = (unsigned char *) f;
84 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
85 p[3] = (unsigned char) fgetc(fin);
86 p[2] = (unsigned char) fgetc(fin);
87 p[1] = (unsigned char) fgetc(fin);
88 p[0] = (unsigned char) fgetc(fin);
89 #else
90 p[0] = (unsigned char) fgetc(fin);
91 p[1] = (unsigned char) fgetc(fin);
92 p[2] = (unsigned char) fgetc(fin);
93 p[3] = (unsigned char) fgetc(fin);
94 #endif
97 void get_int(FILE *fin, int *i)
99 unsigned char *p = (unsigned char *) i;
101 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
102 p[3] = (unsigned char) fgetc(fin);
103 p[2] = (unsigned char) fgetc(fin);
104 p[1] = (unsigned char) fgetc(fin);
105 p[0] = (unsigned char) fgetc(fin);
106 #else
107 p[0] = (unsigned char) fgetc(fin);
108 p[1] = (unsigned char) fgetc(fin);
109 p[2] = (unsigned char) fgetc(fin);
110 p[3] = (unsigned char) fgetc(fin);
111 #endif
114 void get_short(FILE *fin, short *s)
116 unsigned char *p = (unsigned char *) s;
118 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
119 p[1] = (unsigned char) fgetc(fin);
120 p[0] = (unsigned char) fgetc(fin);
121 #else
122 p[0] = (unsigned char) fgetc(fin);
123 p[1] = (unsigned char) fgetc(fin);
124 #endif
127 void get_array(FILE *fin, float *v, size_t n)
129 size_t i;
131 for (i = 0; i < n; i++)
132 get_float(fin, v + i);
135 /*---------------------------------------------------------------------------*/