#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <time.h>
using namespace std;
std::vector<sf::CircleShape> circles(10);
void spawnCircles(){
for (int i = 0; i<10; ++i) {
circles[i].setFillColor(sf::Color::Red);
circles[i].setRadius(5);
circles[i].setPosition(((int)rand() % 700 + 100), ((int)rand() % 700 + 100));
}
}
int main()
{
srand(time(NULL));
sf::Font font;
if (!font.loadFromFile("font.ttf")){
// error...
}
sf::Vector2i screenDimensions(1000,800);
sf::RenderWindow window(sf::VideoMode(screenDimensions.x, screenDimensions.y), "Circle Game");
sf::View view;
view.reset(sf::FloatRect(0, 0, screenDimensions.x, screenDimensions.y));
view.setViewport(sf::FloatRect(0, 0, 1.0f, 1.0f));
//Title Screen
sf::RectangleShape nameBox(sf::Vector2f(400, 50)); //textbox
nameBox.setPosition(200, 400);
nameBox.setFillColor(sf::Color::Transparent);
nameBox.setOutlineColor(sf::Color::Black); nameBox.setOutlineThickness(3);
sf::RectangleShape goButton(sf::Vector2f(100, 100)); //press to the game
goButton.setPosition(75, 550);
goButton.setFillColor(sf::Color::Green);
goButton.setOutlineColor(sf::Color::Black); goButton.setOutlineThickness(3);
sf::CircleShape playerCircle; //player(one?)
playerCircle.setRadius(10);
playerCircle.setFillColor(sf::Color::Yellow);
playerCircle.setOutlineColor(sf::Color::Black); playerCircle.setOutlineThickness(2);
sf::CircleShape border; //world border
border.setFillColor(sf::Color::Transparent);
border.setOutlineThickness(7);
border.setRadius(1500);
border.setPosition(-1000,-1000);
sf::Text nameInstrucs; //instructions; "Enter your name"
nameInstrucs.setFont(font);
nameInstrucs.setString("Enter your name");
nameInstrucs.setFillColor(sf::Color::Black);
nameInstrucs.setCharacterSize(24);
nameInstrucs.setPosition(310, 350);
sf:string str; sf::Text playerName; //player name
playerName.setPosition(250, 410);
playerName.setFont(font);
playerName.setCharacterSize(24);
playerName.setFillColor(sf::Color::Black);
int mouseX = 0;
int mouseY = 0;
int drawStuff = 0; // 0: NameScreen, 1: Info Screen(?), 2: Game
while (window.isOpen())
{
/////////--------------------------------------------
sf::Vector2i pixelPos = sf::Mouse::getPosition(window);
sf::Vector2f worldPos = window.mapPixelToCoords(pixelPos);
mouseX = worldPos.x; mouseY = worldPos.y;
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed) {
window.close();
}
if (event.type == sf::Event::TextEntered)
{
if (drawStuff == 0) {
if (event.text.unicode == '\b' && str.size() >= 1) {
str.erase(str.size() - 1, 1);
cout << "ASCII character typed: Backspace" << endl;
cout << "Total string: " << str << endl;
playerName.setString(str);
}
else if (event.text.unicode < 128 && str.size() <= 30) {
str += static_cast<char>(event.text.unicode);
cout << "ASCII character typed: " << static_cast<char>(event.text.unicode) << endl;
cout << "Total string: " << str << endl;
playerName.setString(str);
}
}
}
if (drawStuff == 0 && event.type == sf::Event::MouseButtonPressed) {
if (event.mouseButton.button == sf::Mouse::Left)
{
if (mouseX > goButton.getGlobalBounds().left &&
mouseY > goButton.getGlobalBounds().top &&
mouseX < goButton.getGlobalBounds().left + goButton.getGlobalBounds().width &&
mouseY < goButton.getGlobalBounds().top + goButton.getGlobalBounds().height) {
cout << "Button pressed." << endl; //press the button to start the game
drawStuff = 2;
}
}
}
if (event.mouseButton.button == sf::Mouse::Right && event.type == sf::Event::MouseButtonReleased) {
spawnCircles();
}
if (event.mouseButton.button == sf::Mouse::Left && event.type == sf::Event::MouseButtonPressed) {
std::cout << "The left mouse button was pressed." << std::endl;
std::cout << "mouseX: " << mouseX << std::endl;
std::cout << "mouseY: " << mouseY << std::endl;
}
}
/////////---------------------------------------------
window.setView(view);
window.clear(sf::Color(200, 200, 200));
if (drawStuff == 0) {
window.draw(nameBox);
window.draw(nameInstrucs);
window.draw(goButton);
}
if (drawStuff == 2) {
for (int n = 0; n < 10; ++n) {
window.draw(circles[n]);
}
window.draw(playerCircle);
window.draw(border);
view.setCenter(worldPos);
playerCircle.setPosition(view.getCenter());
}
window.draw(playerName);
window.display();
}
return 0;
}