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.
19 #include <SDL_endian.h>
23 /*---------------------------------------------------------------------------*/
25 void put_float(fs_file fout, const float *f)
27 const unsigned char *p = (const unsigned char *) f;
29 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
30 fs_putc((int) p[3], fout);
31 fs_putc((int) p[2], fout);
32 fs_putc((int) p[1], fout);
33 fs_putc((int) p[0], fout);
35 fs_putc((int) p[0], fout);
36 fs_putc((int) p[1], fout);
37 fs_putc((int) p[2], fout);
38 fs_putc((int) p[3], fout);
42 void put_index(fs_file fout, const int *i)
44 const unsigned char *p = (const unsigned char *) i;
46 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
47 fs_putc((int) p[3], fout);
48 fs_putc((int) p[2], fout);
49 fs_putc((int) p[1], fout);
50 fs_putc((int) p[0], fout);
52 fs_putc((int) p[0], fout);
53 fs_putc((int) p[1], fout);
54 fs_putc((int) p[2], fout);
55 fs_putc((int) p[3], fout);
59 void put_short(fs_file fout, const short *s)
61 const unsigned char *p = (const unsigned char *) s;
63 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
64 fs_putc((int) p[1], fout);
65 fs_putc((int) p[0], fout);
67 fs_putc((int) p[0], fout);
68 fs_putc((int) p[1], fout);
72 void put_array(fs_file fout, const float *v, size_t n)
76 for (i = 0; i < n; i++)
77 put_float(fout, v + i);
80 /*---------------------------------------------------------------------------*/
82 void get_float(fs_file fin, float *f)
84 unsigned char *p = (unsigned char *) f;
86 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
87 p[3] = (unsigned char) fs_getc(fin);
88 p[2] = (unsigned char) fs_getc(fin);
89 p[1] = (unsigned char) fs_getc(fin);
90 p[0] = (unsigned char) fs_getc(fin);
92 p[0] = (unsigned char) fs_getc(fin);
93 p[1] = (unsigned char) fs_getc(fin);
94 p[2] = (unsigned char) fs_getc(fin);
95 p[3] = (unsigned char) fs_getc(fin);
99 void get_index(fs_file fin, int *i)
101 unsigned char *p = (unsigned char *) i;
103 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
104 p[3] = (unsigned char) fs_getc(fin);
105 p[2] = (unsigned char) fs_getc(fin);
106 p[1] = (unsigned char) fs_getc(fin);
107 p[0] = (unsigned char) fs_getc(fin);
109 p[0] = (unsigned char) fs_getc(fin);
110 p[1] = (unsigned char) fs_getc(fin);
111 p[2] = (unsigned char) fs_getc(fin);
112 p[3] = (unsigned char) fs_getc(fin);
116 void get_short(fs_file fin, short *s)
118 unsigned char *p = (unsigned char *) s;
120 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
121 p[1] = (unsigned char) fs_getc(fin);
122 p[0] = (unsigned char) fs_getc(fin);
124 p[0] = (unsigned char) fs_getc(fin);
125 p[1] = (unsigned char) fs_getc(fin);
129 void get_array(fs_file fin, float *v, size_t n)
133 for (i = 0; i < n; i++)
134 get_float(fin, v + i);
137 /*---------------------------------------------------------------------------*/
139 void put_string(fs_file fout, const char *s)
145 void get_string(fs_file fin, char *s, int max)
149 while ((c = fs_getc(fin)) >= 0)
156 /* Terminate the string, but keep reading until NUL. */
167 /*---------------------------------------------------------------------------*/