diff --git a/RecastDemo/Include/Filelist.h b/RecastDemo/Include/Filelist.h deleted file mode 100644 index 7802aaee..00000000 --- a/RecastDemo/Include/Filelist.h +++ /dev/null @@ -1,25 +0,0 @@ -// -// Copyright (c) 2009-2010 Mikko Mononen memon@inside.org -// -// This software is provided 'as-is', without any express or implied -// warranty. In no event will the authors be held liable for any damages -// arising from the use of this software. -// Permission is granted to anyone to use this software for any purpose, -// including commercial applications, and to alter it and redistribute it -// freely, subject to the following restrictions: -// 1. The origin of this software must not be misrepresented; you must not -// claim that you wrote the original software. If you use this software -// in a product, an acknowledgment in the product documentation would be -// appreciated but is not required. -// 2. Altered source versions must be plainly marked as such, and must not be -// misrepresented as being the original software. -// 3. This notice may not be removed or altered from any source distribution. -// - -#pragma once - -#include -#include - -bool tryReadFile(const std::string& path, char** outBuffer, size_t* outBufferLen); -void scanDirectory(const std::string& path, const std::string& ext, std::vector& fileList); diff --git a/RecastDemo/Include/InputGeom.h b/RecastDemo/Include/InputGeom.h index 02dc4f04..2d3158ee 100644 --- a/RecastDemo/Include/InputGeom.h +++ b/RecastDemo/Include/InputGeom.h @@ -19,7 +19,6 @@ #pragma once #include "ChunkyTriMesh.h" -#include "MeshLoaderObj.h" #include @@ -115,10 +114,7 @@ private: public: InputGeom() = default; - ~InputGeom() - { - delete chunkyMesh; - } + ~InputGeom() { delete chunkyMesh; } InputGeom(const InputGeom&) = delete; InputGeom& operator=(const InputGeom&) = delete; InputGeom(InputGeom&&) = delete; diff --git a/RecastDemo/Include/SampleInterfaces.h b/RecastDemo/Include/SampleInterfaces.h index d14afd51..91a4c460 100644 --- a/RecastDemo/Include/SampleInterfaces.h +++ b/RecastDemo/Include/SampleInterfaces.h @@ -78,8 +78,8 @@ public: FileIO() = default; FileIO(const FileIO&) = delete; FileIO& operator=(const FileIO&) = delete; - FileIO(FileIO&&) = delete; - FileIO& operator=(FileIO&&) = delete; + FileIO(FileIO&&) = default; + FileIO& operator=(FileIO&&) = default; virtual ~FileIO(); bool openForWrite(const char* path); @@ -88,8 +88,11 @@ public: virtual bool isReading() const; virtual bool write(const void* ptr, const size_t size); virtual bool read(void* ptr, const size_t size); + size_t getFileSize(); + static void scanDirectory(const std::string& path, const std::string& ext, std::vector& fileList); private: FILE* fp = nullptr; - int mode = -1; + enum class Mode { none, reading, writing }; + Mode mode = Mode::none; }; diff --git a/RecastDemo/Source/Filelist.cpp b/RecastDemo/Source/Filelist.cpp deleted file mode 100644 index 63b87a20..00000000 --- a/RecastDemo/Source/Filelist.cpp +++ /dev/null @@ -1,109 +0,0 @@ -// -// Copyright (c) 2009-2010 Mikko Mononen memon@inside.org -// -// This software is provided 'as-is', without any express or implied -// warranty. In no event will the authors be held liable for any damages -// arising from the use of this software. -// Permission is granted to anyone to use this software for any purpose, -// including commercial applications, and to alter it and redistribute it -// freely, subject to the following restrictions: -// 1. The origin of this software must not be misrepresented; you must not -// claim that you wrote the original software. If you use this software -// in a product, an acknowledgment in the product documentation would be -// appreciated but is not required. -// 2. Altered source versions must be plainly marked as such, and must not be -// misrepresented as being the original software. -// 3. This notice may not be removed or altered from any source distribution. -// - -#include "Filelist.h" - -#include -#ifdef WIN32 -# include -#else -# include -# include -#endif - -bool tryReadFile(const std::string& path, char** outBuffer, size_t* outBufferLen) -{ - FILE* file = fopen(path.c_str(), "rb"); - if (!file) - { - return false; - } - - if (fseek(file, 0, SEEK_END) != 0) - { - (void)fclose(file); - return false; - } - - const int fileSize = ftell(file); - if (fileSize < 0) - { - (void)fclose(file); - return false; - } - *outBufferLen = fileSize; - - if (fseek(file, 0, SEEK_SET) != 0) - { - (void)fclose(file); - return false; - } - - *outBuffer = new char[*outBufferLen]; - const size_t readLen = fread(*outBuffer, *outBufferLen, 1, file); - (void)fclose(file); - if (readLen != 1) - { - delete[] *outBuffer; - *outBuffer = nullptr; - return false; - } - - return true; -} - -void scanDirectory(const std::string& path, const std::string& ext, std::vector& filelist) -{ -#ifdef WIN32 - std::string pathWithExt = path + "/*" + ext; - - _finddata_t dir; - intptr_t findHandle = _findfirst(pathWithExt.c_str(), &dir); - if (findHandle == -1L) - { - return; - } - - do - { - filelist.emplace_back(dir.name); - } while (_findnext(findHandle, &dir) == 0); - _findclose(findHandle); -#else - dirent* current = 0; - DIR* dp = opendir(path.c_str()); - if (!dp) - { - return; - } - - size_t extLen = strlen(ext.c_str()); - while ((current = readdir(dp)) != 0) - { - size_t len = strlen(current->d_name); - if (len > extLen && strncmp(current->d_name + len - extLen, ext.c_str(), extLen) == 0) - { - filelist.emplace_back(current->d_name); - } - } - closedir(dp); -#endif - - // Sort the list of files alphabetically. - std::sort(filelist.begin(), filelist.end()); -} diff --git a/RecastDemo/Source/InputGeom.cpp b/RecastDemo/Source/InputGeom.cpp index 35fb9f89..207b1c5c 100644 --- a/RecastDemo/Source/InputGeom.cpp +++ b/RecastDemo/Source/InputGeom.cpp @@ -136,13 +136,22 @@ static char* parseRow(char* buf, char* bufEnd, char* row, int len) bool InputGeom::loadMesh(rcContext* ctx, const std::string& filepath) { - char* buffer; - size_t bufferLen; - if (!tryReadFile(filepath, &buffer, &bufferLen)) + FileIO file; + if (!file.openForRead(filepath.c_str())) { ctx->log(RC_LOG_ERROR, "buildTiledNavigation: Could not load '%s'", filepath.c_str()); return false; } + + size_t bufferLen = file.getFileSize(); + char* buffer = new char[bufferLen]; + + if (!file.read(buffer, bufferLen)) + { + ctx->log(RC_LOG_ERROR, "buildTiledNavigation: Could not load '%s'", filepath.c_str()); + return false; + } + filename = filepath; delete chunkyMesh; diff --git a/RecastDemo/Source/SampleInterfaces.cpp b/RecastDemo/Source/SampleInterfaces.cpp index ad34aa28..3738bd53 100644 --- a/RecastDemo/Source/SampleInterfaces.cpp +++ b/RecastDemo/Source/SampleInterfaces.cpp @@ -4,11 +4,16 @@ #include "Recast.h" #include "SDL_opengl.h" +#include #include #include #ifdef WIN32 # define snprintf _snprintf +# include +#else +# include +# include #endif //////////////////////////////////////////////////////////////////////////////////////////////////// @@ -272,7 +277,7 @@ bool FileIO::openForWrite(const char* path) { return false; } - mode = 1; + mode = Mode::writing; return true; } @@ -287,23 +292,23 @@ bool FileIO::openForRead(const char* path) { return false; } - mode = 2; + mode = Mode::reading; return true; } bool FileIO::isWriting() const { - return mode == 1; + return mode == Mode::writing; } bool FileIO::isReading() const { - return mode == 2; + return mode == Mode::reading; } bool FileIO::write(const void* ptr, const size_t size) { - if (!fp || mode != 1) + if (!fp || mode != Mode::writing) { return false; } @@ -313,10 +318,70 @@ bool FileIO::write(const void* ptr, const size_t size) bool FileIO::read(void* ptr, const size_t size) { - if (!fp || mode != 2) + if (!fp || mode != Mode::reading) { return false; } size_t readLen = fread(ptr, size, 1, fp); return readLen == 1; } + +size_t FileIO::getFileSize() +{ + if (!fp || mode != Mode::reading) + { + return false; + } + size_t currentPos = ftell(fp); + if (fseek(fp, 0, SEEK_END) != 0) + { + return 0; + } + size_t size = ftell(fp); + if (fseek(fp, 0, currentPos) != 0) + { + return 0; + } + return size; +} + +void FileIO::scanDirectory(const std::string& path, const std::string& ext, std::vector& filelist) +{ +#ifdef WIN32 + std::string pathWithExt = path + "/*" + ext; + + _finddata_t dir; + intptr_t findHandle = _findfirst(pathWithExt.c_str(), &dir); + if (findHandle == -1L) + { + return; + } + + do + { + filelist.emplace_back(dir.name); + } while (_findnext(findHandle, &dir) == 0); + _findclose(findHandle); +#else + dirent* current = 0; + DIR* dp = opendir(path.c_str()); + if (!dp) + { + return; + } + + size_t extLen = strlen(ext.c_str()); + while ((current = readdir(dp)) != 0) + { + size_t len = strlen(current->d_name); + if (len > extLen && strncmp(current->d_name + len - extLen, ext.c_str(), extLen) == 0) + { + filelist.emplace_back(current->d_name); + } + } + closedir(dp); +#endif + + // Sort the list of files alphabetically. + std::sort(filelist.begin(), filelist.end()); +} diff --git a/RecastDemo/Source/main.cpp b/RecastDemo/Source/main.cpp index eda4c612..f8254e96 100644 --- a/RecastDemo/Source/main.cpp +++ b/RecastDemo/Source/main.cpp @@ -276,7 +276,7 @@ int main(int /*argc*/, char** /*argv*/) //---------------------------------------------------------------------------- - scanDirectory(app.meshesFolder, ".obj", app.files); + FileIO::scanDirectory(app.meshesFolder, ".obj", app.files); app.meshName = app.files[0]; app.inputGeometry = new InputGeom; app.inputGeometry->load(&app.buildContext, app.meshesFolder + "/" + app.meshName); @@ -326,7 +326,7 @@ int main(int /*argc*/, char** /*argv*/) case SDLK_t: app.showTestCases = true; app.files.clear(); - scanDirectory(app.testCasesFolder, ".txt", app.files); + FileIO::scanDirectory(app.testCasesFolder, ".txt", app.files); break; case SDLK_TAB: app.showMenu = !app.showMenu; @@ -660,8 +660,8 @@ int main(int /*argc*/, char** /*argv*/) if (ImGui::BeginCombo("##levelCombo", app.meshName.c_str(), 0)) { app.files.clear(); - scanDirectory(app.meshesFolder, ".obj", app.files); - scanDirectory(app.meshesFolder, ".gset", app.files); + FileIO::scanDirectory(app.meshesFolder, ".obj", app.files); + FileIO::scanDirectory(app.meshesFolder, ".gset", app.files); for (const auto& file : app.files) {