Set a random framerate?


I’m trying to have different image sequences change there speed (frame rate) randomly at certain times in my animation. I have the main framerate set to 100. but I was hoping to play most of the clips a lot slower than that around 25-30 FPS.

I was limiting the frames in each class by using the sleep function as I thought that If I used it within each class it would only effect that class, but alas.

Does anyone have any idea’s?

Here’s the code anyway:

#include "ofTyper.h"  
	xpos = 0;      
	ypos = 0;   
	stage = 1;  
	bool playing = false;      
for (int i = 0; i < typerNumImages; i++){                 
		string myImageName = "01_Typer" + ofToString(i) + ".png";    //INITIATE THE CHARACTER  
		sequenceTyper[i].allocate(250, 350, GL_RGBA);  
    typerFrame = 0 ;   
void ofTyper::update(){  
          if (typerFrame == 0) {  
                   xpos = 680;     //initiate random positions  
                   ypos = (ofRandomuf()*200);  
     if (stage == 1){  
               typerFrame = 1;  
               if (ofRandom(0,10) > 9.9){         //pauses before looking  
                                  stage = 2;  
                                  sleepTime = (int(ofRandom(1,60)));  
     if (stage == 2){  
               typerFrame ++;                     //looking up  
     if (typerFrame == 25){  
                    stage = 3;  
     if (stage == 3){  
               if (ofRandom(0,10) > 9.9){         //going back to sleep  
                                  stage = 2;  
                                  sleepTime = (int(ofRandom(1,40)));  
     if (typerFrame == 52){  
                    stage = 1;                    //looping back to beginning  
//cout << (ypos);  
//cout << endl;  
void ofTyper::draw(){  

sleep() works across the whole system - it will sleep everything. if you could put everything in a separate thread, then you could do it - but that’s certainly not an easy way to go about things.

try this instead. every update() call on testApp, get the amount of elapsed time between this frame and the previous frame, like this:

in testApp.h:

  float prev_time;  

in testApp.cpp:update():

  float now = ofGetElapsedTimef();  
  float elapsed = now - prev_time;  
  prev_time = now;  

now, for each image sequence, have an update method that takes a float argument, like this:


class ofTyper {  
  void update( float elapsed )  
  float timer;  
  float frame_time;  


void ofTyper::update( float elapsed )  
  timer -= elapsed;  
  if ( timer < 0 )  
    // do animation update frame step  
    // reset timer  
    timer += frame_time;  

and call each ofTyper’s update() method with the elapsed time you calculated earlier…

now all that you need to do is set frame_time to the amount of time in seconds between frames - so for 100 fps set frame_time to 0.01 (1.0/100.0), for 25fps set frame_time to 0.04 (1.0/25.0), etc. also set timer to 0 in setup().

hope this helps.

Hey Damian,

I’m working on getting that up and running now, thanks for your help, a lot better than my sleep idea.

Very new to OpenFrameworks ha and programming in general just trying to get my head around how to setup functions and call/set them from different classes. I’ve got your code somewhat working but just not effecting the frames yet. I’m sure I’ll get it soon though.

Hey your from NZ, I just saw your work for wellington film festival (through your site), My tutor actually told me about it. Very cool. OpenFrameworks is powerful software. I must get better at it!


hey! kiwis making code art! that’s o for awesome! wuhkkuhd! etc.

err… Wellington Film Festival? which work? do you mean the Fringe festival? or one of the pieces i did with Alouis Woodhouse?

i’m actually living in Amsterdam at the moment, though perhaps not for much longer… back in Wellington in May, we should definitely meet up for a beer when I get back.

I second that, Seems there’s a few more people getting into it now here in welly, which is great. I heard a rumor one of the uni’s is thinking about using it for a paper next year, which is sweet! Ha I might have to go back and pick it up even though I’ll be finished my degree.

Hey sorry I got the festivals mixed up I was talking about the fringe festival it was "sounds like light, lights like sound " yea my teacher was talking to me about your turntable concept. very cool.

Meeting up for a beer would be great let me know when your back. I’m planning on continuing to do this sort of work next year, so I should be a bit better at it by then!

Hey sorry guys for posting another noooby beginner question.

I’m trying to reference and access “float xpos;” from another class. at the moment I’m going"

whereIsHe = ofTyperClass->xpos;   

The xpos int is public. I get an error “expected a primary expression before ‘->’ token”

Through what I could find on google it looked like you got that error when you dont have a data type. eg for “xpos” but xpos is defined as an “float” in the other class header file.

Weird! does anyone know about this one?

wow. which university? Massey or Vic? i spy a possible teaching job for me :wink:

you need to access the xpos member variable on an instance of the class, not the class itself.


ofTyperClass typer;  
whereIsHe = typer.xpos;  

Vic, I can talk to the lecturer if you want to see how serious he is about it. or I could put you in contact with him. They’d Appreciate your skills there I’m sure!

Thanks for you help with accessing members in classes, it worked in the mother class, the testApp.cpp. I can now access and adjust the children classes which are imported into it. (ha, not sure if thats the correct way to describe the situation).

I have my visual elements as classes all imported and instanced in the testApp. Is there a way I can communicate directly between the children classes I understand I need to reference the instanced version. ha I was thinking something like:


from within the testapp.cpp file myTyper.xpos works fine. I just don’t know how to take it back another level. hmmmm

But I could always just write a lot of the code in the testapp file.

By the way you should definitely teach at Vic next year. I’m at Massey but I’d pick up a paper over there if they were going to have OpenFrameworks type assignments. would be sweet!

man, i’m so into that. yes, do definitely put me in contact with him. my email is damian [at] i assume it’s at the design school, yeah?

re communicating between members: there’s not really an easy way to do this. normally you want to try to design things so that individual instances of a class don’t need to communicate directly with each other. if they need to, you usually need a higher-level class to take care of the communication. the short and easy solution is to put this communication stuff in the testApp class. there’s no way a ‘child’ (member) can access its ‘parent’ internally, unless you program this ability in, and it’s generally bad design practice (since you don’t necessarily know what class you are going to have as parent).

what kind of communication do you need to do? maybe there’s another way to do it…


right I’ll get onto that, I hope you guys can work something out. Yip at the design/architecture campus.

The kind communication I’m looking for is quite simple I think.

its like:

if (myTyper.xpos > 400) {
myWorker.xpos ++;

That sort of thing, just triggering events .etc

but as you say I could just handle all that in the higher level class. I was really just trying to keep it in the other subclasses for tidyness as I can see there being a lot of code in my project by the end. arr it’l be fine!

OK I’ll flick the guy an email now.