caryclark@google.com | d88e089 | 2012-03-27 13:23:51 +0000 | [diff] [blame^] | 1 | #include "EdgeWalker_Test.h" |
| 2 | #include "ShapeOps.h" |
| 3 | #import "SkCanvas.h" |
| 4 | #import "SkPaint.h" |
| 5 | #import "SkWindow.h" |
| 6 | #include "SkGraphics.h" |
| 7 | #include "SkCGUtils.h" |
| 8 | |
| 9 | class SkSampleView : public SkView { |
| 10 | public: |
| 11 | SkSampleView() { |
| 12 | this->setVisibleP(true); |
| 13 | this->setClipToBounds(false); |
| 14 | }; |
| 15 | protected: |
| 16 | virtual void onDraw(SkCanvas* canvas) { |
| 17 | // Three circles bounce inside a rectangle. The circles describe three, four |
| 18 | // or five points which in turn describe a polygon. The polygon points |
| 19 | // bounce inside the circles. The circles rotate and scale over time. The |
| 20 | // polygons are combined into a single path, simplified, and stroked. |
| 21 | static int step = 0; |
| 22 | const int circles = 3; |
| 23 | int scales[circles]; |
| 24 | int angles[circles]; |
| 25 | int locs[circles * 2]; |
| 26 | int pts[circles * 2 * 4]; |
| 27 | int c, p; |
| 28 | for (c = 0; c < circles; ++c) { |
| 29 | scales[c] = abs(10 - (step + c * 4) % 21); |
| 30 | angles[c] = (step + c * 6) % 600; |
| 31 | locs[c * 2] = abs(130 - (step + c * 9) % 261); |
| 32 | locs[c * 2 + 1] = abs(170 - (step + c * 11) % 341); |
| 33 | for (p = 0; p < 4; ++p) { |
| 34 | pts[c * 8 + p * 2] = abs(90 - ((step + c * 121 + p * 13) % 190)); |
| 35 | pts[c * 8 + p * 2 + 1] = abs(110 - ((step + c * 223 + p * 17) % 230)); |
| 36 | } |
| 37 | } |
| 38 | SkPath path, out; |
| 39 | for (c = 0; c < circles; ++c) { |
| 40 | for (p = 0; p < 4; ++p) { |
| 41 | SkScalar x = pts[c * 8 + p * 2]; |
| 42 | SkScalar y = pts[c * 8 + p * 2 + 1]; |
| 43 | x *= 3 + scales[c] / 10.0f; |
| 44 | y *= 3 + scales[c] / 10.0f; |
| 45 | SkScalar angle = angles[c] * 3.1415f * 2 / 600; |
| 46 | SkScalar temp = x * cos(angle) - y * sin(angle); |
| 47 | y = x * sin(angle) + y * cos(angle); |
| 48 | x = temp; |
| 49 | x += locs[c * 2] * 200 / 130.0f; |
| 50 | y += locs[c * 2 + 1] * 200 / 170.0f; |
| 51 | x += 50; |
| 52 | // y += 200; |
| 53 | if (p == 0) { |
| 54 | path.moveTo(x, y); |
| 55 | } else { |
| 56 | path.lineTo(x, y); |
| 57 | } |
| 58 | } |
| 59 | path.close(); |
| 60 | } |
| 61 | showPath(path, "original:"); |
| 62 | simplify(path, true, out); |
| 63 | showPath(out, "simplified:"); |
| 64 | SkPaint paint; |
| 65 | paint.setAntiAlias(true); |
| 66 | paint.setStyle(SkPaint::kStroke_Style); |
| 67 | paint.setStrokeWidth(3); |
| 68 | paint.setColor(0x3F007fbF); |
| 69 | canvas->drawPath(path, paint); |
| 70 | paint.setColor(0xFF60FF00); |
| 71 | paint.setStrokeWidth(1); |
| 72 | canvas->drawColor(SK_ColorWHITE); |
| 73 | canvas->drawPath(out, paint); |
| 74 | ++step; |
| 75 | inval(NULL); |
| 76 | } |
| 77 | private: |
| 78 | typedef SkView INHERITED; |
| 79 | }; |
| 80 | |
| 81 | void application_init() { |
| 82 | SkGraphics::Init(); |
| 83 | SkEvent::Init(); |
| 84 | } |
| 85 | |
| 86 | void application_term() { |
| 87 | SkGraphics::Term(); |
| 88 | SkEvent::Term(); |
| 89 | } |
| 90 | |
| 91 | class FillLayout : public SkView::Layout { |
| 92 | protected: |
| 93 | virtual void onLayoutChildren(SkView* parent) { |
| 94 | SkView* view = SkView::F2BIter(parent).next(); |
| 95 | view->setSize(parent->width(), parent->height()); |
| 96 | } |
| 97 | }; |
| 98 | |
| 99 | #import "SimpleApp.h" |
| 100 | @implementation SimpleNSView |
| 101 | |
| 102 | - (id)initWithDefaults { |
| 103 | if (self = [super initWithDefaults]) { |
| 104 | fWind = new SkOSWindow(self); |
| 105 | fWind->setLayout(new FillLayout, false); |
| 106 | fWind->attachChildToFront(new SkSampleView)->unref(); |
| 107 | } |
| 108 | return self; |
| 109 | } |
| 110 | |
| 111 | - (void)drawRect:(NSRect)dirtyRect { |
| 112 | CGContextRef ctx = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort]; |
| 113 | SkCGDrawBitmap(ctx, fWind->getBitmap(), 0, 0); |
| 114 | } |
| 115 | |
| 116 | @end |