-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnav_buffer.h
41 lines (32 loc) · 947 Bytes
/
nav_buffer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#pragma once
#include <vector>
#include <fstream>
#include <cstring>
namespace nav_mesh {
class nav_buffer {
public:
~nav_buffer();
void load_from_file(std::string_view nav_mesh_file);
void skip(std::size_t bytes_to_skip);
void clear();
/*
* The reason we don't erase the read bytes from the buffer is performance.
* Benchmarks tested on cs_militia.nav:
* Using std::vector::erase: average reading time of 7.5 seconds (!)
* Adding read bytes to buffer pointer: 70ms (100x faster)
*/
template < typename T >
T read() {
T read = *reinterpret_cast<T*>(m_nav_buffer.data() + m_bytes_read);
m_bytes_read += sizeof(T);
return read;
}
void read(void* out_buffer, std::size_t bytes_to_read) {
memcpy(out_buffer, m_nav_buffer.data() + m_bytes_read, bytes_to_read);
m_bytes_read += bytes_to_read;
}
private:
std::size_t m_bytes_read = 0;
std::vector< std::uint8_t > m_nav_buffer = { };
};
}