Sectah Sectah - 1 month ago 13
C++ Question

View class is not receiving updated Model class data

Model class



class spaceshipModel {
private:
Vector2f position;
float speed, acceleration, energy, fuel;

public:
//Contructor
spaceshipModel() : position(0, 0), speed(0), acceleration(0), energy(0), fuel(0) {}

//Destructor
~spaceshipModel() {}

//Sets
void setPosition(float _x, float _y) { position.x = _x; position.y = _y; }

void setSpeed(float _speed) { speed = _speed; }

void setAcceleration(float _acceleration) { acceleration = _acceleration; }

void setEnergy(float _energy) { energy = _energy; }

void setFuel(float _fuel) { fuel = _fuel; }

//Gets
Vector2f getPosition() { return position; }

float getSpeed() { return speed; }

float getAcceleration() { return acceleration; }

float getEnergy() { return energy; }

float getFuel() { return fuel; }

};


View class



class spaceshipView {
private:
Texture* image;
Sprite sprite;
spaceshipModel model;

public:
//Constructor
spaceshipView() : image(0) {}

//Destructor
~spaceshipView() {}

//Setting the image
void setImage(Texture* _image) { image = _image; }

//Drawing the image
void drawImage(RenderWindow* _window) {
sprite.setTexture(*image);
sprite.setPosition(model.getPosition());
sprite.setScale(Vector2f(0.2f, 0.2f));
_window->draw(sprite);
_window->display();
}
};


Main



A left a lot of the code out, but I then call this in main:

int main() {

//Call instance of the Spaceship model
spaceshipModel shipModel;

//Call instance of the Spaceship view
spaceshipView shipView;

//Create the texture of the spaceship from file
Texture spaceship;
spaceship.loadFromFile("spaceship.png");

//Create the window
RenderWindow window(VideoMode(800, 600), "Spaceship with MVC");

//Run the program as long as the window is open
while (window.isOpen()) {

//Check all the window's events that were triggered since the last iteration of the loop
Event event;

while (window.pollEvent(event)) {

//"Close requested" event: we close the window
switch (event.type) {

//Window closed by pressing the X
case Event::Closed:
window.close();
break;

//Checking for key pressed event
case Event::KeyPressed:

//Pressing esc to close the window
if (event.key.code == Keyboard::Escape) {
window.close();
}
break;

//We don't process other types of events
default:
break;
}

//Clear screen with white BG
window.clear(Color::White);

//TESTING THE SETTING OF THE POSITION
std::cout << shipModel.getPosition().x << ", " << shipModel.getPosition().y << std::endl;
shipModel.setPosition(100, 100);
std::cout << shipModel.getPosition().x << ", " << shipModel.getPosition().y << std::endl;

//Set and draw the image
shipView.setImage(&spaceship);
shipView.drawImage(&window);

}
}

return 0;

}


The spaceship draws perfectly, but is only set at (0, 0). Even when setting the position to (100, 100) like it shows above. The image stays at (0, 0). As I'm using the getPosition function from the Model class in the View class, I don't think the data is being updated correctly, even when the cout test does show a change.

What am I doing wrong? Can someone give me some pointers?

Answer

In the code snippet above, shipModel object from main() and shipView.model are two distinct objects. You can either let your shipView be aware of the model using a setter in spaceshipView, or call shipView.model's methods directly.