FS simplification
Fix transparency calculation (33% is 33% opacity, not 33% transparency)
This commit is contained in:
parent
0c827208ec
commit
e2d64e0e5d
112
common/fs.cc
112
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<uint8_t **>(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<uint8_t **>(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;
|
||||
}
|
||||
|
|
@ -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);
|
||||
|
|
@ -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);
|
||||
|
|
@ -42,8 +42,6 @@
|
|||
#include <common/bspfile.hh>
|
||||
#include <common/aabb.hh>
|
||||
|
||||
#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
|
||||
|
|
|
|||
|
|
@ -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] == '{');
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
59
qbsp/file.cc
59
qbsp/file.cc
|
|
@ -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 <cstdarg>
|
||||
//#include <cstdio>
|
||||
|
||||
#include <qbsp/qbsp.hh>
|
||||
#include <qbsp/file.hh>
|
||||
|
||||
/*
|
||||
==============
|
||||
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;
|
||||
}
|
||||
48
qbsp/map.cc
48
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<const char *>(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<const char *>(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))
|
||||
|
|
|
|||
19
qbsp/qbsp.cc
19
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<char *>(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, "\"");
|
||||
|
|
|
|||
Loading…
Reference in New Issue