A lot of changes in almost all files

This commit is contained in:
MihailRis
2022-02-28 02:30:15 +03:00
committed by GitHub
parent cd3e737385
commit bc9d29cf68
28 changed files with 3485 additions and 476 deletions
+67 -1
View File
@@ -14,6 +14,9 @@ union {
#include <fstream>
#include <iostream>
#define SECTION_POSITION 1
#define SECTION_ROTATION 2
unsigned long WorldFiles::totalCompressed = 0;
int bytes2Int(const unsigned char* src, unsigned int offset){
@@ -27,6 +30,23 @@ void int2Bytes(int value, char* dest, unsigned int offset){
dest[offset+3] = (char) (value >> 0 & 255);
}
void float2Bytes(float fvalue, char* dest, unsigned int offset){
uint32_t value = *((uint32_t*)&fvalue);
dest[offset] = (char) (value >> 24 & 255);
dest[offset+1] = (char) (value >> 16 & 255);
dest[offset+2] = (char) (value >> 8 & 255);
dest[offset+3] = (char) (value >> 0 & 255);
}
float bytes2Float(char* srcs, unsigned int offset){
unsigned char* src = (unsigned char*) srcs;
uint32_t value = ((src[offset] << 24) |
(src[offset+1] << 16) |
(src[offset+2] << 8) |
(src[offset+3]));
return *(float*)(&value);
}
WorldFiles::WorldFiles(const char* directory, size_t mainBufferCapacity) : directory(directory){
mainBufferIn = new char[CHUNK_VOL*2];
mainBufferOut = new char[mainBufferCapacity];
@@ -81,6 +101,10 @@ std::string WorldFiles::getRegionFile(int x, int y) {
return directory + std::to_string(x) + "_" + std::to_string(y) + ".bin";
}
std::string WorldFiles::getPlayerFile() {
return directory + "/player.bin";
}
bool WorldFiles::getChunk(int x, int y, char* out){
assert(out != nullptr);
@@ -129,7 +153,7 @@ bool WorldFiles::readChunk(int x, int y, char* out){
input.read((char*)(&offset), 4);
// Ordering bytes from big-endian to machine order (any, just reading)
offset = bytes2Int((const unsigned char*)(&offset), 0);
assert (offset < 1000000);
//assert (offset < 1000000);
if (offset == 0){
input.close();
return false;
@@ -161,6 +185,48 @@ void WorldFiles::write(){
}
}
void WorldFiles::writePlayer(glm::vec3 position, float camX, float camY){
char dst[1+3*4 + 1+2*4];
size_t offset = 0;
dst[offset++] = SECTION_POSITION;
float2Bytes(position.x, dst, offset); offset += 4;
float2Bytes(position.y, dst, offset); offset += 4;
float2Bytes(position.z, dst, offset); offset += 4;
dst[offset++] = SECTION_ROTATION;
float2Bytes(camX, dst, offset); offset += 4;
float2Bytes(camY, dst, offset); offset += 4;
write_binary_file(getPlayerFile(), (const char*)dst, sizeof(dst));
}
bool WorldFiles::readPlayer(glm::vec3& position, float& camX, float& camY) {
size_t length = 0;
char* data = read_binary_file(getPlayerFile(), length);
if (data == nullptr){
std::cerr << "could not to read player.bin" << std::endl;
return false;
}
size_t offset = 0;
while (offset < length){
char section = data[offset++];
switch (section){
case SECTION_POSITION:
position.x = bytes2Float(data, offset); offset += 4;
position.y = bytes2Float(data, offset); offset += 4;
position.z = bytes2Float(data, offset); offset += 4;
break;
case SECTION_ROTATION:
camX = bytes2Float(data, offset); offset += 4;
camY = bytes2Float(data, offset); offset += 4;
break;
}
}
std::cout << position.x << " " << position.y << " " << position.z << std::endl;
return true;
}
unsigned int WorldFiles::writeRegion(char* out, int x, int y, char** region){
unsigned int offset = REGION_VOL * 4;
for (unsigned int i = 0; i < offset; i++)
+5 -1
View File
@@ -3,7 +3,8 @@
#include <map>
#include <unordered_map>
#include <string>
#include <glm/glm.hpp>
#define REGION_SIZE_BIT 5
#define REGION_SIZE (1 << (REGION_SIZE_BIT))
#define REGION_VOL ((REGION_SIZE) * (REGION_SIZE))
@@ -26,13 +27,16 @@ public:
void put(const char* chunkData, int x, int y);
bool readPlayer(glm::vec3& position, float& camX, float& camY);
bool readChunk(int x, int y, char* out);
bool getChunk(int x, int y, char* out);
void readRegion(char* fileContent);
unsigned int writeRegion(char* out, int x, int y, char** region);
void writePlayer(glm::vec3 position, float camX, float camY);
void write();
std::string getRegionFile(int x, int y);
std::string getPlayerFile();
};
extern void longToCoords(int& x, int& y, long key);
+9 -5
View File
@@ -40,14 +40,18 @@ bool read_binary_file(std::string filename, char* data, size_t size) {
return true;
}
bool read_binary_file(std::string filename, char* data, size_t offset, size_t size) {
char* read_binary_file(std::string filename, size_t& length) {
std::ifstream input(filename, std::ios::binary);
if (!input.is_open())
return false;
input.seekg(offset);
input.read(data, size);
return nullptr;
input.seekg(0, std::ios_base::end);
length = input.tellg();
input.seekg(0, std::ios_base::beg);
char* data = new char[length];
input.read(data, length);
input.close();
return true;
return data;
}
// returns decompressed length
+1
View File
@@ -7,6 +7,7 @@ extern bool write_binary_file(std::string filename, const char* data, size_t siz
extern unsigned int append_binary_file(std::string filename, const char* data, size_t size);
extern bool read_binary_file(std::string filename, char* data, size_t size);
extern bool read_binary_file(std::string filename, char* data, size_t offset, size_t size);
extern char* read_binary_file(std::string filename, size_t& length);
extern unsigned int calcRLE(const char* src, unsigned int length);
extern unsigned int compressRLE(const char* src, unsigned int length, char* dst);