Merge pull request #75 from grahamboree/demo_project_improvements

Replaced FileList class with std::vector<std::string>
This commit is contained in:
Ben Hymers 2016-01-08 07:53:37 +00:00
commit 26f2650df8
6 changed files with 63 additions and 79 deletions

View File

@ -21,8 +21,10 @@
struct rcChunkyTriMeshNode struct rcChunkyTriMeshNode
{ {
float bmin[2], bmax[2]; float bmin[2];
int i, n; float bmax[2];
int i;
int n;
}; };
struct rcChunkyTriMesh struct rcChunkyTriMesh

View File

@ -19,17 +19,9 @@
#ifndef FILELIST_H #ifndef FILELIST_H
#define FILELIST_H #define FILELIST_H
struct FileList #include <vector>
{ #include <string>
static const int MAX_FILES = 256;
FileList();
~FileList();
char* files[MAX_FILES];
int size;
};
void scanDirectory(const char* path, const char* ext, FileList& list); void scanDirectory(std::string path, std::string ext, std::vector<std::string>& fileList);
#endif // FILELIST_H #endif // FILELIST_H

View File

@ -20,10 +20,12 @@
#define SLIDESHOW_H #define SLIDESHOW_H
#include "Filelist.h" #include "Filelist.h"
#include <vector>
#include <string>
class SlideShow class SlideShow
{ {
FileList m_files; std::vector<std::string> m_files;
char m_path[256]; char m_path[256];
int m_width; int m_width;

View File

@ -17,84 +17,57 @@
// //
#include "Filelist.h" #include "Filelist.h"
#include <stdio.h>
#include <stdlib.h> #include <algorithm>
#include <string.h>
#ifdef WIN32 #ifdef WIN32
# include <io.h> # include <io.h>
#else #else
# include <dirent.h> # include <dirent.h>
# include <cstring>
#endif #endif
static void fileListAdd(FileList& list, const char* path) using std::vector;
{ using std::string;
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++;
}
static void fileListClear(FileList& list) void scanDirectory(string path, string ext, vector<string>& filelist)
{ {
for (int i = 0; i < list.size; ++i) filelist.clear();
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);
#ifdef WIN32 #ifdef WIN32
string pathWithExt = path + "/*" + ext;
_finddata_t dir; _finddata_t dir;
char pathWithExt[260]; long fh = _findfirst(pathWithExt.c_str(), &dir);
long fh;
strcpy(pathWithExt, path);
strcat(pathWithExt, "/*");
strcat(pathWithExt, ext);
fh = _findfirst(pathWithExt, &dir);
if (fh == -1L) if (fh == -1L)
{
return; return;
}
do do
{ {
fileListAdd(list, dir.name); filelist.push_back(dir.name);
} }
while (_findnext(fh, &dir) == 0); while (_findnext(fh, &dir) == 0);
_findclose(fh); _findclose(fh);
#else #else
dirent* current = 0; dirent* current = 0;
DIR* dp = opendir(path); DIR* dp = opendir(path.c_str());
if (!dp) if (!dp)
{
return; return;
}
while ((current = readdir(dp)) != 0) while ((current = readdir(dp)) != 0)
{ {
int len = strlen(current->d_name); 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); closedir(dp);
#endif #endif
if (list.size > 1) // Sort the list of files alphabetically.
qsort(list.files, list.size, sizeof(char*), cmp); std::sort(filelist.begin(), filelist.end());
} }

View File

@ -17,6 +17,7 @@
// //
#include "SlideShow.h" #include "SlideShow.h"
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include "SDL_opengl.h" #include "SDL_opengl.h"
@ -98,7 +99,7 @@ void SlideShow::prevSlide()
void SlideShow::setSlide(int n) 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; m_nextSlide = n;
if (m_nextSlide < 0) m_nextSlide = 0; if (m_nextSlide < 0) m_nextSlide = 0;
if (m_nextSlide > maxIdx) m_nextSlide = maxIdx; 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) if (m_curSlide != m_nextSlide && m_slideAlpha < 0.01f)
{ {
m_curSlide = m_nextSlide; m_curSlide = m_nextSlide;
if (m_curSlide >= 0 && m_curSlide < m_files.size) if (m_curSlide >= 0 && static_cast<std::size_t>(m_curSlide) < m_files.size())
{ {
char path[256]; char path[256];
strcpy(path, m_path); strcpy(path, m_path);
strcat(path, m_files.files[m_curSlide]); strcat(path, m_files[m_curSlide].c_str());
loadImage(path); loadImage(path);
} }
} }

View File

@ -28,6 +28,9 @@
# include <GL/glu.h> # include <GL/glu.h>
#endif #endif
#include <vector>
#include <string>
#include "imgui.h" #include "imgui.h"
#include "imguiRenderGL.h" #include "imguiRenderGL.h"
@ -47,6 +50,9 @@
# define putenv _putenv # define putenv _putenv
#endif #endif
using std::string;
using std::vector;
struct SampleItem struct SampleItem
{ {
Sample* (*create)(); Sample* (*create)();
@ -163,7 +169,7 @@ int main(int /*argc*/, char** /*argv*/)
char sampleName[64] = "Choose Sample..."; char sampleName[64] = "Choose Sample...";
FileList files; vector<string> files;
char meshName[128] = "Choose Mesh..."; char meshName[128] = "Choose Mesh...";
float markerPosition[3] = {0, 0, 0}; 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)) if (imguiBeginScrollArea("Choose Level", width - 10 - 250 - 10 - 200, height - 10 - 450, 200, 450, &levelScroll))
mouseOverMenu = true; mouseOverMenu = true;
int levelToLoad = -1; vector<string>::const_iterator fileIter = files.begin();
for (int i = 0; i < files.size; ++i) vector<string>::const_iterator filesEnd = files.end();
vector<string>::const_iterator levelToLoad = filesEnd;
for (; fileIter != filesEnd; ++fileIter)
{ {
if (imguiItem(files.files[i])) if (imguiItem(fileIter->c_str()))
levelToLoad = i; {
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'; meshName[sizeof(meshName)-1] = '\0';
showLevels = false; 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)) if (imguiBeginScrollArea("Choose Test To Run", width-10-250-10-200, height-10-450, 200, 450, &testScroll))
mouseOverMenu = true; mouseOverMenu = true;
int testToLoad = -1; vector<string>::const_iterator fileIter = files.begin();
for (int i = 0; i < files.size; ++i) vector<string>::const_iterator filesEnd = files.end();
vector<string>::const_iterator testToLoad = filesEnd;
for (; fileIter != filesEnd; ++fileIter)
{ {
if (imguiItem(files.files[i])) if (imguiItem(fileIter->c_str()))
testToLoad = i; {
testToLoad = fileIter;
}
} }
if (testToLoad != -1) if (testToLoad != filesEnd)
{ {
char path[256]; char path[256];
strcpy(path, "TestCases/"); strcpy(path, "TestCases/");
strcat(path, files.files[testToLoad]); strcat(path, testToLoad->c_str());
test = new TestCase; test = new TestCase;
if (test) if (test)
{ {