Skip to content

Commit 5b53a74

Browse files
authored
Merge pull request #5 from HarbourMasters/vertice-gen
Add undefined behaviour
2 parents 664ffa9 + 6709f51 commit 5b53a74

File tree

4 files changed

+125
-1
lines changed

4 files changed

+125
-1
lines changed

assets/mk64/us/startup_logo.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
startup_logo/startup_logo:
2+
name: startup_logo
3+
type: vtx
4+
size: 688
5+
offset: 0
6+
mio0: 0x825800
7+
# startup_logo/dl1: # not implemented yet
8+
# type: display_list
9+
# mio0: 0x825800
10+
# offset: 0x35C0

src/Companion.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "factories/sm64/SDialogFactory.h"
1616
#include "factories/sm64/SDictionaryFactory.h"
1717
#include "factories/DisplayListFactory.h"
18+
#include "factories/VerticeFactory.h"
1819
#include "factories/sm64/SGeoFactory.h"
1920
#include "spdlog/spdlog.h"
2021

@@ -39,7 +40,9 @@ void Companion::Init() {
3940
this->RegisterFactory("TEXTURE", new TextureFactory());
4041
this->RegisterFactory("BLOB", new BlobFactory());
4142

42-
// SM64 Specific
43+
this->RegisterFactory("VTX", new VerticeFactory());
44+
45+
// SM64 specific
4346
this->RegisterFactory("SM64:DIALOG", new SDialogFactory());
4447
this->RegisterFactory("SM64:ANIM", new SAnimFactory());
4548
this->RegisterFactory("SM64:TEXT", new STextFactory());
@@ -113,6 +116,7 @@ void Companion::Process() {
113116

114117
for(auto asset = root.begin(); asset != root.end(); ++asset){
115118
auto type = asset->second["type"].as<std::string>();
119+
std::transform(type.begin(), type.end(), type.begin(), ::toupper);
116120

117121
auto output = (directory / asset->first.as<std::string>()).string();
118122
std::replace(output.begin(), output.end(), '\\', '/');

src/factories/VerticeFactory.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#include "VerticeFactory.h"
2+
3+
#include "utils/MIODecoder.h"
4+
#include "spdlog/spdlog.h"
5+
#include <iostream>
6+
#include <string>
7+
#include <fstream> // for file stream
8+
9+
namespace fs = std::filesystem;
10+
11+
typedef short s16;
12+
typedef unsigned short u16;
13+
typedef unsigned char u8;
14+
15+
#define str(value) std::to_string(value)
16+
17+
// Byte swap a 16-bit unsigned integer
18+
std::uint16_t SwapBytes(std::uint16_t value) {
19+
return ((value >> 8) & 0x00FF) | ((value << 8) & 0xFF00);
20+
}
21+
22+
void WriteAllText(const std::string& filename, const std::string& text) {
23+
std::ofstream outputFile(filename);
24+
25+
if (!outputFile.is_open()) {
26+
std::cerr << "Failed to open the file: " << filename << std::endl;
27+
return; // Handle the error as needed
28+
}
29+
30+
outputFile << text;
31+
outputFile.close();
32+
}
33+
34+
bool VerticeFactory::process(LUS::BinaryWriter* writer, YAML::Node& node, std::vector<uint8_t>& buffer) {
35+
36+
auto offset = node["offset"].as<size_t>();
37+
auto mio0 = node["mio0"].as<size_t>();
38+
auto numVerts = node["size"].as<size_t>();
39+
auto name = node["name"].as<std::string>();
40+
41+
auto decoded = MIO0Decoder::Decode(buffer, mio0);
42+
43+
auto *data = decoded.data() + offset;
44+
45+
Vtx *vtx = reinterpret_cast<Vtx*>(data);
46+
47+
std::string text = "";
48+
49+
text += "Vtx "+ name + "[] = {\n";
50+
51+
for (size_t i = 0; i < numVerts; i++) {
52+
s16 x = SwapBytes(vtx[i].v.ob[0]);
53+
s16 y = SwapBytes(vtx[i].v.ob[1]);
54+
s16 z = SwapBytes(vtx[i].v.ob[2]);
55+
56+
u16 flag = SwapBytes(vtx[i].v.flag);
57+
s16 tc1 = SwapBytes(vtx[i].v.tc[0]);
58+
s16 tc2 = SwapBytes(vtx[i].v.tc[1]);
59+
60+
u8 cn1 = vtx[i].v.cn[0];
61+
u8 cn2 = vtx[i].v.cn[1];
62+
u8 cn3 = vtx[i].v.cn[2];
63+
u8 cn4 = vtx[i].v.cn[3];
64+
65+
text += " {{ " + str(x) + ", " + str(y) + ", " + str(z) + "}, " + str(flag) + ", {" + str(tc1) + ", " + str(tc2) + "}, ";
66+
67+
text += "{ 0x" + str(cn1) + ", " + "0x" + str(cn2) + ", 0x" + str(cn3) + ", 0x" + str(cn4) + " }}},\n";
68+
}
69+
70+
text += ("};\n");
71+
72+
WriteAllText("out.txt", text);
73+
74+
return true;
75+
}

src/factories/VerticeFactory.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#pragma once
2+
3+
#include "RawFactory.h"
4+
5+
class VerticeFactory : public RawFactory {
6+
/*
7+
* Vertex (set up for use with colors)
8+
*/
9+
typedef struct {
10+
short ob[3]; /* x, y, z */
11+
unsigned short flag;
12+
short tc[2]; /* texture coord */
13+
unsigned char cn[4]; /* color & alpha */
14+
} Vtx_t;
15+
16+
/*
17+
* Vertex (set up for use with normals)
18+
*/
19+
typedef struct {
20+
short ob[3]; /* x, y, z */
21+
unsigned short flag;
22+
short tc[2]; /* texture coord */
23+
signed char n[3]; /* normal */
24+
unsigned char a; /* alpha */
25+
} Vtx_tn;
26+
27+
typedef union {
28+
Vtx_t v; /* Use this one for colors */
29+
Vtx_tn n; /* Use this one for normals */
30+
long long int force_structure_alignment;
31+
} Vtx;
32+
public:
33+
VerticeFactory() = default;
34+
bool process(LUS::BinaryWriter* write, YAML::Node& data, std::vector<uint8_t>& buffer) override;
35+
};

0 commit comments

Comments
 (0)