C++程序  |  116行  |  3.41 KB


/*
 * 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"

static const int kILimit = 101;
static const SkScalar kLimit = SK_Scalar1 * kILimit;

class OvalTestView : public SampleView {
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.allocN32Pixels(kILimit, kILimit);

        fInsideColor = SkPreMultiplyColor(SK_ColorRED);
        fOutsideColor = SkPreMultiplyColor(SK_ColorGREEN);

        this->setBGColor(0xFFDDDDDD);
    }

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 drawOval() {
        SkCanvas canvas(fBitmap);
        SkPaint p;

        fBitmap.eraseColor(SK_ColorTRANSPARENT);
        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 onDrawContent(SkCanvas* 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, unsigned) SK_OVERRIDE {
        this->inval(NULL);
        return NULL;
    }

private:
    typedef SampleView INHERITED;
};

///////////////////////////////////////////////////////////////////////////////

static SkView* MyFactory() { return new OvalTestView; }
static SkViewRegister reg(MyFactory);