Files
rive-cpp/tests/common/rive_mgr.cpp
bodymovin 6362192a61 data bind artboards rcp file (#10214) c542b9b7ac
* make file ref counted

* migrate File to refcnt

* add bindable artboard class to keep file reference

* feat(unity): support rcp file and BindableArtboard class (#10228)

* refactor(apple): use updated file bindable artboard apis (#10229)

* update command queue to support bindable artboards

* use bindable artboards on command queue

* self manage view model instances in js runtime

* change remaining viewModelInstanceRuntimes to rcp

* refactor(apple): update view model instances to use rcp (#10298)

* refactor(unity): support rcp ViewModelInstanceRuntime (#10309)

* rebase fix

* deprecate getArtboard in favor of getBindableArtboard

* fix merge

* remove unused lambda capture

* fix rive binding

* feat(Android): RCP File, VMI, and add bindable artboards (#10456)

* refactor: C++ refactors

- Import from long (incorrect) -> jlong
- Header clang-tidy fix
- Use reinterpret_cast instead of C-style cast
- Break out some variables instead of long one liners
- Use auto
- Remove unused env and thisObj

# Conflicts:
#	packages/runtime_android/kotlin/src/main/cpp/src/helpers/general.cpp

* docs: Improve documentation on the File class

* feat: Support bindable artboard type and RCP VMIs

# Conflicts:
#	packages/runtime_android/kotlin/src/androidTest/java/app/rive/runtime/kotlin/core/RiveDataBindingTest.kt

* feat: Support RCP files

* refactor: Change from +1/-1 ref to just release

* fix: Moved to the more appropriate null pointer for GetStringUTFChars

* replace unref with release

Co-authored-by: Adam <67035612+damzobridge@users.noreply.github.com>
Co-authored-by: David Skuza <david@rive.app>
Co-authored-by: Erik <erik@rive.app>
Co-authored-by: hernan <hernan@rive.app>
2025-09-30 00:01:40 +00:00

112 lines
2.7 KiB
C++

#include "rive_mgr.hpp"
#include "rive/animation/linear_animation_instance.hpp"
#include "rive/animation/state_machine_instance.hpp"
#include "rive/artboard.hpp"
#include "rive/factory.hpp"
#include "rive/file.hpp"
#include "rive/refcnt.hpp"
struct AutoClose
{
FILE* m_File;
AutoClose(FILE* file) : m_File(file) {}
~AutoClose() { this->close(); }
void close()
{
if (m_File)
{
fclose(m_File);
m_File = nullptr;
}
}
};
RiveMgr::RiveMgr(rive::Factory* factory) : m_Factory(factory) {}
RiveMgr::~RiveMgr() {}
bool RiveMgr::loadArtboard(const char filename[], const char artboard[])
{
m_File = nullptr;
m_Artboard = nullptr;
m_Scene = nullptr;
m_File = RiveMgr::loadFile(filename, m_Factory);
if (m_File)
{
m_Artboard = (artboard && artboard[0]) ? m_File->artboardNamed(artboard)
: m_File->artboardDefault();
}
return m_Artboard != nullptr;
}
bool RiveMgr::loadAnimation(const char filename[],
const char artboard[],
const char animation[])
{
if (this->loadArtboard(filename, artboard))
{
m_Scene = (animation && animation[0])
? m_Artboard->animationNamed(animation)
: m_Artboard->animationAt(0);
}
return m_Scene != nullptr;
}
bool RiveMgr::loadMachine(const char filename[],
const char artboard[],
const char machine[])
{
if (this->loadArtboard(filename, artboard))
{
m_Scene = (machine && machine[0])
? m_Artboard->stateMachineNamed(machine)
: m_Artboard->stateMachineAt(0);
}
return m_Scene != nullptr;
}
static void log(const char msg[], const char arg[] = nullptr)
{
std::string err(msg);
if (arg)
{
err = err + std::string(arg);
}
fprintf(stderr, "%s\n", err.c_str());
}
rive::rcp<rive::File> RiveMgr::loadFile(const char filename[],
rive::Factory* factory)
{
AutoClose afc(fopen(filename, "rb"));
FILE* fp = afc.m_File;
if (fp == nullptr)
{
log("Failed to open file ", filename);
return nullptr;
}
fseek(fp, 0, SEEK_END);
auto length = ftell(fp);
fseek(fp, 0, SEEK_SET);
std::vector<uint8_t> bytes(length);
if (fread(bytes.data(), 1, length, fp) != length)
{
log("Failed to read file into bytes array ", filename);
return nullptr;
}
auto file = rive::File::import(bytes, factory);
if (!file)
{
log("Failed to read bytes into Rive file ", filename);
}
return file;
}