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
{
float bmin[2], bmax[2];
int i, n;
float bmin[2];
float bmax[2];
int i;
int n;
};
struct rcChunkyTriMesh

View File

@ -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 <vector>
#include <string>
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

View File

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

View File

@ -17,84 +17,57 @@
//
#include "Filelist.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#ifdef WIN32
# include <io.h>
#else
# include <dirent.h>
# include <cstring>
#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<string>& 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());
}

View File

@ -17,6 +17,7 @@
//
#include "SlideShow.h"
#include <string.h>
#include <stdio.h>
#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<std::size_t>(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);
}
}

View File

@ -28,6 +28,9 @@
# include <GL/glu.h>
#endif
#include <vector>
#include <string>
#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<string> 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<string>::const_iterator fileIter = files.begin();
vector<string>::const_iterator filesEnd = files.end();
vector<string>::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<string>::const_iterator fileIter = files.begin();
vector<string>::const_iterator filesEnd = files.end();
vector<string>::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)
{