add SampleCode::GetAnimTime() so slides go through a central location for
authorreed@android.com <reed@android.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Mon, 23 Nov 2009 21:07:51 +0000 (21:07 +0000)
committerreed@android.com <reed@android.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Mon, 23 Nov 2009 21:07:51 +0000 (21:07 +0000)
animation timing. This allows us to "freeze" time in order to do things like
draw multiple times to test clipping.

git-svn-id: http://skia.googlecode.com/svn/trunk@444 2bbb7eff-a529-9590-31e7-b0007b416f81

samplecode/SampleApp.cpp
samplecode/SampleArc.cpp
samplecode/SampleCode.h
samplecode/SampleLineClipper.cpp
samplecode/SampleMipMap.cpp
samplecode/SamplePicture.cpp
samplecode/SampleShapes.cpp

index 2d0da1f..8fcdfa8 100644 (file)
@@ -6,6 +6,7 @@
 #include "SkPaint.h"
 #include "SkPicture.h"
 #include "SkStream.h"
+#include "SkTime.h"
 #include "SkWindow.h"
 
 #include "SampleCode.h"
@@ -121,6 +122,18 @@ void SampleCode::PrefSizeR(SkEvent* evt, SkScalar width, SkScalar height) {
     evt->setScalars(gPrefSizeEvtName, 2, size);
 }
 
