Skip to content

Commit

Permalink
A small fix for latest compiler on DX12. Some other utility tweaks, a…
Browse files Browse the repository at this point in the history
…nd a cleaner timer
  • Loading branch information
cmaughan committed Oct 25, 2017
1 parent e4d146f commit f127fac
Show file tree
Hide file tree
Showing 22 changed files with 577 additions and 51 deletions.
17 changes: 7 additions & 10 deletions mcommon/animation/timer.cpp
Expand Up @@ -18,18 +18,15 @@ float Timer::GetTime() const
return float(time / 1000000.0);
}

void Timer::Restart()
{
m_startTime = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count();
}

float Timer::GetDelta(TimerSample sample)
float Timer::GetDelta() const
{
auto now = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count();
auto diff = now - m_startTime;
if (sample == TimerSample::Restart)
{
Restart();
}
return float(diff / 1000000.0);
}

void Timer::Restart()
{
m_startTime = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count();
}

4 changes: 2 additions & 2 deletions mcommon/animation/timer.h
Expand Up @@ -11,11 +11,11 @@ class Timer
public:
Timer();
static Timer& GlobalTimer();
float GetTime() const;

void Restart();

float GetDelta(TimerSample sample = TimerSample::Restart);
float GetTime() const;
float GetDelta() const;

private:
int64_t m_startTime;
Expand Down
108 changes: 107 additions & 1 deletion mcommon/file/fileutils.cpp
@@ -1,6 +1,15 @@
#include "mcommon.h"

#if TARGET_PC
#include <windows.h>
#include <shlobj.h>
#endif

#include "fileutils.h"
#include "sdl/include/SDL_filesystem.h"
#include <tinydir/tinydir.h>

#include <queue>
#include <set>

namespace FileUtils
{
Expand Down Expand Up @@ -66,4 +75,101 @@ fs::path RelativeTo(fs::path from, fs::path to)
return finalPath;
}

fs::path GetDocumentsPath()
{
#if TARGET_PC
PWSTR path;
HRESULT hr = SHGetKnownFolderPath(FOLDERID_Documents, 0, NULL, &path);
if (SUCCEEDED(hr))
{
fs::path ret = StringUtils::makeStr(path);
CoTaskMemFree(path);
return ret;
}
return fs::path();
#else
auto pszHome = getenv("HOME");
return fs::path(std::string(pszHome) + "/Documents");
#endif
}

std::vector<fs::path> GatherFiles(const fs::path& root)
{
std::vector<fs::path> ret;

tinydir_dir dir;
if (tinydir_open(&dir, root.string().c_str()) == -1)
{
LOG(ERROR) << "Gather Files, Start Path Invalid: " << root.string();
return ret;
}

std::set<fs::path> checkedPaths;
std::queue<tinydir_dir> dirs;
dirs.push(dir);
while (!dirs.empty())
{
tinydir_dir thisDir = dirs.front();
dirs.pop();

while (thisDir.has_next)
{
tinydir_file file;
if (tinydir_readfile(&thisDir, &file) == -1)
{
LOG(ERROR) << "Couldn't read: " << thisDir.path;
tinydir_next(&thisDir);
continue;
}

try
{
fs::path filePath(file.path);

// Ignore . and ..
// Otherwise we walk forever. Do this before absolute path!
if (filePath.string().find("\\.") != std::string::npos ||
filePath.string().find("..") != std::string::npos)
{
//LOG(INFO) << "Skipping: " << filePath.string();
tinydir_next(&thisDir);
continue;
}

// Keep paths nice and absolute/canonical
filePath = fs::canonical(fs::absolute(filePath));
if (checkedPaths.find(filePath) != checkedPaths.end())
{
LOG(INFO) << "Already checked: " << filePath.string();
tinydir_next(&thisDir);
continue;
}
checkedPaths.insert(filePath);

if (fs::is_directory(filePath))
{
tinydir_dir subDir;
if (tinydir_open(&subDir, filePath.string().c_str()) != -1)
{
fs::path newPath(subDir.path);
newPath = fs::canonical(fs::absolute(newPath));
dirs.push(subDir);
}
}
else
{
ret.push_back(filePath);
}
}
catch (fs::filesystem_error& err)
{
LOG(ERROR) << err.what();
}

tinydir_next(&thisDir);
}
}
return ret;
}

} // FileUtils
3 changes: 3 additions & 0 deletions mcommon/file/fileutils.h
Expand Up @@ -17,4 +17,7 @@ namespace FileUtils
std::string ReadFile(const fs::path& fileName);
bool WriteFile(const fs::path& fileName, const void* pData, size_t size);
fs::path RelativeTo(fs::path from, fs::path to);
fs::path GetDocumentsPath();
std::vector<fs::path> GatherFiles(const fs::path& root);
}

83 changes: 70 additions & 13 deletions mcommon/file/media_manager.cpp
@@ -1,39 +1,78 @@
#include "mcommon.h"
#include "file/media_manager.h"
#include "file/fileutils.h"
#include "config_app.h"

std::string g_AppFriendlyName = APPLICATION_NAME;

MediaManager& MediaManager::Instance()
{
static MediaManager manager;
return manager;
}

