Quantcast
Channel: beginners - openFrameworks
Viewing all articles
Browse latest Browse all 4929

It seems I don't understand C++ loops and/or pointers

$
0
0

@chrisoffner3d wrote:

So in my ofApp.h I declare ofImage imageTiles[20]; and then I have this code:

void ofApp::setup() {
    for (int i = 0; i < 20; i++) {
        imageTiles[i].grabScreen(0, 0, ofGetWidth()/5, ofGetHeight()/4);
        imageTiles[i].setColor(ofColor(ofRandom(255), ofRandom(255), ofRandom(255)));
    }
}

void ofApp::draw() {
    int x = 0;
    int y = 0;
    for (ofImage image : imageTiles) {
        phongSphere(&image, 1, ofVec3f(), ofColor(255,0,0));
        image.draw(x, y);
        x += image.getWidth();
        if (x >= ofGetWidth() - 1) {
            y += image.getHeight();
            x = 0;
        }
    }
}

Which gives me the desired image:

But when I exchange the for loop in my draw function to this, everything breaks:

    for (int i = 0; i < 20; i++) {
        imageTiles[i].draw(x, y);
        x += imageTiles[i].getWidth();
        if (x >= ofGetWidth() - 1) {
            y += imageTiles[i].getHeight();
            x = 0;
        }
    }

I then get a solid coloured screen.

Can somebody explain to me what’s going on here, and why these two loops aren’t equivalent? It seems I fundamentally misunderstand something about pointers or references and how these loops access my ofImage objects.

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 4929

Trending Articles