class Refugio {
  
    float x;
    float y;
    float s;
    int xGrid;
    int yGrid;
    int xGridSize;
    int yGridSize;
    int fillet;
    int imageSize;
    
    PGraphics img;
    PGraphics mk;
    PImage imgClone;
    
    int[][][] segments;
    
    Refugio(float xx, float yy) {

        this.x = xx;
        this.y = yy;
        this.yGrid = 3;
        this.xGrid = 3;
        this.fillet = 0;
        this.xGridSize = int(choclitos.width/xGrid);
        this.yGridSize = int(choclitos.height/yGrid);
        this.img = createGraphics(choclitos.width, choclitos.height);
        this.mk = createGraphics(choclitos.width, choclitos.height);
        this.imageSize = choclitos.width;
        
        this.img.beginDraw();
        
        this.mk.imageMode(CENTER);
        this.mk.rectMode(CENTER);
        this.img.imageMode(CENTER);
        this.img.rectMode(CENTER);
        this.img.rectMode(CENTER);
        this.segments = new int[yGrid][xGrid][3];
        
        this.img.image(choclitos, this.img.width/2, this.img.height/2);     /// IMAGEN A MASCAREAR
        this.img.endDraw();
    }
    
    void init() {
        
        for(int r = 0; r < this.yGrid; r++) {
            for(int c = 0; c < this.xGrid; c++) {
              
                //float[] array = {( c*this.s*2) + this.s, r*this.s*2+this.s, 1};
                int[] array = { (this.xGridSize * c)  , (this.yGridSize * r) , 1};
                
                for(int i = 0; i < 3; i++) {
                    this.segments[r][c][i] = array[i];     ///CENTER MODE ON CREATE GRAPHICS
                 // this.canvasPos[r][c] = new Array(this.x + (c*this.s*2) - this.s*2, this.y + (r*this.s*2) - this.s*2);     ///CENTER OF EACH SEGMENTS RELATIVE TO CANVAS
                }
            }
        }
    }

    void del(int r, int c) {
        this.segments[r][c][2] = 0;
    }

    void render() {
        //this.mk.fill(0,0,255);
        rectMode(CENTER);
        for(int r = 0; r < this.yGrid; r++) {
            for(int c = 0; c < this.xGrid; c++) {
                if (this.segments[r][c][2] == 1) {        
                    this.mk.beginDraw();
                    //strokeWeight(2);
                    this.mk.rect(this.segments[r][c][0],   this.segments[r][c][1],   this.xGridSize-this.fillet,   this.yGridSize-this.fillet);
                    
                    //fill(0,255,255);
                    //rect(this.segments[r][c][0] + this.x,   this.segments[r][c][1]+this.y,   this.xGridSize-this.fillet,   this.yGridSize-this.fillet);
                    // this.img.ellipse(this.segments[r][c][0],this.segments[r][c][1],5,5);
                    this.mk.endDraw();
                }
            }
        }
        //imageMode(CENTER);
        manualMask(img, mk );
        //this.img.beginDraw();
        //this.img.mask( this.mk.get() );
        
        //image(choclitos, this.x, this.y);
        image(this.img, this.x+this.xGridSize, this.y+yGridSize);
        //this.img.endDraw();
        this.mk.clear();

    }
}

private void manualMask(PGraphics display, PGraphics mask) {
    mask.loadPixels();
    display.beginDraw();
    display.loadPixels();
    for (int i = 0; i < display.pixels.length; i++) {
        int d = display.pixels[i];
        // mask alpha
        int m_a = mask.pixels[i] & 0xFF;
        // display alpha
        int d_a = (d >> 24) & 0xFF;
        // output alpha (do not change alpha if already transparent)
        int o_a;
        if( m_a < d_a ) {
        o_a = m_a;
        display.pixels[i] = (o_a << 24) | (0x00FFFFFF & d);
        }
    }
    display.updatePixels();
    display.endDraw();
}
