thatbrock
YaBB Newbies
Offline
Posts: 11
optimising rendering
Mar 5th , 2008, 6:30pm
Hi there, I'm working on a bit of code to draw lots of lines of text in a sketch, and to overlap and animate them, with alpha blending. Loaded from atext file, each line of text is a separate object with its own attribs. Trouble is, when a relatively modest number of objects is painted (e.g., <100), the animation bogs down and becomes a bit jittery. Anyy suggestions to optimize the code to run more smoothly would be warmly welcomed! -B // Code to scroll lines of poetry down the screen // by Brock Craft, March 2008. String [] rawtext = new String[10629]; // an array to hold each line of text int x=0; Fragment[] f; void setup(){ size(600,400,P3D); frameRate(30); fill(255); PFont font = loadFont("Apple-Chancery-48.vlw"); textFont(font,22); textAlign(CENTER); String lines[] = loadStrings("a_poem.txt"); f=new Fragment[100];// or use 10629 if we want to use the whole text file for (int i=0; i < 100; i++) { rawtext[i] = lines[i]; // rawtext is not necessary, but leftover from other kludging f[i]=new Fragment(rawtext[i],width/2,i*40,random(5)); } } void draw(){ background(0); for (int i=0;i<50;i++){ f[i].display(); f[i].sink(); } if (mousePressed){ for (int i=0;i<100;i++){ f[i].reset(); f[i].reseed(); } } } class Fragment { String t; // text float xpos; // xposition float ypos; // yposition float weight; // "weight" or heaviness of text Fragment(String it, float ixp, float iyp, float iw) { xpos = ixp; ypos = iyp; t = it; weight = iw; } void sink (){ if (ypos<height+50){ ypos=ypos+weight; } } void reseed(){ weight=random(5); } void reset(){ ypos=random(10); } void display() { float a=weight*50; fill(255,255,255,a); text(t,xpos, ypos); translate(0,0,10); } }