Imported more code from the old engine.
[peakengine.git] / engine / src / core / Buffer.cpp
blobbaa51fdcd5a4f0cb182805c1a551a57ee4f75a9a
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(unsigned int position, bool relative)
90 if (!relative)
92 if (position > datasize)
93 position = datasize;
94 this->position = position;
96 else
98 if (this->position + position > datasize)
99 position = datasize - this->position;
100 this->position += position;
104 int Buffer::readData(void *dest, unsigned int size)
106 if (datasize - position < size)
108 size = datasize - position;
110 memcpy(dest, data + position, size);
111 position += size;
112 return size;
114 int Buffer::writeData(const void *source, unsigned int size, bool resize)
116 if (this->size - datasize < size)
118 if (resize)
120 setSize(datasize + size);
122 else
124 size = this->size - datasize;
127 memcpy(data + datasize, source, size);
128 datasize += size;
129 return size;
132 void Buffer::writeInt(int value)
134 writeData(&value, sizeof(int), true);
136 int Buffer::readInt(void)
138 int value;
139 readData(&value, sizeof(int));
140 return value;
142 void Buffer::writeFloat(float value)
144 writeData(&value, sizeof(float), true);
146 float Buffer::readFloat(void)
148 float value;
149 readData(&value, sizeof(float));
150 return value;
152 void Buffer::writeString(std::string str)
154 writeData(str.c_str(), str.length() + 1, true);
156 std::string Buffer::readString(void)
158 std::string str = data + position;
159 while (readByte())
161 return str;
163 void Buffer::writeByte(unsigned char value)
165 writeData(&value, sizeof(unsigned char), true);
167 unsigned char Buffer::readByte(void)
169 unsigned char value;
170 readData(&value, sizeof(unsigned char));
171 return value;
173 void Buffer::writeWord(short value)
175 writeData(&value, sizeof(short), true);
177 short Buffer::readWord(void)
179 short value;
180 readData(&value, sizeof(short));
181 return value;
183 void Buffer::writeBool(bool flag)
185 writeByte(flag);
187 bool Buffer::readBool(void)
189 bool flag = readByte();
190 return flag;
192 void Buffer::writeVector2D(Vector2D v)
194 writeFloat(v.x);
195 writeFloat(v.y);
197 Vector2D Buffer::readVector2D(void)
199 Vector2D v;
200 v.x = readFloat();
201 v.y = readFloat();
202 return v;
204 void Buffer::writeVector3D(Vector3D v)
206 writeFloat(v.x);
207 writeFloat(v.y);
208 writeFloat(v.z);
210 Vector3D Buffer::readVector3D(void)
212 Vector3D v;
213 v.x = readFloat();
214 v.y = readFloat();
215 v.z = readFloat();
216 return v;
218 void Buffer::writeQuaternion(Quaternion q)
220 writeWord((unsigned short)(q.x * (1<<15)));
221 writeWord((unsigned short)(q.y * (1<<15)));
222 writeWord((unsigned short)(q.z * (1<<15)));
223 writeWord((unsigned short)(q.w * (1<<15)));
225 Quaternion Buffer::readQuaternion(void)
227 Quaternion q;
228 q.x = (float)readWord() / (1<<15);
229 q.y = (float)readWord() / (1<<15);
230 q.z = (float)readWord() / (1<<15);
231 q.w = (float)readWord() / (1<<15);
232 return q;
236 char *Buffer::getData(void)
238 return data;
241 void Buffer::clear(void)
243 if (data)
244 free(data);
245 data = 0;
246 size = 0;
247 datasize = 0;
248 position = 0;
251 Buffer &Buffer::operator=(const Buffer &buf)
253 clear();
254 *this += buf;
255 return *this;
257 Buffer &Buffer::operator+=(const Buffer &buf)
259 writeData(buf.data, buf.datasize, true);
260 return *this;
262 Buffer &Buffer::operator<<(const Buffer &buf)
264 writeData(buf.data, buf.datasize, true);
265 return *this;
267 Buffer &Buffer::operator<<(int &data)
269 writeData(&data, sizeof(int), true);
270 return *this;
272 Buffer &Buffer::operator<<(float &data)
274 writeData(&data, sizeof(float), true);
275 return *this;