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");
- std::vector<sf::Sprite*> spriteContainer;
- for (int i = 0; i < 3; i++)
- {
- sf::Sprite* sprite = new sf::Sprite;
- spriteContainer.push_back(sprite);
- sprite->setTexture(texture);
- sprite->setPosition(100.0f * (i+1), 100.0f);
- }
- std::vector<sf::FloatRect*> rectangleContainer;
- for (int i = 0; i < 3; i++)
- {
- sf::FloatRect* tileRect0 = new sf::FloatRect(100.0f * (i+1), 100.0f, 48.0f, 48.0f);
- rectangleContainer.push_back(tileRect0);
- }
- // 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 = rectangleContainer.begin(); iter != rectangleContainer.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;
- }
- for (auto iter = spriteContainer.begin(); iter != spriteContainer.end(); iter++)
- {
- window.draw(**iter);
- }
- window.display();
- }
- for (int i = 0; i < 3; i++)
- {
- delete spriteContainer[i];
- delete rectangleContainer[i];
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement