From 1d12b1fd66e5be27fb4769ee09ce4fcd6bcc5979 Mon Sep 17 00:00:00 2001 From: "reed@google.com" Date: Thu, 3 Mar 2011 13:23:35 +0000 Subject: [PATCH] add setXYWH to rects new tests for circles git-svn-id: http://skia.googlecode.com/svn/trunk@877 2bbb7eff-a529-9590-31e7-b0007b416f81 --- include/core/SkRect.h | 14 +++++ samplecode/SampleOvalTest.cpp | 114 ++++++++++++++++++++++++++++++++++ 2 files changed, 128 insertions(+) create mode 100644 samplecode/SampleOvalTest.cpp diff --git a/include/core/SkRect.h b/include/core/SkRect.h index b6caad5cd6..114d1f990c 100644 --- a/include/core/SkRect.h +++ b/include/core/SkRect.h @@ -95,6 +95,13 @@ struct SkIRect { fBottom = bottom; } + void setXYWH(int32_t x, int32_t y, int32_t width, int32_t height) { + fLeft = x; + fTop = y; + fRight = x + width; + fBottom = y + height; + } + /** Offset set the rectangle by adding dx to its left and right, and adding dy to its top and bottom. */ @@ -348,6 +355,13 @@ struct SkRect { */ void set(const SkPoint pts[], int count); + void setXYWH(SkScalar x, SkScalar y, SkScalar width, SkScalar height) { + fLeft = x; + fTop = y; + fRight = x + width; + fBottom = y + height; + } + /** Offset set the rectangle by adding dx to its left and right, and adding dy to its top and bottom. */ diff --git a/samplecode/SampleOvalTest.cpp b/samplecode/SampleOvalTest.cpp new file mode 100644 index 0000000000..daa479b165 --- /dev/null +++ b/samplecode/SampleOvalTest.cpp @@ -0,0 +1,114 @@ +#include "SampleCode.h" +#include "SkView.h" +#include "SkCanvas.h" + +static const int kILimit = 101; +static const SkScalar kLimit = SK_Scalar1 * kILimit; + +class OvalTestView : public SkView { +public: + SkSize fSize; + SkPMColor fInsideColor; // signals an interior pixel that was not set + SkPMColor fOutsideColor; // signals an exterior pixels that was set + SkBitmap fBitmap; + + OvalTestView() { + fSize.set(SK_Scalar1, SK_Scalar1); + + fBitmap.setConfig(SkBitmap::kARGB_8888_Config, kILimit, kILimit); + fBitmap.allocPixels(); + + fInsideColor = SkPreMultiplyColor(SK_ColorRED); + fOutsideColor = SkPreMultiplyColor(SK_ColorGREEN); + } + +protected: + // overrides from SkEventSink + virtual bool onQuery(SkEvent* evt) { + if (SampleCode::TitleQ(*evt)) { + SampleCode::TitleR(evt, "OvalTest"); + return true; + } + return this->INHERITED::onQuery(evt); + } + + void drawBG(SkCanvas* canvas) { + canvas->drawColor(0xFFDDDDDD); + } + + void drawOval() { + SkCanvas canvas(fBitmap); + SkPaint p; + + fBitmap.eraseColor(0); + canvas.drawOval(SkRect::MakeSize(fSize), p); + } + + int checkOval(int* flatCount, int* buldgeCount) { + int flatc = 0; + int buldgec = 0; + const SkScalar rad = SkScalarHalf(fSize.width()); + SkScalar cx = SkScalarHalf(fSize.width()); + SkScalar cy = SkScalarHalf(fSize.height()); + for (int y = 0; y < kILimit; y++) { + for (int x = 0; x < kILimit; x++) { + // measure from pixel centers + SkScalar px = SkIntToScalar(x) + SK_ScalarHalf; + SkScalar py = SkIntToScalar(y) + SK_ScalarHalf; + + SkPMColor* ptr = fBitmap.getAddr32(x, y); + SkScalar dist = SkPoint::Length(px - cx, py - cy); + if (dist <= rad && !*ptr) { + flatc++; + *ptr = fInsideColor; + } else if (dist > rad && *ptr) { + buldgec++; + *ptr = fOutsideColor; + } + } + } + if (flatCount) *flatCount = flatc; + if (buldgeCount) *buldgeCount = buldgec; + return flatc + buldgec; + } + + virtual void onDraw(SkCanvas* canvas) { + this->drawBG(canvas); + + this->drawOval(); + int flatCount, buldgeCount; + this->checkOval(&flatCount, &buldgeCount); + this->inval(NULL); + + canvas->drawBitmap(fBitmap, SkIntToScalar(20), SkIntToScalar(20), NULL); + + + static int gFlatCount; + static int gBuldgeCount; + gFlatCount += flatCount; + gBuldgeCount += buldgeCount; + + if (fSize.fWidth < kLimit) { + SkDebugf("--- width=%g, flat=%d buldge=%d total: flat=%d buldge=%d\n", fSize.fWidth, + flatCount, buldgeCount, gFlatCount, gBuldgeCount); + fSize.fWidth += SK_Scalar1; + fSize.fHeight += SK_Scalar1; + } else { + // fSize.set(SK_Scalar1, SK_Scalar1); + } + } + + virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) { + this->inval(NULL); + return NULL; + } + +private: + typedef SkView INHERITED; +}; + +/////////////////////////////////////////////////////////////////////////////// + +static SkView* MyFactory() { return new OvalTestView; } +static SkViewRegister reg(MyFactory); +