4 #include <Elementary.h>
5 #include <rive_tizen.hpp>
7 #include "animation/linear_animation_instance.hpp"
8 #include "artboard.hpp"
10 #include "tvg_renderer.hpp"
16 #define LIST_HEIGHT 200
18 static unique_ptr<tvg::SwCanvas> canvas = nullptr;
19 static rive::File* currentFile = nullptr;
20 static rive::Artboard* artboard = nullptr;
21 static rive::LinearAnimationInstance* animationInstance = nullptr;
22 static Ecore_Animator *animator = nullptr;
23 static Eo* view = nullptr;
24 static vector<std::string> rivefiles;
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 bool isRiveFile(const char *filename)
39 const char *dot = strrchr(filename, '.');
40 if(!dot || dot == filename) return false;
41 return !strcmp(dot + 1, "riv");
44 static void loadRiveFile(const char* filename)
46 lastTime = ecore_time_get(); //Check point
47 canvas->clear(); //Clear Canvas Buffer
50 FILE* fp = fopen(filename, "r");
52 fseek(fp, 0, SEEK_END);
53 size_t length = ftell(fp);
54 fseek(fp, 0, SEEK_SET);
56 uint8_t* bytes = new uint8_t[length];
57 if (fread(bytes, 1, length, fp) != length)
60 fprintf(stderr, "failed to read all of %s\n", filename);
64 auto reader = rive::BinaryReader(bytes, length);
65 rive::File* file = nullptr;
66 auto result = rive::File::import(reader, &file);
67 if (result != rive::ImportResult::success)
70 fprintf(stderr, "failed to import %s\n", filename);
74 artboard = file->artboard();
75 artboard->advance(0.0f);
77 delete animationInstance;
78 animationInstance = nullptr;
80 auto animation = artboard->firstAnimation();
81 if (animation) animationInstance = new rive::LinearAnimationInstance(animation);
89 static void fileClickedCb (void *data, Evas_Object *obj, void *event_info)
91 Elm_Object_Item *item = elm_list_selected_item_get(obj);
93 for (Elm_Object_Item *iter = item; iter != NULL; iter = elm_list_item_prev(iter))
97 if (rivefiles.size() > 0) loadRiveFile(rivefiles[index-1].c_str());
100 static std::vector<std::string> riveFiles(const std::string &dirName)
104 std::vector<std::string> result;
105 d = opendir(dirName.c_str());
108 while ((dir = readdir(d)) != NULL)
110 if (isRiveFile(dir->d_name)) result.push_back(dirName + dir->d_name);
115 std::sort(result.begin(), result.end(), [](auto & a, auto &b){return a < b;});
120 Eina_Bool animationLoop(void *data)
124 double currentTime = ecore_time_get();
125 float elapsed = currentTime - lastTime;
126 lastTime = currentTime;
128 if (!artboard || !animationInstance) return ECORE_CALLBACK_RENEW;
130 animationInstance->advance(elapsed);
131 animationInstance->apply(artboard);
133 artboard->advance(elapsed);
135 rive::TvgRenderer renderer(canvas.get());
137 renderer.align(rive::Fit::contain,
138 rive::Alignment::center,
139 rive::AABB(0, 0, WIDTH, HEIGHT),
141 artboard->draw(&renderer);
144 evas_object_image_pixels_dirty_set(view, EINA_TRUE);
145 evas_object_image_data_update_add(view, 0, 0, WIDTH, HEIGHT);
147 return ECORE_CALLBACK_RENEW;
150 static void runExample(uint32_t* buffer)
153 canvas = tvg::SwCanvas::gen();
154 canvas->target(buffer, WIDTH, WIDTH, HEIGHT, tvg::SwCanvas::ARGB8888);
155 animator = ecore_animator_add(animationLoop, nullptr);
158 static void cleanExample()
160 delete animationInstance;
163 static void setupScreen(uint32_t* buffer)
165 Eo* win = elm_win_util_standard_add(NULL, "Rive Viewer");
166 evas_object_smart_callback_add(win, "delete,request", deleteWindow, 0);
168 Eo* box = elm_box_add(win);
169 evas_object_size_hint_weight_set(box, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
170 elm_win_resize_object_add(win, box);
171 evas_object_show(box);
173 view = evas_object_image_filled_add(evas_object_evas_get(box));
174 evas_object_image_size_set(view, WIDTH, HEIGHT);
175 evas_object_image_data_set(view, buffer);
176 evas_object_image_pixels_get_callback_set(view, drawToCanvas, nullptr);
177 evas_object_image_pixels_dirty_set(view, EINA_TRUE);
178 evas_object_image_data_update_add(view, 0, 0, WIDTH, HEIGHT);
179 evas_object_size_hint_weight_set(view, EVAS_HINT_EXPAND, 0.0);
180 evas_object_size_hint_min_set(view, WIDTH, HEIGHT);
181 evas_object_show(view);
183 elm_box_pack_end(box, view);
185 Eo *fileList = elm_list_add(box);
186 evas_object_size_hint_weight_set(fileList, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
187 evas_object_size_hint_align_set(fileList, EVAS_HINT_FILL, EVAS_HINT_FILL);
189 // Search Rive Files in Resource Dir
190 rivefiles = riveFiles(RIVE_FILE_DIR);
191 for (size_t i = 0; i < rivefiles.size(); i++)
193 const char *ptr = strrchr(rivefiles[i].c_str(), '/');
194 elm_list_item_append(fileList, ptr + 1, NULL, NULL, fileClickedCb, NULL);
196 elm_list_go(fileList);
198 elm_box_pack_end(box, fileList);
199 evas_object_show(fileList);
201 evas_object_resize(win, WIDTH, HEIGHT + LIST_HEIGHT);
202 evas_object_show(win);
205 int main(int argc, char **argv)
207 static uint32_t buffer[WIDTH * HEIGHT];
209 tvg::Initializer::init(tvg::CanvasEngine::Sw, thread::hardware_concurrency());
211 elm_init(argc, argv);
223 tvg::Initializer::term(tvg::CanvasEngine::Sw);