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