Polyphase Game Engine
Loading...
Searching...
No Matches
Stream.h
Go to the documentation of this file.
1#pragma once
2
3#include <vector>
4#include <string>
5#include "PolyphaseAPI.h"
6#include "Assertion.h"
7#include "Maths.h"
8#include "Swap.h"
9#include <stdint.h>
10#include <stdarg.h>
11#include <string.h>
12
14
15#define STREAM_STRING_LEN_BYTES 4
16
17class AssetRef;
18struct AsyncLoadRequest;
19
21{
22public:
23
24 Stream();
25 Stream(const char* externalData, uint32_t externalSize);
26 ~Stream();
27
28 char* GetData();
29 uint32_t GetSize() const;
30 uint32_t GetPos();
31 void SetPos(uint32_t pos);
32 void Reset();
33 void Resize(uint32_t size);
34
35 void SetExternalData(const char* externalData, uint32_t externalSize);
36
37 bool ReadFile(const char* path, bool isAsset, int32_t maxSize = 0);
38 bool WriteFile(const char* path);
39
40 void SetAsyncRequest(AsyncLoadRequest* request);
41 void SetAssetVersion(uint32_t version);
42 uint32_t GetAssetVersion() const;
43
44 void ReadAsset(AssetRef& asset);
45 void WriteAsset(const AssetRef& asset);
46
47 void ReadString(std::string& dst);
48 void WriteString(const std::string& src);
49
50 void ReadBytes(uint8_t* dst, uint32_t length);
51 void WriteBytes(const uint8_t* src, uint32_t length);
52
53 uint32_t ReadBytesMax(uint8_t* dst, uint32_t length);
54
55 int32_t ReadInt32();
56 uint32_t ReadUint32();
57 int64_t ReadInt64();
58 uint64_t ReadUint64();
59 int16_t ReadInt16();
60 uint16_t ReadUint16();
61 int8_t ReadInt8();
62 uint8_t ReadUint8();
63 float ReadFloat();
64 bool ReadBool();
65 glm::vec2 ReadVec2();
66 glm::vec3 ReadVec3();
67 glm::vec4 ReadVec4();
68 glm::quat ReadQuat();
69 glm::mat4 ReadMatrix();
70
71 void WriteInt32(const int32_t& src);
72 void WriteUint32(const uint32_t& src);
73 void WriteInt64(const int64_t& src);
74 void WriteUint64(const uint64_t& src);
75 void WriteInt16(const int16_t& src);
76 void WriteUint16(const uint16_t& src);
77 void WriteInt8(const int8_t& src);
78 void WriteUint8(const uint8_t& src);
79 void WriteFloat(const float& src);
80 void WriteBool(const bool& src);
81 void WriteVec2(const glm::vec2& src);
82 void WriteVec3(const glm::vec3& src);
83 void WriteVec4(const glm::vec4& src);
84 void WriteQuat(const glm::quat& src);
85 void WriteMatrix(const glm::mat4& src);
86
87 std::string GetLine();
88 int32_t Scan(const char* format, ...);
89
90private:
91
92 void Grow(uint32_t newSize);
93 void Reserve(uint32_t capacity);
94
95 template<typename T>
96 void Read(T& dst)
97 {
98 OCT_ASSERT(sizeof(T) == 1 || sizeof(T) == 2 || sizeof(T) == 4);
99 OCT_ASSERT(mPos + sizeof(T) <= mSize);
100 memcpy(&dst, &mData[mPos], sizeof(T));
101 mPos += sizeof(T);
102
103#if ENDIAN_SWAP
104 if (sizeof(T) == 4)
105 {
106 Swap32(dst);
107 }
108 else if (sizeof(T) == 2)
109 {
110 Swap16(dst);
111 }
112#endif
113 }
114
115 template<typename T>
116 void Write(const T& src)
117 {
118 OCT_ASSERT(sizeof(T) == 1 || sizeof(T) == 2 || sizeof(T) == 4);
119
120 T srcSwapped = src;
121
122#if ENDIAN_SWAP
123 if (sizeof(T) == 4)
124 {
125 Swap32(srcSwapped);
126 }
127 else if (sizeof(T) == 2)
128 {
129 Swap16(srcSwapped);
130 }
131#endif
132
133 uint32_t deltaSize = uint32_t(sizeof(T));
134 if (mPos + deltaSize > mSize)
135 {
136 Grow(mPos + deltaSize);
137 }
138 memcpy(&mData[mPos], &srcSwapped, sizeof(T));
139 mPos += deltaSize;
140 }
141
142private:
143
144 char* mData;
145 uint32_t mSize;
146 uint32_t mCapacity;
147 uint32_t mPos;
148 uint32_t mAssetVersion; // Version of asset being read (for format compatibility)
149 AsyncLoadRequest* mAsyncRequest;
150 bool mExternal;
151};
#define OCT_ASSERT(expr)
Definition Assertion.h:12
Export macros for Polyphase Engine symbols.
#define POLYPHASE_API
Definition PolyphaseAPI.h:31
void Swap32(T &dst)
Definition Swap.h:4
void Swap16(T &dst)
Definition Swap.h:21
Definition AssetRef.h:18
Definition Stream.h:21
Definition AssetManager.h:19