]> git.lizzy.rs Git - dragonblocks_alpha.git/blob - src/types.c
Rework mapblock loading system and add mapgen stage buffer, 64bit floats for player...
[dragonblocks_alpha.git] / src / types.c
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <endian.h>
4 #include <poll.h>
5 #include "types.h"
6
7 bool read_full(int fd, char *buffer, size_t size)
8 {
9         size_t n_read_total = 0;
10         int n_read;
11         while (n_read_total < size) {
12                 if ((n_read = read(fd, buffer + n_read_total, size - n_read_total)) == -1) {
13                         perror("read");
14                         return false;
15                 }
16                 n_read_total += n_read;
17         }
18         return true;
19 }
20
21 #define htobe8(x) x
22 #define be8toh(x) x
23
24 #define READVEC(type, n) \
25         type buf[n]; \
26         for (int i = 0; i < n; i++) { \
27                 if (! read_ ## type(fd, &buf[i])) \
28                         return false; \
29         }
30
31 #define WRITEVEC(type, n) \
32         for (int i = 0; i < n; i++) { \
33                 if (! write_ ## type(fd, vec[i])) \
34                         return false; \
35         } \
36         return true;
37
38 #define DEFVEC(type) \
39         bool read_v2 ## type(int fd, v2 ## type *ptr) \
40         { \
41                 READVEC(type, 2) \
42                 ptr->x = buf[0]; \
43                 ptr->y = buf[1]; \
44                 return true; \
45         } \
46         bool write_v2 ## type(int fd, v2 ## type val) \
47         { \
48                 type vec[2] = {val.x, val.y}; \
49                 WRITEVEC(type, 2) \
50         } \
51         bool v2 ## type ## _equals(v2 ## type a, v2 ## type b) \
52         { \
53                 return a.x == b.x && a.y == b.y; \
54         } \
55         bool read_v3 ## type(int fd, v3 ## type *ptr) \
56         { \
57                 READVEC(type, 3) \
58                 ptr->x = buf[0]; \
59                 ptr->y = buf[1]; \
60                 ptr->z = buf[2]; \
61                 return true; \
62         } \
63         bool write_v3 ## type(int fd, v3 ## type val) \
64         { \
65                 type vec[3] = {val.x, val.y, val.z}; \
66                 WRITEVEC(type, 3) \
67         } \
68         bool v3 ## type ## _equals(v3 ## type a, v3 ## type b) \
69         { \
70                 return a.x == b.x && a.y == b.y && a.z == b.z; \
71         }
72
73 #define DEFTYP(type, bits) \
74         bool read_ ## type(int fd, type *buf) \
75         { \
76                 u ## bits encoded; \
77                 if (! read_full(fd, (char *) &encoded, sizeof(type))) \
78                         return false; \
79                 *buf = be ## bits ## toh(encoded); \
80                 return true; \
81         } \
82         bool write_ ## type(int fd, type val) \
83         { \
84                 u ## bits encoded = htobe ## bits(val); \
85                 if (write(fd, &encoded, sizeof(encoded)) == -1) { \
86                         perror("write"); \
87                         return false; \
88                 } \
89                 return true; \
90         } \
91         DEFVEC(type)
92
93 #define DEFTYPES(bits) \
94         DEFTYP(s ## bits, bits) \
95         DEFTYP(u ## bits, bits)
96
97 DEFTYPES(8)
98 DEFTYPES(16)
99 DEFTYPES(32)
100 DEFTYPES(64)
101
102 #define DEFFLOAT(type) \
103         bool read_ ## type(int fd, type *buf) \
104         { \
105                 if (! read_full(fd, (char *) buf, sizeof(type))) \
106                         return false; \
107                 return true; \
108         } \
109         bool write_ ## type(int fd, type val) \
110         { \
111                 if (write(fd, &val, sizeof(val)) == -1) { \
112                         perror("write"); \
113                         return false; \
114                 } \
115                 return true; \
116         } \
117         DEFVEC(type)
118
119 DEFFLOAT(f32)
120 DEFFLOAT(f64)