From e2d64e0e5d7d5dafba836e9d5eefb606d00d3d89 Mon Sep 17 00:00:00 2001 From: Jonathan Date: Mon, 20 Dec 2021 14:51:14 -0500 Subject: [PATCH] FS simplification Fix transparency calculation (33% is 33% opacity, not 33% transparency) --- common/fs.cc | 112 ------------------------------------------ include/common/fs.hh | 3 -- include/qbsp/file.hh | 24 --------- include/qbsp/qbsp.hh | 2 - light/trace_embree.cc | 2 +- qbsp/CMakeLists.txt | 2 - qbsp/file.cc | 59 ---------------------- qbsp/map.cc | 48 +++++++++--------- qbsp/qbsp.cc | 19 +++---- 9 files changed, 33 insertions(+), 238 deletions(-) delete mode 100644 include/qbsp/file.hh delete mode 100644 qbsp/file.cc diff --git a/common/fs.cc b/common/fs.cc index dd999986..a0247e4c 100644 --- a/common/fs.cc +++ b/common/fs.cc @@ -399,115 +399,3 @@ long SafeTell(const qfile_t &f) { return ftell(f.get()); } - -struct pakheader_t -{ - char magic[4]; - unsigned int tableofs; - unsigned int numfiles; -}; - -struct pakfile_t -{ - char name[56]; - unsigned int offset; - unsigned int length; -}; - -/* - * ============== - * LoadFilePak - * reads a file directly out of a pak, to make re-lighting friendlier - * writes to the filename, stripping the pak part of the name - * ============== - */ -long LoadFilePak(std::filesystem::path &filename, void *destptr) -{ - // check if we have a .pak file in this path - for (auto p = filename.parent_path(); !p.empty() && p != p.root_path(); p = p.parent_path()) { - if (p.extension() == ".pak") { - qfile_t file = SafeOpenRead(p); - - // false positive - if (!file) - continue; - - // got one; calculate the relative remaining path - auto innerfile = filename.lexically_relative(p); - - uint8_t **bufferptr = static_cast(destptr); - pakheader_t header; - long length = -1; - SafeRead(file, &header, sizeof(header)); - - header.numfiles = LittleLong(header.numfiles) / sizeof(pakfile_t); - header.tableofs = LittleLong(header.tableofs); - - if (!strncmp(header.magic, "PACK", 4)) { - pakfile_t *files = new pakfile_t[header.numfiles]; - - SafeSeek(file, header.tableofs, SEEK_SET); - SafeRead(file, files, header.numfiles * sizeof(*files)); - - for (uint32_t i = 0; i < header.numfiles; i++) { - if (innerfile == files[i].name) { - SafeSeek(file, files[i].offset, SEEK_SET); - *bufferptr = new uint8_t[files[i].length + 1]; - SafeRead(file, *bufferptr, files[i].length); - length = files[i].length; - break; - } - } - delete[] files; - } - - if (length < 0) - FError("Unable to find '{}' inside '{}'", innerfile, filename); - - filename = innerfile; - return length; - } - } - - // not in a pak, so load it normally - return LoadFile(filename, destptr); -} - -/* - * =========== - * filelength - * =========== - */ -static long Sys_FileLength(const qfile_t &f) -{ - long pos = ftell(f.get()); - fseek(f.get(), 0, SEEK_END); - long end = ftell(f.get()); - fseek(f.get(), pos, SEEK_SET); - - return end; -} -/* - * ============== - * LoadFile - * ============== - */ -long LoadFile(const std::filesystem::path &filename, void *destptr) -{ - uint8_t **bufferptr = static_cast(destptr); - - qfile_t file = SafeOpenRead(filename, true); - - long length = Sys_FileLength(file); - - uint8_t *buffer = *bufferptr = new uint8_t[length + 1]; - - if (!buffer) - FError("allocation of {} bytes failed.", length); - - SafeRead(file, buffer, length); - - buffer[length] = 0; - - return length; -} \ No newline at end of file diff --git a/include/common/fs.hh b/include/common/fs.hh index 07ef6a55..293b19ff 100644 --- a/include/common/fs.hh +++ b/include/common/fs.hh @@ -134,6 +134,3 @@ size_t SafeRead(const qfile_t &f, void *buffer, size_t count); size_t SafeWrite(const qfile_t &f, const void *buffer, size_t count); void SafeSeek(const qfile_t &f, long offset, int32_t origin); long SafeTell(const qfile_t &f); - -long LoadFilePak(std::filesystem::path &filename, void *destptr); -long LoadFile(const std::filesystem::path &filename, void *destptr); \ No newline at end of file diff --git a/include/qbsp/file.hh b/include/qbsp/file.hh deleted file mode 100644 index 0fb49931..00000000 --- a/include/qbsp/file.hh +++ /dev/null @@ -1,24 +0,0 @@ -/* - Copyright (C) 1996-1997 Id Software, Inc. - Copyright (C) 1997 Greg Lewis - - 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. -*/ - -#pragma once - -size_t LoadFile(const std::filesystem::path &filename, void *bufptr, bool nofail); diff --git a/include/qbsp/qbsp.hh b/include/qbsp/qbsp.hh index 1664853e..eab83fdd 100644 --- a/include/qbsp/qbsp.hh +++ b/include/qbsp/qbsp.hh @@ -42,8 +42,6 @@ #include #include -#include "file.hh" - /* * Clipnodes need to be stored as a 16-bit offset. Originally, this was a * signed value and only the positive values up to 32767 were available. Since diff --git a/light/trace_embree.cc b/light/trace_embree.cc index f2cc1152..d0799f20 100644 --- a/light/trace_embree.cc +++ b/light/trace_embree.cc @@ -354,7 +354,7 @@ static void Embree_FilterFuncN(const struct RTCFilterFunctionNArguments *args) Q2_SURF_TRANSLUCENT); // KMQuake 2-specific. Use texture alpha chanel when both flags are set. isGlass = !isFence && (surf_flags & Q2_SURF_TRANSLUCENT); if (isGlass) - alpha = (surf_flags & Q2_SURF_TRANS33 ? 0.66f : 0.33f); + alpha = (surf_flags & Q2_SURF_TRANS33 ? 0.33f : 0.66f); } else { const char *name = Face_TextureName(bsp_static, face); isFence = (name[0] == '{'); diff --git a/qbsp/CMakeLists.txt b/qbsp/CMakeLists.txt index a0ec4024..6525c62c 100644 --- a/qbsp/CMakeLists.txt +++ b/qbsp/CMakeLists.txt @@ -1,5 +1,4 @@ set(QBSP_INCLUDES - ${CMAKE_SOURCE_DIR}/include/qbsp/file.hh ${CMAKE_SOURCE_DIR}/include/qbsp/qbsp.hh ${CMAKE_SOURCE_DIR}/include/qbsp/wad.hh ${CMAKE_SOURCE_DIR}/include/qbsp/brush.hh @@ -17,7 +16,6 @@ set(QBSP_INCLUDES set(QBSP_SOURCES ${CMAKE_SOURCE_DIR}/qbsp/brush.cc ${CMAKE_SOURCE_DIR}/qbsp/csg4.cc - ${CMAKE_SOURCE_DIR}/qbsp/file.cc ${CMAKE_SOURCE_DIR}/qbsp/globals.cc ${CMAKE_SOURCE_DIR}/qbsp/map.cc ${CMAKE_SOURCE_DIR}/qbsp/merge.cc diff --git a/qbsp/file.cc b/qbsp/file.cc deleted file mode 100644 index 108d441f..00000000 --- a/qbsp/file.cc +++ /dev/null @@ -1,59 +0,0 @@ -/* - Copyright (C) 1996-1997 Id Software, Inc. - Copyright (C) 1997 Greg Lewis - - 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 - -/* -============== -LoadFile -============== -*/ -size_t LoadFile(const std::filesystem::path &filename, void *bufptr, bool nofail) -{ - char **buf = (char **)bufptr; - size_t len; - FILE *f; - - f = fopen(filename.string().c_str(), "rb"); - if (!f) { - if (nofail) - FError("Failed to open {}: {}", filename, strerror(errno)); - return 0; - } - - fseek(f, 0, SEEK_END); - len = ftell(f); - fseek(f, 0, SEEK_SET); - - *buf = new char[len + 1]; - (*buf)[len] = 0; - - if (fread(*buf, 1, len, f) != len) - FError("Failure reading from file"); - - fclose(f); - - return len; -} diff --git a/qbsp/map.cc b/qbsp/map.cc index 16ecb7b7..d66ef9fa 100644 --- a/qbsp/map.cc +++ b/qbsp/map.cc @@ -1839,12 +1839,15 @@ bool IsNonRemoveWorldBrushEntity(const mapentity_t *entity) */ mapentity_t LoadExternalMap(const char *filename) { - char *buf; - int length; mapentity_t dest{}; - length = LoadFile(filename, &buf, true); - parser_t parser(buf); + auto file = fs::load(filename); + + if (!file) { + FError("Couldn't load external map file \"{}.\"\n", filename); + } + + parser_t parser(reinterpret_cast(file->data())); // parse the worldspawn if (!ParseEntity(parser, &dest)) { @@ -1877,34 +1880,35 @@ mapentity_t LoadExternalMap(const char *filename) LogPrint(LOG_STAT, " {}: '{}': Loaded {} mapbrushes.\n", __func__, filename, dest.nummapbrushes); - delete[] buf; - return dest; } void LoadMapFile(void) { - char *buf; - int length; - LogPrint(LOG_PROGRESS, "---- {} ----\n", __func__); - length = LoadFile(options.szMapName, &buf, true); - parser_t parser(buf); + { + auto file = fs::load(options.szMapName); - for (int i = 0;; i++) { - mapentity_t &entity = map.entities.emplace_back(); - - if (!ParseEntity(parser, &entity)) { - break; + if (!file) { + FError("Couldn't load map file \"{}.\"\n", options.szMapName); + return; } - } - // Remove dummy entity inserted above - assert(!map.entities.back().epairs.size()); - assert(map.entities.back().numbrushes == 0); - map.entities.pop_back(); - delete[] buf; + parser_t parser(reinterpret_cast(file->data())); + + for (int i = 0;; i++) { + mapentity_t &entity = map.entities.emplace_back(); + + if (!ParseEntity(parser, &entity)) { + break; + } + } + // Remove dummy entity inserted above + assert(!map.entities.back().epairs.size()); + assert(map.entities.back().numbrushes == 0); + map.entities.pop_back(); + } // Print out warnings for entities if (!(rgfStartSpots & info_player_start)) diff --git a/qbsp/qbsp.cc b/qbsp/qbsp.cc index 840f515d..9d7c023f 100644 --- a/qbsp/qbsp.cc +++ b/qbsp/qbsp.cc @@ -1392,27 +1392,20 @@ InitQBSP */ static void InitQBSP(int argc, const char **argv) { - int i; - char *szBuf; - int length; - - length = LoadFile("qbsp.ini", &szBuf, false); - if (length) { + if (auto file = fs::load("qbsp.ini")) { LogPrint("Loading options from qbsp.ini\n"); - ParseOptions(szBuf); - - delete[] szBuf; + ParseOptions(reinterpret_cast(file->data())); } // Concatenate command line args - length = 1; - for (i = 1; i < argc; i++) { + int length = 1; + for (int i = 1; i < argc; i++) { length += strlen(argv[i]) + 1; if (argv[i][0] != '-') length += 2; /* quotes */ } - szBuf = new char[length]{}; - for (i = 1; i < argc; i++) { + char *szBuf = new char[length]{}; + for (int i = 1; i < argc; i++) { /* Quote filenames for the parsing function */ if (argv[i][0] != '-') strcat(szBuf, "\"");