4 #include <Elementary.h>
5 #include <rive_tizen.hpp>
7 #include "animation/linear_animation_instance.hpp"
8 #include "artboard.hpp"
10 #include "thorvg_renderer.hpp"
16 #define LIST_HEIGHT 200
18 static unique_ptr<tvg::SwCanvas> canvas;
19 static tvg::Canvas *renderCanvas;
20 static rive::File* currentFile = nullptr;
21 static rive::Artboard* artboard = nullptr;
22 static rive::LinearAnimationInstance* animationInstance = nullptr;
23 static Ecore_Animator *animator = nullptr;
24 static Eo* view = nullptr;
25 static vector<std::string> rivefiles;
26 static double lastTime;
28 static void deleteWindow(void *data, Evas_Object *obj, void *ev)
33 static void drawToCanvas(void* data, Eo* obj)
35 if (canvas->draw() == tvg::Result::Success)
41 static bool isRiveFile(const char *filename)
43 const char *dot = strrchr(filename, '.');
44 if(!dot || dot == filename) return false;
45 return !strcmp(dot + 1, "riv");
48 static void loadRiveFile(const char* filename)
50 // Clear Canvas Buffer
51 renderCanvas->clear();
54 FILE* fp = fopen(filename, "r");
56 fseek(fp, 0, SEEK_END);
57 size_t length = ftell(fp);
58 fseek(fp, 0, SEEK_SET);
60 uint8_t* bytes = new uint8_t[length];
61 if (fread(bytes, 1, length, fp) != length)
64 fprintf(stderr, "failed to read all of %s\n", filename);
68 auto reader = rive::BinaryReader(bytes, length);
69 rive::File* file = nullptr;
70 auto result = rive::File::import(reader, &file);
71 if (result != rive::ImportResult::success)
74 fprintf(stderr, "failed to import %s\n", filename);
78 artboard = file->artboard();
79 artboard->advance(0.0f);
81 delete animationInstance;
84 auto animation = artboard->firstAnimation<rive::LinearAnimation>();
85 if (animation != nullptr)
87 animationInstance = new rive::LinearAnimationInstance(animation);
91 animationInstance = nullptr;
98 static void fileClickedCb (void *data, Evas_Object *obj, void *event_info)
100 Elm_Object_Item *item = elm_list_selected_item_get(obj);
102 for (Elm_Object_Item *iter = item; iter != NULL; iter = elm_list_item_prev(iter))
104 if (rivefiles.size() > 0)
105 loadRiveFile(rivefiles[index-1].c_str());
108 static std::vector<std::string> riveFiles(const std::string &dirName)
112 std::vector<std::string> result;
113 d = opendir(dirName.c_str());
115 while ((dir = readdir(d)) != NULL) {
116 if (isRiveFile(dir->d_name))
117 result.push_back(dirName + dir->d_name);
122 std::sort(result.begin(), result.end(), [](auto & a, auto &b){return a < b;});
127 Eina_Bool animationLoop(void *data)
129 double currentTime = ecore_time_get();
130 float elapsed = currentTime - lastTime;
131 static float animationTime = 0;
132 lastTime = currentTime;
134 if (artboard != nullptr)
136 if (animationInstance != nullptr)
138 animationInstance->advance(elapsed);
139 animationInstance->apply(artboard);
141 artboard->advance(elapsed);
143 rive::TvgRenderer renderer(renderCanvas);
145 renderer.align(rive::Fit::contain,
146 rive::Alignment::center,
147 rive::AABB(0, 0, WIDTH, HEIGHT),
149 artboard->draw(&renderer);
153 evas_object_image_pixels_dirty_set(view, EINA_TRUE);
154 evas_object_image_data_update_add(view, 0, 0, WIDTH, HEIGHT);
156 return ECORE_CALLBACK_RENEW;
159 static void runExample(uint32_t* buffer)
162 canvas = tvg::SwCanvas::gen();
163 canvas->target(buffer, WIDTH, WIDTH, HEIGHT, tvg::SwCanvas::ARGB8888);
164 renderCanvas = canvas.get();
166 lastTime = ecore_time_get();
167 ecore_animator_frametime_set(1. / 60);
168 animator = ecore_animator_add(animationLoop, nullptr);
171 static void cleanExample()
173 delete animationInstance;
176 static void setupScreen(uint32_t* buffer)
178 Eo* win = elm_win_util_standard_add(NULL, "Rive Viewer");
179 evas_object_smart_callback_add(win, "delete,request", deleteWindow, 0);
181 Eo* box = elm_box_add(win);
182 evas_object_size_hint_weight_set(box, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
183 elm_win_resize_object_add(win, box);
184 evas_object_show(box);
186 view = evas_object_image_filled_add(evas_object_evas_get(box));
187 evas_object_image_size_set(view, WIDTH, HEIGHT);
188 evas_object_image_data_set(view, buffer);
189 evas_object_image_pixels_get_callback_set(view, drawToCanvas, nullptr);
190 evas_object_image_pixels_dirty_set(view, EINA_TRUE);
191 evas_object_image_data_update_add(view, 0, 0, WIDTH, HEIGHT);
192 evas_object_size_hint_weight_set(view, EVAS_HINT_EXPAND, 0.0);
193 evas_object_size_hint_min_set(view, WIDTH, HEIGHT);
194 evas_object_show(view);
196 elm_box_pack_end(box, view);
198 Eo *fileList = elm_list_add(box);
199 evas_object_size_hint_weight_set(fileList, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
200 evas_object_size_hint_align_set(fileList, EVAS_HINT_FILL, EVAS_HINT_FILL);
202 // Search Rive Files in Resource Dir
203 rivefiles = riveFiles(RIVE_FILE_DIR);
204 for (int i = 0; i < rivefiles.size(); i++)
206 const char *ptr = strrchr(rivefiles[i].c_str(), '/');
207 Elm_Object_Item *item = elm_list_item_append(fileList, ptr + 1, NULL, NULL, fileClickedCb, NULL);
209 elm_list_go(fileList);
211 elm_box_pack_end(box, fileList);
212 evas_object_show(fileList);
214 evas_object_resize(win, WIDTH, HEIGHT + LIST_HEIGHT);
215 evas_object_show(win);
218 int main(int argc, char **argv)
220 static uint32_t buffer[WIDTH * HEIGHT];
222 tvg::Initializer::init(tvg::CanvasEngine::Sw, thread::hardware_concurrency());
224 elm_init(argc, argv);
236 tvg::Initializer::term(tvg::CanvasEngine::Sw);