Add file structure to code
- Rename project to CopiumEngine
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
#pragma once
|
||||
|
||||
#include "copium/util/VulkanException.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#define CP_TERM_RED "\x1B[31m"
|
||||
#define CP_TERM_GREEN "\x1B[32m"
|
||||
#define CP_TERM_YELLOW "\x1B[33m"
|
||||
#define CP_TERM_GRAY "\x1B[90m"
|
||||
#define CP_TERM_CLEAR "\033[0m"
|
||||
|
||||
#define CP_DEBUG(format, ...) std::cout << CP_TERM_GRAY << "[DBG] " << Copium::StringFormat(format, __VA_ARGS__) << CP_TERM_CLEAR << std::endl
|
||||
#define CP_INFO(format, ...) std::cout << "[INF] " << Copium::StringFormat(format, __VA_ARGS__) << std::endl
|
||||
#define CP_WARN(format, ...) std::cout << CP_TERM_YELLOW << "[WRN] " << Copium::StringFormat(format, __VA_ARGS__) << CP_TERM_CLEAR << std::endl
|
||||
#define CP_ERR(format, ...) std::cout << CP_TERM_RED << "[ERR] " << Copium::StringFormat(format, __VA_ARGS__) << CP_TERM_CLEAR << std::endl
|
||||
|
||||
// Continue traces, will not print the [XXX] tag before the log
|
||||
#define CP_DEBUG_CONT(format, ...) std::cout << CP_TERM_GRAY << " " << Copium::StringFormat(format, __VA_ARGS__) << CP_TERM_CLEAR << std::endl
|
||||
#define CP_INFO_CONT(format, ...) std::cout << " " << Copium::StringFormat(format, __VA_ARGS__) << std::endl
|
||||
#define CP_WARN_CONT(format, ...) std::cout << CP_TERM_YELLOW << " " << Copium::StringFormat(format, __VA_ARGS__) << CP_TERM_CLEAR << std::endl
|
||||
#define CP_ERR_CONT(format, ...) std::cout << CP_TERM_RED << " " << Copium::StringFormat(format, __VA_ARGS__) << CP_TERM_CLEAR << std::endl
|
||||
|
||||
#define CP_UNIMPLEMENTED() CP_WARN("%s is unimplemented", __FUNCTION__)
|
||||
#define CP_ABORT(format, ...) \
|
||||
do \
|
||||
{ \
|
||||
CP_ERR(format, __VA_ARGS__); \
|
||||
throw std::runtime_error(Copium::StringFormat(format, __VA_ARGS__)); \
|
||||
} while(false)
|
||||
#define CP_ASSERT(Function, format, ...) \
|
||||
do \
|
||||
{ \
|
||||
if(!(Function)) \
|
||||
{ \
|
||||
CP_ERR(format, __VA_ARGS__); \
|
||||
throw std::runtime_error(Copium::StringFormat(format, __VA_ARGS__)); \
|
||||
} \
|
||||
} while(false)
|
||||
#define CP_VK_ASSERT(Function, format, ...) \
|
||||
do \
|
||||
{ \
|
||||
if(Function != VK_SUCCESS) \
|
||||
{ \
|
||||
CP_ERR(format, __VA_ARGS__); \
|
||||
throw VulkanException(Copium::StringFormat(format, __VA_ARGS__)); \
|
||||
} \
|
||||
} while(false)
|
||||
|
||||
#define CP_STATIC_CLASS(ClassName)\
|
||||
ClassName() = delete
|
||||
#define CP_DELETE_COPY_AND_MOVE_CTOR(ClassName) \
|
||||
ClassName(ClassName&&) = delete; \
|
||||
ClassName(const ClassName&) = delete; \
|
||||
ClassName& operator=(ClassName&&) = delete; \
|
||||
ClassName& operator=(const ClassName&) = delete
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
template<typename ... Args>
|
||||
std::string StringFormat(const std::string& format, Args... args)
|
||||
{
|
||||
int size = std::snprintf(nullptr, 0, format.c_str(), args...) + 1;
|
||||
CP_ASSERT(size > 0, "StringFormat : Error during formatting");
|
||||
std::unique_ptr<char[]> buf(new char[size]);
|
||||
std::snprintf(buf.get(), size, format.c_str(), args...);
|
||||
return std::string(buf.get(), buf.get() + size - 1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
#include "copium/util/FileSystem.h"
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
std::vector<char> FileSystem::ReadFile(const std::string& filename)
|
||||
{
|
||||
std::ifstream file(filename, std::ios::ate | std::ios::binary);
|
||||
CP_ASSERT(file.is_open(), "ReadFile : Failed to open file");
|
||||
|
||||
size_t fileSize = (size_t)file.tellg();
|
||||
std::vector<char> buffer(fileSize);
|
||||
|
||||
file.seekg(0);
|
||||
file.read(buffer.data(), fileSize);
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
std::string FileSystem::ReadFileStr(const std::string& filename)
|
||||
{
|
||||
std::ifstream file(filename, std::ios::ate | std::ios::binary);
|
||||
CP_ASSERT(file.is_open(), "ReadFileStr : Failed to open file");
|
||||
|
||||
size_t fileSize = (size_t)file.tellg();
|
||||
std::string buffer;
|
||||
buffer.resize(fileSize);
|
||||
|
||||
file.seekg(0);
|
||||
file.read(buffer.data(), fileSize);
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
void FileSystem::WriteFile(const std::string& filename, const std::string& data)
|
||||
{
|
||||
std::filesystem::path path{filename};
|
||||
std::filesystem::create_directories(path.parent_path());
|
||||
std::ofstream file(filename, std::ios::binary);
|
||||
CP_ASSERT(file.is_open(), "WriteFile : Failed to open file");
|
||||
|
||||
file.write(data.c_str(), data.size());
|
||||
}
|
||||
|
||||
void FileSystem::WriteFile(const std::string& filename, const char* data, size_t size)
|
||||
{
|
||||
std::filesystem::path path{filename};
|
||||
std::filesystem::create_directories(path.parent_path());
|
||||
std::ofstream file(filename, std::ios::binary);
|
||||
CP_ASSERT(file.is_open(), "WriteFile : Failed to open file");
|
||||
|
||||
file.write(data, size);
|
||||
}
|
||||
|
||||
bool FileSystem::FileExists(const std::string& filename)
|
||||
{
|
||||
std::ifstream file(filename);
|
||||
return file.good();
|
||||
}
|
||||
|
||||
int64_t FileSystem::DateModified(const std::string& filename)
|
||||
{
|
||||
struct stat result;
|
||||
CP_ASSERT(stat(filename.c_str(), &result) == 0, "DataModified : Cannot stat file %s", filename.c_str());
|
||||
return (int64_t)result.st_mtime;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include "copium/util/Common.h"
|
||||
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
class FileSystem
|
||||
{
|
||||
CP_STATIC_CLASS(FileSystem);
|
||||
public:
|
||||
static std::vector<char> ReadFile(const std::string& filename);
|
||||
static std::string ReadFileStr(const std::string& filename);
|
||||
static void WriteFile(const std::string& filename, const std::string& data);
|
||||
static void WriteFile(const std::string& filename, const char* data, size_t size);
|
||||
static bool FileExists(const std::string& filename);
|
||||
static int64_t DateModified(const std::string& filename);
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
#include "copium/util/Timer.h"
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
Timer::Timer()
|
||||
: startTime{std::chrono::steady_clock::now()}
|
||||
{}
|
||||
|
||||
void Timer::Start()
|
||||
{
|
||||
startTime = std::chrono::steady_clock::now();
|
||||
}
|
||||
|
||||
double Timer::Elapsed()
|
||||
{
|
||||
return std::chrono::duration<double>(std::chrono::high_resolution_clock::now() - startTime).count();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include <chrono>
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
class Timer
|
||||
{
|
||||
private:
|
||||
std::chrono::time_point<std::chrono::steady_clock> startTime;
|
||||
public:
|
||||
Timer();
|
||||
|
||||
void Start();
|
||||
double Elapsed();
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
namespace Copium
|
||||
{
|
||||
class VulkanException : public std::runtime_error
|
||||
{
|
||||
public:
|
||||
VulkanException(const std::string& str)
|
||||
: runtime_error{str.c_str()}
|
||||
{}
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user