Fix redundant glTexEnv calls
[neverball] / share / binary.c
1 /*
2  * Copyright (C) 2003 Robert Kooima
3  *
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.
8  *
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.
13  */
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18
19 #include <SDL_endian.h>
20
21 #include "fs.h"
22
23 /*---------------------------------------------------------------------------*/
24
25 void put_float(fs_file fout, float f)
26 {
27     unsigned char *p = (unsigned char *) &f;
28
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);
34 #else
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);
39 #endif
40 }
41
42 void put_index(fs_file fout, int i)
43 {
44     unsigned char *p = (unsigned char *) &i;
45
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);
51 #else
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);
56 #endif
57 }
58
59 void put_short(fs_file fout, short s)
60 {
61     unsigned char *p = (unsigned char *) &s;
62
63 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
64     fs_putc((int) p[1], fout);
65     fs_putc((int) p[0], fout);
66 #else
67     fs_putc((int) p[0], fout);
68     fs_putc((int) p[1], fout);
69 #endif
70 }
71
72 void put_array(fs_file fout, const float *v, size_t n)
73 {
74     size_t i;
75
76     for (i = 0; i < n; i++)
77         put_float(fout, v[i]);
78 }
79
80 /*---------------------------------------------------------------------------*/
81
82 void get_float(fs_file fin, float *f)
83 {
84     unsigned char *p = (unsigned char *) f;
85
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);
91 #else
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);
96 #endif
97 }
98
99 void get_index(fs_file fin, int *i)
100 {
101     unsigned char *p = (unsigned char *) i;
102
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);
108 #else
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);
113 #endif
114 }
115
116 void get_short(fs_file fin, short *s)
117 {
118     unsigned char *p = (unsigned char *) s;
119
120 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
121     p[1] = (unsigned char) fs_getc(fin);
122     p[0] = (unsigned char) fs_getc(fin);
123 #else
124     p[0] = (unsigned char) fs_getc(fin);
125     p[1] = (unsigned char) fs_getc(fin);
126 #endif
127 }
128
129 void get_array(fs_file fin, float *v, size_t n)
130 {
131     size_t i;
132
133     for (i = 0; i < n; i++)
134         get_float(fin, v + i);
135 }
136
137 /*---------------------------------------------------------------------------*/
138
139 void put_string(fs_file fout, const char *s)
140 {
141     fs_puts(s, fout);
142     fs_putc('\0', fout);
143 }
144
145 void get_string(fs_file fin, char *s, int max)
146 {
147     int c;
148
149     while ((c = fs_getc(fin)) >= 0)
150     {
151         if (max > 0)
152         {
153             *s++ = c;
154             max--;
155
156             /* Terminate the string, but keep reading until NUL. */
157
158             if (max == 0)
159                 *(s - 1) = 0;
160         }
161
162         if (c == 0)
163             break;
164     }
165 }
166
167 /*---------------------------------------------------------------------------*/