1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
//Version 0.0.5
#include <iostream>
#include <string>
#include <sstream>
#include <SFML/Graphics.hpp>
sf::Text CreateText(const sf::Font& font, int characterSize, const sf::Color& color, float posX, float posY, const std::string& str)
{
sf::Text text;
text.setCharacterSize(characterSize);
text.setFont(font);
text.setFillColor(color);
text.setPosition(sf::Vector2f(posX, posY));
text.setString(str);
return text;
}
std::unique_ptr<sf::RectangleShape> CreateRectangle(float width, float height, const sf::Color& color, float setXPos, float setYPos, float setXOrigin, float setYOrigin)
{
std::unique_ptr<sf::RectangleShape> rectangle(new sf::RectangleShape);
rectangle->setSize(sf::Vector2f(width, height));
rectangle->setFillColor(sf::Color::White);
rectangle->setPosition(sf::Vector2f(setXPos, setYPos));
rectangle->setOrigin(sf::Vector2f(setXOrigin, setYOrigin));
return rectangle;
}
int main()
{
sf::RenderWindow window(sf::VideoMode(1280, 720), "SFML Feature Testing");
sf::Font Arial;
Arial.loadFromFile("Resources/Fonts/arial.ttf");
std::ostringstream oss;
std::istringstream iss;
float windowCenterX{ window.getSize().x / 2.0f };
float windowCenterY{ window.getSize().y / 2.0f };
sf::Text MouseXText{ CreateText(Arial, 32, sf::Color::White, 0.0f, 0.0f, "Mouse X Pos: ") };
sf::Text MouseXValue{ CreateText(Arial, 32, sf::Color::White, 0.0f, 0.0f, "Mouse X Value: ") };
sf::Text MouseYText{ CreateText(Arial, 32, sf::Color::White, 0.0f, 0.0f, "Mouse Y Pos: ") };
sf::Text MouseYValue{ CreateText(Arial, 32, sf::Color::White, 204, 30, "Mouse Y Value : ") };
MouseXValue.setPosition(sf::Vector2f(MouseXText.getPosition().x + MouseXText.getGlobalBounds().width, 0.0f));
MouseYText.setPosition(sf::Vector2f(0, MouseXText.getPosition().y + MouseXText.getGlobalBounds().height + 5));
std::cout << "MouseYText Position: " << MouseXText.getPosition().x << '\n';
//Center Square
auto square{ CreateRectangle(300, 300, sf::Color::White, windowCenterX, windowCenterY, 0, 0) };
square->setOrigin(sf::Vector2f(square->getSize().x / 2, square->getSize().y / 2));
//Squares
auto cornerSquare_1 { CreateRectangle(35, 35, sf::Color::Blue, 0, 0, 0, 0) };
cornerSquare_1->setOrigin(sf::Vector2f(cornerSquare_1->getSize().x / 2.0f, cornerSquare_1->getSize().y / 2.0f));
auto cornerSquare_2 { CreateRectangle(35, 35, sf::Color::Cyan, 0, 0, 0, 0) };
cornerSquare_2->setOrigin(sf::Vector2f(cornerSquare_2->getSize().x / 2.0f, cornerSquare_2->getSize().y / 2.0f));
auto cornerSquare_3 { CreateRectangle(35, 35, sf::Color::Green, 0, 0, 0, 0) };
cornerSquare_3->setOrigin(sf::Vector2f(cornerSquare_3->getSize().x / 2.0f, cornerSquare_3->getSize().y / 2.0f));
auto cornerSquare_4 { CreateRectangle(35, 35, sf::Color::Red, 0, 0, 0, 0) };
cornerSquare_4->setOrigin(sf::Vector2f(cornerSquare_4->getSize().x / 2.0f, cornerSquare_4->getSize().y / 2.0f));
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
window.close();
}
if (event.type == sf::Event::Resized)
{
window.setView(sf::View(sf::FloatRect(0.0f, 0.0f, static_cast<float>(event.size.width), static_cast<float>(event.size.height))));
square->setPosition(sf::Vector2f(window.getSize().x / 2, window.getSize().y / 2));
}
}
//DEBUG
//Put debug info in here so i can see the data instead of having it scroll by
if (sf::Keyboard::isKeyPressed(sf::Keyboard::P))
{
//Set the white square in the dead center of the screen even when its resized
square->setPosition(sf::Vector2f(window.getSize().x / 2, window.getSize().y / 2));
std::cout << "\nMouse X: " << sf::Mouse::getPosition(window).x << " Mouse Y: " << sf::Mouse::getPosition(window).y << '\n';
std::cout << "\nSquare X Position: " << square->getPosition().x << " - Square Y Position: " << square->getPosition().y << '\n';
std::cout << "\nSquare global bounds height: " << square->getGlobalBounds().height << '\n';
std::cout << "Square global bounds left: " << square->getGlobalBounds().left << '\n';
std::cout << "Square global bounds top: " << square->getGlobalBounds().top << '\n';
std::cout << "Square global bounds width: " << square->getGlobalBounds().width << '\n';
std::cout << "\nX Origin: " << square->getOrigin().x << '\n';
std::cout << "Y Origin: " << square->getOrigin().y << '\n';
}
oss << sf::Mouse::getPosition(window).x;
MouseXValue.setString(oss.str());
oss.str("");
oss << sf::Mouse::getPosition(window).y;
MouseYValue.setString(oss.str());
oss.str("");
//std::cout << "\nMouse X: " << sf::Mouse::getPosition(window).x << " Mouse Y: " << sf::Mouse::getPosition(window).y << '\n';
//Collision detection
/*if (sf::Mouse::getPosition(window).x >= square->getPosition().x
&& sf::Mouse::getPosition(window).x <= square->getPosition().x + square->getSize().x
&& sf::Mouse::getPosition(window).y >= square->getPosition().y
&& sf::Mouse::getPosition(window).y <= square->getPosition().y + square->getSize().y
)*/
//TODO - Figure out how to get collision to detect if its within square
if (sf::Mouse::getPosition(window).x >= square->getPosition().x
&& sf::Mouse::getPosition(window).x <= square->getPosition().x + square->getSize().x
&& sf::Mouse::getPosition(window).y >= square->getPosition().y
&& sf::Mouse::getPosition(window).y <= square->getPosition().y + square->getSize().y
)
{
std::cout << "Within white square\n";
}
//cornersquare->setPosition(sf::Vector2f(sf::Mouse::getPosition(window).x - cornersquare->getSize().x, sf::Mouse::getPosition(window).y + cornersquare->getSize().y));
//Set cornerSquare to one corner.
//Bottom Right
cornerSquare_1->setPosition(sf::Vector2f(square->getPosition().x + square->getSize().x, square->getPosition().y + square->getSize().y));
////Bottom Left
cornerSquare_2->setPosition(sf::Vector2f(square->getPosition().x - cornerSquare_2->getSize().x, square->getPosition().y + square->getSize().y));
////Top Right
cornerSquare_3->setPosition(sf::Vector2f(square->getPosition().x + square->getSize().x, square->getPosition().y - cornerSquare_3->getSize().y));
//Top Left
cornerSquare_4->setPosition(sf::Vector2f(square->getPosition().x - cornerSquare_4->getSize().x, square->getPosition().y - cornerSquare_4->getSize().y));
window.clear();
window.draw(MouseXText);
window.draw(MouseXValue);
window.draw(MouseYText);
window.draw(MouseYValue);
window.draw(*square);
window.draw(*cornerSquare_1);
window.draw(*cornerSquare_2);
window.draw(*cornerSquare_3);
window.draw(*cornerSquare_4);
window.display();
}
return 0;
}
|