Commit de6a5220 authored by Paul Asmuth's avatar Paul Asmuth
Browse files

add ElementTree API

parent 1c271b86
Loading
Loading
Loading
Loading
+35 −0
Original line number Diff line number Diff line
/**
 * This file is part of the "FnordMetric" project
 *   Copyright (c) 2018 Paul Asmuth
 *
 * FnordMetric is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License v3.0. You should have received a
 * copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */
#pragma once
#include "utils/return_code.h"
#include "element.h"
#include "element_spec.h"

namespace signaltk {
class Layer;

struct ElementTree {
  std::vector<ElementRef> roots;
};

ReturnCode buildElementTree(
    const PropertyList& plist,
    ElementTree* tree);

ReturnCode buildElementTree(
    const std::string& spec,
    ElementTree* tree);

ReturnCode renderElements(
    const ElementTree& tree,
    Layer* frame);

} // namespace signaltk
+22 −8
Original line number Diff line number Diff line
@@ -18,7 +18,8 @@
#include <sys/resource.h>
#include <sys/file.h>
#include "signaltk.h"
#include "elements/context.h"
#include "elements/element_tree.h"
#include "graphics/layer.h"
#include <utils/flagparser.h>
#include <utils/fileutil.h>
#include <utils/return_code.h>
@@ -26,6 +27,10 @@

using namespace signaltk;

void printError(const ReturnCode& rc) {
  std::cerr << StringUtil::format("ERROR: $0", rc.getMessage()) << std::endl;
}

int main(int argc, const char** argv) {
  FlagParser flag_parser;

@@ -73,15 +78,24 @@ int main(int argc, const char** argv) {
    return 0;
  }

  auto ctx = context_create_image(1200, 800);
  context_frame(ctx)->clear(Colour{1, 1, 1, 1});
  auto spec = FileUtil::read(flag_in).toString(); // FIXME
  signaltk::ElementTree elems;
  if (auto rc = buildElementTree(spec, &elems); !rc.isSuccess()) {
    printError(rc);
    return EXIT_FAILURE;
  }

  auto rc = context_frame(ctx)->writeToFile(flag_out);
  Layer frame{1200, 800};
  frame.clear(Colour{1, 1, 1, 1});
  if (auto rc = renderElements(elems, &frame); !rc.isSuccess()) {
    printError(rc);
    return EXIT_FAILURE;
  }

  if (rc) {
    std::cerr << StringUtil::format("ERROR: $0\n", "...");
  if (auto rc = frame.writeToFile(flag_out); rc) {
    std::cerr << "ERROR: can't write output file" << std::endl;
    return EXIT_FAILURE;
  } else {
    return EXIT_SUCCESS;
  }

  return EXIT_SUCCESS;
}