Skip to content

Commit

Permalink
Split gaf code into multiple files
Browse files Browse the repository at this point in the history
  • Loading branch information
MHeasell committed Dec 8, 2020
1 parent 349056e commit 7c162f5
Show file tree
Hide file tree
Showing 13 changed files with 179 additions and 137 deletions.
8 changes: 6 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -377,8 +377,12 @@ set(SOURCE_FILES
src/rwe/io/featuretdf/FeatureDefinition.h
src/rwe/io/fnt/Fnt.cpp
src/rwe/io/fnt/Fnt.h
src/rwe/io/gaf/Gaf.cpp
src/rwe/io/gaf/Gaf.h
src/rwe/io/gaf/GafArchive.cpp
src/rwe/io/gaf/GafArchive.h
src/rwe/io/gaf/GafReaderAdapter.h
src/rwe/io/gaf/gaf_headers.h
src/rwe/io/gaf/gaf_util.cpp
src/rwe/io/gaf/gaf_util.h
src/rwe/io/gui/gui.cpp
src/rwe/io/gui/gui.h
src/rwe/io/hpi/HpiArchive.cpp
Expand Down
2 changes: 1 addition & 1 deletion src/gaf_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <iostream>
#include <memory>
#include <png++/png.hpp>
#include <rwe/io/gaf/Gaf.h>
#include <rwe/io/gaf/GafArchive.h>
#include <string>

namespace fs = boost::filesystem;
Expand Down
2 changes: 1 addition & 1 deletion src/rwe/MeshService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <rwe/fixed_point.h>
#include <rwe/geometry/CollisionMesh.h>
#include <rwe/io/_3do/_3do.h>
#include <rwe/io/gaf/Gaf.h>
#include <rwe/io/gaf/GafArchive.h>
#include <rwe/match.h>
#include <rwe/math/Vector3f.h>
#include <rwe/math/rwe_math.h>
Expand Down
2 changes: 1 addition & 1 deletion src/rwe/TextureService.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "TextureService.h"
#include <boost/interprocess/streams/bufferstream.hpp>
#include <rwe/io/fnt/Fnt.h>
#include <rwe/io/gaf/Gaf.h>
#include <rwe/io/gaf/GafArchive.h>
#include <rwe/io/pcx/pcx.h>
#include <rwe/io/tnt/TntArchive.h>
#include <rwe/rwe_string.h>
Expand Down
2 changes: 1 addition & 1 deletion src/rwe/atlas_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <boost/interprocess/streams/bufferstream.hpp>
#include <rwe/BoxTreeSplit.h>
#include <rwe/Index.h>
#include <rwe/io/gaf/Gaf.h>
#include <rwe/io/gaf/GafArchive.h>
#include <rwe/match.h>

namespace rwe
Expand Down
74 changes: 74 additions & 0 deletions src/rwe/io/gaf/GafArchive.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#include "GafArchive.h"
#include <algorithm>
#include <memory>
#include <rwe/io/gaf/gaf_util.h>
#include <rwe/io_utils.h>
#include <rwe/rwe_string.h>

namespace rwe
{
GafArchive::Entry readEntry(std::istream& stream)
{
auto entry = readRaw<GafEntry>(stream);

auto nullPos = std::find(entry.name, entry.name + GafMaxNameLength, '\0');
auto nameLength = nullPos - entry.name;
std::string name(reinterpret_cast<char*>(entry.name), nameLength);

std::vector<GafFrameEntry> frames;
frames.reserve(entry.frames);

for (std::size_t i = 0; i < entry.frames; ++i)
{
auto frameEntry = readRaw<GafFrameEntry>(stream);
frames.emplace_back(frameEntry);
}

return GafArchive::Entry{std::move(name), entry.unknown1, entry.unknown2, std::move(frames)};
}

const std::vector<GafArchive::Entry>& GafArchive::entries() const
{
return _entries;
}

GafArchive::GafArchive(std::istream* stream)
{
auto header = readRaw<GafHeader>(*stream);
if (header.version != GafVersionNumber)
{
throw GafException("Invalid GAF version number");
}

_entries.reserve(header.entries);

for (std::size_t i = 0; i < header.entries; ++i)
{
auto pointer = readRaw<uint32_t>(*stream);
auto previous = stream->tellg();

stream->seekg(pointer);
_entries.push_back(readEntry(*stream));
stream->seekg(previous);
}

_stream = stream;
}

std::optional<std::reference_wrapper<const GafArchive::Entry>> GafArchive::findEntry(const std::string& name) const
{
auto pos = std::find_if(_entries.begin(), _entries.end(), [&name](const Entry& e) { return toUpper(e.name) == toUpper(name); });

if (pos == _entries.end())
{
return std::nullopt;
}

return *pos;
}

void GafArchive::extract(const GafArchive::Entry& entry, GafReaderAdapter& adapter)
{
extractGafEntry(_stream, entry.frameOffsets, adapter);
}
}
39 changes: 39 additions & 0 deletions src/rwe/io/gaf/GafArchive.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

#include <cstdint>
#include <functional>
#include <istream>
#include <optional>
#include <rwe/io/gaf/GafReaderAdapter.h>
#include <rwe/io/gaf/gaf_headers.h>
#include <stdexcept>
#include <string>
#include <vector>

namespace rwe
{
class GafArchive
{
public:
struct Entry
{
std::string name;
uint16_t unknown1;
uint32_t unknown2;
std::vector<GafFrameEntry> frameOffsets;
};

private:
std::vector<Entry> _entries;
std::istream* _stream;

public:
explicit GafArchive(std::istream* stream);

const std::vector<Entry>& entries() const;

std::optional<std::reference_wrapper<const Entry>> findEntry(const std::string& name) const;

void extract(const Entry& entry, GafReaderAdapter& adapter);
};
}
25 changes: 25 additions & 0 deletions src/rwe/io/gaf/GafReaderAdapter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

#include <rwe/io/gaf/gaf_headers.h>

namespace rwe
{
class GafReaderAdapter
{
public:
struct LayerData
{
int x;
int y;
unsigned int width;
unsigned int height;
unsigned char transparencyKey;
char* data;
};

public:
virtual void beginFrame(const GafFrameEntry& entry, const GafFrameData& header) = 0;
virtual void frameLayer(const LayerData& data) = 0;
virtual void endFrame() = 0;
};
}
59 changes: 0 additions & 59 deletions src/rwe/io/gaf/Gaf.h → src/rwe/io/gaf/gaf_headers.h
Original file line number Diff line number Diff line change
@@ -1,25 +1,13 @@
#pragma once

#include <cstdint>
#include <functional>
#include <istream>
#include <optional>
#include <stdexcept>
#include <string>
#include <vector>

namespace rwe
{
static const unsigned int GafVersionNumber = 0x00010100;

static const unsigned int GafMaxNameLength = 32;

class GafException : public std::runtime_error
{
public:
explicit GafException(const char* message);
};

#pragma pack(1) // don't pad members

struct GafHeader
Expand Down Expand Up @@ -84,51 +72,4 @@ namespace rwe
};

#pragma pack()

class GafReaderAdapter
{
public:
struct LayerData
{
int x;
int y;
unsigned int width;
unsigned int height;
unsigned char transparencyKey;
char* data;
};

public:
virtual void beginFrame(const GafFrameEntry& entry, const GafFrameData& header) = 0;
virtual void frameLayer(const LayerData& data) = 0;
virtual void endFrame() = 0;
};

class GafArchive
{
public:
struct Entry
{
std::string name;
uint16_t unknown1;
uint32_t unknown2;
std::vector<GafFrameEntry> frameOffsets;
};

private:
std::vector<Entry> _entries;
std::istream* _stream;

public:
explicit GafArchive(std::istream* stream);

const std::vector<Entry>& entries() const;

std::optional<std::reference_wrapper<const Entry>> findEntry(const std::string& name) const;

void extract(const Entry& entry, GafReaderAdapter& adapter);

private:
Entry readEntry(std::istream& stream) const;
};
}
78 changes: 8 additions & 70 deletions src/rwe/io/gaf/Gaf.cpp → src/rwe/io/gaf/gaf_util.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#include "Gaf.h"
#include <algorithm>
#include <memory>
#include "gaf_util.h"

