aboutsummaryrefslogtreecommitdiff
path: root/samples/amber.cc
diff options
context:
space:
mode:
authorasuonpaa <34128694+asuonpaa@users.noreply.github.com>2020-04-14 16:06:06 +0300
committerGitHub <noreply@github.com>2020-04-14 09:06:06 -0400
commitaf654dcf8c14f4a296de754c865e10c751c2a91a (patch)
tree217e3aa1ca9ede1b7bc4f9199c4a8dbf11a656ed /samples/amber.cc
parenta40bef4dba98d2d80b48e5a940d8574fbfceb197 (diff)
downloadamber-af654dcf8c14f4a296de754c865e10c751c2a91a.tar.gz
Added implementation for loading buffer data from a binary file. (#838)
* Added implementation for loading buffer data from a binary file. * Use PNG, BINARY, and TEXT to select the type of file to load buffer data from.
Diffstat (limited to 'samples/amber.cc')
-rw-r--r--samples/amber.cc40
1 files changed, 29 insertions, 11 deletions
diff --git a/samples/amber.cc b/samples/amber.cc
index 31e66f9..01ff159 100644
--- a/samples/amber.cc
+++ b/samples/amber.cc
@@ -254,7 +254,7 @@ bool ParseArgs(const std::vector<std::string>& args, Options* opts) {
return true;
}
-std::string ReadFile(const std::string& input_file) {
+std::vector<char> ReadFile(const std::string& input_file) {
FILE* file = nullptr;
#if defined(_MSC_VER)
fopen_s(&file, input_file.c_str(), "rb");
@@ -287,7 +287,7 @@ std::string ReadFile(const std::string& input_file) {
return {};
}
- return std::string(data.begin(), data.end());
+ return data;
}
class SampleDelegate : public amber::Delegate {
@@ -327,19 +327,36 @@ class SampleDelegate : public amber::Delegate {
void SetScriptPath(std::string path) { path_ = path; }
amber::Result LoadBufferData(const std::string file_name,
+ amber::BufferDataFileType file_type,
amber::BufferInfo* buffer) const override {
+ if (file_type == amber::BufferDataFileType::kPng) {
#if AMBER_ENABLE_LODEPNG
- // Try to load as png first.
- amber::Result r = png::LoadPNG(path_ + file_name, &buffer->width,
- &buffer->height, &buffer->values);
-
- if (r.IsSuccess())
- return r;
+ return png::LoadPNG(path_ + file_name, &buffer->width, &buffer->height,
+ &buffer->values);
+#else
+ return amber::Result("PNG support is not enabled in compile options.");
#endif // AMBER_ENABLE_LODEPNG
+ } else if (file_type == amber::BufferDataFileType::kBinary) {
+ auto data = ReadFile(path_ + file_name);
+ if (data.empty())
+ return amber::Result("Failed to load buffer data " + file_name);
+
+ for (auto d : data) {
+ amber::Value v;
+ v.SetIntValue(static_cast<uint64_t>(d));
+ buffer->values.push_back(v);
+ }
+
+ buffer->width = 1;
+ buffer->height = 1;
- // TODO(asuonpaa): Try to load a binary format.
+ } else {
+ assert(file_type == amber::BufferDataFileType::kText);
- return amber::Result("Failed to load buffer data " + file_name);
+ // TODO(asuonpaa): Read text file and pass it to parser.
+ }
+
+ return {};
}
private:
@@ -432,7 +449,8 @@ int main(int argc, const char** argv) {
};
std::vector<RecipeData> recipe_data;
for (const auto& file : options.input_filenames) {
- auto data = ReadFile(file);
+ auto char_data = ReadFile(file);
+ auto data = std::string(char_data.begin(), char_data.end());
if (data.empty()) {
std::cerr << file << " is empty." << std::endl;
failures.push_back(file);