- Partly implemented client side prediction (still unstable).
[peakengine.git] / engine / src / core / Buffer.cpp
blob63f7e6b77a9a39ba44f54eb23f7e99f99f4cc4aa
1 /*
2 Copyright (C) 2008 Mathias Gottschlag
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in the
6 Software without restriction, including without limitation the rights to use,
7 copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
8 Software, and to permit persons to whom the Software is furnished to do so,
9 subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16 PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
18 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
19 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 #include "core/Buffer.h"
24 #include <stdlib.h>
25 #include <string.h>
27 namespace peak
29 Buffer::Buffer()
31 data = 0;
32 size = 0;
33 position = 0;
34 datasize = 0;
36 Buffer::Buffer(unsigned int size)
38 data = (char*)malloc(size);
39 size = size;
40 position = 0;
41 datasize = 0;
43 Buffer::Buffer(void *data, unsigned int size)
45 this->data = (char*)malloc(size);
46 size = size;
47 datasize = size;
48 position = 0;
49 memcpy(this->data, data, size);
51 Buffer::Buffer(const Buffer &buf)
53 data = 0;
54 size = 0;
55 datasize = 0;
56 position = 0;
57 *this += buf;
59 Buffer::~Buffer()
61 if (data)
62 free(data);
65 void Buffer::setSize(unsigned int size)
67 data = (char*)realloc(data, size);
68 if (datasize > size)
69 datasize = size;
70 if (position > datasize)
71 position = datasize;
72 this->size = size;
75 unsigned int Buffer::getSize(void)
77 return size;
79 unsigned int Buffer::getDataSize(void)
81 return datasize;
84 unsigned int Buffer::getPosition(void)
86 return position;
88 void Buffer::setPosition(int position, bool relative)
90 if (!relative)
92 if (position < 0) position = 0;
93 if (position > (int)datasize)
94 position = datasize;
95 this->position = position;
97 else
99 if (this->position + position > datasize)
100 position = datasize - this->position;
101 if ((int)this->position + position < 0)
102 position = -this->position;
103 this->position = (int)this->position + position;
107 int Buffer::readData(void *dest, unsigned int size)
109 if (datasize - position < size)
111 size = datasize - position;
113 memcpy(dest, data + position, size);
114 position += size;
115 return size;
117 int Buffer::writeData(const void *source, unsigned int size, bool resize)
119 if (this->size - datasize < size)
121 if (resize)
123 setSize(datasize + size);
125 else
127 size = this->size - datasize;
130 memcpy(data + datasize, source, size);
131 datasize += size;
132 return size;
135 void Buffer::writeInt(int value)
137 writeData(&value, sizeof(int), true);
139 int Buffer::readInt(void)
141 int value;
142 readData(&value, sizeof(int));
143 return value;
145 void Buffer::writeFloat(float value)
147 writeData(&value, sizeof(float), true);
149 float Buffer::readFloat(void)
151 float value;
152 readData(&value, sizeof(float));
153 return value;
155 void Buffer::writeString(std::string str)
157 writeData(str.c_str(), str.length() + 1, true);
159 std::string Buffer::readString(void)
161 std::string str = data + position;
162 while (readByte())
164 return str;
166 void Buffer::writeByte(unsigned char value)
168 writeData(&value, sizeof(unsigned char), true);
170 unsigned char Buffer::readByte(void)
172 unsigned char value;
173 readData(&value, sizeof(unsigned char));
174 return value;
176 void Buffer::writeWord(short value)
178 writeData(&value, sizeof(short), true);
180 short Buffer::readWord(void)
182 short value;
183 readData(&value, sizeof(short));
184 return value;
186 void Buffer::writeBool(bool flag)
188 writeByte(flag);
190 bool Buffer::readBool(void)
192 bool flag = readByte();
193 return flag;
195 void Buffer::writeVector2D(Vector2D v)
197 writeFloat(v.x);
198 writeFloat(v.y);
200 Vector2D Buffer::readVector2D(void)
202 Vector2D v;
203 v.x = readFloat();
204 v.y = readFloat();
205 return v;
207 void Buffer::writeVector3D(Vector3D v)
209 writeFloat(v.x);
210 writeFloat(v.y);
211 writeFloat(v.z);
213 Vector3D Buffer::readVector3D(void)
215 Vector3D v;
216 v.x = readFloat();
217 v.y = readFloat();
218 v.z = readFloat();
219 return v;
221 void Buffer::writeQuaternion(Quaternion q)
223 writeWord((unsigned short)(q.x * (1<<15)));
224 writeWord((unsigned short)(q.y * (1<<15)));
225 writeWord((unsigned short)(q.z * (1<<15)));
226 writeWord((unsigned short)(q.w * (1<<15)));
228 Quaternion Buffer::readQuaternion(void)
230 Quaternion q;
231 q.x = (float)readWord() / (1<<15);
232 q.y = (float)readWord() / (1<<15);
233 q.z = (float)readWord() / (1<<15);
234 q.w = (float)readWord() / (1<<15);
235 return q;
239 char *Buffer::getData(void)
241 return data;
244 void Buffer::clear(void)
246 if (data)
247 free(data);
248 data = 0;
249 size = 0;
250 datasize = 0;
251 position = 0;
254 Buffer &Buffer::operator=(const Buffer &buf)
256 clear();
257 *this += buf;
258 return *this;
260 Buffer &Buffer::operator+=(const Buffer &buf)
262 writeData(buf.data, buf.datasize, true);
263 return *this;
265 Buffer &Buffer::operator<<(const Buffer &buf)
267 writeData(buf.data, buf.datasize, true);
268 return *this;
270 Buffer &Buffer::operator<<(int &data)
272 writeData(&data, sizeof(int), true);
273 return *this;
275 Buffer &Buffer::operator<<(float &data)
277 writeData(&data, sizeof(float), true);
278 return *this;