summary refs log tree commit diff
path: root/src/Serialize.cpp
blob: c07a5db864da76e86bf283c390aa3b5c8048476b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <unistd.h>

#include "Models.h"
#include "Quaternions.h"
#include "Serialize.h"

/* these all read big-endian data */

int ReadBool(int fd, int count, bool *b)
{
	while (count--) {
		unsigned char buf[1];

		if (read(fd, buf, 1) != 1) {
		}

		*b = (buf[0] != 0) ? true : false;

		b++;
	}

	return 1;
}

int ReadShort(int fd, int count, short *s)
{
	while (count--) {
		unsigned char buf[2];

		if (read(fd, buf, 2) != 2) {
		}

		*s = (short)((buf[0] << 8) | buf[1]);

		s++;
	}

	return 1;
}

int ReadInt(int fd, int count, int *s)
{
	while (count--) {
		unsigned char buf[4];

		if (read(fd, buf, 4) != 4) {
		}

		*s = (int)((buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]);

		s++;
	}

	return 1;
}

union intfloat {
	int i;
	float f;
} intfloat;

int ReadFloat(int fd, int count, float *f)
{
	union intfloat infl;

	while (count--) {
		ReadInt(fd, 1, &(infl.i));

		*f = infl.f;

		f++;
	}

	return 1;
}

int ReadXYZ(int fd, int count, XYZ *xyz)
{
	while (count--) {
		ReadFloat(fd, 1, &(xyz->x));
		ReadFloat(fd, 1, &(xyz->y));
		ReadFloat(fd, 1, &(xyz->z));

		xyz++;
	}

	return 1;
}

int ReadTexturedTriangle(int fd, int count, TexturedTriangle *tt)
{
	while (count--) {
		short pad;
		ReadShort(fd, 3, tt->vertex);
		ReadShort(fd, 1, &pad); /* crud */
		ReadFloat(fd, 1, &(tt->r));
		ReadFloat(fd, 1, &(tt->g));
		ReadFloat(fd, 1, &(tt->b));

		tt++;
	}

	return count;
}