Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Map Editor Problem  (Read 1958 times)

0 Members and 1 Guest are viewing this topic.

Njifra

  • Guest
Map Editor Problem
« on: February 18, 2014, 10:53:39 pm »
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;
}
 

Azaral

  • Full Member
  • ***
  • Posts: 110
    • View Profile
Re: Map Editor Problem
« Reply #1 on: February 18, 2014, 11:07:08 pm »
sf::Texture textureObj;
 

Your sprites all point to this texture. If you change the actual texture inside of the sf::Texture, which is what the sprites all point to, then you will change what the sprites display.

What you need is a vector or map for your textures.

Njifra

  • Guest
Re: Map Editor Problem
« Reply #2 on: February 18, 2014, 11:49:14 pm »
Damn, I'm so stupid...
Anyway, thanks for help...

Azaral

  • Full Member
  • ***
  • Posts: 110
    • View Profile
Re: Map Editor Problem
« Reply #3 on: February 19, 2014, 12:05:47 am »
Damn, I'm so stupid...
Anyway, thanks for help...

I would not say stupid, you just committed an over sight. Everyone does it and we all feel stupid when we find them.

 

anything