Skip to content

Commit

Permalink
Default scene API idea
Browse files Browse the repository at this point in the history
  • Loading branch information
mikerreed committed May 25, 2022
1 parent 9e6a9a3 commit fbb6d6e
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 9 deletions.
2 changes: 2 additions & 0 deletions include/rive/scene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ namespace rive {
virtual SMIBool* getBool(const std::string&) const;
virtual SMINumber* getNumber(const std::string&) const;
virtual SMITrigger* getTrigger(const std::string&) const;

static std::unique_ptr<Scene> importDefault(Span<uint8_t>, Factory*);
};

} // namespace rive
Expand Down
57 changes: 57 additions & 0 deletions src/scene_default.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#include "rive/artboard.hpp"
#include "rive/file.hpp"
#include "rive/scene.hpp"

using namespace rive;

class SelfContainedScene : public Scene {
std::unique_ptr<File> m_File;
std::unique_ptr<ArtboardInstance> m_ABI;
std::unique_ptr<Scene> m_Scene;

public:
SelfContainedScene(std::unique_ptr<File> file,
std::unique_ptr<ArtboardInstance> abi,
std::unique_ptr<Scene> scene)
: Scene(abi.get())
, m_File(std::move(file))
, m_ABI(std::move(abi))
, m_Scene(std::move(scene)) {}

~SelfContainedScene() = default;

// Forward to our m_Scene

std::string name() const { return m_Scene->name(); }
Loop loop() const { return m_Scene->loop(); }
bool isTranslucent() const { return m_Scene->isTranslucent(); }
float durationSeconds() const { return m_Scene->durationSeconds(); }
bool advanceAndApply(float seconds) { return m_Scene->advanceAndApply(seconds); }

void draw(Renderer* renderer) { return m_Scene->draw(renderer); }
void pointerDown(Vec2D pos) { return m_Scene->pointerDown(pos); }
void pointerMove(Vec2D pos) { return m_Scene->pointerMove(pos); }
void pointerUp(Vec2D pos) { return m_Scene->pointerUp(pos); }

size_t inputCount() const { return m_Scene->inputCount(); }
SMIInput* input(size_t index) const { return m_Scene->input(index); }
SMIBool* getBool(const std::string& name) const { return m_Scene->getBool(name); }
SMINumber* getNumber(const std::string& name) const { return m_Scene->getNumber(name); }
SMITrigger* getTrigger(const std::string& name) const { return m_Scene->getTrigger(name); }
};

std::unique_ptr<Scene> Scene::importDefault(Span<uint8_t> span, Factory* factory) {
auto file = File::import(span, factory);
if (file) {
auto abi = file->artboardDefault();
if (abi) {
auto scene = abi->defaultScene();
if (scene) {
return std::make_unique<SelfContainedScene>(std::move(file),
std::move(abi),
std::move(scene));
}
}
}
return nullptr;
}
6 changes: 6 additions & 0 deletions test/default_state_machine_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,9 @@ TEST_CASE("default state machine is detected at load", "[file]") {
REQUIRE(scene != nullptr);
REQUIRE(scene->name() == smi->name());
}

TEST_CASE("load default scene", "[file]") {
auto bytes = ReadFile("../../test/assets/entry.riv");
auto scene = rive::Scene::importDefault(rive::toSpan(bytes), &rive::gNoOpFactory);
REQUIRE(scene.get());
}
24 changes: 15 additions & 9 deletions test/rive_file_reader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,9 @@
#include "rive_testing.hpp"
#include "no_op_factory.hpp"

static inline std::unique_ptr<rive::File>
ReadRiveFile(const char path[],
rive::Factory* factory = nullptr,
rive::FileAssetResolver* resolver = nullptr) {
if (!factory) {
factory = &rive::gNoOpFactory;
}

static inline std::vector<uint8_t> ReadFile(const char path[]) {
FILE* fp = fopen(path, "rb");
REQUIRE(fp != nullptr);
REQUIRE(fp);

fseek(fp, 0, SEEK_END);
const size_t length = ftell(fp);
Expand All @@ -23,6 +16,19 @@ ReadRiveFile(const char path[],
REQUIRE(fread(bytes.data(), 1, length, fp) == length);
fclose(fp);

return bytes;
}

static inline std::unique_ptr<rive::File>
ReadRiveFile(const char path[],
rive::Factory* factory = nullptr,
rive::FileAssetResolver* resolver = nullptr) {
if (!factory) {
factory = &rive::gNoOpFactory;
}

auto bytes = ReadFile(path);

rive::ImportResult result;
auto file = rive::File::import(rive::toSpan(bytes), factory, &result, resolver);
REQUIRE(result == rive::ImportResult::success);
Expand Down

0 comments on commit fbb6d6e

Please sign in to comment.