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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
//============================================================================
// Name : SFML Game
// Author : Chay Hawk
// Version : 0.0.14
// Version Date : March 24th 2023 @ 11:27 AM
// Date Created :
// Lines of Code : 224
// Description :
//============================================================================
#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)
{
std::unique_ptr<sf::RectangleShape> rectangle(new sf::RectangleShape);
rectangle->setSize(sf::Vector2f(width, height));
rectangle->setFillColor(color);
rectangle->setPosition(sf::Vector2f(setXPos, setYPos));
rectangle->setOrigin(sf::Vector2f(rectangle->getSize().x / 2, rectangle->getSize().y / 2));
return rectangle;
}
int main()
{
sf::RenderWindow window(sf::VideoMode(1280, 720), "SFML Feature Testing");
window.setFramerateLimit(60);
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 Position: ") };
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));
//Center Square
auto square{ CreateRectangle(300, 300, sf::Color::White, windowCenterX, windowCenterY) };
float squareLeft{ };
float squareTop{ };
float squareRight{ };
float squareBottom{ };
//Squares
auto cornerSquare_1 { CreateRectangle(35, 35, sf::Color::Blue, 0, 0) };
auto cornerSquare_2 { CreateRectangle(35, 35, sf::Color::Cyan, 0, 0) };
auto cornerSquare_3 { CreateRectangle(35, 35, sf::Color::Green, 0, 0) };
auto cornerSquare_4 { CreateRectangle(35, 35, sf::Color::Red, 0, 0) };
int playerX{ 150 };
int playerY{ 150 };
//Player
auto player{ CreateRectangle(32, 32, sf::Color::Yellow, playerX, playerY) };
float playerLeft{ };
float playerTop{ };
float playerRight{ };
float playerBottom{ };
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));
}
}
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 << "Player X Position: " << player->getPosition().x << '\n';
std::cout << "Player Y Position: " << player->getPosition().y << '\n';*/
std::cout << "Player X Left Side: " << player->getPosition().x - player->getOrigin().x << '\n';
std::cout << "Player Y Top: " << player->getPosition().y - player->getOrigin().y << '\n';
std::cout << "Player X Right Side: " << player->getPosition().x + player->getOrigin().x << '\n';
std::cout << "Player Y Bottom: " << player->getPosition().y + player->getOrigin().y << '\n';
if ( sf::Mouse::getPosition(window).x >= square->getPosition().x - square->getOrigin().x
&& sf::Mouse::getPosition(window).x <= square->getPosition().x + square->getOrigin().x
&& sf::Mouse::getPosition(window).y >= square->getPosition().y - square->getOrigin().y
&& sf::Mouse::getPosition(window).y <= square->getPosition().y + square->getOrigin().y
)
{
std::cout << "Within white square\n";
}
playerLeft = player->getPosition().x - player->getOrigin().x;
playerTop = player->getPosition().y - player->getOrigin().y;
playerRight = player->getPosition().x + player->getOrigin().x;
playerBottom = player->getPosition().y + player->getOrigin().y;
squareLeft = square->getPosition().x - square->getOrigin().x;
squareTop = square->getPosition().y - square->getOrigin().y;
squareRight = square->getPosition().x + square->getOrigin().x;
squareBottom = square->getPosition().y + square->getOrigin().y;
//player->setPosition();
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{
if (playerTop >= squareBottom)
{
player->move(0, -5);
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
{
if (playerLeft <= squareRight)
{
player->move(-5, 0);
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
if (playerBottom <= squareTop)
{
player->move(0, 5);
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
{
if (playerRight >= squareLeft)
{
player->move(5, 0);
}
}
//Set cornerSquare to one corner.
//Bottom Right (Blue)
cornerSquare_1->setPosition(sf::Vector2f(square->getPosition().x + square->getOrigin().x + cornerSquare_1->getOrigin().x, square->getPosition().y + square->getOrigin().y + cornerSquare_1->getOrigin().y));
//Top Left (Cyan)
cornerSquare_2->setPosition(sf::Vector2f(square->getPosition().x - square->getOrigin().x - cornerSquare_2->getOrigin().x, square->getPosition().y - square->getOrigin().y - cornerSquare_2->getOrigin().y));
//Top Right (Green)
cornerSquare_3->setPosition(sf::Vector2f(square->getPosition().x + square->getOrigin().x + cornerSquare_3->getOrigin().y, square->getPosition().y - square->getOrigin().y - cornerSquare_3->getOrigin().y));
//Bottom Left (Red)
cornerSquare_4->setPosition(sf::Vector2f(square->getPosition().x - square->getOrigin().x - cornerSquare_4->getOrigin().y, square->getPosition().y + square->getOrigin().y + cornerSquare_4->getOrigin().y));
window.clear();
window.draw(MouseXText);
window.draw(MouseXValue);
window.draw(MouseYText);
window.draw(MouseYValue);
window.draw(*player);
window.draw(*square);
window.draw(*cornerSquare_1);
window.draw(*cornerSquare_2);
window.draw(*cornerSquare_3);
window.draw(*cornerSquare_4);
window.display();
}
return 0;
}
|