move serialize_bsp() to common/bspinfo.cc

This commit is contained in:
Eric Wasylishen 2022-05-14 14:53:08 -06:00
parent 2195a3f5a4
commit ba37ae3a54
5 changed files with 135 additions and 81 deletions

View File

@ -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

104
bspinfo/main.cc Normal file
View File

@ -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 <common/bspinfo.hh>
#include <common/cmdlib.hh>
#include <common/bspfile.hh>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <fmt/ostream.h>
#include <common/json.hh>
#include "common/fs.hh"
#include "common/polylib.hh"
#include "common/bsputils.hh"
static void PrintBSPTextureUsage(const mbsp_t &bsp)
{
std::unordered_map<std::string, vec_t> 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<std::tuple<std::string, vec_t>> 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<mbsp_t>(bsp.bsp), fs::path(source).replace_extension("bsp.json"));
PrintBSPTextureUsage(std::get<mbsp_t>(bsp.bsp));
printf("---------------------\n");
fs::clear();
}
return 0;
}

View File

@ -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

View File

@ -17,6 +17,7 @@
See file, 'COPYING', for details.
*/
#include <common/bspinfo.hh>
#include <common/cmdlib.hh>
#include <common/bspfile.hh>
@ -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<std::string, vec_t> 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<std::tuple<std::string, vec_t>> 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<mbsp_t>(bsp.bsp), fs::path(source).replace_extension("bsp.json"));
PrintBSPTextureUsage(std::get<mbsp_t>(bsp.bsp));
printf("---------------------\n");
fs::clear();
}
return 0;
}

25
include/common/bspinfo.hh Normal file
View File

@ -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);