Gradients no longer created on every frame

This commit is contained in:
Matt Sullivan
2020-09-29 12:39:10 -07:00
parent 06d8e3a94a
commit fd4d104c12
5 changed files with 20 additions and 14 deletions

Binary file not shown.

View File

@@ -15,6 +15,7 @@
C99E2FE3251E8DB4009227CA /* van.riv in Resources */ = {isa = PBXBuildFile; fileRef = C99E2FE0251E8DB4009227CA /* van.riv */; };
C99E2FE8251E8E35009227CA /* bird.riv in Resources */ = {isa = PBXBuildFile; fileRef = C99E2FE6251E8E35009227CA /* bird.riv */; };
C99E2FE9251E8E35009227CA /* funtime.riv in Resources */ = {isa = PBXBuildFile; fileRef = C99E2FE7251E8E35009227CA /* funtime.riv */; };
C9A7D7F52523C1D900AFB875 /* sheep.riv in Resources */ = {isa = PBXBuildFile; fileRef = C9A7D7F22523C1D900AFB875 /* sheep.riv */; };
C9C73E9824FC471E00EF9516 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9C73E9724FC471E00EF9516 /* AppDelegate.swift */; };
C9C73E9A24FC471E00EF9516 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9C73E9924FC471E00EF9516 /* SceneDelegate.swift */; };
C9C73E9C24FC471E00EF9516 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = C9C73E9B24FC471E00EF9516 /* ContentView.swift */; };
@@ -65,6 +66,7 @@
C99E2FE0251E8DB4009227CA /* van.riv */ = {isa = PBXFileReference; lastKnownFileType = file; path = van.riv; sourceTree = "<group>"; };
C99E2FE6251E8E35009227CA /* bird.riv */ = {isa = PBXFileReference; lastKnownFileType = file; path = bird.riv; sourceTree = "<group>"; };
C99E2FE7251E8E35009227CA /* funtime.riv */ = {isa = PBXFileReference; lastKnownFileType = file; path = funtime.riv; sourceTree = "<group>"; };
C9A7D7F22523C1D900AFB875 /* sheep.riv */ = {isa = PBXFileReference; lastKnownFileType = file; path = sheep.riv; sourceTree = "<group>"; };
C9C73E9424FC471E00EF9516 /* RiveExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = RiveExample.app; sourceTree = BUILT_PRODUCTS_DIR; };
C9C73E9724FC471E00EF9516 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
C9C73E9924FC471E00EF9516 /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = "<group>"; };
@@ -91,6 +93,7 @@
C9696B0E24FC6FD10041502A /* Assets */ = {
isa = PBXGroup;
children = (
C9A7D7F22523C1D900AFB875 /* sheep.riv */,
C99E2FE6251E8E35009227CA /* bird.riv */,
C99E2FE7251E8E35009227CA /* funtime.riv */,
C99E2FE0251E8DB4009227CA /* van.riv */,
@@ -260,6 +263,7 @@
C970484A250822F300CB3AB3 /* simple.riv in Resources */,
C9C73E9E24FC471E00EF9516 /* Assets.xcassets in Resources */,
C9927C1B250986BF009F17F5 /* truck.riv in Resources */,
C9A7D7F52523C1D900AFB875 /* sheep.riv in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

View File

@@ -11,7 +11,6 @@ import RiveRuntime
struct ContentView: View {
var body: some View {
// Text("Hello, World!")
MyRive()
}
}

View File

@@ -81,8 +81,9 @@ namespace rive
RiveBlendMode currentBlendMode;
float paintThickness;
// Gradient
RiveGradient gradient = RiveGradient::None;
// Gradient data
RiveGradient gradientType = RiveGradient::None;
CGGradientRef gradient = NULL;
CGPoint gradientStart;
CGPoint gradientEnd;
std::vector<CGFloat> colorStops;

View File

@@ -155,7 +155,7 @@ void RiveRenderPaint::blendMode(BlendMode value) {
void RiveRenderPaint::linearGradient(float sx, float sy, float ex, float ey) {
// NSLog(@" --- RenderPaint::linearGradient (%.1f,%.1f), (%.1f,%.1f)", sx, sy, ex, ey);
gradient = RiveGradient::Linear;
gradientType = RiveGradient::Linear;
gradientStart = CGPointMake(sx, sy);
gradientEnd = CGPointMake(ex, ey);
@@ -166,7 +166,7 @@ void RiveRenderPaint::linearGradient(float sx, float sy, float ex, float ey) {
}
void RiveRenderPaint::radialGradient(float sx, float sy, float ex, float ey) {
// NSLog(@" --- RenderPaint::radialGradient");
gradient = RiveGradient::Radial;
gradientType = RiveGradient::Radial;
gradientStart = CGPointMake(sx, sy);
gradientEnd = CGPointMake(ex, ey);
@@ -185,6 +185,10 @@ void RiveRenderPaint::addStop(unsigned int color, float stop) {
}
void RiveRenderPaint::completeGradient() {
// NSLog(@" --- RenderPaint::completeGradient");
if (gradient != NULL) {
CGGradientRelease(gradient); gradient = NULL;
}
gradient = CGGradientCreateWithColorComponents(baseSpace, &colorStops[0], &stops[0], stops.size());
}
/*
@@ -391,9 +395,9 @@ void NewRiveRenderer::drawPath(RenderPath* path, RenderPaint* paint) {
}
// Draw gradient
if (rivePaint->gradient != RiveGradient::None) {
if (rivePaint->gradientType != RiveGradient::None) {
// NSLog(@"Drawing a gradient");
CGGradientRef gradient = CGGradientCreateWithColorComponents(baseSpace, &rivePaint->colorStops[0], &rivePaint->stops[0], rivePaint->stops.size());
// CGGradientRef gradient = CGGradientCreateWithColorComponents(baseSpace, &rivePaint->colorStops[0], &rivePaint->stops[0], rivePaint->stops.size());
// If the path is a stroke, then convert the path to a stroked path to prevent the gradient from filling the path
if (rivePaint->paintStyle == RivePaintStyle::Stroke) {
@@ -402,18 +406,16 @@ void NewRiveRenderer::drawPath(RenderPath* path, RenderPaint* paint) {
}
CGContextClip(ctx);
if (rivePaint->gradient == RiveGradient::Linear) {
CGContextDrawLinearGradient(ctx, gradient, rivePaint->gradientStart, rivePaint->gradientEnd, 0x3);
} else if (rivePaint->gradient == RiveGradient:: Radial) {
if (rivePaint->gradientType == RiveGradient::Linear) {
CGContextDrawLinearGradient(ctx, rivePaint->gradient, rivePaint->gradientStart, rivePaint->gradientEnd, 0x3);
} else if (rivePaint->gradientType == RiveGradient:: Radial) {
// Calculate the end radius
float dx = rivePaint->gradientEnd.x - rivePaint->gradientStart.x;
float dy = rivePaint->gradientEnd.y - rivePaint->gradientStart.y;
float endRadius = sqrt(dx*dx + dy*dy);
CGContextDrawRadialGradient(ctx, gradient, rivePaint->gradientStart, 0, rivePaint->gradientStart, endRadius, kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
CGContextDrawRadialGradient(ctx, rivePaint->gradient, rivePaint->gradientStart, 0, rivePaint->gradientStart, endRadius, kCGGradientDrawsBeforeStartLocation | kCGGradientDrawsAfterEndLocation);
}
// Gradient is no longer needed
CGGradientRelease(gradient); gradient = NULL;
if (rivePaint->paintStyle == RivePaintStyle::Fill) {
CGContextDrawPath(ctx, kCGPathFill);
} else if (rivePaint->paintStyle == RivePaintStyle::Stroke) {