caryclark@google.com | d88e089 | 2012-03-27 13:23:51 +0000 | [diff] [blame^] | 1 | #include "EdgeWalker_Test.h" |
| 2 | #include "ShapeOps.h" |
| 3 | #include "SkApplication.h" |
| 4 | #include "SkCanvas.h" |
| 5 | #include "SkEvent.h" |
| 6 | #include "SkGraphics.h" |
| 7 | #include "SkPaint.h" |
| 8 | |
| 9 | SkCanvas* canvas = 0; |
| 10 | SkBitmap* bitmap; |
| 11 | |
| 12 | static bool test15(SkCanvas* canvas) { |
| 13 | // Three circles bounce inside a rectangle. The circles describe three, four |
| 14 | // or five points which in turn describe a polygon. The polygon points |
| 15 | // bounce inside the circles. The circles rotate and scale over time. The |
| 16 | // polygons are combined into a single path, simplified, and stroked. |
| 17 | static int step = 0; |
| 18 | const int circles = 3; |
| 19 | int scales[circles]; |
| 20 | int angles[circles]; |
| 21 | int locs[circles * 2]; |
| 22 | int pts[circles * 2 * 4]; |
| 23 | int c, p; |
| 24 | for (c = 0; c < circles; ++c) { |
| 25 | scales[c] = abs(10 - (step + c * 4) % 21); |
| 26 | angles[c] = (step + c * 6) % 600; |
| 27 | locs[c * 2] = abs(130 - (step + c * 9) % 261); |
| 28 | locs[c * 2 + 1] = abs(170 - (step + c * 11) % 341); |
| 29 | for (p = 0; p < 4; ++p) { |
| 30 | pts[c * 8 + p * 2] = abs(90 - ((step + c * 121 + p * 13) % 190)); |
| 31 | pts[c * 8 + p * 2 + 1] = abs(110 - ((step + c * 223 + p * 17) % 230)); |
| 32 | } |
| 33 | } |
| 34 | SkPath path, out; |
| 35 | for (c = 0; c < circles; ++c) { |
| 36 | for (p = 0; p < 4; ++p) { |
| 37 | SkScalar x = pts[c * 8 + p * 2]; |
| 38 | SkScalar y = pts[c * 8 + p * 2 + 1]; |
| 39 | x *= 3 + scales[c] / 10.0f; |
| 40 | y *= 3 + scales[c] / 10.0f; |
| 41 | SkScalar angle = angles[c] * 3.1415f * 2 / 600; |
| 42 | SkScalar temp = x * cos(angle) - y * sin(angle); |
| 43 | y = x * sin(angle) + y * cos(angle); |
| 44 | x = temp; |
| 45 | x += locs[c * 2] * 200 / 130.0f; |
| 46 | y += locs[c * 2 + 1] * 200 / 170.0f; |
| 47 | x += 50; |
| 48 | // y += 200; |
| 49 | if (p == 0) { |
| 50 | path.moveTo(x, y); |
| 51 | } else { |
| 52 | path.lineTo(x, y); |
| 53 | } |
| 54 | } |
| 55 | path.close(); |
| 56 | } |
| 57 | showPath(path, "original:"); |
| 58 | simplify(path, true, out); |
| 59 | showPath(out, "simplified:"); |
| 60 | SkPaint paint; |
| 61 | paint.setAntiAlias(true); |
| 62 | paint.setStyle(SkPaint::kStroke_Style); |
| 63 | paint.setStrokeWidth(3); |
| 64 | paint.setColor(0x3F007fbF); |
| 65 | canvas->drawPath(path, paint); |
| 66 | paint.setColor(0xFF60FF00); |
| 67 | paint.setStrokeWidth(1); |
| 68 | canvas->drawPath(out, paint); |
| 69 | ++step; |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | static bool (*tests[])(SkCanvas* ) = { |
| 74 | test15, |
| 75 | }; |
| 76 | |
| 77 | static size_t testsCount = sizeof(tests) / sizeof(tests[0]); |
| 78 | |
| 79 | static bool (*firstTest)(SkCanvas*) = test15; |
| 80 | |
| 81 | extern "C" void* getPixels(bool* animate); |
| 82 | extern "C" void unlockPixels(); |
| 83 | |
| 84 | extern "C" void* getPixels(bool* animate) { |
| 85 | if (!canvas) { |
| 86 | canvas = new SkCanvas(); |
| 87 | bitmap = new SkBitmap(); |
| 88 | SkBitmap::Config config = SkBitmap::kARGB_8888_Config; |
| 89 | |
| 90 | bitmap->setConfig(config, 1100, 630); |
| 91 | bitmap->allocPixels(); |
| 92 | bitmap->setIsOpaque(true); |
| 93 | canvas->setBitmapDevice(*bitmap); |
| 94 | } |
| 95 | canvas->drawColor(SK_ColorWHITE); |
| 96 | size_t index = 0; |
| 97 | if (index == 0 && firstTest) { |
| 98 | while (index < testsCount && tests[index] != firstTest) { |
| 99 | ++index; |
| 100 | } |
| 101 | } |
| 102 | *animate = (tests[index])(canvas); |
| 103 | bitmap->lockPixels(); |
| 104 | return bitmap->getPixels(); |
| 105 | } |
| 106 | |
| 107 | extern "C" void unlockPixels() { |
| 108 | bitmap->unlockPixels(); |
| 109 | } |
| 110 | |