Panda3D
physxMemoryWriteBuffer.cxx
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file physxMemoryWriteBuffer.cxx
10  * @author enn0x
11  * @date 2009-10-11
12  */
13 
14 #include "physxMemoryWriteBuffer.h"
15 
16 /**
17  *
18  */
19 PhysxMemoryWriteBuffer::PhysxMemoryWriteBuffer() : currentSize(0), maxSize(0), data(nullptr)
20 {
21 
22 }
23 
24 /**
25  *
26  */
27 PhysxMemoryWriteBuffer::~PhysxMemoryWriteBuffer()
28 {
29  NxGetPhysicsSDKAllocator()->free(data);
30 }
31 
32 /**
33  *
34  */
35 NxStream &PhysxMemoryWriteBuffer::storeByte(NxU8 b)
36 {
37  storeBuffer(&b, sizeof(NxU8));
38  return *this;
39 }
40 
41 /**
42  *
43  */
44 NxStream &PhysxMemoryWriteBuffer::storeWord(NxU16 w)
45 {
46  storeBuffer(&w, sizeof(NxU16));
47  return *this;
48 }
49 
50 /**
51  *
52  */
53 NxStream &PhysxMemoryWriteBuffer::storeDword(NxU32 d)
54 {
55  storeBuffer(&d, sizeof(NxU32));
56  return *this;
57 }
58 
59 /**
60  *
61  */
62 NxStream &PhysxMemoryWriteBuffer::storeFloat(NxReal f)
63 {
64  storeBuffer(&f, sizeof(NxReal));
65  return *this;
66 }
67 
68 /**
69  *
70  */
71 NxStream &PhysxMemoryWriteBuffer::storeDouble(NxF64 f)
72 {
73  storeBuffer(&f, sizeof(NxF64));
74  return *this;
75 }
76 
77 /**
78  *
79  */
80 NxStream &PhysxMemoryWriteBuffer::storeBuffer(const void *buffer, NxU32 size)
81 {
82  NxU32 expectedSize = currentSize + size;
83  if (expectedSize > maxSize)
84  {
85  maxSize = expectedSize + 4096;
86 
87  NxU8 *newData = (NxU8 *)NxGetPhysicsSDKAllocator()->malloc(maxSize, NX_MEMORY_PERSISTENT);
88  if(data)
89  {
90  memcpy(newData, data, currentSize);
91  NxGetPhysicsSDKAllocator()->free(data);
92  }
93  data = newData;
94  }
95  memcpy(data + currentSize, buffer, size);
96  currentSize += size;
97  return *this;
98 }
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.