Hello.
When I change texture using key A or D all objects in vector items change its texture
Can anyone help me?
Here is full source:
// R.I.P. 4.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include <iostream>
#include <string>
#include "SFML\Graphics.hpp"
#include "SFML\System.hpp"
#include "SFML\Window.hpp"
#include "SFML\Audio.hpp"
#include "Game.h"
#include <sstream>
using namespace std;
int main()
{
window.create(sf::VideoMode(800, 600, 32), "RIP 4: The Ressurection", sf::Style::Close | sf::Style::Titlebar);
window.setMouseCursorVisible(false);
sf::Texture texture;
texture.loadFromFile("bin/imgs/background0.jpg");
texture.setSmooth(true);
sf::Sprite background;
background.setTexture(texture);
float zoominmap = 0.785f;
background.setScale(zoominmap, zoominmap);
int bgId = 0;
sf::Texture textureObj;
int currId = 0;
textureObj.loadFromFile("bin/imgs/objects/obj_0.png");
vector<sf::Sprite> items;
while (window.isOpen())
{
sf::Sprite currentObj;
currentObj.setTexture(textureObj);
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
else if (event.type == sf::Event::KeyReleased)
{
/*if (event.key.code == sf::Keyboard::Left || event.key.code == sf::Keyboard::A)
pl.dir[0] = false;*/
}
else if (event.type == sf::Event::KeyPressed)
{
if (event.key.code == sf::Keyboard::Q)
{
if (bgId > 0)
bgId--;
string w;
stringstream ss;
ss << bgId;
ss >> w;
texture.loadFromFile("bin/imgs/background" + w + ".jpg");
background.setTexture(texture);
}
if (event.key.code == sf::Keyboard::E)
{
if (bgId < 14)
bgId++;
string w;
stringstream ss;
ss << bgId;
ss >> w;
texture.loadFromFile("bin/imgs/background" + w + ".jpg");
background.setTexture(texture);
}
if (event.key.code == sf::Keyboard::D)
{
if (currId < 121)
currId++;
string w;
stringstream ss;
ss << currId;
ss >> w;
textureObj.loadFromFile("bin/imgs/objects/obj_" + w + ".png");
}
if (event.key.code == sf::Keyboard::A)
{
if (currId > 0)
currId--;
string w;
stringstream ss;
ss << currId;
ss >> w;
textureObj.loadFromFile("bin/imgs/objects/obj_" + w + ".png");
}
}
else if (event.type == sf::Event::MouseButtonPressed)
{
if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
currentObj.setPosition(sf::Mouse::getPosition(window).x - (currentObj.getGlobalBounds().width / 2), sf::Mouse::getPosition(window).y - (currentObj.getGlobalBounds().height / 2));
cout << "Ey" << endl;
items.push_back(currentObj);
}
}
}
window.clear();
currentObj.setPosition(sf::Mouse::getPosition(window).x - (currentObj.getGlobalBounds().width / 2), sf::Mouse::getPosition(window).y - (currentObj.getGlobalBounds().height / 2));
//draw state
window.draw(background);
for (int i = 0; i < items.size(); i++)
{
window.draw(items[i]);
}
window.draw(currentObj);
window.display();
}
return 0;
}