ohyeahq
YaBB Newbies
Offline
Posts: 4
frameRate low! How to improve?
Jan 11th , 2009, 9:37am
Hi, I have a newbie question: I tried to make a simple slideshow with a transition effect. I found that background() is fast, but image() and PImage.pixels[] are rather slow; I can't beat 30fps (max 24fps or so). Since I've seen numbers like 300fps on the Processing discussions, I suspect I'm doing something wrong. Could someone give me a hint? Here's my sample code, which is simple but can't go faster than 48fps with my PC (winxp, AthlonX2 5000+ 2.6GHz, 3GB RAM, GeForce6100): //import processing.opengl.*; PImage a, b; PGraphics pg; void setup() { size(640, 480, P3D); // P2D similar; OPENGL slower a = loadImage("small.jpg"); a.resize(width, height); b = loadImage("medium.jpg"); b.resize(width, height); frameRate(120); // fps a.loadPixels(); b.loadPixels(); pg = createGraphics(width, height, P3D); } void draw() { float ratio; ratio = frameCount / (60.0 * 10); ratio = constrain(ratio, 0.0, 1.0); transition(a, b, ratio); if (frameCount % 100 == 0) println(frameRate); } void transition(PImage a, PImage b, float ratio) { int method = 0; // select 0, 1, 2, 3 switch (method) { case 0: // 48fps background(a); tint(255, ratio * 255); image(b, 0, 0, width, height); noTint(); break; case 1: // 40fps int x, y, loc; loadPixels(); for (x = 0; x < width; x++) for (y = 0; y < height; y++) { loc = x + y * width; pixels[loc] = lerpColor(a.pixels[loc], b.pixels[loc], ratio); } updatePixels(); break; case 2: // 44fps pg.beginDraw(); pg.background(a); //pg.background(b); pg.tint(255, ratio * 255); pg.image(b, 0, 0, width, height); pg.noTint(); pg.endDraw(); background(pg); break; case 3: // 45fps background(a); blend(b, 0, 0, b.width, b.height, 0, 0, width, height, BLEND); break; } } Any suggestion will be appreciated. Thanks.