Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <iostream>
- #include "SFML Files\include\SFML\Window.hpp"
- #include "SFML Files\include\SFML\Graphics.hpp"
- #include <vector>
- int main()
- {
- int id = 0;
- sf::RenderWindow window(sf::VideoMode(800, 600), "My window");
- sf::Vector2f cursorPosition = window.mapPixelToCoords(sf::Mouse::getPosition(window));
- sf::Texture texture;
- texture.loadFromFile("tile1.png");
- sf::Sprite sprite;
- sprite.setTexture(texture);
- sprite.setPosition(100, 100);
- sf::FloatRect tileRect(100.0f, 100.0f, 48.0f, 48.0f);
- sf::Sprite sprite2;
- sprite2.setTexture(texture);
- sprite2.setPosition(200.0f, 100.0f);
- sf::FloatRect tileRect2(200.0f, 100.0f, 48.0f, 48.0f);
- std::vector<sf::FloatRect*> vec;
- vec.push_back(&tileRect);
- vec.push_back(&tileRect2);
- // 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
- sf::Event event;
- while (window.pollEvent(event))
- {
- // "close requested" event: we close the window
- if (event.type == sf::Event::Closed)
- window.close();
- }
- window.clear();
- cursorPosition = window.mapPixelToCoords(sf::Mouse::getPosition(window));
- if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
- {
- for (auto iter = vec.begin(); iter != vec.end(); iter++, id++)
- {
- if ((*iter)->contains(cursorPosition))
- {
- std::cout << "THEY INTERSECT" << std::endl;
- std::cout << "ID: " << id << std::endl;
- break;
- }
- else
- {
- std::cout << "NO INTERSECTION" << std::endl;
- }
- }
- id = 0;
- std::cout << sf::Mouse::getPosition(window).x << std::endl;
- std::cout << sf::Mouse::getPosition(window).y << std::endl;
- }
- window.draw(sprite);
- window.draw(sprite2);
- window.display();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement