skia2/samplecode/SampleCircle.cpp

137 lines
3.9 KiB
C++
Raw Normal View History

Automatic update of all copyright notices to reflect new license terms. I have manually examined all of these diffs and restored a few files that seem to require manual adjustment. The following files still need to be modified manually, in a separate CL: android_sample/SampleApp/AndroidManifest.xml android_sample/SampleApp/res/layout/layout.xml android_sample/SampleApp/res/menu/sample.xml android_sample/SampleApp/res/values/strings.xml android_sample/SampleApp/src/com/skia/sampleapp/SampleApp.java android_sample/SampleApp/src/com/skia/sampleapp/SampleView.java experimental/CiCarbonSampleMain.c experimental/CocoaDebugger/main.m experimental/FileReaderApp/main.m experimental/SimpleCocoaApp/main.m experimental/iOSSampleApp/Shared/SkAlertPrompt.h experimental/iOSSampleApp/Shared/SkAlertPrompt.m experimental/iOSSampleApp/SkiOSSampleApp-Base.xcconfig experimental/iOSSampleApp/SkiOSSampleApp-Debug.xcconfig experimental/iOSSampleApp/SkiOSSampleApp-Release.xcconfig gpu/src/android/GrGLDefaultInterface_android.cpp gyp/common.gypi gyp_skia include/ports/SkHarfBuzzFont.h include/views/SkOSWindow_wxwidgets.h make.bat make.py src/opts/memset.arm.S src/opts/memset16_neon.S src/opts/memset32_neon.S src/opts/opts_check_arm.cpp src/ports/SkDebug_brew.cpp src/ports/SkMemory_brew.cpp src/ports/SkOSFile_brew.cpp src/ports/SkXMLParser_empty.cpp src/utils/ios/SkImageDecoder_iOS.mm src/utils/ios/SkOSFile_iOS.mm src/utils/ios/SkStream_NSData.mm tests/FillPathTest.cpp Review URL: http://codereview.appspot.com/4816058 git-svn-id: http://skia.googlecode.com/svn/trunk@1982 2bbb7eff-a529-9590-31e7-b0007b416f81
2011-07-28 14:26:00 +00:00
/*
* Copyright 2011 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SampleCode.h"
#include "SkView.h"
#include "SkCanvas.h"
#include "SkDevice.h"
#include "SkPaint.h"
// ensure that we don't accidentally screw up the bounds when the oval is
// fractional, and the impl computes the center and radii, and uses them to
// reconstruct the edges of the circle.
// see bug# 1504910
static void test_circlebounds(SkCanvas* canvas) {
#ifdef SK_SCALAR_IS_FLOAT
SkRect r = { 1.39999998f, 1, 21.3999996f, 21 };
SkPath p;
p.addOval(r);
SkASSERT(r == p.getBounds());
#endif
}
class CircleView : public SampleView {
public:
static const SkScalar ANIM_DX;
static const SkScalar ANIM_DY;
static const SkScalar ANIM_RAD;
SkScalar fDX, fDY, fRAD;
CircleView() {
fDX = fDY = fRAD = 0;
fN = 3;
}
protected:
// overrides from SkEventSink
virtual bool onQuery(SkEvent* evt) {
if (SampleCode::TitleQ(*evt)) {
SampleCode::TitleR(evt, "Circles");
return true;
}
return this->INHERITED::onQuery(evt);
}
void circle(SkCanvas* canvas, int width, bool aa) {
SkPaint paint;
paint.setAntiAlias(aa);
if (width < 0) {
paint.setStyle(SkPaint::kFill_Style);
} else {
paint.setStyle(SkPaint::kStroke_Style);
paint.setStrokeWidth(SkIntToScalar(width));
}
canvas->drawCircle(0, 0, SkIntToScalar(9) + fRAD, paint);
if (false) { // avoid bit rot, suppress warning
test_circlebounds(canvas);
}
}
void drawSix(SkCanvas* canvas, SkScalar dx, SkScalar dy) {
for (int width = -1; width <= 1; width++) {
canvas->save();
circle(canvas, width, false);
canvas->translate(0, dy);
circle(canvas, width, true);
canvas->restore();
canvas->translate(dx, 0);
}
}
static void blowup(SkCanvas* canvas, const SkIRect& src, const SkRect& dst) {
SkDevice* device = canvas->getDevice();
const SkBitmap& bm = device->accessBitmap(false);
canvas->drawBitmapRect(bm, &src, dst, NULL);
}
static void make_poly(SkPath* path, int n) {
if (n <= 0) {
return;
}
path->incReserve(n + 1);
path->moveTo(SK_Scalar1, 0);
SkScalar step = SK_ScalarPI * 2 / n;
SkScalar angle = 0;
for (int i = 1; i < n; i++) {
angle += step;
SkScalar c, s = SkScalarSinCos(angle, &c);
path->lineTo(c, s);
}
path->close();
}
static void rotate(SkCanvas* canvas, SkScalar angle, SkScalar px, SkScalar py) {
canvas->translate(-px, -py);
canvas->rotate(angle);
canvas->translate(px, py);
}
virtual void onDrawContent(SkCanvas* canvas) {
SkPaint paint;
paint.setAntiAlias(true);
paint.setStyle(SkPaint::kStroke_Style);
// canvas->drawCircle(250, 250, 220, paint);
SkMatrix matrix;
matrix.setScale(SkIntToScalar(100), SkIntToScalar(100));
matrix.postTranslate(SkIntToScalar(200), SkIntToScalar(200));
canvas->concat(matrix);
for (int n = 3; n < 20; n++) {
SkPath path;
make_poly(&path, n);
SkAutoCanvasRestore acr(canvas, true);
canvas->rotate(SkIntToScalar(10) * (n - 3));
canvas->translate(-SK_Scalar1, 0);
canvas->drawPath(path, paint);
}
}
private:
int fN;
typedef SampleView INHERITED;
};
const SkScalar CircleView::ANIM_DX(SK_Scalar1 / 67);
const SkScalar CircleView::ANIM_DY(SK_Scalar1 / 29);
const SkScalar CircleView::ANIM_RAD(SK_Scalar1 / 19);
//////////////////////////////////////////////////////////////////////////////
static SkView* MyFactory() { return new CircleView; }
static SkViewRegister reg(MyFactory);