Make animated background with loaded image

I am creating a simple space shooter game to practice OF. I want to create a background that moves so it looks like the spaceship is moving through the star.jpeg I loaded. Anyone have any suggestions. As of now my image is just static and I am using

textstarbckgd.draw(0,0,ofGetWidth(), ofGetHeight());`

Create a vec2 in ofApp.h to save the position of the background.


class ofApp
    // more stuff
    glm::vec2 bgPos;

In each ofApp::update() set the new position

void ofApp::setup()
    bgPos = glm::vec2(0,0);

void ofApp::update()
    bgPos.x -= 2 * ofGetLastFrame() // 2 pixels per second

Then draw it with:

textstarbckgd.draw(bgPos.x % ofGetWidth(), bgPos.y, ofGetWidth(), ofGetHeight());

To make it continuous draw it again on the other side of the screen

textstarbckgd.draw((ofGetWidth() + bgPos.x) % ofGetWidth() , bgPos.y, ofGetWidth(), ofGetHeight());

I haven’t tested the code, but the idea is to have two images that scroll together, so when one goes out the left side of the screen, it gets drawn on the right side again. The images should be continuous (no visible margin when put side to side)

Ok I entered the code into my project but I have two errors.

bgPos = 0; No viable overloaded ‘=’

starbckgd.draw(bgPos.x % ofGetWidth(), bgPos.y, ofGetWidth(), ofGetHeight());

Invalid operands to binary expression (float and float)

I am still having a hard time adding glm file to my Xcode project. If anyone has suggestions.

Sorry, bgPos is a glm::vec2, so you should use bgPos = glm::vec2(0,0);

glm library is already included in OF latest version, no need to add it manually. Use ofVec2f instead of glm::vec2 if you are not using latest OF version

pd. use bgPos.x -= 2 * ofGetLastFrame() // 2 pixels per second… note the bgPos.x