mirror of
https://github.com/thorvg/thorvg.git
synced 2025-06-08 05:33:36 +00:00
examples: added LottieExpressions
This commit is contained in:
parent
cfc8f13584
commit
b915455e89
15 changed files with 27031 additions and 15 deletions
|
@ -25,7 +25,6 @@
|
|||
#include <vector>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
#include <thorvg.h>
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_syswm.h>
|
||||
|
@ -137,10 +136,10 @@ struct Window
|
|||
bool needDraw = false;
|
||||
bool print = false;
|
||||
|
||||
Window(tvg::CanvasEngine engine, Example* example, uint32_t width, uint32_t height)
|
||||
Window(tvg::CanvasEngine engine, Example* example, uint32_t width, uint32_t height, uint32_t threadsCnt)
|
||||
{
|
||||
//Initialize ThorVG Engine (4: threads count, engine: raster method)
|
||||
if (!verify(tvg::Initializer::init(4, engine), "Failed to init ThorVG engine!")) return;
|
||||
//Initialize ThorVG Engine (engine: raster method)
|
||||
if (!verify(tvg::Initializer::init(threadsCnt, engine), "Failed to init ThorVG engine!")) return;
|
||||
|
||||
//Initialize the SDL
|
||||
SDL_Init(SDL_INIT_VIDEO);
|
||||
|
@ -266,7 +265,7 @@ struct SwWindow : Window
|
|||
{
|
||||
unique_ptr<tvg::SwCanvas> canvas = nullptr;
|
||||
|
||||
SwWindow(Example* example, uint32_t width, uint32_t height) : Window(tvg::CanvasEngine::Sw, example, width, height)
|
||||
SwWindow(Example* example, uint32_t width, uint32_t height, uint32_t threadsCnt) : Window(tvg::CanvasEngine::Sw, example, width, height, threadsCnt)
|
||||
{
|
||||
window = SDL_CreateWindow("ThorVG Example (Software)", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_HIDDEN | SDL_WINDOW_RESIZABLE);
|
||||
|
||||
|
@ -307,7 +306,7 @@ struct GlWindow : Window
|
|||
|
||||
unique_ptr<tvg::GlCanvas> canvas = nullptr;
|
||||
|
||||
GlWindow(Example* example, uint32_t width, uint32_t height) : Window(tvg::CanvasEngine::Gl, example, width, height)
|
||||
GlWindow(Example* example, uint32_t width, uint32_t height, uint32_t threadsCnt) : Window(tvg::CanvasEngine::Gl, example, width, height, threadsCnt)
|
||||
{
|
||||
#ifdef THORVG_GL_TARGET_GLES
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
|
||||
|
@ -363,7 +362,7 @@ struct WgWindow : Window
|
|||
WGPUInstance instance;
|
||||
WGPUSurface surface;
|
||||
|
||||
WgWindow(Example* example, uint32_t width, uint32_t height) : Window(tvg::CanvasEngine::Wg, example, width, height)
|
||||
WgWindow(Example* example, uint32_t width, uint32_t height, uint32_t threadsCnt) : Window(tvg::CanvasEngine::Wg, example, width, height, threadsCnt)
|
||||
{
|
||||
window = SDL_CreateWindow("ThorVG Example (WebGPU)", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_HIDDEN);
|
||||
|
||||
|
@ -443,7 +442,7 @@ struct WgWindow : Window
|
|||
#else
|
||||
struct WgWindow : Window
|
||||
{
|
||||
WgWindow(Example* example, uint32_t width, uint32_t height) : Window(tvg::CanvasEngine::Wg, example, width, height)
|
||||
WgWindow(Example* example, uint32_t width, uint32_t height, uint32_t threadsCnt) : Window(tvg::CanvasEngine::Wg, example, width, height, threadsCnt)
|
||||
{
|
||||
cout << "webgpu driver is not detected!" << endl;
|
||||
}
|
||||
|
@ -496,7 +495,7 @@ bool verify(tvg::Result result, string failMsg)
|
|||
}
|
||||
|
||||
|
||||
int main(Example* example, int argc, char **argv, uint32_t width = 800, uint32_t height = 800, bool print = false)
|
||||
int main(Example* example, int argc, char **argv, uint32_t width = 800, uint32_t height = 800, uint32_t threadsCnt = 4, bool print = false)
|
||||
{
|
||||
auto engine = tvg::CanvasEngine::Sw;
|
||||
|
||||
|
@ -508,11 +507,11 @@ int main(Example* example, int argc, char **argv, uint32_t width = 800, uint32_t
|
|||
unique_ptr<Window> window;
|
||||
|
||||
if (engine == tvg::CanvasEngine::Sw) {
|
||||
window = unique_ptr<Window>(new SwWindow(example, width, height));
|
||||
window = unique_ptr<Window>(new SwWindow(example, width, height, threadsCnt));
|
||||
} else if (engine == tvg::CanvasEngine::Gl) {
|
||||
window = unique_ptr<Window>(new GlWindow(example, width, height));
|
||||
window = unique_ptr<Window>(new GlWindow(example, width, height, threadsCnt));
|
||||
} else if (engine == tvg::CanvasEngine::Wg) {
|
||||
window = unique_ptr<Window>(new WgWindow(example, width, height));
|
||||
window = unique_ptr<Window>(new WgWindow(example, width, height, threadsCnt));
|
||||
}
|
||||
|
||||
window->print = print;
|
||||
|
|
|
@ -122,5 +122,5 @@ struct UserExample : tvgexam::Example
|
|||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
return tvgexam::main(new UserExample, argc, argv, 1280, 1280, true);
|
||||
return tvgexam::main(new UserExample, argc, argv, 1280, 1280, 4, true);
|
||||
}
|
126
examples/LottieExpressions.cpp
Normal file
126
examples/LottieExpressions.cpp
Normal file
|
@ -0,0 +1,126 @@
|
|||
/*
|
||||
* Copyright (c) 2024 the ThorVG project. All rights reserved.
|
||||
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "Example.h"
|
||||
|
||||
/************************************************************************/
|
||||
/* ThorVG Drawing Contents */
|
||||
/************************************************************************/
|
||||
|
||||
#define NUM_PER_ROW 3
|
||||
#define NUM_PER_COL 3
|
||||
|
||||
struct UserExample : tvgexam::Example
|
||||
{
|
||||
std::vector<unique_ptr<tvg::Animation>> animations;
|
||||
uint32_t w, h;
|
||||
uint32_t size;
|
||||
|
||||
int counter = 0;
|
||||
|
||||
void populate(const char* path) override
|
||||
{
|
||||
if (counter >= NUM_PER_ROW * NUM_PER_COL) return;
|
||||
|
||||
//ignore if not lottie.
|
||||
const char *ext = path + strlen(path) - 4;
|
||||
if (strcmp(ext, "json")) return;
|
||||
|
||||
//Animation Controller
|
||||
auto animation = tvg::Animation::gen();
|
||||
auto picture = animation->picture();
|
||||
|
||||
if (!tvgexam::verify(picture->load(path))) return;
|
||||
|
||||
//image scaling preserving its aspect ratio
|
||||
float scale;
|
||||
float shiftX = 0.0f, shiftY = 0.0f;
|
||||
float w, h;
|
||||
picture->size(&w, &h);
|
||||
|
||||
if (w > h) {
|
||||
scale = size / w;
|
||||
shiftY = (size - h * scale) * 0.5f;
|
||||
} else {
|
||||
scale = size / h;
|
||||
shiftX = (size - w * scale) * 0.5f;
|
||||
}
|
||||
|
||||
picture->scale(scale);
|
||||
picture->translate((counter % NUM_PER_ROW) * size + shiftX, (counter / NUM_PER_ROW) * (this->h / NUM_PER_COL) + shiftY);
|
||||
|
||||
animations.push_back(std::move(animation));
|
||||
|
||||
cout << "Lottie: " << path << endl;
|
||||
|
||||
counter++;
|
||||
}
|
||||
|
||||
bool update(tvg::Canvas* canvas, uint32_t elapsed) override
|
||||
{
|
||||
if (!canvas) return false;
|
||||
|
||||
for (auto& animation : animations) {
|
||||
auto progress = tvgexam::progress(elapsed, animation->duration());
|
||||
animation->frame(animation->totalFrame() * progress);
|
||||
}
|
||||
|
||||
canvas->update();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool content(tvg::Canvas* canvas, uint32_t w, uint32_t h) override
|
||||
{
|
||||
if (!canvas) return false;
|
||||
|
||||
//Background
|
||||
auto shape = tvg::Shape::gen();
|
||||
shape->appendRect(0, 0, w, h);
|
||||
shape->fill(75, 75, 75);
|
||||
|
||||
canvas->push(std::move(shape));
|
||||
|
||||
this->w = w;
|
||||
this->h = h;
|
||||
this->size = w / NUM_PER_ROW;
|
||||
|
||||
this->scandir(EXAMPLE_DIR"/lottie/expressions");
|
||||
|
||||
//Run animation loop
|
||||
for (auto& animation : animations) {
|
||||
canvas->push(tvg::cast(animation->picture()));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/************************************************************************/
|
||||
/* Entry Point */
|
||||
/************************************************************************/
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
return tvgexam::main(new UserExample, argc, argv, 800, 800, 0, true);
|
||||
}
|
|
@ -77,5 +77,5 @@ struct UserExample : tvgexam::Example
|
|||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
return tvgexam::main(new UserExample, argc, argv, 1024, 1024, true);
|
||||
return tvgexam::main(new UserExample, argc, argv, 1024, 1024, 4, true);
|
||||
}
|
|
@ -78,5 +78,5 @@ struct UserExample : tvgexam::Example
|
|||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
return tvgexam::main(new UserExample, argc, argv, 1024, 1024, true);
|
||||
return tvgexam::main(new UserExample, argc, argv, 1024, 1024, 4, true);
|
||||
}
|
|
@ -81,6 +81,9 @@ if lottie_loader
|
|||
source_file += 'LottieExtension.cpp'
|
||||
endif
|
||||
|
||||
if lottie_expressions
|
||||
source_file += 'LottieExpressions.cpp'
|
||||
endif
|
||||
|
||||
foreach current_file : source_file
|
||||
name = current_file.split('.')[0]
|
||||
|
|
1
examples/resources/lottie/expressions/bicycle.json
Normal file
1
examples/resources/lottie/expressions/bicycle.json
Normal file
File diff suppressed because one or more lines are too long
1
examples/resources/lottie/expressions/driving.json
Normal file
1
examples/resources/lottie/expressions/driving.json
Normal file
File diff suppressed because one or more lines are too long
15465
examples/resources/lottie/expressions/jolly_walker.json
Normal file
15465
examples/resources/lottie/expressions/jolly_walker.json
Normal file
File diff suppressed because it is too large
Load diff
1
examples/resources/lottie/expressions/loopOut.json
Normal file
1
examples/resources/lottie/expressions/loopOut.json
Normal file
File diff suppressed because one or more lines are too long
11416
examples/resources/lottie/expressions/night_own.json
Normal file
11416
examples/resources/lottie/expressions/night_own.json
Normal file
File diff suppressed because it is too large
Load diff
1
examples/resources/lottie/expressions/snail.json
Normal file
1
examples/resources/lottie/expressions/snail.json
Normal file
File diff suppressed because one or more lines are too long
1
examples/resources/lottie/expressions/traveling.json
Normal file
1
examples/resources/lottie/expressions/traveling.json
Normal file
File diff suppressed because one or more lines are too long
1
examples/resources/lottie/expressions/windmill.json
Normal file
1
examples/resources/lottie/expressions/windmill.json
Normal file
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Loading…
Add table
Reference in a new issue