Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exploration: a self-contained default scene #324

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
58 changes: 58 additions & 0 deletions src/scene_default.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#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 @@ -6,16 +6,9 @@
#include "no_op_factory.hpp"
#include "../src/render_counter.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 @@ -24,6 +17,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