#include <rwe/io_utils.h>
#include <rwe/rwe_string.h>

namespace rwe
{
GafException::GafException(const char* message) : runtime_error(message) {}

void decompressRow(std::istream& stream, char* buffer, std::size_t rowLength, char transparencyIndex)
{
auto compressedRowLength = readRaw<uint16_t>(stream);
Expand Down Expand Up @@ -78,73 +78,13 @@ namespace rwe
}
}

const std::vector<GafArchive::Entry>& GafArchive::entries() const
void extractGafEntry(std::istream* _stream, const std::vector<GafFrameEntry>& frameEntries, GafReaderAdapter& adapter)
{
return _entries;
}

GafArchive::GafArchive(std::istream* stream)
{
auto header = readRaw<GafHeader>(*stream);
if (header.version != GafVersionNumber)
{
throw GafException("Invalid GAF version number");
}

_entries.reserve(header.entries);

for (std::size_t i = 0; i < header.entries; ++i)
for (auto entry : frameEntries)
{
auto pointer = readRaw<uint32_t>(*stream);
auto previous = stream->tellg();

stream->seekg(pointer);
_entries.push_back(readEntry(*stream));
stream->seekg(previous);
}

_stream = stream;
}

GafArchive::Entry GafArchive::readEntry(std::istream& stream) const
{
auto entry = readRaw<GafEntry>(stream);

auto nullPos = std::find(entry.name, entry.name + GafMaxNameLength, '\0');
auto nameLength = nullPos - entry.name;
std::string name(reinterpret_cast<char*>(entry.name), nameLength);

std::vector<GafFrameEntry> frames;
frames.reserve(entry.frames);

for (std::size_t i = 0; i < entry.frames; ++i)
{
auto frameEntry = readRaw<GafFrameEntry>(stream);
frames.emplace_back(frameEntry);
}

return Entry{std::move(name), entry.unknown1, entry.unknown2, std::move(frames)};
}

