Skip to content

Commit

Permalink
SWBF2 model loading and Godot integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyuu17 committed Jun 10, 2024
1 parent b74c02f commit aa6079d
Show file tree
Hide file tree
Showing 22 changed files with 667 additions and 44 deletions.
5 changes: 4 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ target_sources(${PROJECT_NAME}
PRIVATE
"SWBF2/Chunks/ChunkHeader.cpp"
"SWBF2/Chunks/ChunkProcessor.cpp"
"SWBF2/Chunks/ModelChunk.cpp"
"SWBF2/Chunks/ModelSegmentChunk.cpp"
"SWBF2/Chunks/StreamReader.cpp"
"SWBF2/Chunks/UcfbChunk.cpp"
"SWBF2/Chunks/WorldChunk.cpp"
"SWBF2/Types.hpp"
"SWBF2/Model.cpp"
"SWBF2/Models.cpp"
"Core.cpp"
"Core.hpp"
"RegisterExtension.cpp"
Expand Down
29 changes: 13 additions & 16 deletions src/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,27 @@
#include "Version.h"

#include "SWBF2/Chunks/ChunkProcessor.hpp"
#include "SWBF2/Models.hpp"

#include <chrono>
#include <iostream>
#include <thread>

namespace SWBF2
Core::Core()
{
Core::Core()
{
}
}

void Core::_ready()
{
using namespace std::chrono_literals;
std::this_thread::sleep_for(5000ms);
void Core::_ready()
{
godot::UtilityFunctions::print("hello world!");

godot::UtilityFunctions::print("hello world!");
// UcfbChunk::ReadUcfbFile("data/_lvl_pc/common.lvl");
// UcfbChunk::ReadUcfbFile("data/_lvl_pc/core.lvl");
SWBF2::UcfbChunk::ReadUcfbFile("data/_lvl_pc/cor/cor1.lvl");

// UcfbChunk::ReadUcfbFile("data/_lvl_pc/common.lvl");
// UcfbChunk::ReadUcfbFile("data/_lvl_pc/core.lvl");
UcfbChunk::ReadUcfbFile("data/_lvl_pc/cor/cor1.lvl");
}
SWBF2::Models::m_models;
}

void Core::_bind_methods()
{
}
void Core::_bind_methods()
{
}
22 changes: 9 additions & 13 deletions src/Core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,15 @@

#include <godot_cpp/classes/node.hpp>

namespace SWBF2
{
class Core : public godot::Node
{
GDCLASS(Core, godot::Node)
class Core : public godot::Node {
GDCLASS(Core, godot::Node)

public:
Core();
~Core() = default;
public:
Core();
~Core() = default;

void _ready() override;
void _ready() override;

private:
static void _bind_methods();
};
}
private:
static void _bind_methods();
};
13 changes: 13 additions & 0 deletions src/ModelMesh.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

#pragma once

#include <godot_cpp/classes/mesh.hpp>

#include "SWBF2/Model.hpp"

class ModelMesh : public SWBF2::Model, public godot::Mesh {
GDCLASS(Core, godot::Mesh)

public:

};
2 changes: 1 addition & 1 deletion src/RegisterExtension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace
return;
}

godot::ClassDB::register_class<SWBF2::Core>();
godot::ClassDB::register_class<Core>();
}

/// @brief Called by Godot to let us do any cleanup.
Expand Down
4 changes: 3 additions & 1 deletion src/SWBF2/Chunks/ChunkProcessor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "ChunkHeader.hpp"
#include "StreamReader.hpp"

#include "ModelChunk.hpp"
#include "UcfbChunk.hpp"
#include "WorldChunk.hpp"

Expand All @@ -17,7 +18,8 @@ namespace SWBF2
static inline const std::unordered_map<uint32_t, ChunkProcessingFunction> m_functions
{
{ "ucfb"_m, UcfbChunk::ProcessChunk },
{ "wrld"_m, WorldChunk::ProcessChunk }
{ "wrld"_m, WorldChunk::ProcessChunk },
{ "modl"_m, ModelChunk::ProcessChunk }
};

static void ProcessChunk(StreamReader &streamReader, StreamReader &parentReader);
Expand Down
57 changes: 57 additions & 0 deletions src/SWBF2/Chunks/ModelChunk.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include <godot_cpp/variant/utility_functions.hpp>

#include "StreamReader.hpp"

#include "ModelChunk.hpp"
#include "ModelSegmentChunk.hpp"

#include "../Models.hpp"
#include "../Model.hpp"
#include "../ModelSegment.hpp"

namespace SWBF2
{
void ModelChunk::ProcessChunk(StreamReader &streamReader)
{
Model model;

auto modelNameReaderChild = streamReader.ReadChildWithHeader<"NAME"_m>();

*modelNameReaderChild >> model.m_name;

auto vertexReaderChild = streamReader.ReadChildWithHeader<"VRTX"_m>();

std::vector<std::byte> vertex;
vertex.resize(vertexReaderChild->GetHeader().size);

*vertexReaderChild >> vertex;

auto nodeReaderChild = streamReader.ReadChildWithHeader<"NODE"_m>();

*nodeReaderChild >> model.m_node;

auto infoReaderChild = streamReader.ReadChildWithHeader<"INFO"_m>();

*infoReaderChild >> model.m_info;

std::optional<StreamReader> readerChild;
while ((readerChild = streamReader.ReadChild()).has_value())
{
switch (readerChild->GetHeader().m_Magic)
{
case "segm"_m: {
StreamReader r{ *readerChild };
ModelSegmentChunk::ProcessChunk(r, model);
break;
}

case "SPHR"_m:
default:
godot::UtilityFunctions::printerr(__FILE__, ":", __LINE__, ": ", readerChild->GetHeader().ToString().c_str(), " not implemented");
break;
}
}

Models::m_models.insert_or_assign(model.m_name, model);
}
}
12 changes: 12 additions & 0 deletions src/SWBF2/Chunks/ModelChunk.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include "StreamReader.hpp"

namespace SWBF2
{
class ModelChunk {
public:
static void ProcessChunk(StreamReader &streamReader);
};

}
Loading

0 comments on commit aa6079d

Please sign in to comment.