diff --git a/bspinfo/CMakeLists.txt b/bspinfo/CMakeLists.txt index 5c1c091a..025f6350 100644 --- a/bspinfo/CMakeLists.txt +++ b/bspinfo/CMakeLists.txt @@ -1,8 +1,5 @@ -set(BSPINFO_SOURCES - bspinfo.cc) - -add_executable(bspinfo ${BSPINFO_SOURCES}) -target_link_libraries(bspinfo common fmt::fmt nlohmann_json::nlohmann_json) +add_executable(bspinfo main.cc) +target_link_libraries(bspinfo common fmt::fmt) # HACK: copy .dll dependencies add_custom_command(TARGET bspinfo POST_BUILD diff --git a/bspinfo/main.cc b/bspinfo/main.cc new file mode 100644 index 00000000..8d0d69f3 --- /dev/null +++ b/bspinfo/main.cc @@ -0,0 +1,104 @@ +/* Copyright (C) 1996-1997 Id Software, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + See file, 'COPYING', for details. +*/ + +#include +#include +#include + +#include +#include +#include +#include +#include +#include "common/fs.hh" + +#include "common/polylib.hh" +#include "common/bsputils.hh" + +static void PrintBSPTextureUsage(const mbsp_t &bsp) +{ + std::unordered_map areas; + + for (auto &face : bsp.dfaces) { + const char *name = Face_TextureName(&bsp, &face); + + if (!name || !*name) { + continue; + } + + auto points = GLM_FacePoints(&bsp, &face); + polylib::winding_t w(points.begin(), points.end()); + vec_t area = w.area(); + + areas[name] += area; + } + + std::vector> areasVec; + + for (auto &area : areas) { + areasVec.push_back(std::make_tuple(area.first, area.second)); + } + + std::sort(areasVec.begin(), areasVec.end(), [](auto &l, auto &r) { return std::get<1>(r) < std::get<1>(l); }); + + printf("\n"); + + for (auto &area : areasVec) { + fmt::print("{},{:.0f}\n", std::get<0>(area), std::get<1>(area)); + } +} + +// TODO +settings::common_settings options; + +int main(int argc, char **argv) +{ + printf("---- bspinfo / ericw-tools " stringify(ERICWTOOLS_VERSION) " ----\n"); + if (argc == 1) { + printf("usage: bspinfo bspfile [bspfiles]\n"); + exit(1); + } + + for (int32_t i = 1; i < argc; i++) { + printf("---------------------\n"); + fs::path source = DefaultExtension(argv[i], ".bsp"); + fmt::print("{}\n", source); + + bspdata_t bsp; + LoadBSPFile(source, &bsp); + + bsp.version->game->init_filesystem(source, options); + + PrintBSPFileSizes(&bsp); + + // WriteBSPFile(fs::path(source).replace_extension("bsp.rewrite"), &bsp); + + ConvertBSPFormat(&bsp, &bspver_generic); + + serialize_bsp(bsp, std::get(bsp.bsp), fs::path(source).replace_extension("bsp.json")); + + PrintBSPTextureUsage(std::get(bsp.bsp)); + + printf("---------------------\n"); + + fs::clear(); + } + + return 0; +} diff --git a/common/CMakeLists.txt b/common/CMakeLists.txt index 99810edf..12e4a3b3 100644 --- a/common/CMakeLists.txt +++ b/common/CMakeLists.txt @@ -1,4 +1,5 @@ add_library(common STATIC + ${CMAKE_SOURCE_DIR}/common/bspinfo.cc ${CMAKE_SOURCE_DIR}/common/bspfile.cc ${CMAKE_SOURCE_DIR}/common/bsputils.cc ${CMAKE_SOURCE_DIR}/common/cmdlib.cc @@ -14,6 +15,7 @@ add_library(common STATIC ${CMAKE_SOURCE_DIR}/common/debugger.natvis ${CMAKE_SOURCE_DIR}/include/common/aabb.hh ${CMAKE_SOURCE_DIR}/include/common/bitflags.hh + ${CMAKE_SOURCE_DIR}/include/common/bspinfo.hh ${CMAKE_SOURCE_DIR}/include/common/bspfile.hh ${CMAKE_SOURCE_DIR}/include/common/bsputils.hh ${CMAKE_SOURCE_DIR}/include/common/cmdlib.hh diff --git a/bspinfo/bspinfo.cc b/common/bspinfo.cc similarity index 89% rename from bspinfo/bspinfo.cc rename to common/bspinfo.cc index 51bccd97..658a4550 100644 --- a/bspinfo/bspinfo.cc +++ b/common/bspinfo.cc @@ -17,6 +17,7 @@ See file, 'COPYING', for details. */ +#include #include #include @@ -219,7 +220,7 @@ static std::string serialize_image(const qvec3b *palette, const uint8_t *image, return str; } -static void serialize_bsp(const bspdata_t &bspdata, const mbsp_t &bsp, const fs::path &name) +void serialize_bsp(const bspdata_t &bspdata, const mbsp_t &bsp, const fs::path &name) { json j = json::object(); @@ -474,78 +475,3 @@ static void serialize_bsp(const bspdata_t &bspdata, const mbsp_t &bsp, const fs: std::ofstream(name, std::fstream::out | std::fstream::trunc) << std::setw(4) << j; } - -#include "common/polylib.hh" -#include "common/bsputils.hh" - -static void PrintBSPTextureUsage(const mbsp_t &bsp) -{ - std::unordered_map areas; - - for (auto &face : bsp.dfaces) { - const char *name = Face_TextureName(&bsp, &face); - - if (!name || !*name) { - continue; - } - - auto points = GLM_FacePoints(&bsp, &face); - polylib::winding_t w(points.begin(), points.end()); - vec_t area = w.area(); - - areas[name] += area; - } - - std::vector> areasVec; - - for (auto &area : areas) { - areasVec.push_back(std::make_tuple(area.first, area.second)); - } - - std::sort(areasVec.begin(), areasVec.end(), [](auto &l, auto &r) { return std::get<1>(r) < std::get<1>(l); }); - - printf("\n"); - - for (auto &area : areasVec) { - fmt::print("{},{:.0f}\n", std::get<0>(area), std::get<1>(area)); - } -} - -// TODO -settings::common_settings options; - -int main(int argc, char **argv) -{ - printf("---- bspinfo / ericw-tools " stringify(ERICWTOOLS_VERSION) " ----\n"); - if (argc == 1) { - printf("usage: bspinfo bspfile [bspfiles]\n"); - exit(1); - } - - for (int32_t i = 1; i < argc; i++) { - printf("---------------------\n"); - fs::path source = DefaultExtension(argv[i], ".bsp"); - fmt::print("{}\n", source); - - bspdata_t bsp; - LoadBSPFile(source, &bsp); - - bsp.version->game->init_filesystem(source, options); - - PrintBSPFileSizes(&bsp); - - // WriteBSPFile(fs::path(source).replace_extension("bsp.rewrite"), &bsp); - - ConvertBSPFormat(&bsp, &bspver_generic); - - serialize_bsp(bsp, std::get(bsp.bsp), fs::path(source).replace_extension("bsp.json")); - - PrintBSPTextureUsage(std::get(bsp.bsp)); - - printf("---------------------\n"); - - fs::clear(); - } - - return 0; -} diff --git a/include/common/bspinfo.hh b/include/common/bspinfo.hh new file mode 100644 index 00000000..03f33d38 --- /dev/null +++ b/include/common/bspinfo.hh @@ -0,0 +1,25 @@ +/* Copyright (C) 1996-1997 Id Software, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + See file, 'COPYING', for details. +*/ + +#include "common/fs.hh" + +struct bspdata_t; +struct mbsp_t; + +void serialize_bsp(const bspdata_t &bspdata, const mbsp_t &bsp, const fs::path &name);