1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-11-22 18:32:44 +01:00

clang-format files in rwengine/src

This commit is contained in:
Daniel Evans 2016-09-09 21:13:18 +01:00
parent 7b7e65e002
commit 36a2c57d91
4 changed files with 161 additions and 161 deletions

View File

@ -1,89 +1,94 @@
#include <BinaryStream.hpp> #include <BinaryStream.hpp>
#include <cstring> #include <cstring>
#include <iostream>
#include <fstream> #include <fstream>
#include <iostream>
namespace RW namespace RW {
{
std::unique_ptr<BinaryStream> BinaryStream::parse(const std::string& filename) std::unique_ptr<BinaryStream> BinaryStream::parse(const std::string &filename) {
{ std::ifstream dfile(filename, std::ios_base::binary);
std::ifstream dfile(filename, std::ios_base::binary); if (!dfile.is_open()) {
if ( ! dfile.is_open()) { std::cerr << "Error opening file " << filename << std::endl;
std::cerr << "Error opening file " << filename << std::endl; return nullptr;
return nullptr; }
}
dfile.seekg(0, std::ios_base::end); dfile.seekg(0, std::ios_base::end);
size_t length = dfile.tellg(); size_t length = dfile.tellg();
dfile.seekg(0); dfile.seekg(0);
char *data = new char[length]; char *data = new char[length];
dfile.read(data, length); dfile.read(data, length);
// std::cout << "File is " << length << " bytes" << std::endl << std::endl; // std::cout << "File is " << length << " bytes" << std::endl << std::endl;
auto BS = std::unique_ptr<BinaryStream>(new BinaryStream); auto BS = std::unique_ptr<BinaryStream>(new BinaryStream);
// Set file's ACTUAL length // Set file's ACTUAL length
auto header = reinterpret_cast<nativeSectionHeader_t *>(data); auto header = reinterpret_cast<nativeSectionHeader_t *>(data);
length = header->size + sizeof(nativeSectionHeader_t); length = header->size + sizeof(nativeSectionHeader_t);
sectionHeader_t *prevHeader = nullptr; sectionHeader_t *prevHeader = nullptr;
size_t offset = 0; size_t offset = 0;
while (offset < length) { while (offset < length) {
nativeSectionHeader_t *sectionHeader = reinterpret_cast<nativeSectionHeader_t *>(data + offset); nativeSectionHeader_t *sectionHeader =
sectionHeader_t *sec = new sectionHeader_t; reinterpret_cast<nativeSectionHeader_t *>(data + offset);
sec->ID = sectionHeader->ID; sectionHeader_t *sec = new sectionHeader_t;
sec->size = sectionHeader->size; sec->ID = sectionHeader->ID;
sec->version = sectionHeader->version; sec->size = sectionHeader->size;
if (prevHeader == nullptr) sec->version = sectionHeader->version;
BS->rootHeader = sec; if (prevHeader == nullptr)
else BS->rootHeader = sec;
prevHeader->next = sec; else
prevHeader->next = sec;
if (sectionHeader->ID == 0) { if (sectionHeader->ID == 0) {
std::cout << "Section ID is ZERO! Abort!" << std::endl; std::cout << "Section ID is ZERO! Abort!" << std::endl;
break; break;
} }
std::cout << "Section " << std::hex << sectionHeader->ID std::cout << "Section " << std::hex << sectionHeader->ID << " ("
<< " (" << sectionIdString(sectionHeader->ID) << ")" << sectionIdString(sectionHeader->ID) << ")"
<< " - " << std::dec << sectionHeader->size << " bytes" << std::endl; << " - " << std::dec << sectionHeader->size << " bytes"
/* << std::endl;
std::cout << "Offset " << std::hex << offset << std::endl; /*
*/ std::cout << "Offset " << std::hex << offset << std::endl;
*/
size_t bytesOfData = 0; size_t bytesOfData = 0;
switch (sectionHeader->ID) { switch (sectionHeader->ID) {
case STRUCT: case STRUCT:
bytesOfData = sectionHeader->size; bytesOfData = sectionHeader->size;
sec->data = new uint8_t[bytesOfData]; sec->data = new uint8_t[bytesOfData];
memcpy(sec->data, data + offset + sizeof(nativeSectionHeader_t), bytesOfData); memcpy(sec->data, data + offset + sizeof(nativeSectionHeader_t),
break; bytesOfData);
} break;
// std::cout << "It has " << std::dec << bytesOfData << " bytes of data!" << std::endl; }
offset += sizeof(nativeSectionHeader_t) + bytesOfData; // std::cout << "It has " << std::dec << bytesOfData << " bytes of
// data!" << std::endl;
offset += sizeof(nativeSectionHeader_t) + bytesOfData;
// std::cout << std::endl; // std::cout << std::endl;
prevHeader = sec; prevHeader = sec;
} }
delete[] data; delete[] data;
return BS; return BS;
} }
std::string BinaryStream::sectionIdString(uint32_t id) std::string BinaryStream::sectionIdString(uint32_t id) {
{ switch (id) {
switch (id) { case STRUCT:
case STRUCT: return "STRUCT"; return "STRUCT";
case EXTENSION: return "EXTENSION"; case EXTENSION:
case TEXTURE_NATIVE: return "TEXTURE_NATIVE"; return "EXTENSION";
case TEXTURE_DICTIONARY: return "TEXTURE_DICTIONARY"; case TEXTURE_NATIVE:
default: return "UNKNOWN"; return "TEXTURE_NATIVE";
} case TEXTURE_DICTIONARY:
return "TEXTURE_DICTIONARY";
default:
return "UNKNOWN";
}
} }
} }

View File

@ -2,43 +2,40 @@
#ifndef _BINARYSTREAM_HPP_ #ifndef _BINARYSTREAM_HPP_
#define _BINARYSTREAM_HPP_ #define _BINARYSTREAM_HPP_
#include <string>
#include <memory> #include <memory>
#include <string>
namespace RW namespace RW {
{
class BinaryStream class BinaryStream {
{
private: private:
struct nativeSectionHeader_t struct nativeSectionHeader_t {
{ uint32_t ID;
uint32_t ID; uint32_t size;
uint32_t size; uint32_t version;
uint32_t version; };
};
public: public:
enum { enum {
STRUCT = 0x0001, STRUCT = 0x0001,
EXTENSION = 0x0003, EXTENSION = 0x0003,
TEXTURE_NATIVE = 0x0015, TEXTURE_NATIVE = 0x0015,
TEXTURE_DICTIONARY = 0x0016, TEXTURE_DICTIONARY = 0x0016,
}; };
struct sectionHeader_t { struct sectionHeader_t {
uint32_t ID; uint32_t ID;
uint32_t size; uint32_t size;
uint32_t version; uint32_t version;
uint8_t *data = nullptr; uint8_t *data = nullptr;
sectionHeader_t *next = nullptr; sectionHeader_t *next = nullptr;
} *rootHeader; } * rootHeader;
static std::unique_ptr<BinaryStream> parse(const std::string& filename); static std::unique_ptr<BinaryStream> parse(const std::string &filename);
static std::string sectionIdString(uint32_t id); static std::string sectionIdString(uint32_t id);
}; };
} }
#endif #endif

View File

@ -1,50 +1,52 @@
#include <TextureArchive.hpp> #include <TextureArchive.hpp>
#include <cassert>
#include <cstring> #include <cstring>
#include <iostream> #include <iostream>
#include <cassert>
namespace RW namespace RW {
{
std::unique_ptr<TextureArchive> TextureArchive::create(BinaryStream &binaryStream) std::unique_ptr<TextureArchive> TextureArchive::create(
{ BinaryStream &binaryStream) {
auto textureArchive = std::unique_ptr<TextureArchive>(new TextureArchive); auto textureArchive = std::unique_ptr<TextureArchive>(new TextureArchive);
auto section = binaryStream.rootHeader; auto section = binaryStream.rootHeader;
assert( assert(section->ID == BinaryStream::TEXTURE_DICTIONARY &&
section->ID == BinaryStream::TEXTURE_DICTIONARY "BinaryStream passed to this function must be a TEXTURE DICTIONARY");
&& "BinaryStream passed to this function must be a TEXTURE DICTIONARY"
);
// Struct // Struct
section = section->next; section = section->next;
textureArchive->numTextures = ((uint16_t *) section->data)[0]; textureArchive->numTextures = ((uint16_t *)section->data)[0];
for (size_t i = 0; i < textureArchive->numTextures; i++) { for (size_t i = 0; i < textureArchive->numTextures; i++) {
section = section->next; // Texture Native section = section->next; // Texture Native
section = section->next; // Struct section = section->next; // Struct
Texture texture = *reinterpret_cast<Texture *>(section->data); Texture texture = *reinterpret_cast<Texture *>(section->data);
if (texture.header.rasterFormat & 0x2000) { if (texture.header.rasterFormat & 0x2000) {
memcpy(texture.body.palette, section->data + sizeof(TextureHeader) - 4, 1024); memcpy(texture.body.palette,
section->data + sizeof(TextureHeader) - 4, 1024);
texture.body.pixels = new uint8_t[texture.header.width * texture.header.height];
memcpy(texture.body.pixels, section->data + sizeof(TextureHeader) + 1024, texture.header.width * texture.header.height);
} else {
size_t bufSize = texture.header.width * texture.header.height * texture.header.bpp/8;
texture.body.pixels = new uint8_t[bufSize];
memcpy(texture.body.pixels, section->data + sizeof(TextureHeader), bufSize);
}
textureArchive->textures.push_back(texture); texture.body.pixels =
new uint8_t[texture.header.width * texture.header.height];
memcpy(texture.body.pixels,
section->data + sizeof(TextureHeader) + 1024,
texture.header.width * texture.header.height);
} else {
size_t bufSize = texture.header.width * texture.header.height *
texture.header.bpp / 8;
texture.body.pixels = new uint8_t[bufSize];
memcpy(texture.body.pixels, section->data + sizeof(TextureHeader),
bufSize);
}
section = section->next; // Extension textureArchive->textures.push_back(texture);
}
return textureArchive; section = section->next; // Extension
}
return textureArchive;
} }
} }

View File

@ -6,45 +6,41 @@
#include <vector> #include <vector>
namespace RW namespace RW {
{
class TextureArchive class TextureArchive {
{
public: public:
struct TextureHeader { struct TextureHeader {
uint32_t platformID; uint32_t platformID;
uint16_t filterFlags; uint16_t filterFlags;
uint8_t textureWrapV; uint8_t textureWrapV;
uint8_t textureWrapU; uint8_t textureWrapU;
uint8_t diffuseName[32]; uint8_t diffuseName[32];
uint8_t alphaName[32]; uint8_t alphaName[32];
uint32_t rasterFormat; uint32_t rasterFormat;
uint32_t alpha; uint32_t alpha;
uint16_t width; uint16_t width;
uint16_t height; uint16_t height;
uint8_t bpp; uint8_t bpp;
uint8_t numMipmap; uint8_t numMipmap;
uint8_t rasterType; uint8_t rasterType;
uint8_t compression; uint8_t compression;
uint32_t dataSize; uint32_t dataSize;
}; };
struct TextureBody { struct TextureBody {
uint8_t palette[1024]; uint8_t palette[1024];
uint8_t *pixels; uint8_t *pixels;
}; };
struct Texture { struct Texture {
TextureHeader header; TextureHeader header;
TextureBody body; TextureBody body;
}; };
size_t numTextures;
std::vector<Texture> textures;
size_t numTextures; static std::unique_ptr<TextureArchive> create(BinaryStream &binaryStream);
std::vector<Texture> textures;
static std::unique_ptr<TextureArchive> create(BinaryStream &binaryStream);
}; };
} }
#endif #endif