+static SkMSec gAnimTime;
+SkMSec SampleCode::GetAnimTime() { return gAnimTime; }
+
+SkScalar SampleCode::GetAnimScalar(SkScalar speed, SkScalar period) {
+    SkScalar seconds = SkFloatToScalar(gAnimTime / 1000.0f);
+    SkScalar value = SkScalarMul(speed, seconds);
+    if (period) {
+        value = SkScalarMod(value, period);
+    }
+    return value;
+}
+
 //////////////////////////////////////////////////////////////////////////////
 
 class SampleWindow : public SkOSWindow {
@@ -241,10 +254,13 @@ SampleWindow::~SampleWindow() {
     delete fGLCanvas;
 }
 
-#define XCLIP_N  4
-#define YCLIP_N  1
+#define XCLIP_N  8
+#define YCLIP_N  8
 
 void SampleWindow::draw(SkCanvas* canvas) {
+    // update the animation time
+    gAnimTime = SkTime::GetMSecs();
+
     if (fNClip) {
      //   this->INHERITED::draw(canvas);
      //   SkBitmap orig = capture_bitmap(canvas);
index 19bf5f8..ed62537 100644 (file)
@@ -127,6 +127,9 @@ protected:
     virtual void onDraw(SkCanvas* canvas)
     {
         this->drawBG(canvas);
+        
+        fSweep = SampleCode::GetAnimScalar(SkIntToScalar(360)/24,
+                                           SkIntToScalar(360));
 
         SkRect  r;
         SkPaint paint;
@@ -167,10 +170,6 @@ protected:
         paint.setColor(SK_ColorBLUE);
         canvas->drawArc(r, 0, fSweep, false, paint);
         
-        fSweep += SK_Scalar1/4;
-        if (fSweep > SkIntToScalar(360))
-            fSweep = 0;
-        
         drawArcs(canvas);
         this->inval(NULL);
     }
index fd3ed2a..c901af3 100644 (file)
@@ -10,6 +10,9 @@ public:
     
     static bool PrefSizeQ(const SkEvent&);
     static void PrefSizeR(SkEvent*, SkScalar width, SkScalar height);
+    
+    static SkMSec GetAnimTime();
+    static SkScalar GetAnimScalar(SkScalar speedPerSec, SkScalar period = 0);
 };
 
 //////////////////////////////////////////////////////////////////////////////
index 09606f6..8e2d620 100644 (file)
@@ -147,6 +147,7 @@ enum {
 };
 
 class LineClipperView : public SkView {
+    SkMSec      fNow;
     int         fCounter;
     int         fProcIndex;
     SkRect      fClip;
@@ -196,7 +197,14 @@ protected:
     
     virtual void onDraw(SkCanvas* canvas) {
         this->drawBG(canvas);
-        
+
+        SkMSec now = SampleCode::GetAnimTime();
+        if (fNow != now) {
+            fNow = now;
+            this->randPts();
+            this->inval(NULL);
+        }
+
      //   fProcIndex = test0(fPts, &fClip);
 
         SkPaint paint, paint1;
@@ -219,11 +227,6 @@ protected:
         paint1.setColor(SK_ColorRED);
         paint1.setStyle(SkPaint::kStroke_Style);
         gProcs[fProcIndex](fPts, fClip, canvas, paint, paint1);
-        
-        if (AUTO_ANIMATE) {
-            this->randPts();
-            this->inval(NULL);
-        }
     }
 
     virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) {
index 5885591..07227e5 100644 (file)
@@ -38,7 +38,6 @@ public:
         fBitmap = createBitmap(N);
         
         fWidth = N;
-        fDW = -1;
     }
     
 protected:
@@ -97,15 +96,13 @@ protected:
         SkBitmap bitmap(fBitmap);
         bitmap.buildMipMap();
         drawN2(canvas, bitmap);
-        
-        fWidth += fDW;
-        if (fDW > 0 && fWidth > N) {
-            fDW = -fDW;
-            fWidth = N;
-        } else if (fDW < 0 && fWidth < 8) {
-            fDW = -fDW;
-            fWidth = 8;
+
+        SkScalar time = SampleCode::GetAnimScalar(SkIntToScalar(1)/4,
+                                                  SkIntToScalar(2));
+        if (time >= SK_Scalar1) {
+            time = SkIntToScalar(2) - time;
         }
+        fWidth = 8 + SkScalarRound(N * time);
 
         SkRect dst;
         dst.set(0, 0, SkIntToScalar(fWidth), SkIntToScalar(fWidth));
@@ -140,7 +137,7 @@ protected:
     }
     
 private:
-    int fWidth, fDW;
+    int fWidth;
 
     typedef SkView INHERITED;
 };
index 4ca5b2a..e1b05ad 100644 (file)
@@ -148,9 +148,10 @@ protected:
         
         // test that we can re-record a subpicture, and see the results
         
+        SkRandom rand(SampleCode::GetAnimTime());
         canvas->translate(SkIntToScalar(10), SkIntToScalar(250));
         drawCircle(fSubPicture->beginRecording(50, 50), 25,
-                   fRand.nextU() | 0xFF000000);
+                   rand.nextU() | 0xFF000000);
         canvas->drawPicture(*fPicture);
         delayInval(500);
     }
@@ -172,7 +173,6 @@ private:
 
     SkPicture*  fPicture;
     SkPicture*  fSubPicture;
-    SkRandom    fRand;
 
     typedef SkView INHERITED;
 };
index 07f2d00..b92614b 100644 (file)
@@ -60,8 +60,6 @@ public:
         for (size_t i = 0; i < SK_ARRAY_COUNT(fMatrixRefs); i++) {
             SkSafeRef(fMatrixRefs[i] = fGroup.getShapeMatrixRef(i));
         }
-        
-        fAngle = 0;
     }
     
     virtual ~ShapesView() {
@@ -98,15 +96,15 @@ protected:
 #endif
     }
     
-    int fAngle;
-    
     virtual void onDraw(SkCanvas* canvas) {
         this->drawBG(canvas);
         
+        SkScalar angle = SampleCode::GetAnimScalar(SkIntToScalar(360)/2,
+                                                   SkIntToScalar(360));
+
         SkMatrix saveM = *fMatrixRefs[3];
-        fAngle = (fAngle + 5) % 360;
         SkScalar c = SkIntToScalar(50);
-        fMatrixRefs[3]->preRotate(SkIntToScalar(fAngle), c, c);
+        fMatrixRefs[3]->preRotate(angle, c, c);
         
         SkMatrix matrix;