I am looking for suggestions for how to approach my image loading/unloading.
My program loads 100 sequences (mini-3-frame animations) of carts. These carts roll across the screen one after another. I am doing this by having the 2nd cart begin after the first cart has made it onto the screen.
As I suspected, I am only on cart #19 and the program is running slow.
How can I dynamically load and delete cart-image-sequences as they enter or leave the screen?
Also, are there any suggestions, given the code below, for other places where I can consolidate my code? (Ie: for loops or arrays in place of long lists of variables?)
Thanks for any suggestions!
My carts.cpp:
#include "carts.h"
carts::carts(){
cartWidth3 = 500*1.25; //x
cartHeight2 = 495*1.25;
cartWidth4 = 500*1.25; //x
cartHeight4 = 471*1.25;
cartWidth5 = 500*1.25; //x
cartHeight5 = 494*1.25;
cartWidth6 = 500*1.25; //x
cartHeight6 = 412*1.25;
cartWidth7 = 500*1.25; //x
cartHeight7 = 376*1.25;
cartWidth8 = 500*1.25;
cartHeight8= 450*1.25;
cartWidth9= 600*1.25;
cartHeight9= 370*1.25;
cartWidth10=500*1.25;
cartHeight10=427*1.25;
cartWidth11=500*1.25;
cartHeight11=433*1.25;
cartWidth12=292*1.25;
cartHeight12=300*1.25;
cartWidth13=262*1.25;
cartHeight13=500*1.25;
cartWidth14=500*1.25;
cartHeight14=433*1.25;
cartWidth15=350*1.25;
cartHeight15=385*1.25;
cartWidth16=550*1.25;
cartHeight16=701*1.25;
cartWidth17=439*1.25;
cartHeight17=496*1.25;
cartWidth18=308*1.25;
cartHeight18==300*1.25;
cartWidth19=400*1.25;
cartHeight19=345*1.25;
for (int i = 0; i < NUM_IMAGES_3; i++){
string cartAnim3s = "cart3/cart0" + ofToString(i) + ".png";
cartAnim3[i].loadImage(cartAnim3s);
}
for (int i = 0; i < NUM_IMAGES_3; i++){
string cartAnim4s = "cart4/cart0" + ofToString(i) + ".png";
cartAnim4[i].loadImage(cartAnim4s);
}
for (int i = 0; i < NUM_IMAGES_3; i++){
string cartAnim5s = "cart5/cart0" + ofToString(i) + ".png";
cartAnim5[i].loadImage(cartAnim5s);
}
for (int i = 0; i < NUM_IMAGES_3; i++){
string cartAnim6s = "cart6/cart0" + ofToString(i) + ".png";
cartAnim6[i].loadImage(cartAnim6s);
}
for (int i = 0; i < NUM_IMAGES_3; i++){
string cartAnim7s = "cart7/cart0" + ofToString(i) + ".png";
cartAnim7[i].loadImage(cartAnim7s);
}
for (int i = 0; i < NUM_IMAGES_3; i++){
string cartAnim8s = "cart8/cart0" + ofToString(i) + ".png";
cartAnim8[i].loadImage(cartAnim8s);
}
for (int i = 0; i < NUM_IMAGES_3; i++){
string cartAnim9s = "cart9/cart0" + ofToString(i) + ".png";
cartAnim9[i].loadImage(cartAnim9s);
}
for (int i = 0; i < NUM_IMAGES_3; i++){
string cartAnim10s = "cart10/cart0" + ofToString(i) + ".png";
cartAnim10[i].loadImage(cartAnim10s);
}
for (int i = 0; i < NUM_IMAGES_3; i++){
string cartAnim11s = "cart11/cart0" + ofToString(i) + ".png";
cartAnim11[i].loadImage(cartAnim11s);
}
for (int i = 0; i < NUM_IMAGES_3; i++){
string cartAnim12s = "cart12/cart0" + ofToString(i) + ".png";
cartAnim12[i].loadImage(cartAnim12s);
}
for (int i = 0; i < NUM_IMAGES_3; i++){
string cartAnim13s = "cart13/cart0" + ofToString(i) + ".png";
cartAnim13[i].loadImage(cartAnim13s);
}
for (int i = 0; i < NUM_IMAGES_3; i++){
string cartAnim14s = "cart14/cart0" + ofToString(i) + ".png";
cartAnim14[i].loadImage(cartAnim14s);
}
for (int i = 0; i < NUM_IMAGES_3; i++){
string cartAnim15s = "cart15/cart0" + ofToString(i) + ".png";
cartAnim15[i].loadImage(cartAnim15s);
}
for (int i = 0; i < NUM_IMAGES_3; i++){
string cartAnim16s = "cart16/cart0" + ofToString(i) + ".png";
cartAnim16[i].loadImage(cartAnim16s);
}
for (int i = 0; i < NUM_IMAGES_3; i++){
string cartAnim17s = "cart17/cart0" + ofToString(i) + ".png";
cartAnim17[i].loadImage(cartAnim17s);
}
for (int i = 0; i < NUM_IMAGES_3; i++){
string cartAnim18s = "cart18/cart0" + ofToString(i) + ".png";
cartAnim18[i].loadImage(cartAnim18s);
}
for (int i = 0; i < NUM_IMAGES_3; i++){
string cartAnim19s = "cart19/cart0" + ofToString(i) + ".png";
cartAnim19[i].loadImage(cartAnim19s);
}
curImage3 = 0;
curImage4 = 0;
curImage5 = 0;
curImage6 = 0;
curImage7 = 0;
curImage8 = 0;
curImage9 = 0;
curImage10 = 0;
curImage11 = 0;
curImage12 = 0;
curImage13 = 0;
curImage14 = 0;
curImage15 = 0;
curImage16 = 0;
curImage17 = 0;
curImage18 = 0;
curImage19 = 0;
baseXpos = ofGetWidth() + 1000;
xpos3 = ofGetWidth() + cartWidth3;//cartWidth3;
xpos4 = ofGetWidth() + cartWidth4;//cartWidth4;
xpos5 = ofGetWidth() + cartWidth5;//cartWidth5;
xpos6 = ofGetWidth() + cartWidth6;//cartWidth6;
xpos7 = ofGetWidth() + cartWidth7;//cartWidth7;
xpos8 = ofGetWidth() + cartWidth8;//cartWidth3;
xpos9 = ofGetWidth() + cartWidth9;//cartWidth4;
xpos10 = ofGetWidth() + cartWidth10;//cartWidth5;
xpos11 = ofGetWidth() + cartWidth11;//cartWidth6;
xpos12 = ofGetWidth() + cartWidth12;//cartWidth7;
xpos13 = ofGetWidth() + cartWidth13;//cartWidth3;
xpos14 = ofGetWidth() + cartWidth14;//cartWidth4;
xpos15 = ofGetWidth() + cartWidth15;//cartWidth5;
xpos16 = ofGetWidth() + cartWidth16;//cartWidth6;
xpos17 = ofGetWidth() + cartWidth17;//cartWidth7;
xpos18 = ofGetWidth() + cartWidth18;//cartWidth3;
xpos19 = ofGetWidth() + cartWidth19;//cartWidth4;
}
//--------------------------------------------------------------
void carts::update(){
if (xpos3 >= 0-cartWidth3 ){
xpos3 -= 10;
}
if (xpos3 <= ofGetWidth()-cartWidth3*1.25){
xpos4 -= 10;
}
if (xpos4 <= ofGetWidth()-cartWidth4*1.25){
xpos5 -= 10;
}
if (xpos5 <= ofGetWidth()-cartWidth5*1.25){
xpos6 -= 10;
}
if (xpos6 <= ofGetWidth()-cartWidth6*1.25){
xpos7 -= 10;
}
if (xpos7 <= ofGetWidth()-cartWidth7*1.25){
xpos8 -= 10;
}
if (xpos8 <= ofGetWidth()-cartWidth8*1.25){
xpos9 -= 10;
}
if (xpos9 <= ofGetWidth()-cartWidth9*1.25){
xpos10 -= 10;
}
if (xpos10 <= ofGetWidth()-cartWidth10*1.25){
xpos11 -= 10;
}
if (xpos11 <= ofGetWidth()-cartWidth11*1.25){
xpos12 -= 10;
}
if (xpos12 <= ofGetWidth()-cartWidth12*1.25){
xpos13 -= 10;
}
if (xpos13 <= ofGetWidth()-cartWidth13*1.25){
xpos14 -= 10;
}
if (xpos14 <= ofGetWidth()-cartWidth14*1.25){
xpos15 -= 10;
}
if (xpos15 <= ofGetWidth()-cartWidth15*1.25){
xpos16 -= 10;
}
if (xpos16 <= ofGetWidth()-cartWidth16*1.25){
xpos17 -= 10;
}
if (xpos17 <= ofGetWidth()-cartWidth17*1.25){
xpos18 -= 10;
}
if (xpos18 <= ofGetWidth()-cartWidth18*1.25){
xpos19 -= 10;
}
curImage3++;
curImage4++;
curImage5++;
curImage6++;
curImage7++;
curImage8++;
curImage9++;
curImage10++;
curImage11++;
curImage12++;
curImage13++;
curImage14++;
curImage15++;
curImage16++;
curImage17++;
curImage18++;
curImage19++;
if (curImage3 >= NUM_IMAGES_3){
curImage3 = 0;
}
if (curImage4 >= NUM_IMAGES_3){
curImage4 = 0;
}
if (curImage5 >= NUM_IMAGES_3){
curImage5 = 0;
}
if (curImage6 >= NUM_IMAGES_3){
curImage6 = 0;
}
if (curImage7 >= NUM_IMAGES_3){
curImage7 = 0;
}
if (curImage8 >= NUM_IMAGES_3){
curImage8 = 0;
}
if (curImage9 >= NUM_IMAGES_3){
curImage9 = 0;
}
if (curImage10 >= NUM_IMAGES_3){
curImage10 = 0;
}
if (curImage11 >= NUM_IMAGES_3){
curImage11 = 0;
}
if (curImage12 >= NUM_IMAGES_3){
curImage12 = 0;
}
if (curImage13 >= NUM_IMAGES_3){
curImage13 = 0;
}
if (curImage14 >= NUM_IMAGES_3){
curImage14 = 0;
}
if (curImage15 >= NUM_IMAGES_3){
curImage15 = 0;
}
if (curImage16 >= NUM_IMAGES_3){
curImage16 = 0;
}
if (curImage17 >= NUM_IMAGES_3){
curImage17 = 0;
}
if (curImage18 >= NUM_IMAGES_3){
curImage18 = 0;
}
if (curImage19 >= NUM_IMAGES_3){
curImage19 = 0;
}
}
//--------------------------------------------------------------
void carts::draw(int y){
cartAnim3[curImage3].draw(xpos3,y,cartWidth3,cartHeight3);
cartAnim4[curImage4].draw(xpos4,y,cartWidth4,cartHeight4);
cartAnim5[curImage5].draw(xpos5,y,cartWidth5,cartHeight5);
cartAnim6[curImage6].draw(xpos6,y,cartWidth6,cartHeight6);
cartAnim7[curImage7].draw(xpos7,y,cartWidth7,cartHeight7);
cartAnim8[curImage8].draw(xpos8,y,cartWidth8,cartHeight8);
cartAnim9[curImage9].draw(xpos9,y,cartWidth9,cartHeight9);
cartAnim10[curImage10].draw(xpos10,y,cartWidth10,cartHeight10);
cartAnim11[curImage11].draw(xpos11,y,cartWidth11,cartHeight11);
cartAnim12[curImage12].draw(xpos12,y,cartWidth12,cartHeight12);
cartAnim13[curImage13].draw(xpos13,y,cartWidth13,cartHeight13);
cartAnim14[curImage14].draw(xpos14,y,cartWidth14,cartHeight14);
cartAnim15[curImage15].draw(xpos15,y,cartWidth15,cartHeight15);
cartAnim16[curImage16].draw(xpos16,y,cartWidth16,cartHeight16);
cartAnim17[curImage17].draw(xpos17,y,cartWidth17,cartHeight17);
cartAnim18[curImage18].draw(xpos18,y,cartWidth18,cartHeight18);
cartAnim19[curImage19].draw(xpos19,y,cartWidth19,cartHeight19);
if(xpos3 <= ofGetWidth()-cartWidth3){
delete [] cartAnim3 ;
}
}
myTestApp.cpp:
#include "testApp.h"
//--------------------------------------------------------------
void testApp::setup(){
// turn off vertical sync so we can evaluate performance
ofSetVerticalSync(false);
// the bind() function might complain about texture not
// being allocated, yet it seems to work fine. Uncomment
// the following line to stop the warning from displaying.
ofSetLogLevel(OF_LOG_ERROR);
vidWidth = 320;
vidHeight = 240;
backgroundHaggenbeck1.loadImage("backgrounds/poster.jpg");
ofSetFrameRate(8);
//FACES
webcam.setVerbose(true);
webcam.initGrabber(vidWidth,vidHeight);
colorImg.allocate(vidWidth,vidHeight);
grayImage.allocate(vidWidth,vidHeight);
//blobTracker.setListener( this );
// load the correct xml file from the bin/data/haarXML/ folder
//mask.loadImage("backgrounds/curtain.png");
haarFinder.setup("haarXML/haarcascade_frontalface_default.xml");
//FIRE TIMECOUNT!
timecount = 0;
}
//--------------------------------------------------------------
void testApp::update(){
ofBackground(200,200,200);
bool bNewFrame = false;
///CARTS DRIVING
AllCarts.update();
man1.update();
//FACES
ofBackground(0,0,0);
//bool bNewFrame = false;
webcam.grabFrame();
bNewFrame = webcam.isFrameNew();
colorImg.mirror( false, true );
if (bNewFrame){
colorImg.setFromPixels(webcam.getPixels(), 320,240);
colorImg = webcam.getPixels();
grayImage = colorImg;
haarFinder.findHaarObjects(grayImage);
}
myFire.update();
}
//--------------------------------------------------------------
void testApp::draw(){
//myFire.draw(400.0,500.0,500.0);
ofEnableAlphaBlending();
//ofSetColor(0, 0, 0, 20); //Neat sillouette effect!
ofSetColor(255,255,255,20);//makes background image transparent
backgroundHaggenbeck1.draw(-150,-150,ofGetWidth()*1.25, ofGetHeight()*1.25);
backgroundHaggenbeck1.draw(-145,-145,ofGetWidth()*1.25, ofGetHeight()*1.25);
ofSetColor(255,255,255,100);
man1.draw(0,810,239,410);
//FACES
// how many matches did you find?
int numMatches = haarFinder.blobs.size();
if (numMatches >= 1) {
timecount++;
ofSetColor(255,255,255,75);//makes things transparent
//TOP OF SCREEN
colorImg.draw(-200,-1200,vidWidth*7,vidHeight*7);
//CARTS
AllCarts.draw(600);
//FIRE!!!!
if (timecount > 15) {
myFire.draw(ofRandom(0,100),ofRandom(ofGetHeight()-75,ofGetHeight()-50),ofRandom(75,125));
myFire.draw(ofRandom(75,175),ofRandom(ofGetHeight()-75,ofGetHeight()-50),ofRandom(75,125));
myFire.draw(ofRandom(150,225),ofRandom(ofGetHeight()-75,ofGetHeight()-50),ofRandom(75,125));
myFire.draw(ofRandom(220,300),ofRandom(ofGetHeight()-75,ofGetHeight()-50),ofRandom(75,125));
myFire.draw(ofRandom(325,475),ofRandom(ofGetHeight()-75,ofGetHeight()-50),ofRandom(75,125));
myFire.draw(ofRandom(425,575),ofRandom(ofGetHeight()-75,ofGetHeight()-50),ofRandom(75,125));
myFire.draw(ofRandom(525,675),ofRandom(ofGetHeight()-75,ofGetHeight()-50),ofRandom(75,125));
myFire.draw(ofRandom(650,775),ofRandom(ofGetHeight()-75,ofGetHeight()-50),ofRandom(75,125));
myFire.draw(ofRandom(750,900),ofRandom(ofGetHeight()-75,ofGetHeight()-50),ofRandom(75,125));
myFire.draw(ofRandom(875,1000),ofRandom(ofGetHeight()-75,ofGetHeight()-50),ofRandom(75,125));
myFire.draw(ofRandom(950,1225),ofRandom(ofGetHeight()-75,ofGetHeight()-50),ofRandom(75,125));
myFire.draw(ofRandom(1175,1300),ofRandom(ofGetHeight()-75,ofGetHeight()-50),ofRandom(75,125));
myFire.draw(ofRandom(1275,1400),ofRandom(ofGetHeight()-75,ofGetHeight()-50),ofRandom(75,125));
myFire.draw(ofRandom(1375,1500),ofRandom(ofGetHeight()-75,ofGetHeight()-50),ofRandom(75,125));
myFire.draw(ofRandom(1450,1575),ofRandom(ofGetHeight()-75,ofGetHeight()-50),ofRandom(75,125));
}
if(timecount > 54){
myFire.draw(ofRandom(0,100),ofRandom(ofGetHeight()-150,ofGetHeight()-225),ofRandom(120,225));
myFire.draw(ofRandom(75,175),ofRandom(ofGetHeight()-150,ofGetHeight()-225),ofRandom(120,225));
myFire.draw(ofRandom(150,225),ofRandom(ofGetHeight()-150,ofGetHeight()-225),ofRandom(120,225));
myFire.draw(ofRandom(220,300),ofRandom(ofGetHeight()-150,ofGetHeight()-225),ofRandom(120,225));
myFire.draw(ofRandom(325,475),ofRandom(ofGetHeight()-150,ofGetHeight()-225),ofRandom(120,225));
myFire.draw(ofRandom(425,575),ofRandom(ofGetHeight()-150,ofGetHeight()-225),ofRandom(120,225));
myFire.draw(ofRandom(525,675),ofRandom(ofGetHeight()-150,ofGetHeight()-225),ofRandom(120,225));
myFire.draw(ofRandom(650,775),ofRandom(ofGetHeight()-150,ofGetHeight()-225),ofRandom(120,225));
myFire.draw(ofRandom(750,900),ofRandom(ofGetHeight()-150,ofGetHeight()-225),ofRandom(120,225));
myFire.draw(ofRandom(875,1000),ofRandom(ofGetHeight()-150,ofGetHeight()-225),ofRandom(120,225));
myFire.draw(ofRandom(950,1225),ofRandom(ofGetHeight()-150,ofGetHeight()-225),ofRandom(120,225));
myFire.draw(ofRandom(1175,1300),ofRandom(ofGetHeight()-150,ofGetHeight()-225),ofRandom(120,225));
myFire.draw(ofRandom(1275,1400),ofRandom(ofGetHeight()-150,ofGetHeight()-225),ofRandom(120,225));
myFire.draw(ofRandom(1375,1500),ofRandom(ofGetHeight()-150,ofGetHeight()-225),ofRandom(120,225));
myFire.draw(ofRandom(1450,1575),ofRandom(ofGetHeight()-150,ofGetHeight()-225),ofRandom(120,225));
}
if (timecount >= 75 ){//&& timecount < 125){
myFire.draw(ofRandom(0,ofGetWidth()),ofRandom(500,ofGetHeight()),ofRandom(400,700));
}
else if (timecount >= 100){// && timecount < 250){
myFire.draw(ofRandom(ofGetWidth()/2-100,ofGetWidth()/2+100),ofRandom(400,550),ofRandom(250,450));
myFire.draw(ofRandom(200,500),ofRandom(700,710),ofRandom(400,560));
myFire.draw(ofRandom(1000,1300),ofRandom(600,620),ofRandom(250,500));
myFire.draw(ofRandom(ofGetWidth()-100,ofGetWidth()),ofRandom(440,500),ofRandom(600,750));
myFire.draw(ofRandom(0,100),ofRandom(440,500),ofRandom(450,600));
}
else if (timecount >=250) {
myFire.draw(ofRandom(ofGetWidth()/2-100,ofGetWidth()/2+100),ofRandom(400,550),ofRandom(100,200));
myFire.draw(ofRandom(200,500),ofRandom(700,710),ofRandom(200,260));
myFire.draw(ofRandom(1000,1300),ofRandom(600,620),ofRandom(150,300));
myFire.draw(ofRandom(ofGetWidth()-100,ofGetWidth()),ofRandom(440,500),ofRandom(200,250));
myFire.draw(ofRandom(0,100),ofRandom(440,500),ofRandom(250,300));
myFire.draw(ofRandom(90,250),ofRandom(450,550),ofRandom(300,500));
myFire.draw(ofRandom(ofGetWidth()-90,ofGetWidth()-250),ofRandom(450,550),ofRandom(400,600));
myFire.draw(ofRandom(225,325),ofRandom(500,700),ofRandom(500,700));
myFire.draw(ofRandom(ofGetWidth()-225,ofGetWidth()-325),ofRandom(500,700),ofRandom(500,700));
}
}//end (numMatches >=1)
if (numMatches == 0) {
//RESET CARTS SOMEHOW?!?!?!?!?!??!?!
timecount = 0;
}
ofDisableAlphaBlending();
}
void testApp::keyPressed(int key){
man1.keyPressed(key);
}