std::optional<std::reference_wrapper<const GafArchive::Entry>> GafArchive::findEntry(const std::string& name) const
{
auto pos = std::find_if(_entries.begin(), _entries.end(), [&name](const Entry& e) { return toUpper(e.name) == toUpper(name); });

if (pos == _entries.end())
{
return std::nullopt;
}

return *pos;
}

void GafArchive::extract(const GafArchive::Entry& entry, GafReaderAdapter& adapter)
{
for (auto offset : entry.frameOffsets)
{
_stream->seekg(offset.frameDataOffset);
_stream->seekg(entry.frameDataOffset);
auto frameHeader = readRaw<GafFrameData>(*_stream);
adapter.beginFrame(offset, frameHeader);
adapter.beginFrame(entry, frameHeader);

_stream->seekg(frameHeader.frameDataOffset);
if (frameHeader.subframesCount == 0)
Expand Down Expand Up @@ -208,6 +148,4 @@ namespace rwe
adapter.endFrame();
}
}

GafException::GafException(const char* message) : runtime_error(message) {}
}
21 changes: 21 additions & 0 deletions src/rwe/io/gaf/gaf_util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include <rwe/io/gaf/GafReaderAdapter.h>
#include <rwe/io/gaf/gaf_headers.h>
#include <istream>
#include <vector>

namespace rwe
{
class GafException : public std::runtime_error
{
public:
explicit GafException(const char* message);
};

void decompressRow(std::istream& stream, char* buffer, std::size_t rowLength, char transparencyIndex);

void decompressFrame(std::istream& stream, char* buffer, std::size_t width, std::size_t height, char transparencyIndex);

void extractGafEntry(std::istream* _stream, const std::vector<GafFrameEntry>& frameEntries, GafReaderAdapter& adapter);
}
Loading

0 comments on commit 7c162f5

Please sign in to comment.