1
0
Fork 0

Merge branch '6-abstract-filesystem' into dev

This commit is contained in:
Henrik Hautakoski 2020-09-17 09:50:06 +02:00
commit afc1491718
4 changed files with 275 additions and 40 deletions

View file

@ -1,4 +1,5 @@
#include <Spectre/System/File.h>
#include <Spectre/System/Path.h>
#include <Spectre/System/Log.h>
#include <Spectre/Graphics/Image.h>
@ -18,22 +19,17 @@
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
namespace sp {
bool ImageLoader::loadFromFile(const char *filename, Image& img)
{
FILE *fd = fopen(filename, "rb");
File file(filename);
if (fd) {
if (file.isOpen()) {
std::vector<unsigned char> buf;
fseek(fd, 0, SEEK_END);
buf.resize(ftell(fd));
rewind(fd);
fread(&buf[0], 1, buf.size(), fd);
fclose(fd);
file.read(buf);
if (loadFromMemory(&buf[0], buf.size(), img) == false) {
Log::warn("ImageLoader: could not load file '%s'. Reason: %s",
@ -44,7 +40,7 @@ bool ImageLoader::loadFromFile(const char *filename, Image& img)
}
Log::warn("ImageLoader: could not open file '%s'. Reason: %s",
filename, strerror(errno));
filename, file.getErrorMessage().c_str());
return false;
}
@ -91,10 +87,9 @@ bool ImageLoader::saveToFile(const Image& img, const char *filename)
if (encoded_data.size() > 0) {
FILE *fd = fopen(filename, "wb");
if (fd) {
fwrite(&encoded_data[0], 1, encoded_data.size(), fd);
fclose(fd);
File file(filename, File::Access::WRITE);
if (file.isOpen()) {
file.write(encoded_data);
}
return true;
}

View file

@ -1,6 +1,6 @@
#include <fstream>
#include <Spectre/System/Path.h>
#include <Spectre/System/File.h>
#include <Spectre/Graphics/Shader.h>
#include <Graphics/GL/gl.h>
@ -30,26 +30,26 @@ const std::string& Shader::getName() const
return m_name;
}
bool Shader::loadFromFile(const std::string& file)
bool Shader::loadFromFile(const std::string& filename)
{
std::string src;
std::ifstream strm;
File file;
// If this shader does not have a name, pick the filename.
if (m_name.length() < 1) {
m_name = Path::getBasename(file);
m_name = Path::getBasename(filename);
}
// Load file into memory.
strm.open(file.c_str(), std::fstream::in);
if (!strm.is_open()) {
m_error = "Can't open file: " + file;
if (!file.open(filename)) {
m_error = "Can't open file: " + filename;
return false;
}
src.assign(std::istreambuf_iterator<char>(strm), std::istreambuf_iterator<char>());
strm.close();
// Load file into memory.
if (!file.read(src)) {
m_error = "Could not read file: " + filename;
return false;
}
return loadFromMemory(src);
}