We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Jerky scroller
Page Index Toggle Pages: 1
Jerky scroller (Read 1707 times)
Jerky scroller
Jul 15th, 2005, 10:00am
 
Here's a very basic scroll routine that uses the copy function and an offset into a PImage acting as a scroll buffer.

This works ok when the scroll rate is at 1 pixel per frame, but gets awful jerky at higher speeds, though there shouldn't be any reason for it.


Any ideas? (you'll need an image in the data folder of course!)

I need a smooth scroller basically, and this seemed to be the most economic in machine cycles, though I could be wrong!

Code:


float xpos;  // index into current source image
float xmag;

PImage source; // holds current source image

void setup() {

size(534, 300, P2D);
background(255);
 
source = loadImage("a.jpg");

framerate(24);
}

void draw() {
 
 xmag = (mouseX-width/2)*0.030;
 xpos = (xpos+xmag)%source.width;
 
 copy(source, (int)xpos, 0, width, source.height, 0, 0, width, height);  // Display new video image at the end of scroll buffer.  
}

Re: Jerky scroller
Reply #1 - Jul 18th, 2005, 1:02pm
 
You can judt draw the image with it's "origin" off screen, and move that point.. you don't need to copy the image at all, just use image(bigImage,xpos,0);

At elast that's what I'm doing in a little sketch I'm writing at the moment, and it runs fine at 50+fps at 600x400...
Re: Jerky scroller
Reply #2 - Jul 18th, 2005, 5:04pm
 

Well, the method works for actually scrolling, but it's even slower now.

My PImage is 9000 * 300 and screen is 400 * 300

Code:

image(source, xoffset, 0);

xoffset++;


Is there something I'm missing?
Re: Jerky scroller
Reply #3 - Jul 18th, 2005, 11:15pm
 
Possibly for really large images, your original way is faster.. the image I was using was only about 3 times wider than the screen.

Here's the code I was using, which looks pretty similar:
Code:
PImage source;
int x;

void setup()
{
size(600,400);
source=loadImage("land1.tga");
x=0;
}

void draw()
{
background(60,156,255);
image(source,-x,height-source.height);
x++;
x%=source.width-width;
}
Re: Jerky scroller
Reply #4 - Jul 19th, 2005, 9:32am
 

Yeah, no joy as yet!

Do you need the background(); call. Seems that its unecessary given the image() function that immediately follows, unless your only doing a partial blit.

Re: Jerky scroller
Reply #5 - Jul 19th, 2005, 11:46am
 
I do the background() because the image has an alpha channel, and is partially transaprent.
Re: Jerky scroller
Reply #6 - Jul 19th, 2005, 12:09pm
 
That would be a good reason! Smiley
Re: Jerky scroller
Reply #7 - Jul 19th, 2005, 2:08pm
 
things to try to make this faster:

1) not using the background and an image that has alpha.. pre-composite the background color plus the alpha in the image since it's not going to change. when drawing an image that has alpha, each pixel has to be mixed between the background and the image itself. because filling the screen with the image over the background means that things really won't ever change, better to do this once, and then you can nix the call to background (that'll improve speed) and just do the image call. that way, you can just use an opaque image, much faster in general.

other things that can improve speed (just off the top of my head, not necessarily all applicable in your case)

2) you might try using P3D, it may be faster in some cases. you're not doing it here, but unless you use set() with P3D, avoid using translate/rotate etc before drawing the image because it'll have to use slower code because it will think you're trying to stretch the image.

3) you might also try to use set() to draw the image. in some cases, it'll be faster.

4) also make sure you're not using tint() (though it looks like you're ok). pre-tinting images once will be much faster than doingn it on each frame.

good luck!
Re: Jerky scroller
Reply #8 - Jul 19th, 2005, 3:58pm
 
I tried P3D before and it went slower, but I'll look into your suggestions.

Thanks.
Re: Jerky scroller
Reply #9 - Jul 19th, 2005, 4:02pm
 

But tried P3D again and it's now much faster!! Must be partly to do with John's image() suggestion.

Looking good!
Re: Jerky scroller
Reply #10 - Jul 19th, 2005, 4:08pm
 

Another quick question:

It looks as though I'm getting a refresh occuring mid-screen. Is there any way of waiting for the fly. A bit like a $D012 C64 style?

Or am I talking nonsense?
Re: Jerky scroller
Reply #11 - Jul 19th, 2005, 5:27pm
 
hm, it should only be updating the screen when it's good and ready (vsync ala c64 shouldn't be necessary) although if it's taking too long to update the image it may be trying to blit right in the middle of drawing..

also, did you try the suggestion to mix in the background color? you may get a 2x speedup or better by getting rid of the background() call, plus all the time it takes to composite things with alpha. that may also fix the flicker if you use an RGB image instead and pre-composite the alpha channel.
Re: Jerky scroller
Reply #12 - Jul 19th, 2005, 8:21pm
 
I've not been using a background() call (although John is). The composite is already taken care of, or isn't it, if I'm using a jpeg? Or does it still try to do it with the alpha channel regardless?

Do I need to go run through my image in setup() and fix the alpha?

I should say that it's moving a sight quicker, the only problem is when the step is significant say 3-pixels per frame then you really notice a jerk, but it's sporadic. I'm hoping for a smooth scroll.

I may revert to a <-- pixel[] shift and dump in the new stuff on the edge, just to see, but I don't think it'll improve things, as it's hanging on the cpu doing other stuff it seems.
Page Index Toggle Pages: 1