4 #include <Elementary.h>
5 #include <rive_tizen.hpp>
8 #include "animation/linear_animation_instance.hpp"
9 #include "artboard.hpp"
11 #include "tvg_renderer.hpp"
17 #define LIST_HEIGHT 200
19 static unique_ptr<tvg::SwCanvas> canvas = nullptr;
20 static rive::File* currentFile = nullptr;
21 static rive::Artboard* artboard = nullptr;
22 static rive::LinearAnimationInstance* animationInstance;
23 static Ecore_Animator *animator = nullptr;
24 static Eo* view = nullptr;
25 static double lastTime;
27 static void deleteWindow(void *data, Evas_Object *obj, void *ev)
32 static void drawToCanvas(void* data, Eo* obj)
34 if (canvas->draw() == tvg::Result::Success) canvas->sync();
37 static void loadRiveFile(const char* filename)
39 lastTime = ecore_time_get();
42 FILE* fp = fopen(filename, "r");
44 fseek(fp, 0, SEEK_END);
45 size_t length = ftell(fp);
46 fseek(fp, 0, SEEK_SET);
48 uint8_t* bytes = new uint8_t[length];
49 if (fread(bytes, 1, length, fp) != length)
52 fprintf(stderr, "failed to read all of %s\n", filename);
56 auto reader = rive::BinaryReader(bytes, length);
57 rive::File* file = nullptr;
58 auto result = rive::File::import(reader, &file);
59 if (result != rive::ImportResult::success)
62 fprintf(stderr, "failed to import %s\n", filename);
66 artboard = file->artboard();
67 artboard->advance(0.0f);
69 delete animationInstance;
70 animationInstance = nullptr;
72 auto animation = artboard->firstAnimation();
73 if (animation) animationInstance = new rive::LinearAnimationInstance(animation);
81 Eina_Bool animationLoop(void *data)
85 double currentTime = ecore_time_get();
86 float elapsed = currentTime - lastTime;
87 lastTime = currentTime;
89 if (!artboard) return ECORE_CALLBACK_RENEW;
91 animationInstance->advance(elapsed);
92 animationInstance->apply(artboard);
94 artboard->advance(elapsed);
96 rive::TvgRenderer renderer(canvas.get());
98 renderer.align(rive::Fit::contain,
99 rive::Alignment::center,
100 rive::AABB(0, 0, WIDTH, HEIGHT),
102 artboard->draw(&renderer);
105 evas_object_image_pixels_dirty_set(view, EINA_TRUE);
106 evas_object_image_data_update_add(view, 0, 0, WIDTH, HEIGHT);
108 return ECORE_CALLBACK_RENEW;
111 static void runExample(uint32_t* buffer)
113 std::string path = RIVE_FILE_DIR;
114 path.append("flame-and-spark.riv");
115 loadRiveFile(path.c_str());
118 canvas = tvg::SwCanvas::gen();
119 canvas->target(buffer, WIDTH, WIDTH, HEIGHT, tvg::SwCanvas::ARGB8888);
120 animator = ecore_animator_add(animationLoop, nullptr);
123 static void cleanExample()
125 delete animationInstance;
128 static void mouseMoveCb(void *data, Evas *evas EINA_UNUSED, Evas_Object *obj, void *event_info)
130 Evas_Event_Mouse_Move *ev = (Evas_Event_Mouse_Move*)event_info;
133 evas_object_geometry_get(obj, &viewx, &viewy, nullptr, nullptr);
135 // Viewx and viewy are the view start position
136 int posx = ev->cur.canvas.x - viewx;
137 // 250 is the constant for align y center
138 int posy = ev->cur.canvas.y - viewy + 250;
140 // Get the root instance
141 auto root = artboard->find("root");
142 auto nodeRoot = root->as<rive::Node>();
144 auto spark = artboard->find("spark");
145 auto nodeSpark = spark->as<rive::Node>();
151 // Set spark position, 400 is the constant
152 nodeSpark->x(posx - 400);
156 static void setupScreen(uint32_t* buffer)
158 Eo* win = elm_win_util_standard_add(nullptr, "Rive-Tizen Viewer");
159 evas_object_smart_callback_add(win, "delete,request", deleteWindow, 0);
161 Eo* box = elm_box_add(win);
162 evas_object_size_hint_weight_set(box, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
163 elm_win_resize_object_add(win, box);
164 evas_object_show(box);
166 view = evas_object_image_filled_add(evas_object_evas_get(box));
167 evas_object_image_size_set(view, WIDTH, HEIGHT);
168 evas_object_image_data_set(view, buffer);
169 evas_object_image_pixels_get_callback_set(view, drawToCanvas, nullptr);
170 evas_object_image_pixels_dirty_set(view, EINA_TRUE);
171 evas_object_image_data_update_add(view, 0, 0, WIDTH, HEIGHT);
172 evas_object_size_hint_weight_set(view, EVAS_HINT_EXPAND, 0.0);
173 evas_object_size_hint_min_set(view, WIDTH, HEIGHT);
174 evas_object_show(view);
175 elm_box_pack_end(box, view);
177 evas_object_event_callback_add(view, EVAS_CALLBACK_MOUSE_MOVE, mouseMoveCb, nullptr);
179 evas_object_resize(win, WIDTH, HEIGHT + LIST_HEIGHT);
180 evas_object_show(win);
183 int main(int argc, char **argv)
185 static uint32_t buffer[WIDTH * HEIGHT];
187 tvg::Initializer::init(tvg::CanvasEngine::Sw, thread::hardware_concurrency());
189 elm_init(argc, argv);
201 tvg::Initializer::term(tvg::CanvasEngine::Sw);