summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2024-04-08 01:21:41 +0200
committerAki <please@ignore.pl>2024-04-08 01:21:41 +0200
commit3cc6b923edb69907794c3dc6daa61a445b3a56c0 (patch)
tree2e9c736042ef364bba0957052e9fddf0fae41097
parent3b1776352524c9f24b478260e18cb04d2d27efb5 (diff)
downloadstarshatter-3cc6b923edb69907794c3dc6daa61a445b3a56c0.zip
starshatter-3cc6b923edb69907794c3dc6daa61a445b3a56c0.tar.gz
starshatter-3cc6b923edb69907794c3dc6daa61a445b3a56c0.tar.bz2
Added 3ds2mag converter
Solidified the converters a bit to prevent selected segvs/page faults.
-rw-r--r--MagicEx/CMakeLists.txt15
-rw-r--r--MagicEx/include/MagicLoad.h4
-rw-r--r--MagicEx/src/3ds2mag.cpp23
-rw-r--r--MagicEx/src/convert.h22
-rw-r--r--MagicEx/src/convert.inl.h39
-rw-r--r--MagicEx/src/mag2obj.cpp18
-rw-r--r--MagicEx/src/obj2mag.cpp18
7 files changed, 109 insertions, 30 deletions
diff --git a/MagicEx/CMakeLists.txt b/MagicEx/CMakeLists.txt
index 5c552fc..8d81e81 100644
--- a/MagicEx/CMakeLists.txt
+++ b/MagicEx/CMakeLists.txt
@@ -9,9 +9,9 @@ target_include_directories(${PROJECT_NAME} PUBLIC include/)
target_link_libraries(${PROJECT_NAME} PUBLIC StarsEx PRIVATE l3ds)
-add_executable(${PROJECT_NAME}_obj2mag src/obj2mag.cpp)
-target_link_libraries(${PROJECT_NAME}_obj2mag PRIVATE ${PROJECT_NAME})
-set_target_properties(${PROJECT_NAME}_obj2mag PROPERTIES OUTPUT_NAME obj2mag)
+add_executable(${PROJECT_NAME}_3ds2mag src/3ds2mag.cpp)
+target_link_libraries(${PROJECT_NAME}_3ds2mag PRIVATE ${PROJECT_NAME})
+set_target_properties(${PROJECT_NAME}_3ds2mag PROPERTIES OUTPUT_NAME 3ds2mag)
add_executable(${PROJECT_NAME}_mag2obj src/mag2obj.cpp)
@@ -19,6 +19,11 @@ target_link_libraries(${PROJECT_NAME}_mag2obj PRIVATE ${PROJECT_NAME})
set_target_properties(${PROJECT_NAME}_mag2obj PROPERTIES OUTPUT_NAME mag2obj)
+add_executable(${PROJECT_NAME}_obj2mag src/obj2mag.cpp)
+target_link_libraries(${PROJECT_NAME}_obj2mag PRIVATE ${PROJECT_NAME})
+set_target_properties(${PROJECT_NAME}_obj2mag PROPERTIES OUTPUT_NAME obj2mag)
+
+
install(
- TARGETS ${PROJECT_NAME}_obj2mag ${PROJECT_NAME}_mag2obj RUNTIME
- COMPONENT Tools DESTINATION ${CMAKE_INSTALL_PREFIX})
+ TARGETS ${PROJECT_NAME}_3ds2mag ${PROJECT_NAME}_mag2obj ${PROJECT_NAME}_obj2mag
+ RUNTIME COMPONENT Tools DESTINATION ${CMAKE_INSTALL_PREFIX})
diff --git a/MagicEx/include/MagicLoad.h b/MagicEx/include/MagicLoad.h
index f447945..c1322bd 100644
--- a/MagicEx/include/MagicLoad.h
+++ b/MagicEx/include/MagicLoad.h
@@ -8,8 +8,6 @@
#include <windows.h>
-#include <filesystem>
-
#include <Bitmap.h>
#include <Solid.h>
@@ -17,7 +15,7 @@
int LoadBuffer(const char* filename, BYTE*& buf, bool null_terminate=false);
int LoadTexture(const char* name, Bitmap*& bmp, int type=0);
int LoadAlpha(const char* name, Bitmap& bitmap, int type=0);
-template <typename ModelLoader> bool ImportInto(const std::filesystem::path& pathname, Solid* target);
+template <typename ModelLoader> bool ImportInto(const char* pathname, Solid* target);
#include "MagicLoad.inl.h"
diff --git a/MagicEx/src/3ds2mag.cpp b/MagicEx/src/3ds2mag.cpp
new file mode 100644
index 0000000..3bf9460
--- /dev/null
+++ b/MagicEx/src/3ds2mag.cpp
@@ -0,0 +1,23 @@
+/* Starshatter: The Open Source Project
+ Copyright (c) 2021-2024, Starshatter: The Open Source Project Contributors
+ Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors
+ Copyright (c) 1997-2006, Destroyer Studios LLC.
+*/
+
+#include <string>
+
+#include <ModelFile3DS.h>
+#include <ModelFileMAG.h>
+
+#include "convert.h"
+
+
+int
+main(int argc, char* argv[])
+{
+ if (argc < 2)
+ return 2;
+ const std::string input {argv[1]};
+ const auto output = input + ".mag";
+ return starshatter::convert<ModelFile3DS, ModelFileMAG>(input, output);
+}
diff --git a/MagicEx/src/convert.h b/MagicEx/src/convert.h
new file mode 100644
index 0000000..9bba5b8
--- /dev/null
+++ b/MagicEx/src/convert.h
@@ -0,0 +1,22 @@
+/* Starshatter: The Open Source Project
+ Copyright (c) 2021-2024, Starshatter: The Open Source Project Contributors
+ Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors
+ Copyright (c) 1997-2006, Destroyer Studios LLC.
+*/
+
+#pragma once
+
+#include <string>
+
+
+namespace starshatter
+{
+
+
+template <typename Input, typename Output> int convert(const std::string& input, const std::string& output);
+
+
+} // namespace starshatter
+
+
+#include "convert.inl.h"
diff --git a/MagicEx/src/convert.inl.h b/MagicEx/src/convert.inl.h
new file mode 100644
index 0000000..7bf29ee
--- /dev/null
+++ b/MagicEx/src/convert.inl.h
@@ -0,0 +1,39 @@
+/* Starshatter: The Open Source Project
+ Copyright (c) 2021-2024, Starshatter: The Open Source Project Contributors
+ Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors
+ Copyright (c) 1997-2006, Destroyer Studios LLC.
+*/
+
+#include <memory>
+#include <string>
+
+#include <DataLoader.h>
+#include <Solid.h>
+
+#include <MagicLoad.h>
+
+
+namespace starshatter
+{
+
+
+template <typename Input, typename Output>
+int
+convert(const std::string& input, const std::string& output)
+{
+ DataLoader::Initialize();
+ auto solid = std::make_unique<Solid>();
+ if (!ImportInto<Input>(input.c_str(), solid.get()))
+ return 1;
+ Output exporter {output.c_str()};
+ auto* model = solid->GetModel();
+ if (model == nullptr)
+ return 1;
+ if (!exporter.Save(model))
+ return 1;
+ DataLoader::Close();
+ return 0;
+}
+
+
+} // namespace starshatter
diff --git a/MagicEx/src/mag2obj.cpp b/MagicEx/src/mag2obj.cpp
index a8ba386..1439ac7 100644
--- a/MagicEx/src/mag2obj.cpp
+++ b/MagicEx/src/mag2obj.cpp
@@ -4,24 +4,20 @@
Copyright (c) 1997-2006, Destroyer Studios LLC.
*/
-#include <memory>
+#include <string>
-#include <Solid.h>
-
-#include <MagicLoad.h>
#include <ModelFileMAG.h>
#include <ModelFileOBJ.h>
+#include "convert.h"
+
int
main(int argc, char* argv[])
{
if (argc < 2)
- return 1;
- std::string pathname {argv[1]};
- auto solid = std::make_unique<Solid>();
- if (!ImportInto<ModelFileMAG>(pathname.c_str(), solid.get()))
- return 1;
- ModelFileOBJ exporter {(pathname + ".obj").c_str()};
- exporter.Save(solid->GetModel());
+ return 2;
+ const std::string input {argv[1]};
+ const auto output = input + ".obj";
+ return starshatter::convert<ModelFileMAG, ModelFileOBJ>(input, output);
}
diff --git a/MagicEx/src/obj2mag.cpp b/MagicEx/src/obj2mag.cpp
index c9d22e0..5b71bb5 100644
--- a/MagicEx/src/obj2mag.cpp
+++ b/MagicEx/src/obj2mag.cpp
@@ -4,24 +4,20 @@
Copyright (c) 1997-2006, Destroyer Studios LLC.
*/
-#include <memory>
+#include <string>
-#include <Solid.h>
-
-#include <MagicLoad.h>
#include <ModelFileMAG.h>
#include <ModelFileOBJ.h>
+#include "convert.h"
+
int
main(int argc, char* argv[])
{
if (argc < 2)
- return 1;
- std::string pathname {argv[1]};
- auto solid = std::make_unique<Solid>();
- if (!ImportInto<ModelFileOBJ>(pathname.c_str(), solid.get()))
- return 1;
- ModelFileMAG exporter {(pathname + ".mag").c_str()};
- exporter.Save(solid->GetModel());
+ return 2;
+ const std::string input {argv[1]};
+ const auto output = input + ".mag";
+ return starshatter::convert<ModelFileOBJ, ModelFileMAG>(input, output);
}