2 #include "GLFW/glfw3.h"
4 #include "GrBackendSurface.h"
7 #include "SkColorSpace.h"
10 #include "gl/GrGLInterface.h"
12 #include "animation/linear_animation_instance.hpp"
13 #include "artboard.hpp"
16 #include "math/aabb.hpp"
17 #include "skia_renderer.hpp"
22 rive::File* currentFile = nullptr;
23 rive::Artboard* artboard = nullptr;
24 rive::LinearAnimationInstance* animationInstance = nullptr;
26 void glfwErrorCallback(int error, const char* description)
31 void glfwDropCallback(GLFWwindow* window, int count, const char** paths)
33 // Just get the last dropped file for now...
34 auto filename = paths[count - 1];
35 FILE* fp = fopen(filename, "r");
36 fseek(fp, 0, SEEK_END);
37 auto length = ftell(fp);
38 fseek(fp, 0, SEEK_SET);
39 uint8_t* bytes = new uint8_t[length];
40 if (fread(bytes, 1, length, fp) != length)
43 fprintf(stderr, "failed to read all of %s\n", filename);
46 auto reader = rive::BinaryReader(bytes, length);
47 rive::File* file = nullptr;
48 auto result = rive::File::import(reader, &file);
49 if (result != rive::ImportResult::success)
52 fprintf(stderr, "failed to import %s\n", filename);
55 artboard = file->artboard();
56 artboard->advance(0.0f);
58 delete animationInstance;
61 auto animation = artboard->firstAnimation<rive::LinearAnimation>();
62 if (animation != nullptr)
64 animationInstance = new rive::LinearAnimationInstance(animation);
68 animationInstance = nullptr;
79 fprintf(stderr, "Failed to initialize glfw.\n");
82 glfwSetErrorCallback(glfwErrorCallback);
84 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
85 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
86 GLFWwindow* window = glfwCreateWindow(1280, 720, "Rive Viewer", NULL, NULL);
87 if (window == nullptr)
89 fprintf(stderr, "Failed to make window or GL.\n");
94 glfwSetDropCallback(window, glfwDropCallback);
95 glfwMakeContextCurrent(window);
100 GrContextOptions options;
101 sk_sp<GrContext> context = GrContext::MakeGL(nullptr, options);
102 GrGLFramebufferInfo framebufferInfo;
103 framebufferInfo.fFBOID = 0;
104 framebufferInfo.fFormat = GL_RGBA8;
106 SkSurface* surface = nullptr;
107 SkCanvas* canvas = nullptr;
110 int width = 0, height = 0;
111 int lastScreenWidth = 0, lastScreenHeight = 0;
112 double lastTime = glfwGetTime();
113 while (!glfwWindowShouldClose(window))
115 glfwGetFramebufferSize(window, &width, &height);
118 if (surface == nullptr || width != lastScreenWidth ||
119 height != lastScreenHeight)
121 lastScreenWidth = width;
122 lastScreenHeight = height;
124 SkColorType colorType =
125 kRGBA_8888_SkColorType; // GrColorTypeToSkColorType(GrPixelConfigToColorType(kRGBA_8888_GrPixelConfig));
127 // if (kRGBA_8888_GrPixelConfig == kSkia8888_GrPixelConfig)
129 // colorType = kRGBA_8888_SkColorType;
133 // colorType = kBGRA_8888_SkColorType;
136 GrBackendRenderTarget backendRenderTarget(width,
143 surface = SkSurface::MakeFromBackendRenderTarget(
146 kBottomLeft_GrSurfaceOrigin,
151 if (surface == nullptr)
153 fprintf(stderr, "Failed to create Skia surface\n");
156 canvas = surface->getCanvas();
159 double time = glfwGetTime();
160 float elapsed = (float)(time - lastTime);
165 paint.setColor(SK_ColorDKGRAY);
166 canvas->drawPaint(paint);
168 if (artboard != nullptr)
170 if (animationInstance != nullptr)
172 animationInstance->advance(elapsed);
173 animationInstance->apply(artboard);
175 artboard->advance(elapsed);
177 rive::SkiaRenderer renderer(canvas);
179 renderer.align(rive::Fit::contain,
180 rive::Alignment::center,
181 rive::AABB(0, 0, width, height),
183 artboard->draw(&renderer);
189 glfwSwapBuffers(window);
200 glfwDestroyWindow(window);