Image masking help needed. Framerate drops while drawing PImage
in
Core Library Questions
•
7 months ago
Hello everyone,
I am new to the forum. I am using processing through another framework (MT4J), which uses Processing 1.5
What I am trying to achieve is simple. I have a background image-bgImage , a foreground image-fgImage .(both of same size drawn on screen with fgImage on top). Now when a person touches at x,y . The background image is revealed near the finger with some animation (say one star rotating/circle with border on fire etc).
So I draw the bgImage , then set alpha of respective pixels of fgImage to 0. I have loaded the animated png sequence to calculate the animated pixels. The draw code is as follows.
P.S - I am using processing 1.5 through mt4j , therefore I have to call the functions using graphics.image() instead of image() and app.alpha() instead of alpha().
- void draw()
- {
- graphics.image(bgImage,0,0); //draw backgroundimage
- PImage current = animatedSprite.getCurrent(); //Returns a PImage
- int spritew = current.width;
- int spriteh = current.height;
- fgImage.loadPixels(); //LoadPixels
- //fgImage.pixels = origPixels;
- for(Position p:idToPosition.values()) //Iterate through all the finger positions
- {
- for (int x=0; x<current.width; x++) {
- for(int y=0; y<current.height; y++) {
- int a = y*spritew+x;
- float alpha =255- app.alpha(current.pixels[a]);
- float xpos = p.x()-spritew/2 + x;
- float ypos = p.y() -spriteh/2 + y;
- if (xpos < 0 || ypos < 0 || xpos > fgWidth || ypos> fgHeight){continue;}
- int pos =(int)xpos+(int)ypos*fgImage.width;
- fgImage.pixels[pos] = graphics.color(graphics.color(fgImage.pixels[pos]),alpha); ;
- }
- }
- }
- fgImage.updatePixels(); //Update the pixels, seems like this reduces the fps
- graphics.image(fgImage,0,0);
- }
Till line 8 - Everything runs normally with 60fps. (i.e with other parts commented out)
Till line 26 - Runs fine with 60fps , but with one finger on , runs with 40 fps , average
When line 27 is uncommented , the fps drops directly to 4-5 fps.
I don't see any obvious error. Can anyone please point me where I am doing wrong ? Or is there any better approach to handle this problem ?
Thanks in advance.
Thanks in advance.
Edit: Seems like if I comment out line #21 & #25 , it runs @ 60fps. i.e. if I dont update the pixels in fgImage, with #25 commented out , runs @ 40-45 fps when there's any finger.
1