Hi there,
I know the basics of cpp, it's only a habit of collecting old/experimental code, that could become useful one day.....and lack of concentration, while writing/reading my code. Sorry that most of the time I produce such chaos code, it will improve. Promised! :-) Regarding the vectors: I know how to use them, but for now I'd like to keep my list :-)
Ok, now I´ve some reworked code (VS 2010 Standard), where at least I can move those sprites around, but only, if clicking inside of the corresponding unit, that I want to move. Another workaround would be to make the "invisible" background around the spritetexture bigger, but that would be problematic, when doing collisions.....Now, I only need being able to drag&drop them or at least "beam" them, if its less difficult (first click to select a unitsprite, second click for beaming it to a target position). Do I still need a Z value with the new code? What to do next? And what to do for telling him "Select THIS unit and stay focused on it, until I tell you to give it free" (if that could be done, than everything would be possible :-) )
#include <SFML/Graphics.hpp>
#include <iostream>
#include <list>
int main()
{
sf::RenderWindow mMainWindow(sf::VideoMode(500, 500), "Map");
mMainWindow.setFramerateLimit(60);
sf::RectangleShape maprect;
maprect.setSize(sf::Vector2f(500, 500));
maprect.setFillColor(sf::Color::Blue);
sf::Texture unittexture;
unittexture.loadFromFile("warrior.png");
std::list<sf::Sprite> EnemyList;
while (mMainWindow.isOpen())
{
sf::Event event;
bool isPressedA = false;
bool dragging = false;
while (mMainWindow.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
mMainWindow.close();
}
else if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::A)
{
sf::Sprite newSprite;
newSprite.setTexture(unittexture);
newSprite.setPosition(mMainWindow.mapPixelToCoords(sf::Vector2i(sf::Mouse::getPosition(mMainWindow).x, sf::Mouse::getPosition(mMainWindow).y)));
//this set position could be done without using real time input by checking (isPressedA) in a mousemovedevent, if you're in the same frame there is no difference //dont understand-->a is only for unit creation
isPressedA = true;
EnemyList.push_back(newSprite);
std::cout << EnemyList.size() << std::endl;
}
else if (event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == sf::Mouse::Left)
{
for (auto &EnemyIt = EnemyList.rbegin(); EnemyIt != EnemyList.rend(); ++EnemyIt)
{
{
dragging = true;
}
}
}
else if (event.type == sf::Event::MouseButtonReleased && event.mouseButton.button == sf::Mouse::Left)
{
dragging = false;
}
if (dragging == true)
{
for (auto &EnemyIt = EnemyList.rbegin(); EnemyIt != EnemyList.rend(); ++EnemyIt)
if (EnemyIt->getGlobalBounds().contains(static_cast<float>(event.mouseButton.x), static_cast<float>(event.mouseButton.y)))
{
EnemyIt->setPosition((mMainWindow.mapPixelToCoords(sf::Vector2i(sf::Mouse::getPosition(mMainWindow).x-50, sf::Mouse::getPosition(mMainWindow).y-50))));
break;
}
}
}
mMainWindow.clear();
mMainWindow.draw(maprect);
for (auto &EnemyIt = EnemyList.begin(); EnemyIt != EnemyList.end(); ++EnemyIt)
{
mMainWindow.draw(*EnemyIt);
}
mMainWindow.display();
}
return 0;
}