Skip to content

Commit 894e36f

Browse files
committed
Try adding C wrappers
1 parent a3e730e commit 894e36f

5 files changed

Lines changed: 232 additions & 3 deletions

File tree

dev/test/premake5.lua

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,10 @@ buildoptions {"-Wall", "-fno-exceptions", "-fno-rtti"}
8484

8585
includedirs {"./include", "../../include"}
8686

87-
files {"../../src/**.cpp", -- the Rive runtime source
88-
"../../test/**.cpp" -- the tests
87+
files {
88+
"../../src/**.cpp", -- the Rive runtime source
89+
"../../test/**.cpp", -- the tests
90+
"../../include/capi/**.cpp",
8991
}
9092

9193
defines {"TESTING", "ENABLE_QUERY_FLAT_VERTICES"}
@@ -97,4 +99,4 @@ symbols "On"
9799
filter "system:windows"
98100
architecture "x64"
99101
defines {"_USE_MATH_DEFINES"}
100-
buildoptions {WINDOWS_CLANG_CL_SUPPRESSED_WARNINGS}
102+
buildoptions {WINDOWS_CLANG_CL_SUPPRESSED_WARNINGS}

include/capi/rive_api.cpp

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
#include "capi/rive_api.h"
2+
3+
#include "rive/file.hpp"
4+
#include "rive/artboard.hpp"
5+
#include "rive/span.hpp"
6+
#include "rive/scene.hpp"
7+
#include "rive/animation/linear_animation_instance.hpp"
8+
#include "rive/animation/state_machine_instance.hpp"
9+
#include "rive/math/aabb.hpp"
10+
11+
static inline rive_file_t* toC(rive::File* file) { return (rive_file_t*)file; }
12+
static inline rive_artboard_t* toC(rive::ArtboardInstance* abi) { return (rive_artboard_t*)abi; }
13+
static inline rive_scene_t* toC(rive::Scene* scene) { return (rive_scene_t*)scene; }
14+
static inline rive_animation_t* toC(rive::LinearAnimationInstance* anim) {
15+
return (rive_animation_t*)anim;
16+
}
17+
static inline rive_statemachine_t* toC(rive::StateMachineInstance* smi) {
18+
return (rive_statemachine_t*)smi;
19+
}
20+
21+
static inline rive::Factory* toRive(rive_factory_t* factory) { return (rive::Factory*)factory; }
22+
static inline rive::Renderer* toRive(rive_renderer_t* renderer) { return (rive::Renderer*)renderer; }
23+
24+
static inline rive::File* toRive(rive_file_t* file) { return (rive::File*)file; }
25+
static inline rive::ArtboardInstance* toRive(rive_artboard_t* abi) { return (rive::ArtboardInstance*)abi; }
26+
static inline rive::Scene* toRive(rive_scene_t* scene) { return (rive::Scene*)scene; }
27+
28+
#if 0
29+
static inline rive_factory_t* toC(rive::Factory* factory) { return (rive_factory_t*)factory; }
30+
static inline rive_renderer_t* toC(rive::Renderer* renderer) { return (rive_renderer_t*)renderer; }
31+
static inline rive::LinearAnimationInstance* toRive(rive_animation_t* anim) {
32+
return (rive::LinearAnimationInstance*)anim;
33+
}
34+
static inline rive::StateMachineInstance* toRive(rive_statemachine_t* smi) {
35+
return (rive::StateMachineInstance*)smi;
36+
}
37+
#endif
38+
39+
//////////////////////////////////////
40+
41+
void rive_file_delete(rive_file_t* file) { delete toRive(file); }
42+
void rive_artboard_delete(rive_artboard_t* abi) { delete toRive(abi); }
43+
void rive_scene_delete(rive_scene_t* scene) { delete toRive(scene); }
44+
45+
rive_file_t* rive_file_import(rive_span_t cspan, rive_factory_t* factory) {
46+
rive::Span<const uint8_t> span((const uint8_t*)cspan.buffer, cspan.size);
47+
return toC(rive::File::import(span, toRive(factory)).release());
48+
}
49+
50+
int32_t rive_file_artboard_count(rive_file_t* file) {
51+
return toRive(file)->artboardCount();
52+
}
53+
54+
rive_artboard_t* rive_file_artboard_default(rive_file_t* file) {
55+
return toC(toRive(file)->artboardDefault().release());
56+
}
57+
58+
rive_artboard_t* rive_file_artboard_at(rive_file_t* file, int32_t index) {
59+
return toC(toRive(file)->artboardAt(index).release());
60+
}
61+
62+
rive_artboard_t* rive_file_artboard_named(rive_file_t* file, const char name[]) {
63+
return toC(toRive(file)->artboardNamed(name).release());
64+
}
65+
66+
///////////////////////////
67+
68+
int32_t rive_artboard_animation_count(rive_artboard_t* abi) {
69+
return toRive(abi)->animationCount();
70+
}
71+
72+
rive_animation_t* rive_artboard_animation_at(rive_artboard_t* abi, int32_t index) {
73+
return toC(toRive(abi)->animationAt(index).release());
74+
}
75+
76+
rive_animation_t* rive_artboard_animation_named(rive_artboard_t* abi, const char name[]) {
77+
return toC(toRive(abi)->animationNamed(name).release());
78+
}
79+
80+
int32_t rive_artboard_statemachine_count(rive_artboard_t* abi) {
81+
return toRive(abi)->stateMachineCount();
82+
}
83+
84+
rive_statemachine_t* rive_artboard_statemachine_at(rive_artboard_t* abi, int32_t index) {
85+
return toC(toRive(abi)->stateMachineAt(index).release());
86+
}
87+
88+
rive_statemachine_t* rive_artboard_statemachine_named(rive_artboard_t* abi, const char name[]) {
89+
return toC(toRive(abi)->stateMachineNamed(name).release());
90+
}
91+
92+
rive_statemachine_t* rive_artboard_statemachine_default(rive_artboard_t* abi) {
93+
return toC(toRive(abi)->defaultStateMachine().release());
94+
}
95+
96+
rive_scene_t* rive_artboard_scene_default(rive_artboard_t* abi) {
97+
return toC(toRive(abi)->defaultScene().release());
98+
}
99+
100+
////////////////////////////////
101+
102+
rive_aabb_t rive_scene_bounds(rive_scene_t* scene) {
103+
auto aabb = toRive(scene)->bounds();
104+
return {aabb.left(), aabb.top(), aabb.right(), aabb.bottom()};
105+
}
106+
107+
void rive_scene_draw(rive_scene_t* scene, rive_renderer_t* renderer) {
108+
toRive(scene)->draw(toRive(renderer));
109+
}
110+
111+
bool rive_scene_advance(rive_scene_t* scene, float seconds) {
112+
return toRive(scene)->advanceAndApply(seconds);
113+
}

include/capi/rive_api.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#ifndef _RIVE_FILE_H_
2+
#define _RIVE_FILE_H_
3+
4+
#include "capi/rive_types.h"
5+
6+
RIVE_C_PLUS_PLUS_BEGIN_GUARD
7+
8+
rive_file_t* rive_file_import(rive_span_t, rive_factory_t*);
9+
10+
int32_t rive_file_artboard_count(rive_file_t*);
11+
rive_artboard_t* rive_file_artboard_default(rive_file_t*);
12+
rive_artboard_t* rive_file_artboard_at(rive_file_t*, int32_t index);
13+
rive_artboard_t* rive_file_artboard_named(rive_file_t*, const char name[]);
14+
15+
rive_statemachine_t* rive_artboard_statemachine_default(rive_artboard_t*);
16+
rive_scene_t* rive_artboard_scene_default(rive_artboard_t*);
17+
18+
int32_t rive_artboard_animation_count(rive_artboard_t*);
19+
rive_animation_t* rive_artboard_animation_at(rive_artboard_t*, int32_t index);
20+
rive_animation_t* rive_artboard_animation_named(rive_artboard_t*, const char name[]);
21+
22+
int32_t rive_artboard_count_statemachines(rive_artboard_t*);
23+
rive_statemachine_t* rive_artboard_statemachine_at(rive_artboard_t*, int32_t index);
24+
rive_statemachine_t* rive_artboard_statemachine_named(rive_artboard_t*, const char name[]);
25+
26+
rive_aabb_t rive_scene_bounds(rive_scene_t*);
27+
void rive_scene_draw(rive_scene_t*, rive_renderer_t*);
28+
bool rive_scene_advance(rive_scene_t*, float seconds);
29+
30+
void rive_file_delete(rive_file_t*);
31+
void rive_artboard_delete(rive_artboard_t*);
32+
void rive_scene_delete(rive_scene_t*);
33+
34+
RIVE_C_PLUS_PLUS_END_GUARD
35+
36+
#endif

include/capi/rive_types.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#ifndef _RIVE_TYPES_H_
2+
#define _RIVE_TYPES_H_
3+
4+
#include <stdint.h>
5+
#include <stddef.h>
6+
7+
#ifdef __cplusplus
8+
#define RIVE_C_PLUS_PLUS_BEGIN_GUARD extern "C" {
9+
#define RIVE_C_PLUS_PLUS_END_GUARD }
10+
#else
11+
#include <stdbool.h>
12+
#define RIVE_C_PLUS_PLUS_BEGIN_GUARD
13+
#define RIVE_C_PLUS_PLUS_END_GUARD
14+
#endif
15+
16+
RIVE_C_PLUS_PLUS_BEGIN_GUARD
17+
18+
typedef uint32_t rive_colorint_t;
19+
20+
typedef struct {
21+
float x, y;
22+
} rive_vec2d_t;
23+
24+
typedef struct {
25+
float left, top, right, bottom;
26+
} rive_aabb_t;
27+
28+
typedef struct {
29+
float mat[6];
30+
} rive_mat2d_t;
31+
32+
typedef struct {
33+
void* buffer;
34+
size_t size;
35+
} rive_span_t;
36+
37+
typedef struct rive_file_t rive_file_t;
38+
typedef struct rive_artboard_t rive_artboard_t;
39+
typedef struct rive_scene_t rive_scene_t;
40+
typedef struct rive_animation_t rive_animation_t;
41+
typedef struct rive_statemachine_t rive_statemachine_t;
42+
43+
typedef struct rive_factory_t rive_factory_t;
44+
typedef struct rive_renderer_t rive_renderer_t;
45+
46+
RIVE_C_PLUS_PLUS_END_GUARD
47+
48+
#endif

test/capi_test.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include "capi/rive_api.h"
2+
3+
#include "no_op_factory.hpp"
4+
#include <catch.hpp>
5+
#include <cstdio>
6+
7+
static rive::NoOpFactory gNoOpFactory;
8+
9+
static rive_file_t* loadfile(const char path[]) {
10+
FILE* fp = fopen(path, "rb");
11+
REQUIRE(fp != nullptr);
12+
13+
fseek(fp, 0, SEEK_END);
14+
const size_t length = ftell(fp);
15+
fseek(fp, 0, SEEK_SET);
16+
std::vector<uint8_t> bytes(length);
17+
REQUIRE(fread(bytes.data(), 1, length, fp) == length);
18+
fclose(fp);
19+
20+
rive_span_t span = {bytes.data(), length};
21+
return rive_file_import(span, (rive_factory_t*)&gNoOpFactory);
22+
}
23+
24+
TEST_CASE("capi", "[file]") {
25+
auto file = loadfile("../../test/assets/two_artboards.riv");
26+
27+
REQUIRE(rive_file_artboard_count(file) == 2);
28+
29+
rive_file_delete(file);
30+
}

0 commit comments

Comments
 (0)