MediaManager::MediaManager()
bool MediaManager::SetAssetPath(const fs::path& assetPath)
{
fs::path basePath(SDL_GetBasePath());
basePath = basePath / "assets";
if (fs::exists(basePath))
{
m_mediaPath = fs::canonical(fs::absolute(basePath));
}
else
{
m_mediaPath = fs::canonical(fs::absolute(basePath));
}
m_documentsPath = FileUtils::GetDocumentsPath() / fs::path(g_AppFriendlyName);
m_mediaPath = fs::canonical(fs::absolute(assetPath));

LOG(INFO) << "Media Path: " << m_mediaPath.string();

m_texturePaths.push_back(fs::path("textures"));
m_shaderPaths.push_back(fs::path("shaders"));
m_shaderPaths.push_back(fs::path("shaders") / fs::path("GL"));
m_shaderPaths.push_back(fs::path("shaders") / fs::path("DX12"));
m_modelPaths.push_back(fs::path("models"));
m_docPaths.push_back(fs::path("documents"));
m_modelPaths.push_back(fs::path("test") / fs::path("models"));

m_fontPaths.push_back(fs::path("fonts"));

m_textureExtensions.push_back(".dds");
m_textureExtensions.push_back(".png");

m_textureSubstringReplacements["_bump"] = "_normal";
return true;
}

bool MediaManager::SetProjectPath(const fs::path& project)
{
if (fs::exists(project))
{
m_projectPath = fs::canonical(fs::absolute(project));
return true;
}

try
{
fs::path newPath;
if (project.is_relative())
{
newPath = m_documentsPath / project;
}
else
{
newPath = project;
}

if (!fs::exists(newPath))
{
if (!fs::create_directories(newPath))
{
return false;
}
}
m_projectPath = fs::canonical(fs::absolute(newPath));
return true;
}
catch (fs::filesystem_error& err)
{
LOG(ERROR) << err.what();
}
return false;
}

MediaManager::MediaManager()
{
}

std::string MediaManager::LoadAsset(const char* pszPath, uint32_t mediaType, const fs::path* assetBase)
Expand Down Expand Up @@ -175,6 +214,25 @@ fs::path MediaManager::FindAssetInternal(const char* pszPath, uint32_t mediaType
}
}

if (mediaType & MediaType::Document)
{
fs::path found = searchPaths(parent, searchPath, m_docPaths);
if (!found.empty())
{
LOG(DEBUG) << "Found document: " << found.string();
return found;
}
}

if (mediaType & MediaType::Font)
{
fs::path found = searchPaths(parent, searchPath, m_fontPaths);
if (!found.empty())
{
LOG(DEBUG) << "Found font: " << found.string();
return found;
}
}
if (mediaType & MediaType::Shader)
{
fs::path found = searchPaths(parent, searchPath, m_shaderPaths);
Expand All @@ -185,7 +243,6 @@ fs::path MediaManager::FindAssetInternal(const char* pszPath, uint32_t mediaType
}
}


LOG(DEBUG) << "** File not found: " << searchPath.string();
return fs::path();
}
Expand Down
25 changes: 20 additions & 5 deletions mcommon/file/media_manager.h
@@ -1,14 +1,17 @@
#pragma once


namespace MediaType
{
enum : uint32_t
{
Texture = (1 << 0),
Model = (1 << 1),
Shader = (1 << 2),
Local = (1 << 3),
All = (0xFFFFFFFF)
Local = (1 << 3),
Document = (1 << 4),
Project = (1 << 5),
Font = (1 << 6)
};
}

Expand All @@ -20,21 +23,33 @@ class MediaManager
const MediaManager& operator= (const MediaManager& rhs) = delete;
MediaManager(const MediaManager& rhs) = delete;

fs::path FindAsset(const char* pszAssetName, uint32_t type = MediaType::All, const fs::path* assetBase = nullptr);
std::string LoadAsset(const char* pszPath, uint32_t mediaType = MediaType::All, const fs::path* assetBase = nullptr);
bool SetAssetPath(const fs::path& asset);
bool SetProjectPath(const fs::path& asset);

const fs::path& GetProjectPath() { return m_projectPath; }

fs::path GetDocumentsPath() { return m_documentsPath; }

fs::path FindAsset(const char* pszAssetName, uint32_t type = MediaType::Document, const fs::path* assetBase = nullptr);
std::string LoadAsset(const char* pszPath, uint32_t mediaType = MediaType::Document, const fs::path* assetBase = nullptr);

private:
fs::path FindAssetInternal(const char* pszAssetName, uint32_t type = MediaType::All, const fs::path* assetBase = nullptr);
fs::path FindAssetInternal(const char* pszAssetName, uint32_t type, const fs::path* assetBase = nullptr);

MediaManager();

fs::path m_mediaPath;
std::vector<fs::path> m_texturePaths;
std::vector<fs::path> m_modelPaths;
std::vector<fs::path> m_docPaths;
std::vector<fs::path> m_shaderPaths;
std::vector<fs::path> m_fontPaths;

std::vector<std::string> m_textureExtensions;
std::map<std::string, std::string> m_textureSubstringReplacements;

std::map<std::string, fs::path> m_foundAssets;

fs::path m_documentsPath;
fs::path m_projectPath;
};
4 changes: 4 additions & 0 deletions mcommon/list.cmake
Expand Up @@ -19,6 +19,7 @@ mcommon/file/media_manager.h

mcommon/math/mathutils.h
mcommon/math/mathutils.cpp
mcommon/math/rectstack.h

mcommon/string/stringutils.cpp
mcommon/string/stringutils.h
Expand All @@ -33,4 +34,7 @@ mcommon/graphics/primitives2d.cpp
mcommon/graphics/primitives2d.h

mcommon/mcommon.h
mcommon/mcommon.cpp

)
set(MCOMMON_ROOT ${CMAKE_CURRENT_LIST_DIR} CACHE STRING "" FORCE)

0 comments on commit f127fac

Please sign in to comment.