Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/WolfireGames/overgrowth.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorredruin1 <tfschaefer222@gmail.com>2022-10-17 09:12:24 +0300
committerredruin1 <tfschaefer222@gmail.com>2022-10-17 09:12:24 +0300
commit5ff4ae6485dcb4d82eadcebaffca96057a0e841d (patch)
tree09562123777f9ec083ed1490ce711e2d5ca44ff4
parent9becdaf628478502b3a2157b0361fa6aa9792002 (diff)
fixed stbi heightmap loading
minor formatting changes as well
-rw-r--r--Source/AI/navmesh.cpp20
-rw-r--r--Source/Asset/Asset/image_sampler.cpp8
-rw-r--r--Source/Graphics/heightmap.cpp14
-rw-r--r--Source/Internal/zip_util.cpp31
4 files changed, 53 insertions, 20 deletions
diff --git a/Source/AI/navmesh.cpp b/Source/AI/navmesh.cpp
index e8ce7c94..2f0d2e95 100644
--- a/Source/AI/navmesh.cpp
+++ b/Source/AI/navmesh.cpp
@@ -566,15 +566,15 @@ void NavMesh::Save(const string& level_name, const Path& level_path) {
string nav_path = GenerateParallelPath("Data/Levels", "Data/LevelNavmeshes", ".nav", level_path);
- if (config["allow_game_dir_save"].toBool() == false) {
+ if (config["allow_game_dir_save"].toBool() == false) { // change save location to user directory
nav_path = AssemblePath(GetWritePath(level_path.GetModsource()), nav_path);
}
- char zip_path[kPathSize];
+ char zip_path[kPathSize]; // Path of the outermost file
FormatString(zip_path, kPathSize, "%s.zip", nav_path.c_str());
- char in_zip_file_name[kPathSize];
- FormatString(in_zip_file_name, kPathSize, "%s.zip", level_name.c_str());
+ char in_zip_file_name[kPathSize]; // Name of the file inside the outermost zip file
+ FormatString(in_zip_file_name, kPathSize, "%s.nav", level_name.c_str()); // used to be .zip
LOGI << "Saving nav mesh to " << zip_path << endl;
@@ -582,18 +582,22 @@ void NavMesh::Save(const string& level_name, const Path& level_path) {
CreateParentDirs(temp_navmesh_path);
+ // Save the .NAV file to the Temp directory
sample_tile_mesh_.Save(temp_navmesh_path.c_str());
CreateParentDirs(zip_path);
+ // Copy the .NAV file from the Temp directory and save to the Zip file as 'level_name.zip' (why .zip?)
Zip(temp_navmesh_path, string(zip_path), in_zip_file_name, _YES_OVERWRITE);
+ // Write the .OBJ file to the LevelNavmeshes directory
if (!vertices_.empty() && !faces_.empty()) {
char model_path[kPathSize];
FormatString(model_path, kPathSize, "%s.obj", nav_path.c_str());
WriteObj(model_path, vertices_, faces_);
}
+ // Write the .XML file to the LevelNavmeshes directory
char meta_path[kPathSize];
FormatString(meta_path, kPathSize, "%s.xml", nav_path.c_str());
@@ -751,6 +755,7 @@ bool NavMesh::Load(const string& level_name, const Path& level_path) {
bool NavMesh::LoadFromAbs(const Path& level_path, const char* abs_meta_path, const char* abs_model_path, const char* abs_nav_path, const char* abs_zip_path, bool has_zip) {
{
+ // Load the XML file
PROFILER_ZONE(g_profiler_ctx, "Loading XML file");
TiXmlDocument doc(abs_meta_path);
doc.LoadFile();
@@ -835,10 +840,13 @@ bool NavMesh::LoadFromAbs(const Path& level_path, const char* abs_meta_path, con
PROFILER_ZONE(g_profiler_ctx, "handleMeshChanged");
sample_tile_mesh_.handleMeshChanged(&geom_);
}
+
{
- if (has_zip) {
+ if (has_zip) { // Attempt to load data from the zip file
ExpandedZipFile ezf;
- UnZip(abs_zip_path, ezf);
+ UnZip(abs_zip_path, ezf);
+
+ LOGI << "success!" << std::endl;
if (ezf.GetNumEntries() == 1) {
const char* filename;
diff --git a/Source/Asset/Asset/image_sampler.cpp b/Source/Asset/Asset/image_sampler.cpp
index 08579fba..19cc5007 100644
--- a/Source/Asset/Asset/image_sampler.cpp
+++ b/Source/Asset/Asset/image_sampler.cpp
@@ -115,9 +115,11 @@ int ImageSampler::Load(const std::string& path, uint32_t load_flags) {
int n = 0;
// Tell stb_image that we want 4 components (RGBA)
- stbi_uc* data = stbi_load(abs_path, &width_, &width_, &n, 4);
+ stbi_uc* data = stbi_load(abs_path, &width_, &height_, &n, 4);
- if (data == nullptr || n != 4) {
+ // stbi will always ensure that the total output channels will match the specified (4); 'n' will contain the source images' channels
+ // So we only need to throw an error if the data doesn't exist, it's format is guaranteed
+ if (data == nullptr) { // || n != 4
return kLoadErrorGeneralFileError;
}
@@ -139,7 +141,7 @@ const char* ImageSampler::GetLoadErrorString() {
case 0:
return "";
default:
- return "Unknown error";
+ return stbi_failure_reason();
}
}
diff --git a/Source/Graphics/heightmap.cpp b/Source/Graphics/heightmap.cpp
index e4119265..b5f6d49e 100644
--- a/Source/Graphics/heightmap.cpp
+++ b/Source/Graphics/heightmap.cpp
@@ -92,9 +92,12 @@ bool HeightmapImage::LoadData(const std::string& rel_path, HMScale scaled) {
FindFilePath(rel_path.c_str(), abs_path, kPathSize, kDataPaths | kModPaths, true, NULL, &modsource);
modsource_ = modsource;
- // Tell stb_image that we want 4 components (RGBA)
+ // Tell stb to load with positive Y instead of negative Y
+ // FIXME: find a permanent home for this
+ stbi_set_flip_vertically_on_load(true);
+
int img_width = 0, img_height = 0, num_comp = 0;
- float* data = stbi_loadf(abs_path, &img_width, &img_height, &num_comp, 0);
+ unsigned short* data = stbi_load_16(abs_path, &img_width, &img_height, &num_comp, 0);
if (data == NULL) {
FatalError("Error", "Could not load heightmap: %s", rel_path.c_str());
@@ -118,11 +121,12 @@ bool HeightmapImage::LoadData(const std::string& rel_path, HMScale scaled) {
float x_scale = img_width / (float)width_;
float z_scale = img_height / (float)depth_;
- if (num_comp != 1) { // monochrome texture
+ if (num_comp == 1) { // monochrome texture
for (int z = 0; z < depth_; z++) { // flipped
- float* bits = &data[((int)(z * z_scale)) * img_height];
+ unsigned short* bits = &data[((int)(z * z_scale)) * img_height];
for (int x = 0; x < width_; x++) {
- height_data_[x + (((depth_ - 1) - z) * width_)] = (bits[(int)(x * x_scale)] * 65535.0f) / scale_factor;
+ // Convert unsigned shorts to floats
+ height_data_[x + (((depth_ - 1) - z) * width_)] = (float)(bits[(int)(x * x_scale)]) / scale_factor;
}
}
} else {
diff --git a/Source/Internal/zip_util.cpp b/Source/Internal/zip_util.cpp
index f7dbffca..bac7202f 100644
--- a/Source/Internal/zip_util.cpp
+++ b/Source/Internal/zip_util.cpp
@@ -113,10 +113,11 @@ void ZipFile::AddFile(const std::string& src_file_path,
}
{
- FILE* file;
+ FILE* file = nullptr;
file = FOPEN_FUNC(src_file_path.c_str(), "rb");
if (file == NULL) {
FatalError("Error", "Could not open file: %s", src_file_path.c_str());
+ return;
}
int size_read = 0;
@@ -188,7 +189,7 @@ struct ExpandedZipEntry {
unsigned size;
};
-ExpandedZipFile::ExpandedZipFile() : buf(NULL), filename_buf(NULL), entries(NULL) {}
+ExpandedZipFile::ExpandedZipFile() : buf(NULL), filename_buf(NULL), entries(NULL), num_entries(NULL) {}
ExpandedZipFile::~ExpandedZipFile() {
Dispose();
@@ -395,12 +396,17 @@ void UnZipFile::ExtractAll(ExpandedZipFile& expanded_zip_file) {
unsigned entry_id = 0;
unsigned data_offset = 0;
unsigned filename_offset = 0;
- ScopedBuffer scoped_buf(size_buf);
+ ScopedBuffer scoped_buf(size_buf); // raw malloc scoped to be less dangerous
unzGoToFirstFile(uf);
do {
+ int result;
char filename_buf[256];
+
unz_file_info file_info;
- unzGetCurrentFileInfo(uf, &file_info, filename_buf, 256, NULL, 0, NULL, 0);
+ result = unzGetCurrentFileInfo(uf, &file_info, filename_buf, 256, NULL, 0, NULL, 0);
+ if (result != UNZ_OK) {
+ FatalError("Error", "Unable to get zip file info (Error code: %d)", result);
+ }
if (file_info.size_filename > 256) {
FatalError("Error", "Zip file contains filename with length greater than 256");
}
@@ -409,12 +415,23 @@ void UnZipFile::ExtractAll(ExpandedZipFile& expanded_zip_file) {
expanded_zip_file.SetFilename(filename_offset, filename_buf, file_info.size_filename + 1);
filename_offset += file_info.size_filename + 1;
- unzOpenCurrentFile(uf);
+ /*LOGI << file_info.version << std::endl;
+ LOGI << file_info.uncompressed_size << std::endl;
+ LOGI << filename_buf << std::endl;*/
+
+ result = unzOpenCurrentFile(uf);
+ if (result != UNZ_OK) {
+ FatalError("Error", "Unable to open current zipped file (Error code: %d)", result);
+ // TODO: get better user errors instead of codes
+ }
+
int bytes_read = 0;
do {
bytes_read = unzReadCurrentFile(uf, scoped_buf.ptr, size_buf);
+ LOGI << bytes_read << std::endl; // failure
if (bytes_read < 0) {
- FatalError("Error", "Error reading from UnZip file");
+ FatalError("Error", "Error reading from UnZip file (Error code: %d)", bytes_read);
+ // TODO: get better user errors instead of codes
} else if (bytes_read > 0) {
expanded_zip_file.SetData(data_offset, (char*)scoped_buf.ptr, bytes_read);
data_offset += bytes_read;
@@ -426,6 +443,8 @@ void UnZipFile::ExtractAll(ExpandedZipFile& expanded_zip_file) {
unzCloseCurrentFile(uf);
} while (unzGoToNextFile(uf) == UNZ_OK);
}
+ // Don't forget to clean up afterwards!
+ unzClose(uf);
}
void UnZip(const std::string& zip_file_path,