From e5f9f21b30c54bbfa5fc786584ae05c11750239b Mon Sep 17 00:00:00 2001 From: Graham Pentheny Date: Tue, 20 Jan 2015 20:36:02 -0500 Subject: [PATCH] Removed FileList class and replaced it with std::vector in RecastDemo --- RecastDemo/Include/ChunkyTriMesh.h | 6 ++- RecastDemo/Include/Filelist.h | 14 ++---- RecastDemo/Include/SlideShow.h | 4 +- RecastDemo/Source/Filelist.cpp | 71 +++++++++--------------------- RecastDemo/Source/SlideShow.cpp | 7 +-- RecastDemo/Source/main.cpp | 40 +++++++++++------ 6 files changed, 63 insertions(+), 79 deletions(-) diff --git a/RecastDemo/Include/ChunkyTriMesh.h b/RecastDemo/Include/ChunkyTriMesh.h index 0336c1d..7f428b6 100644 --- a/RecastDemo/Include/ChunkyTriMesh.h +++ b/RecastDemo/Include/ChunkyTriMesh.h @@ -21,8 +21,10 @@ struct rcChunkyTriMeshNode { - float bmin[2], bmax[2]; - int i, n; + float bmin[2]; + float bmax[2]; + int i; + int n; }; struct rcChunkyTriMesh diff --git a/RecastDemo/Include/Filelist.h b/RecastDemo/Include/Filelist.h index 63c85b1..31c8d95 100644 --- a/RecastDemo/Include/Filelist.h +++ b/RecastDemo/Include/Filelist.h @@ -19,17 +19,9 @@ #ifndef FILELIST_H #define FILELIST_H -struct FileList -{ - static const int MAX_FILES = 256; - - FileList(); - ~FileList(); - - char* files[MAX_FILES]; - int size; -}; +#include +#include -void scanDirectory(const char* path, const char* ext, FileList& list); +void scanDirectory(std::string path, std::string ext, std::vector& fileList); #endif // FILELIST_H diff --git a/RecastDemo/Include/SlideShow.h b/RecastDemo/Include/SlideShow.h index c2f5d16..90f2a61 100644 --- a/RecastDemo/Include/SlideShow.h +++ b/RecastDemo/Include/SlideShow.h @@ -20,10 +20,12 @@ #define SLIDESHOW_H #include "Filelist.h" +#include +#include class SlideShow { - FileList m_files; + std::vector m_files; char m_path[256]; int m_width; diff --git a/RecastDemo/Source/Filelist.cpp b/RecastDemo/Source/Filelist.cpp index 9df3034..0af3cd6 100644 --- a/RecastDemo/Source/Filelist.cpp +++ b/RecastDemo/Source/Filelist.cpp @@ -17,84 +17,57 @@ // #include "Filelist.h" -#include -#include -#include + +#include #ifdef WIN32 # include #else # include +# include #endif -static void fileListAdd(FileList& list, const char* path) -{ - if (list.size >= FileList::MAX_FILES) - return; - int n = strlen(path); - list.files[list.size] = new char[n+1]; - strcpy(list.files[list.size], path); - list.size++; -} +using std::vector; +using std::string; -static void fileListClear(FileList& list) +void scanDirectory(string path, string ext, vector& filelist) { - for (int i = 0; i < list.size; ++i) - delete [] list.files[i]; - list.size = 0; -} - -FileList::FileList() : size(0) -{ - memset(files, 0, sizeof(char*)*MAX_FILES); -} - -FileList::~FileList() -{ - fileListClear(*this); -} - -static int cmp(const void* a, const void* b) -{ - return strcmp(*(const char**)a, *(const char**)b); -} - -void scanDirectory(const char* path, const char* ext, FileList& list) -{ - fileListClear(list); + filelist.clear(); #ifdef WIN32 + string pathWithExt = path + "/*" + ext; + _finddata_t dir; - char pathWithExt[260]; - long fh; - strcpy(pathWithExt, path); - strcat(pathWithExt, "/*"); - strcat(pathWithExt, ext); - fh = _findfirst(pathWithExt, &dir); + long fh = _findfirst(pathWithExt.c_str(), &dir); if (fh == -1L) + { return; + } + do { - fileListAdd(list, dir.name); + filelist.push_back(dir.name); } while (_findnext(fh, &dir) == 0); _findclose(fh); #else dirent* current = 0; - DIR* dp = opendir(path); + DIR* dp = opendir(path.c_str()); if (!dp) + { return; + } while ((current = readdir(dp)) != 0) { int len = strlen(current->d_name); - if (len > 4 && strncmp(current->d_name+len-4, ext, 4) == 0) + if (len > 4 && strncmp(current->d_name + len - 4, ext.c_str(), 4) == 0) { - fileListAdd(list, current->d_name); + filelist.push_back(current->d_name); } } closedir(dp); #endif - - if (list.size > 1) - qsort(list.files, list.size, sizeof(char*), cmp); + + // Sort the list of files alphabetically. + std::sort(filelist.begin(), filelist.end()); } diff --git a/RecastDemo/Source/SlideShow.cpp b/RecastDemo/Source/SlideShow.cpp index ca82cc4..b980a41 100644 --- a/RecastDemo/Source/SlideShow.cpp +++ b/RecastDemo/Source/SlideShow.cpp @@ -17,6 +17,7 @@ // #include "SlideShow.h" + #include #include #include "SDL_opengl.h" @@ -98,7 +99,7 @@ void SlideShow::prevSlide() void SlideShow::setSlide(int n) { - const int maxIdx = m_files.size ? m_files.size-1 : 0; + const int maxIdx = m_files.size() ? m_files.size() - 1 : 0; m_nextSlide = n; if (m_nextSlide < 0) m_nextSlide = 0; if (m_nextSlide > maxIdx) m_nextSlide = maxIdx; @@ -120,11 +121,11 @@ void SlideShow::updateAndDraw(float dt, const float w, const float h) if (m_curSlide != m_nextSlide && m_slideAlpha < 0.01f) { m_curSlide = m_nextSlide; - if (m_curSlide >= 0 && m_curSlide < m_files.size) + if (m_curSlide >= 0 && static_cast(m_curSlide) < m_files.size()) { char path[256]; strcpy(path, m_path); - strcat(path, m_files.files[m_curSlide]); + strcat(path, m_files[m_curSlide].c_str()); loadImage(path); } } diff --git a/RecastDemo/Source/main.cpp b/RecastDemo/Source/main.cpp index 382472c..68223dc 100644 --- a/RecastDemo/Source/main.cpp +++ b/RecastDemo/Source/main.cpp @@ -28,6 +28,9 @@ # include #endif +#include +#include + #include "imgui.h" #include "imguiRenderGL.h" @@ -47,6 +50,9 @@ # define putenv _putenv #endif +using std::string; +using std::vector; + struct SampleItem { Sample* (*create)(); @@ -163,7 +169,7 @@ int main(int /*argc*/, char** /*argv*/) char sampleName[64] = "Choose Sample..."; - FileList files; + vector files; char meshName[128] = "Choose Mesh..."; float markerPosition[3] = {0, 0, 0}; @@ -714,16 +720,20 @@ int main(int /*argc*/, char** /*argv*/) if (imguiBeginScrollArea("Choose Level", width - 10 - 250 - 10 - 200, height - 10 - 450, 200, 450, &levelScroll)) mouseOverMenu = true; - int levelToLoad = -1; - for (int i = 0; i < files.size; ++i) + vector::const_iterator fileIter = files.begin(); + vector::const_iterator filesEnd = files.end(); + vector::const_iterator levelToLoad = filesEnd; + for (; fileIter != filesEnd; ++fileIter) { - if (imguiItem(files.files[i])) - levelToLoad = i; + if (imguiItem(fileIter->c_str())) + { + levelToLoad = fileIter; + } } - if (levelToLoad != -1) + if (levelToLoad != filesEnd) { - strncpy(meshName, files.files[levelToLoad], sizeof(meshName)); + strncpy(meshName, levelToLoad->c_str(), sizeof(meshName)); meshName[sizeof(meshName)-1] = '\0'; showLevels = false; @@ -792,18 +802,22 @@ int main(int /*argc*/, char** /*argv*/) if (imguiBeginScrollArea("Choose Test To Run", width-10-250-10-200, height-10-450, 200, 450, &testScroll)) mouseOverMenu = true; - int testToLoad = -1; - for (int i = 0; i < files.size; ++i) + vector::const_iterator fileIter = files.begin(); + vector::const_iterator filesEnd = files.end(); + vector::const_iterator testToLoad = filesEnd; + for (; fileIter != filesEnd; ++fileIter) { - if (imguiItem(files.files[i])) - testToLoad = i; + if (imguiItem(fileIter->c_str())) + { + testToLoad = fileIter; + } } - if (testToLoad != -1) + if (testToLoad != filesEnd) { char path[256]; strcpy(path, "TestCases/"); - strcat(path, files.files[testToLoad]); + strcat(path, testToLoad->c_str()); test = new TestCase; if (test) {