BVB Source Codes

CRYENGINE Show XMLCPB_BufferWriter.cpp Source code

Return Download CRYENGINE: download XMLCPB_BufferWriter.cpp Source code - Download CRYENGINE Source code - Type:.cpp
  1. // Copyright 2001-2016 Crytek GmbH / Crytek Group. All rights reserved.
  2.  
  3. /*************************************************************************
  4. *************************************************************************/
  5.  
  6. #include "StdAfx.h"
  7. #include "XMLCPB_BufferWriter.h"
  8. #include "XMLCPB_Writer.h"
  9.  
  10. using namespace XMLCPB;
  11.  
  12. //////////////////////////////////////////////////////////////////////////
  13.  
  14. void CBufferWriter::SSimpleBuffer::WriteToFile(CWriter& writer)
  15. {
  16.         if (m_used > 0)
  17.                 writer.WriteDataIntoFile(GetBasePointer(), m_used);
  18. }
  19.  
  20. void CBufferWriter::SSimpleBuffer::WriteToMemory(CWriter& Writer, uint8*& rpData, uint32& outWriteLoc)
  21. {
  22.         if (m_used > 0)
  23.                 Writer.WriteDataIntoMemory(rpData, GetBasePointer(), m_used, outWriteLoc);
  24. }
  25.  
  26. //////////////////////////////////////////////////////////////////////////
  27.  
  28. CBufferWriter::CBufferWriter(int bufferSize)
  29.         : m_bufferSize(bufferSize)
  30.         , m_usingStreaming(false)
  31.         , m_pWriter(NULL)
  32. {
  33.         assert(bufferSize <= 64 * 1024); // because SAddr is using 16 bits to offset inside buffers
  34. }
  35.  
  36. void CBufferWriter::Init(CWriter* pWriter, bool useStreaming)
  37. {
  38.         assert(pWriter);
  39.         m_pWriter = pWriter;
  40.         m_usingStreaming = useStreaming;
  41.         AddBuffer();
  42. }
  43.  
  44. //////////////////////////////////////////////////////////////////////////
  45.  
  46. void CBufferWriter::AddBuffer()
  47. {
  48.         if (m_usingStreaming && !m_buffers.empty())
  49.         {
  50.                 m_buffers.back()->WriteToFile(*m_pWriter);
  51.                 m_buffers.back()->FreeBuffer();
  52.         }
  53.  
  54.         SSimpleBufferT pBuffer = SSimpleBufferT(new SSimpleBuffer(m_bufferSize));
  55.  
  56.         m_buffers.push_back(pBuffer);
  57. }
  58.  
  59. //////////////////////////////////////////////////////////////////////////
  60.  
  61. void CBufferWriter::GetCurrentAddr(SAddr& addr) const
  62. {
  63.         addr.bufferIndex = static_cast<uint16>(m_buffers.size() - 1);
  64.         addr.bufferOffset = m_buffers.back()->GetUsed();
  65. }
  66.  
  67. //////////////////////////////////////////////////////////////////////////
  68.  
  69. FlatAddr CBufferWriter::ConvertSegmentedAddrToFlatAddr(const SAddr& addr)
  70. {
  71.         FlatAddr flatAddr = addr.bufferOffset;
  72.  
  73.         for (int b = 0; b < addr.bufferIndex; ++b)
  74.         {
  75.                 flatAddr += m_buffers[b]->GetUsed();
  76.         }
  77.  
  78.         return flatAddr;
  79. }
  80.  
  81. //////////////////////////////////////////////////////////////////////////
  82.  
  83. void CBufferWriter::AddData(const void* _pSource, int size)
  84. {
  85.         const uint8* pSource = (const uint8*)_pSource;
  86.  
  87.         SSimpleBuffer* pBuf = m_buffers.back().get();
  88.  
  89.         while (!pBuf->CanAddData(size))
  90.         {
  91.                 uint32 size1 = pBuf->GetFreeSpaceSize();
  92.                 pBuf->AddData(pSource, size1);
  93.  
  94.                 pSource += size1;
  95.                 size = size - size1;
  96.  
  97.                 AddBuffer();
  98.                 pBuf = m_buffers.back().get();
  99.         }
  100.  
  101.         pBuf->AddData(pSource, size);
  102.  
  103.         if (pBuf->IsFull())
  104.                 AddBuffer();
  105. }
  106.  
  107. //////////////////////////////////////////////////////////////////////////
  108. // NoSplit means that all the data will be in the same internal buffer, so all of it can be read directly using the address conversion
  109.  
  110. void CBufferWriter::AddDataNoSplit(SAddr& addr, const void* _pSource, int size)
  111. {
  112.         const uint8* pSource = (const uint8*)_pSource;
  113.  
  114.         if (!m_buffers.back()->CanAddData(size))
  115.                 AddBuffer();
  116.  
  117.         GetCurrentAddr(addr);
  118.  
  119.         SSimpleBufferT& buffer = m_buffers.back();
  120.         buffer->AddData(pSource, size);
  121.  
  122.         if (buffer->IsFull())
  123.                 AddBuffer();
  124. }
  125.  
  126. //////////////////////////////////////////////////////////////////////////
  127.  
  128. //////////////////////////////////////////////////////////////////////////
  129.  
  130. uint32 CBufferWriter::GetUsedMemory() const
  131. {
  132.         uint32 memTot = 0;
  133.  
  134.         for (uint i = 0; i < m_buffers.size(); i++)
  135.         {
  136.                 memTot += m_buffers[i]->GetUsed();
  137.         }
  138.  
  139.         return memTot;
  140. }
  141.  
  142. //////////////////////////////////////////////////////////////////////////
  143. // when is using streaming, this actually writes only the remaning not-yet writen data
  144.  
  145. void CBufferWriter::WriteToFile()
  146. {
  147.         if (m_usingStreaming)
  148.         {
  149.                 m_buffers.back()->WriteToFile(*m_pWriter);
  150.                 m_buffers.back()->FreeBuffer();
  151.         }
  152.         else
  153.         {
  154.                 for (uint b = 0; b < m_buffers.size(); ++b)
  155.                 {
  156.                         SSimpleBufferT& buffer = m_buffers[b];
  157.                         buffer->WriteToFile(*m_pWriter);
  158.                 }
  159.         }
  160. }
  161.  
  162. //////////////////////////////////////////////////////////////////////////
  163.  
  164. void CBufferWriter::WriteToMemory(uint8*& rpData, uint32& outWriteLoc)
  165. {
  166.         for (uint b = 0; b < m_buffers.size(); ++b)
  167.         {
  168.                 SSimpleBufferT& buffer = m_buffers[b];
  169.                 buffer->WriteToMemory(*m_pWriter, rpData, outWriteLoc);
  170.         }
  171. }
  172.  
  173. //////////////////////////////////////////////////////////////////////////
  174.  
  175. uint32 CBufferWriter::GetDataSize() const
  176. {
  177.         uint32 uSize = 0;
  178.         for (uint b = 0; b < m_buffers.size(); ++b)
  179.         {
  180.                 const SSimpleBufferT& buffer = m_buffers[b];
  181.                 uSize += buffer->GetDataSize();
  182.         }
  183.         return uSize;
  184. }
  185.  
downloadXMLCPB_BufferWriter.cpp Source code - Download CRYENGINE Source code
Related Source Codes/Software:
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top