void initTexts()
{
    if (!font.loadFromFile("arial.ttf"))cout << "error";
    endText.setFont(font);
    endText.setString("Game Over");
    endText.setCharacterSize(50);
    endText.setColor(sf::Color( 156, 39, 176));
    endText.setPosition(SIZE/2-100,SIZE/2-100);
    scoreText.setFont(font);
    scoreText.setCharacterSize(50);
    scoreText.setColor(sf::Color(255,255,255));
    scoreText.setPosition(SIZE/2-200,SIZE/2);
}
示例#2
0
void endGame()
{
	endTitle.setFont(myFont);
	endTitle.setCharacterSize(WIDTH / 16);
	endTitle.setStyle(sf::Text::Bold);
	endTitle.setColor(sf::Color::Black);
	string str;
	if (end1P)
	{
		stringstream message;
		message << "Final score: " << mover;
		str = message.str();
		
	}
	else if (end2P)
	{
		stringstream message;
		message << "Player " << mover << " wins!";
		str = message.str();
	}
	endTitle.setString(str);
	endTitle.setOrigin(endTitle.getLocalBounds().width / 2, endTitle.getLocalBounds().height / 2);
	endTitle.setPosition(WIDTH / 2, 2 * HEIGHT / 5);

	sf::Vector2f menuRectSize(3 * WIDTH / 16, HEIGHT / 8);

	for (int i = 0; i < endMenuRect.size(); i++)
	{
		endMenuRect[i].setSize(menuRectSize);
		endMenuRect[i].setFillColor(grey);
		endMenuRect[i].setOrigin(menuRectSize.x / 2, menuRectSize.y / 2);
		endMenuRect[i].setPosition(WIDTH / 2, 4 * HEIGHT / 5);

		endMenuText[i].setFont(myFont);
		endMenuText[i].setCharacterSize(2 * pause.getCharacterSize() / 5);
		endMenuText[i].setStyle(sf::Text::Bold);
		endMenuText[i].setColor(sf::Color::Black);
		endMenuText[i].setString(endMenuCommands[i]);
		endMenuText[i].setOrigin(endMenuText[i].getLocalBounds().width / 2, endMenuText[i].getLocalBounds().height / 2);
		endMenuText[i].setPosition(endMenuRect[i].getPosition().x, endMenuRect[i].getPosition().y);
	}

	BILLIARDS.draw(endTitle);
	for (int i = 0; i < endMenuRect.size(); i++)
	{
		BILLIARDS.draw(endMenuRect[i]);
		BILLIARDS.draw(endMenuText[i]);
	}
}
void TextBox::Render(sf::RenderWindow & window)
{
	std::string content;
	for (auto &itr : _messages) {
		content.append(itr + "\n");
	}

	if (content == "") {
		return;
	}

	_content.setString(content);
	window.draw(_backdrop);
	window.draw(_content);
}
示例#4
0
	// Rendu
	void draw(sf::RenderTarget& renderer, const sf::Vector2f& center)
	{

		sf::Vector2f position(center);
		_text.setString(_label);
		position.x -= _text.getLocalBounds().width / 2.0f;
		position.y -= _text.getLocalBounds().height / 2.0f;
		_border.setSize(sf::Vector2f(_text.getLocalBounds().width + 10,
									 _text.getLocalBounds().height + 10));
		_border.setPosition(position);

		_text.setPosition(position);
		renderer.draw(_text);
		renderer.draw(_border);
	}
示例#5
0
void assetHandle::trimTextToRectangleWidth(sf::Text &text, sf::RectangleShape &rect)
{

    while(true)
    {
        sf::FloatRect floatRect = text.getGlobalBounds();
        if ((floatRect.left + floatRect.width) > rect.getSize().x)
        {
            std::string newString = text.getString();
            newString = newString.substr(0, newString.size()-1);
            text.setString(newString);
        }
        else
            break;
    }
}
示例#6
0
	void init_char_width(){
		if (!PTSANS.loadFromFile("PTN57F.ttf" )) return;
		PTSANS_loaded = true;

		string tq;
		
		int tmp = log_string.getPosition().x;
		
		for( int i = 255; i--; ){
			tq = (char) i;
			log_string.setString( tq );
			char_width[i] = log_string.findCharacterPos( 1 ).x - tmp;
		}


	}
int main()
{
    sf::Clock clock;
    initTexts();
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }
        window.clear(sf::Color(244, 67, 54));
        if(game_over)
        {
            window.draw(endText);
            ostringstream s;
            s << "Your score was : " << snake.length();
            scoreText.setString(s.str());
            window.draw(scoreText);
        }
        else
        {

            input(dir);
            sf::Time elapsed = clock.getElapsedTime();
            if(elapsed >= delta)
            {
                if(snake.head()->position() ==  f.position())
                {
                    snake.add();
                    f.generatePos();
                    delta = sf::seconds(delta_i.asSeconds() - (float)snake.length()/100);
                }
                snake.move(dir);
                clock.restart();

            }
            o.draw();
            f.draw();
            snake.draw();
            }
        window.display();
    }
    return 0;
}
示例#8
0
文件: Menu.cpp 项目: Coguar/Field964
void showMenu(Game1 & game, MenuText & menu_text, sf::Text & text, MenuImg & fon, menuBools & bools)
{

	game.window->draw(*fon.sprite);
	if (!bools.about) {
		text.setString(menu_text.new_game);
		menu_text.new_game_rect = { fon.sprite->getPosition().x - menu_text.new_game_pos.x - text.getLocalBounds().width / 2, fon.sprite->getPosition().y - menu_text.new_game_pos.y, text.getLocalBounds().width, text.getLocalBounds().height };
		_choose_color(text, game.pos, menu_text.new_game_rect);
		text.setOrigin(text.getLocalBounds().width / 2, 0);
		text.setPosition(fon.sprite->getPosition().x - menu_text.new_game_pos.x, fon.sprite->getPosition().y - menu_text.new_game_pos.y);
		game.window->draw(text);

		text.setString(menu_text.continue_game);
		menu_text.continue_game_rect = { fon.sprite->getPosition().x - menu_text.continue_game_pos.x - text.getLocalBounds().width / 2, fon.sprite->getPosition().y - menu_text.continue_game_pos.y, text.getLocalBounds().width, text.getLocalBounds().height };
		if (bools.first_play) {
			text.setColor(Color(128, 128, 128));
		}
		else {
			_choose_color(text, game.pos, menu_text.continue_game_rect);
		}
		text.setOrigin(text.getLocalBounds().width / 2, 0);
		text.setPosition(fon.sprite->getPosition().x - menu_text.continue_game_pos.x, fon.sprite->getPosition().y - menu_text.continue_game_pos.y);
		game.window->draw(text);

		text.setString(menu_text.about);
		menu_text.about_rect = { fon.sprite->getPosition().x - menu_text.about_pos.x - text.getLocalBounds().width / 2, fon.sprite->getPosition().y - menu_text.about_pos.y, text.getLocalBounds().width, text.getLocalBounds().height };
		_choose_color(text, game.pos, menu_text.about_rect);
		text.setOrigin(text.getLocalBounds().width / 2, 0);
		text.setPosition(fon.sprite->getPosition().x - menu_text.about_pos.x, fon.sprite->getPosition().y - menu_text.about_pos.y);
		game.window->draw(text);

		text.setString(menu_text.exit);
		menu_text.exit_rect = { fon.sprite->getPosition().x - menu_text.exit_pos.x - text.getLocalBounds().width / 2, fon.sprite->getPosition().y - menu_text.exit_pos.y, text.getLocalBounds().width, text.getLocalBounds().height };
		_choose_color(text, game.pos, menu_text.exit_rect);
		text.setOrigin(text.getLocalBounds().width / 2, 0);
		text.setPosition(fon.sprite->getPosition().x - menu_text.exit_pos.x, fon.sprite->getPosition().y - menu_text.exit_pos.y);
		game.window->draw(text);
	}
	else {
		text.setColor(Color::Red);
		text.setString(menu_text.about_text);
		text.setOrigin(text.getLocalBounds().width / 2, 0);
		text.setPosition(fon.sprite->getPosition().x - menu_text.about_text_pos.x, fon.sprite->getPosition().y - menu_text.about_text_pos.y);
		game.window->draw(text);

		text.setString(menu_text.back);
		menu_text.back_rect = { fon.sprite->getPosition().x - menu_text.back_pos.x - text.getLocalBounds().width / 2, fon.sprite->getPosition().y - menu_text.back_pos.y, text.getLocalBounds().width, text.getLocalBounds().height };
		_choose_color(text, game.pos, menu_text.back_rect);
		text.setOrigin(text.getLocalBounds().width / 2, 0);
		text.setPosition(fon.sprite->getPosition().x - menu_text.back_pos.x, fon.sprite->getPosition().y - menu_text.back_pos.y);
		game.window->draw(text);
	}
}
void TextBox::Setup(int visible, int charSize, int width, sf::Vector2f screenPos)
{
	_numVisible = visible;

	sf::Vector2f offset(2.0f, 2.0f);

	_font.loadFromFile("arial.ttf");
	_content.setFont(_font);
	_content.setString("");
	_content.setCharacterSize(charSize);
	_content.setColor(sf::Color::White);
	_content.setPosition(screenPos + offset);

	auto backdropHeight = visible * (charSize * 1.2f);
	_backdrop.setSize(sf::Vector2f(width, backdropHeight));
	_backdrop.setFillColor(sf::Color(90, 90, 90, 90));
	_backdrop.setPosition(screenPos);
}
void setAll(int& x,int& y,int& xBlock,int& yBlock)
{
	x = 375; 
	y = 250;
	
	if(!font.loadFromFile("ethnocentric rg.ttf"))
	{
		//error...
	};

	text = convertInt(score1);
	score.setString(text);
	score.setFont(font);
	score.setCharacterSize(24);
	score.setColor(sf::Color(60,60,255));
	
	square.setPosition(x,y);
	xBlock = 100;
	yBlock = 100;
}
示例#11
0
void StateManager::drawUi(sf::RenderTarget& target)
{
    Kunlaboro::Message msg(Kunlaboro::Type_Message, NULL, &target);
    mSystem.sendGlobalMessage(mRDrawUi, msg);

    if (frames++ > 10)
    {
        prof.setFont(mGlobalFont);
        prof.setCharacterSize(12);

        char tmp[256];
#ifdef WIN32
        sprintf_s(tmp, "Components: %d, entities: %d.\nFPS: %-4d (%.2f avg)\nDT:  %-4.2f (%.2f avg)", mSystem.numCom(), mSystem.numEnt(), mTelem->getFPS(), mTelem->getAverageFPS(), mTelem->getDT(), mTelem->getAverageDT());
#else
        sprintf(tmp, "Components: %d, entities: %d.\nFPS: %-4d (%.2f avg)\nDT:  %-4.2f (%.2f avg)", mSystem.numCom(), mSystem.numEnt(), mTelem->getFPS(), mTelem->getAverageFPS(), mTelem->getDT(), mTelem->getAverageDT());
#endif
        prof.setString(tmp);
    }

    target.draw(prof);
}
示例#12
0
	//Enemy constructor.
	Enemy(sf::Texture& texture, int x, int y, sf::Font& font) {
		mSprite.setTexture(texture);
		mRect = sf::FloatRect(x, y, 120, 110);					//Character x, y, width, height.

		mName = "Enemy";
		mTextName.setString(mName);
		mTextName.setFont(font);
		mTextName.setCharacterSize(30);
		mTextName.setStyle(sf::Text::Bold);
		mTextName.setColor(sf::Color::Magenta);

		mSprite.setTextureRect(sf::IntRect(0, 15, 120, 110));

		mDirection = rand() % 4;
		mSpeed = 0.05;

		mCurrentFrame = 0;
		mAnimationSpeed = mSpeed * 0.05;

		mIsAlive = true;
		mHP = 100;

		mDamage = 30;
	}
void setup(sf::RenderWindow& s, sf::Event& e, sf::Text& t, sf::Font& tf, sf::Text& v, tgui::ListBox::Ptr& res, tgui::ListBox::Ptr& mode, tgui::EditBox::Ptr& c1, tgui::EditBox::Ptr& c2, tgui::Button::Ptr& b, tgui::Gui& sg)
{
	tf.loadFromFile("sansation.ttf");

	//WINDOW
	s.setFramerateLimit(10);

	//TITLE
	t.setFont(tf);
	t.setPosition(-5, 0);
	t.setStyle(sf::Text::Underlined);
	t.setString(" GRAVITY SIMULATOR ");
	t.setCharacterSize(30);

	//VERSIONTEXT
	v.setFont(tf);
	v.setColor(sf::Color(51, 255, 255));
	v.setPosition(247, 33);
	v.setString("v1.0");
	v.setCharacterSize(25);

	//GUI
	sg.setFont(tf);
	sg.add(res);
	sg.add(mode);
	sg.add(c1);
	sg.add(c2);
	sg.add(b);

	//VIDEOMODE
	mode->setScrollbar(nullptr);
	mode->setPosition(20, 80);
	mode->setItemHeight(15);
	mode->setSize(95, 30);
	mode->setTextSize(15);
	mode->addItem("FULLSCREEN");
	mode->addItem("WINDOWED");
	mode->setSelectedItemByIndex(0);

	//RESOLUTIONS
	res->setScrollbar(nullptr);
	res->setPosition(20, 115);
	res->setItemHeight(15);
	res->setTextSize(15);

	res->addItem("2560 x 1440");
	res->addItem("1920 x 1080");
	res->addItem("1366 x 768");
	res->addItem("CUSTOM");
	res->setSize(95, res->getItemCount() * 15);

	//CUSTOM RESOLUTION
	c1->setDefaultText("X");
	c1->setPosition(125, 140);
	c1->setSize(50, 15);
	c1->setTextSize(15);
	c1->setMaximumCharacters(4);
	
	c2->setDefaultText("Y");
	c2->setPosition(125, 160);
	c2->setSize(50, 15);
	c2->setTextSize(15);
	c2->setMaximumCharacters(4);

	//STARTBUTTON
	b->setPosition(185, 140);
	b->setSize(95, 35);
	b->setText("START");
	b->setTextSize(15);
	b->connect("pressed", start, res, mode, c1, c2);

}
示例#14
0
void draw_statistics(
	window& window,
	const black_label::path& asset_directory,
	const black_label::rendering::pipeline& rendering_pipeline,
	bool options_complete )
{
	static sf::Font font;
	static sf::Text text;
	static bool uninitialized{true};
	if (uninitialized) {
		using namespace black_label;
		system::error_code error_code;
		auto font_file = canonical_and_preferred("fonts/Vegur-Regular.ttf", asset_directory, error_code);

		if (error_code || !font.loadFromFile(font_file.string())) return;

		text.setFont(font);
		uninitialized = false;
	}


	gpu::vertex_array::unbind();
	gpu::buffer::unbind();
	gpu::framebuffer::unbind();
	window.window_.resetGLStates();

	static bool has_written_message{false};
	if (options_complete && rendering_pipeline.is_complete()) {
		has_written_message = false;
		static unordered_map<string, double> averages;
		stringstream ss;
		ss.precision(4);
		auto output_pass = [&] ( const std::string& name, const chrono::high_resolution_clock::duration& render_duration )
		{ 
			if (10s < render_duration || 10us > render_duration) return;
			auto render_time = duration_cast<duration<double, milli>>(render_duration).count();
			static const double alpha{0.75};
			averages[name] = alpha * render_time + (1.0 - alpha) * averages[name];
			ss << name << " [ms]: " << render_time << " (" << averages[name] << ")\n"; 
		};

		output_pass("rendering_pipeline.json", rendering_pipeline.render_time);

		output_pass(
			"\t" + rendering_pipeline.shadow_mapping.name, 
			rendering_pipeline.shadow_mapping.render_time);

		auto all_passes = rendering_pipeline.shadow_mapping.render_time;
		chrono::high_resolution_clock::duration ldm{0};
		for (const auto& pass : rendering_pipeline.passes) {
			all_passes += pass.render_time;
			if ("ldm" == pass.name.substr(0, 3)) {
				ldm += pass.render_time;
				continue;
			}
			output_pass("\t" + pass.name, pass.render_time);
		}
	
		output_pass("\tldm (all passes)", ldm);

		output_pass("\t(sequencing overhead)", rendering_pipeline.render_time - all_passes);

		ss << "data_buffer_size" << " [MB]: " << rendering_pipeline.data_buffer_size * 1.0e-6f << "\n"; 
		ss << "photon_buffer_size" << " [MB]: " << rendering_pipeline.photon_buffer_size * 1.0e-6f << "\n"; 

		text.setString(ss.str());
		text.setCharacterSize(16);

		text.setColor(sf::Color::Black);
		text.setPosition(6, 1);
		window.window_.draw(text);

		text.setColor(sf::Color::White);
		text.setPosition(5, 0);
		window.window_.draw(text);
	}
	else if (!has_written_message) {
		has_written_message = true;
		auto window_size = window.window_.getSize();
		sf::RectangleShape rectangle{sf::Vector2f{window_size}};
		rectangle.setFillColor(sf::Color{0, 0, 0, 180});
		window.window_.draw(rectangle);
		
		text.setString("Error in rendering config.");
		text.setCharacterSize(22);
		sf::FloatRect textBounds = text.getLocalBounds();
		text.setPosition(sf::Vector2f(
			floor(window_size.x / 2.0f - (textBounds.left + textBounds.width / 2.0f)), 
			floor(window_size.y / 2.0f - (textBounds.top  + textBounds.height / 2.0f))));
		text.setColor(sf::Color::White);
		window.window_.draw(text);
	}
}
示例#15
0
/*
	This function changing RGB values of Current Backgorund Color 
	to new color

	@param current color;
	@param new color;
	@param Handle to window.
	@param Handle to font

*/
void setFadeColor(sf::Color &currentColor, sf::Color &k, sf::RenderWindow &win, sf::Text &font)
{
	int tmpR, tmpG, tmpB = 0;
	font.setString(points);
	/*
		config fade for R pat.
	*/
	if (currentColor.r != k.r)
	{

		if (currentColor.r > k.r)
		{
			tmpR = -1;

		}
		else if (currentColor.r < k.r)
		{
			tmpR = 1;

		}

		currentColor.r += tmpR;
		font.setColor(currentColor);

	}
	else
	{
		tmpR = 0;
	}


	/*
		config fade for G pat.
	*/
	if (currentColor.g != k.g)
	{

		if (currentColor.g > k.g)
		{
			tmpG = -1;

		}
		else if (currentColor.g < k.g)
		{
			tmpG = 1;

		}

		currentColor.g += tmpG;
		font.setColor(currentColor);

	}
	else
	{
		tmpG = 0;
	}


	/*
		config fade for B pat.
	*/
	if (currentColor.b != k.b)
	{

		if (currentColor.b > k.b)
		{
			tmpB = -1;
		}
		else if (currentColor.b < k.b)
		{
			tmpB = 1;
		}

		currentColor.b += tmpB;
		font.setColor(currentColor);

	}
	else
	{
		tmpB = 0;
	}
	font.setColor(currentColor);
	win.clear(currentColor);
}
示例#16
0
int main()
{
	// Create the main window 
	sf::RenderWindow window(sf::VideoMode(960, 640, 32), "SFML First Program");

	//load a font
	sf::Font font;
	font.loadFromFile("GOCMP.ttf");

	Player* player = new Player();

	displayText.setFont(font);
	displayText.setPosition(20, 20);
	displayText.setString("");
	displayText.setColor(sf::Color::Blue);

	//std::thread t1;
	sf::Thread thread(&Fetch);

	sf::Clock clock;
	clock.restart();

	// Start game loop 
	while (window.isOpen())
	{
		// Process events 
		sf::Event Event;
		while (window.pollEvent(Event))
		{
			// Close window : exit 
			if (Event.type == sf::Event::Closed)
				window.close();

			// Escape key : exit 
			if ((Event.type == sf::Event::KeyPressed) && (Event.key.code == sf::Keyboard::Escape))
				window.close();

			////right
			if ((Event.type == sf::Event::KeyReleased) && (Event.key.code == sf::Keyboard::D))
			{
				player->Move(1);
				monitor->Deposit("Move Right");
			}

			//left
			if ((Event.type == sf::Event::KeyReleased) && (Event.key.code == sf::Keyboard::A))
			{
				player->Move(2);
				monitor->Deposit("Move Left");
			}

		}
		
		if (clock.getElapsedTime().asSeconds() > 0.5)
		{
			if (monitor->StringAdded() == true)
				thread.launch();
			else std::cout << "Nothing added to Buffer " << std::endl;
			clock.restart();
		}

		//prepare frame
		window.clear();
		player->Draw(window);
		window.draw(displayText);

		// Finally, display rendered frame on screen 
		window.display();

	} //loop back for next frame

	return EXIT_SUCCESS;
}
示例#17
0
        Player () {
            PlayerNumber = totalPlayers;
            Sprite = loadSpriteData("Player " + intToString(PlayerNumber + 1));
            //Get Global width, seperate to rotation
            CharacterWidth = Sprite.getGlobalBounds().width;
            nameSpacing = 30;
            healthBarSpacing = nameSpacing + 15;

            Sprite.setOrigin(Sprite.getGlobalBounds().width/2, Sprite.getGlobalBounds().height/2);
            Sprite.setPosition((App.getSize().x/2) - (Sprite.getGlobalBounds().width/2), (App.getSize().y/2) - (Sprite.getGlobalBounds().height/2));

            //PlayerName.setFont(sf::Font::getDefaultFont());
            PlayerName.setCharacterSize(15);
            PlayerName.setString("Player: " + intToString(PlayerNumber + 1));
            PlayerName.setOrigin(PlayerName.getGlobalBounds().width/2, PlayerName.getGlobalBounds().height/2);
            PlayerName.setPosition(Sprite.getPosition().x, Sprite.getPosition().y + nameSpacing);

            NameOutline.setSize(sf::Vector2f(PlayerName.getGlobalBounds().width, PlayerName.getGlobalBounds().height));
            NameOutline.setOutlineThickness(2);
            NameOutline.setFillColor(sf::Color(0, 0, 0, 120));
            NameOutline.setOutlineColor(sf::Color::Black);
            NameOutline.setOrigin(NameOutline.getGlobalBounds().width/2, NameOutline.getGlobalBounds().height/2);
            NameOutline.setPosition(Sprite.getPosition().x + 1.5, Sprite.getPosition().y + nameSpacing + 4);

            MaxHealth = Health = 100;
            HealthBarSize = 50;

            HealthBarRed.setSize(sf::Vector2f(HealthBarSize, 5));
            HealthBarRed.setOutlineThickness(1);
            HealthBarRed.setFillColor(sf::Color::Red);
            HealthBarRed.setOutlineColor(sf::Color::Black);
            HealthBarRed.setPosition(Sprite.getPosition().x - (CharacterWidth/2) - 5, Sprite.getPosition().y + healthBarSpacing);

            HealthBarGreen.setSize(sf::Vector2f(HealthBarSize, 5));
            HealthBarGreen.setOutlineThickness(1);
            HealthBarGreen.setFillColor(sf::Color::Green);
            HealthBarGreen.setOutlineColor(sf::Color::Black);
            HealthBarGreen.setPosition(Sprite.getPosition().x - (CharacterWidth/2) - 5, Sprite.getPosition().y + healthBarSpacing);

            if (controllerConnected) {
                ControlScheme = ControlSchemes::Joystick;
                Key k;
                k.InputType = Input::JoystickMovedNeg;
                k.KeyCode = sf::Joystick::Axis::Y;
                Bindings["Up"] = k;

                k.InputType = Input::JoystickMovedPos;
                k.KeyCode = sf::Joystick::Axis::Y;
                Bindings["Down"] = k;

                k.InputType = Input::JoystickMovedNeg;
                k.KeyCode = sf::Joystick::Axis::X;
                Bindings["Left"] = k;

                k.InputType = Input::JoystickMovedPos;
                k.KeyCode = sf::Joystick::Axis::X;
                Bindings["Right"] = k;
                } else {
                    ControlScheme = ControlSchemes::KeyboardMouse;
                    Key k;
                    k.InputType = Input::KeyboardInput;
                    k.KeyCode = sf::Keyboard::W;
                    Bindings["Up"] = k;

                    k.InputType = Input::KeyboardInput;
                    k.KeyCode = sf::Keyboard::S;
                    Bindings["Down"] = k;

                    k.InputType = Input::KeyboardInput;
                    k.KeyCode = sf::Keyboard::A;
                    Bindings["Left"] = k;

                    k.InputType = Input::KeyboardInput;
                    k.KeyCode = sf::Keyboard::D;
                    Bindings["Right"] = k;
                    }

            totalPlayers++;
            }
示例#18
0
static void updateGameOverLabel(sf::Text &label, const std::string &text)
{
    label.setString(text);
    utils::centerizeTextOrigin(label);
}
示例#19
0
//Entity.cpp
void Entity::setName(sf::String name) {
	mName = name;
	mTextName.setString(mName);
}
void main()
	{
	menu();
	while(true){
	sf::Clock clock, clockInvincible;
    score1 = 0;
	int  xBlock, yBlock, x, y, seconds = 0, red, green, blue;
	int direction = 0, directionOfBack = 0, size;
	bool checkGameOver = false, outOfBounds = false, restarter = false, breaker = false;
			size = Robjects.size();
			Robjects.erase(Robjects.begin()+0, Robjects.begin()+size);
			window.clear();
	//---------------------------------------------------------------------------
	//sf::RenderWindow window(sf::VideoMode(600, 500), "Testing");
	//---------------------------------------------------------------------------
	event.type = sf::Event::Count;
	sf::RectangleShape square(sf::Vector2f(sizex, sizey));
	square.setFillColor(sf::Color(200, 60, 0));
	square.setOutlineThickness(2);
    square.setOutlineColor(sf::Color(140, 30, 0));
	//---------------------------------------------------------------------------
	sf::RectangleShape egg(sf::Vector2f(sizex + 2, sizey + 2));
	egg.setFillColor(sf::Color(red, green, blue));
	egg.setOutlineThickness(2);
    egg.setOutlineColor(sf::Color(200, 200, 200));
	//---------------------------------------------------------------------------
	setAll(x, y, xBlock, yBlock);
	//---------------------------------------------------------------------------
  	egg.setPosition(xBlock, yBlock);
	//---------------------------------------------------------------------------
	setEgg(red, green, blue);
 while(window.isOpen())
	  {
		  if(invincible1 == true){
			  invincible2(x, y);
		for(int i = 0; i < Robjects.size(); i++){
	Robjects[i].setFillColor(sf::Color(200, 50, 50));
	Robjects[i].setOutlineThickness(2);
    Robjects[i].setOutlineColor(sf::Color(40, 40, 40));
		}
		  }
		  else 
              outOfBounds = checkOutOfBounds(x , y);

	sf::Time elapsed1 = clock.getElapsedTime();
	seconds = elapsed1.asMilliseconds();
	//---------------------------------------------------------------------------
    restarter = updateGame(x, y, xBlock, yBlock, direction, directionOfBack, checkGameOver, seconds, red, green, blue);

	egg.setFillColor(sf::Color(red, green, blue));
	if(restarter == true)
	clock.restart();
	directionOfBack = CreateTail(x, y, direction, checkGameOver, false);
   	//---------------------------------------------------------------------------
	if((x % 25 == 0)&&(y % 25 == 0)){
		//------------------------------------------------------------
		  while(window.pollEvent(event)) {
			  if(event.type == sf::Event::Closed){
			size = Robjects.size();
			Robjects.erase(Robjects.begin()+0, Robjects.begin()+size);
				  window.close();

			  }
			  //---------------------------------------------------------------------
			  if((event.key.code == sf::Keyboard::P)&&(event.type == sf::Event::KeyPressed)){
			  window.draw(square);
			  for(int i = 0; i < Robjects.size(); i++)
			  window.draw(Robjects[i]);

			  if(eggs == true){}
			  else
			  window.draw(egg);

			  window.draw(score);
		      window.display();
				  while(true){
			  while(window.pollEvent(event)) {
			  if((event.key.code == sf::Keyboard::P)&&(event.type == sf::Event::KeyPressed))
				  breaker = true;
			  			  }
			  if(breaker == true)
				  break;
			  }
			  }
				  breaker = false;
				  event.key.code == sf::Keyboard::Q;
			  if(event.type == sf::Event::KeyPressed){
			  direction = move(direction, x, y);
			  square.setPosition(x,y);
			  }
		    }
		  }
	//---------------------------------------------------------------------------
		if((checkGameOver == true)||(outOfBounds == true))
		{
			  window.clear();
		      window.draw(square);
			  for(int i = 0; i < Robjects.size(); i++)
			  window.draw(Robjects[i]);
			  window.draw(egg);
			  window.draw(score);
			  window.draw(gameOver);
              exit();
			  window.clear();
			  menu();
			  break;
		 }
	//---------------------------------------------------------------------------
		  square.setPosition(x,y);
		  egg.setPosition(xBlock, yBlock);

		  	  window.clear();
		      window.draw(square);
			  for(int i = 0; i < Robjects.size(); i++)
			  window.draw(Robjects[i]);

			  if(eggs == false){
				    window.draw(egg);
			  }
			  else{
		for(int i = 0; i < 120; i++)
		window.draw(egger[i]); 
			  }
			  			if(invincible == true)
	{
			invincible3.setString("I");
	        invincible3.setFont(font);
	        invincible3.setCharacterSize(10);
	        invincible3.setColor(sf::Color(255, 255, 255));
	        invincible3.setPosition(xBlock + 4, yBlock + 4);
			window.draw(invincible3);
	}
			  window.draw(score);
		      window.display();
			  if(eggs == true)
				  sf::sleep(sf::seconds(.0009));
			  else
			 sf::sleep(sf::seconds(.003));
	  }
}
    //return 0;
}
bool addHighScores()
{
	std::ofstream outFile;
	std::ifstream inFile;
	std::string filename, name, finalName;
	std::vector<std::string> names;
	std::vector<int> scores;
	int score, count = 0, scored;
	bool enterIt = false, breaker = false;
	scored = score1;
	if(!font.loadFromFile("ethnocentric rg.ttf"))
	{
		//error...
	};

	filename = "Highscores.txt";

	inFile.open(filename);

	enterName.setString("Enter Name: ");
	enterName.setFont(font);
	enterName.setCharacterSize(20);
	enterName.setColor(sf::Color(100, 100, 100));
	enterName.setPosition(20, 357);

	nameEnter.setString(name);
	nameEnter.setFont(font);
	nameEnter.setCharacterSize(20);
	nameEnter.setColor(sf::Color(100, 100, 100));
	nameEnter.setPosition(225, 357);

	inFile>>name>>score;
	while(inFile){
      names.push_back(name);
      scores.push_back(score);
      inFile>>name>>score;
	}
//------------------------------------------------------------
if(scores.size() == 5){
for(int i = 0; i < scores.size(); i++) {
	if(scores[i] < scored)
		enterIt = true;
}
}
//------------------------------------------------------------
else {
	for(int i = 0; i < scores.size(); i++) {
	if(scores[i] < scored){
		enterIt = true;
		break;
	}
}
	if(enterIt != true) 
		enterIt = true;
}
//--------------------------------------------------------------
int i = 0;
name = "";

window.clear();
sf::Event event;

if(enterIt == true) {
	while(true){

		if(enterIt = true)
			window.clear();
		enterIt = false;
	nameEnter.setString(name);
	window.draw(enterName);
	window.draw(nameEnter);
	window.display();

	  while(window.pollEvent(event)) {
		   switch(event.type){
		   case sf::Event::TextEntered:

			   if(sf::Keyboard::isKeyPressed(sf::Keyboard::Return)){
				   breaker = true;
				   break;
			   }
			   else if((event.text.unicode != 8)&&(i < 9)){
				   name += (char)event.text.unicode;
				   i++;
				   finalName = name;
			   }

			   else if (event.text.unicode == '\b'){
                       name = name.substr(0, name.length() - 1);
					   window.clear();
					   enterIt = true;
					   if(!i == 0)
					   i--;
                }
    }
	
	  }
	  if(breaker == true)
			   break;
	}

if(names.size() == 0){
	names.push_back(finalName);
	scores.push_back(scored);
}
else if(names.size() < 5){
	for(int i = 0; i < names.size(); i++){
		if(scores[i] < scored){
			names.insert(names.begin() + i, finalName);
			scores.insert(scores.begin() + i, scored);
			enterIt = true;
			break;
		}
	}
	if(!enterIt == true){
	names.push_back(finalName);
	scores.push_back(scored);
	}
}
else {
		for(int i = 0; i < names.size(); i++){
		if(scores[i] < scored){
			names.insert(names.begin() + i, finalName);
			scores.insert(scores.begin() + i, scored);
			names.pop_back();
			scores.pop_back();
			break;
		}
		}
}

    inFile.close();
    std::ofstream outFile(filename);
	for(int i = 0; i < names.size(); i++)
		outFile<<names[i]<<" "<<scores[i]<<std::endl;
	outFile.close();
	return true;
}
return false;
outFile.close();
}
void highScores()
{
	bool exit1 = false;
	std::string filename, name1, score1;
	std::vector<sf::Text> HighNames;
	std::vector<sf::Text> HighScores;
	int x = 75, y = 100, x2 = 300, y2 = 100, size;
	filename = "Highscores.txt";
    std::ifstream inFile(filename);

	if(!font.loadFromFile("ethnocentric rg.ttf"))
	{
		//error...
	};

	int count = 0;
	inFile>>name1>>score1;

	while(count != 5){
	HighName.setString(convertInt(count + 1) + ".  " + name1);
	HighName.setFont(font);
	HighName.setCharacterSize(20); 
	HighName.setColor(sf::Color(50, 50, 50));
	HighName.setPosition(x, y + (50 * count));

	HighScore.setString(score1);
	HighScore.setFont(font);
	HighScore.setCharacterSize(20);
	HighScore.setColor(sf::Color(50, 50, 50));
	HighScore.setPosition(x2, y2 + (50 * count));

	HighScoreTitle.setString("High Scores");
	HighScoreTitle.setFont(font);
	HighScoreTitle.setCharacterSize(50);
	HighScoreTitle.setColor(sf::Color(130, 30, 30));
	HighScoreTitle.setPosition(50, 20);

	menuButton.setString("Menu");
	menuButton.setFont(font);
	menuButton.setCharacterSize(20);
	menuButton.setColor(sf::Color(100, 100, 100));
	menuButton.setPosition(100, 357);

	sf::RectangleShape menu(sf::Vector2f(80, 40));
	menu.setFillColor(sf::Color(255, 0, 0));
	menu.setOutlineThickness(4);
    menu.setOutlineColor(sf::Color(255, 100, 0));
	menu.setPosition(100, 350);

		HighNames.push_back(HighName);
		HighScores.push_back(HighScore);
		count++;
		name1 = "";
		score1 = "";
		inFile>>name1>>score1;
	}
	
	size = HighNames.size();

window.clear();
window.draw(HighScoreTitle);
window.draw(HighNames[0]);
window.draw(HighScores[0]);
window.draw(HighNames[1]);
window.draw(HighScores[1]);
window.draw(HighNames[2]);
window.draw(HighScores[2]);
window.draw(HighNames[3]);
window.draw(HighScores[3]);
window.draw(HighNames[4]);
window.draw(HighScores[4]);
window.draw(menuButton);
window.display();
while(true){
		  while(window.pollEvent(event)) {
			  			  if(event.type == sf::Event::Closed){
			size = Robjects.size();
			Robjects.erase(Robjects.begin()+0, Robjects.begin()+size);
				  window.close();

			  }
			 if (sf::Mouse::isButtonPressed(sf::Mouse::Left)){
				 sf::Vector2i localPosition = sf::Mouse::getPosition(window);
				 x = localPosition.x;
				 y = localPosition.y;
				 if((x > 100) && (x < 180) && (y < 390) && (y > 350))
					exit1 = true;
			 }
		  }
			 if(exit1 == true)
				 break;
	}
inFile.close();
window.clear();
}
示例#23
0
void OknoPostaci::rysujStatystyki(sf::Text text)
{
		float y = 330.;
		float x = 10.;

		text.setCharacterSize(25U);
		text.setPosition(x, y);
		text.setString("Imie: " + postac->imie);
		okno.draw(text);
		text.setPosition(x, y+=35);
		text.setString("Klasa: " + postac->klasa_postaci);
		okno.draw(text);
		text.setPosition(x, y += 35);
		text.setString("Zdrowie: " + to_string(postac->zdrowie) + " / " + to_string(postac->getMaxZdrowie()));
		okno.draw(text);
		text.setPosition(x, y += 35);
		text.setString("Wytrzymalosc: " + to_string(postac->wytrzymalosc) + " / " + to_string(postac->getMaxWytrzymalosc()));
		okno.draw(text);

		text.setCharacterSize(20U);
		text.setPosition(x, y += 55);
		text.setString("Atak: " + to_string(postac->getAtak()));
		okno.draw(text);
		text.setPosition(x + 180, y);
		text.setString("Obrona: " + to_string(postac->getObrona()));
		okno.draw(text);
		text.setPosition(x, y += 25);
		text.setString("Szybkosc: " + to_string(postac->getSzybkosc()));
		okno.draw(text);
		text.setPosition(x + 180, y);
		text.setString("Szczescie: " + to_string(postac->getSzczescie()));
		okno.draw(text);
		text.setPosition(x, y += 25);
		text.setString("Zasieg: " + to_string(postac->getZasieg()));
		okno.draw(text);
		text.setPosition(x + 180, y);
		text.setString("Obrazenia: " + to_string(postac->getObrazenia()));
		okno.draw(text);
		if (postac->klasa_postaci == "lucznik")
		{
			text.setPosition(x, y += 25);
			text.setString("Posiadane strzaly: " + to_string(postac->strzaly));
			okno.draw(text);
		}
	
}
void menu()
{
	int x, y;
	bool exit = false, high = false;
		if(!font.loadFromFile("ethnocentric rg.ttf"))
	{
		//error...
	};
		window.clear();
	sf::RectangleShape menuSquare(sf::Vector2f(550, 450));
	menuSquare.setFillColor(sf::Color(75, 75, 75));
	menuSquare.setOutlineThickness(4);
    menuSquare.setOutlineColor(sf::Color(50, 50, 50));
	menuSquare.setPosition(25, 25);

	sf::RectangleShape play(sf::Vector2f(80, 40));
	play.setFillColor(sf::Color(255, 0, 0));
	play.setOutlineThickness(4);
    play.setOutlineColor(sf::Color(255, 50, 0));
	play.setPosition(100, 350);

	playButton.setString("Play");
	playButton.setFont(font);
	playButton.setCharacterSize(20);
	playButton.setColor(sf::Color(30, 30, 30));
	playButton.setPosition(100, 357);

		sf::RectangleShape High(sf::Vector2f(90, 40));
	High.setFillColor(sf::Color(255, 0, 0));
	High.setOutlineThickness(4);
    High.setOutlineColor(sf::Color(255, 50, 0));
	High.setPosition(300, 350);

	HighButton.setString("High\nScores");
	HighButton.setFont(font);
	HighButton.setCharacterSize(15);
	HighButton.setColor(sf::Color(30, 30, 30));
	HighButton.setPosition(300, 350);

	snake.setString("SNAKE 2.0");
	snake.setFont(font);
	snake.setCharacterSize(50);
	snake.setColor(sf::Color(30, 30, 255));
	snake.setPosition(80, 75);

    CreatedBy.setString("Created by\nBrandon Aderholdt\nJanuary 10th, 2014");
	CreatedBy.setFont(font);
	CreatedBy.setCharacterSize(15);
	CreatedBy.setColor(sf::Color(30, 30, 70));
	CreatedBy.setPosition(140, 200);

	window.clear();
	while(true){
		sf::Event event;
		  while(window.pollEvent(event)) {
			  if(event.type == sf::Event::Closed)
				  window.close();

			 if (sf::Mouse::isButtonPressed(sf::Mouse::Left)){
				 sf::Vector2i localPosition = sf::Mouse::getPosition(window);
				 x = localPosition.x;
				 y = localPosition.y;
				 if((x > 100) && (x < 180) && (y < 390) && (y > 350))
				 {
					exit = true;
				 }
				 if((x > 300) && (x < 390) && (y < 390) && (y > 350))
				 {
					 high = true;
				 }
		       }
	         }
		 
		  	  window.draw(menuSquare);
			  window.draw(High);
		      window.draw(HighButton);
			  window.draw(play);
			  window.draw(playButton);
			  window.draw(snake);
			  window.draw(CreatedBy);
		      window.display();

			  if(high == true){
				  highScores();
				  window.clear();
				  high = false;
				  	HighButton.setString("High\nScores");
	HighButton.setFont(font);
	HighButton.setCharacterSize(15);
	HighButton.setColor(sf::Color(30, 30, 30));
	HighButton.setPosition(300, 350);

	snake.setString("SNAKE 2.0");
	snake.setFont(font);
	snake.setCharacterSize(50);
	snake.setColor(sf::Color(30, 30, 255));
	snake.setPosition(80, 75);

	playButton.setString("Play");
	playButton.setFont(font);
	playButton.setCharacterSize(20);
	playButton.setColor(sf::Color(30, 30, 30));
	playButton.setPosition(100, 357);

	CreatedBy.setString("Created by\nBrandon Aderholdt\nJanuary 10th, 2014");
	CreatedBy.setFont(font);
	CreatedBy.setCharacterSize(15);
	CreatedBy.setColor(sf::Color(30, 30, 70));
	CreatedBy.setPosition(140, 200);
			  }

			  if(exit == true){
				  for(int i = 4; i <= 0; i--){
				  play.setOutlineThickness(i);
				  sf::sleep(sf::seconds(1));
			      window.draw(play);
		          window.display();
				  }
			  break;
			  }
	}
}
bool updateGame(int& x, int& y, int& xBlock, int& yBlock, int& direction, int& directionOfBack,
	            bool checkGameOver, float timer, int& red, int& green, int& blue)
{
	bool bump = false;
	int timing, xNew, yNew, size;

	keepMoving(direction, x, y);
	square.setPosition(x,y);
	elapsedInvincible = clockInvincible.getElapsedTime();

	if(invincible1 == true){

	if((invincible1 == true)&&(timeInv - elapsedInvincible.asSeconds() <= 0)){
		invincible1 = false;
		for(int i = 0; i < Robjects.size(); i++){
    Robjects[i].setFillColor(sf::Color(61, 245, 0));
	Robjects[i].setOutlineThickness(2);
    Robjects[i].setOutlineColor(sf::Color(42, 160, 0));
		}
	}

	else if((invincible1 == true)&&(timeInv - elapsedInvincible.asSeconds() < 5)){
		timing = (timeInv - elapsedInvincible.asSeconds());
		if(timing > 1){
	 setColor(counter, 1);
		}
		else if (timing > 2){
	 setColor(counter, 2);
		}
		else if(timing > 3){
     setColor(counter, 3);
		}
		else if(timing > 4){
     setColor(counter, 4);
		}
		counter++;
	}

	else if(invincible1 == true){
		for(int i = 0; i < Robjects.size(); i++){
	Robjects[i].setFillColor(sf::Color(200, 50, 50));
	Robjects[i].setOutlineThickness(2);
    Robjects[i].setOutlineColor(sf::Color(40, 40, 40));
		}
	}

	}
	else{
		for(int i = 0; i < Robjects.size(); i++){
    Robjects[i].setFillColor(sf::Color(61, 245, 0));
	Robjects[i].setOutlineThickness(2);
    Robjects[i].setOutlineColor(sf::Color(42, 160, 0));
		}
	}

	if(eggs == true){
	if(timeInv - elapsedInvincible.asSeconds() <= 0){
		size = egger.size();
		eggs = false;
		egger.erase(egger.begin()+0, egger.begin()+size);
		window.clear();
	}
	else{
	for(int i = 0; i < 120; i++){
	sf::Vector2f position = egger[i].getPosition();	
	xNew = position.x;
	yNew = position.y;
	bump = checkBlock(x, y, xNew, yNew);
	if(bump == true){
	    score1 = score1 + 10;
	    text = convertInt(score1);
	    score.setString(text);
		egger[i].setPosition(-50, -50);
		bump = false;
		break;
	}
	}
	}
}

else{
	bump = checkBlock(x, y, xBlock, yBlock);
	if ((bump == true)&&(timer > 200)){
		if(invincible == true){
			invincible1 = true;
			invincible = false;
			elapsedInvincible = clockInvincible.getElapsedTime();
			timeInv = 15 + elapsedInvincible.asSeconds();
		}

		if((eggs1 == true)){
			sf::RectangleShape square1(sf::Vector2f(sizex, sizey));
			    square1.setFillColor(sf::Color(175, 175, 175));
	            square1.setOutlineThickness(2);
                square1.setOutlineColor(sf::Color(30, 80, 30));

					for(int j = 0; j < 10; j++){
				for(int i = 0; i < 12; i++){
					square1.setPosition((i*50) + 10, (j*50) + 10);
					egger.push_back(square1);
				}
					}
		
					eggs = true;
					eggs1 = false;
			elapsedInvincible = clockInvincible.getElapsedTime();
			timeInv = 3 + elapsedInvincible.asSeconds();
		}

		BlockSpawn(xBlock, yBlock);
		if(eggs == true){
		}
		else
		egg.setPosition(xBlock,yBlock);
		directionOfBack = CreateTail(x, y, direction, checkGameOver, true);

		if(killTail == true){
			size = Robjects.size();
			if(size > 5)
			Robjects.erase(Robjects.begin()+size -5, Robjects.begin()+size);
			else if(size == 4)
			Robjects.erase(Robjects.begin()+size -4, Robjects.begin()+size);
			else if(size == 3)
            Robjects.erase(Robjects.begin()+size -3, Robjects.begin()+size);
			else if(size == 2)
			Robjects.erase(Robjects.begin()+size -2, Robjects.begin()+size);
			else if(size == 1)
            Robjects.erase(Robjects.begin()+size -1, Robjects.begin()+size);
			else
			{}
			killTail = false;
		}

		if(red == 150)
		    score1 = score1 + 10;
		else if(red == 200)
			score1 = score1 + 20;
		else if(red == 40)
			score1 = score1 + 30;
		else if(red == 204)
			score1 = score1 + 40;
		else if(red == 41)
			score1 = score1 + 50;
		else if(red == 205)
			score1 = score1 + 80;

		setEgg(red, green, blue);
		text = convertInt(score1);
	    score.setString(text);

		return true;
         }
	return false;
}
}
示例#26
0
void Entity::setTextName(const sf::String& textName, const sf::Font& font, unsigned int characterSize = 30) {
	mTextName.setString(textName);
	mTextName.setFont(font);
	mTextName.setCharacterSize(characterSize);
}
int mainMenu(sf::Text title, sf::Font font, sf::RenderWindow& window)
{
  // create and load texture and sprite to be drawn to the screen
  sf::Texture earth;
  if(!earth.loadFromFile("img/earth2.png")) cout << "Error, could not load earth.png" << endl;

  sf::Sprite earthSprite;
  earthSprite.setTexture(earth);
  earthSprite.setPosition(20, 100);

  // set title color, position, size and style
  title.setColor(sf::Color::Black);
  title.setString("B i g   C o u n t r y   Q u i z");
  title.setPosition(5, 5);
  title.setCharacterSize(25);
  title.setStyle(sf::Text::Bold);

  // menu text objects:
  sf::Text world("World", font);
  world.setColor(sf::Color::Black);
  world.setPosition(400, 140);

  sf::Text continent("Continent Mode", font);
  continent.setColor(sf::Color::Black);
  continent.setPosition(400, 200);

  sf::Text practice("Practice", font);
  practice.setColor(sf::Color::Black);
  practice.setPosition(400, 260);

  sf::Text exit("Exit", font);
  exit.setColor(sf::Color::Black);
  exit.setPosition(400, 320);

  while(window.isOpen())
  {
    sf::Event event;
    while (window.pollEvent(event)) // process events
    {
      // QUIT
      if(event.type == sf::Event::Closed)
      {
          window.close(); // close the window
          return 0;
      }

      // if the left mouse button is clicked, run these checks
      if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
      {
        // if the user clicks world mode
        if(isTextClicked(world, window))
          return 1;

        // if the user clicks practice
        if(isTextClicked(practice, window))
          return 3;

        // if the user clicks exit
        if(isTextClicked(exit, window))
        {
          window.close();
          return 0;
        }
      }
    }

    window.clear(sf::Color(255, 255, 235, 0));

    window.draw(title);
    window.draw(world);
    window.draw(continent);
    window.draw(practice);
    window.draw(exit);
    window.draw(earthSprite);
    window.display();
  }

  return false;
}
示例#28
0
int PlayerConnection::Run(sf::RenderWindow &mainWin, int screen)
{
	
	if (!this->font.loadFromFile("resources/fonts/" + DEFAULT_FONT))
	{
		std::cerr << "Error loading the font " + DEFAULT_FONT << std::endl;
		system("pause");
		return (-1);
	}

	this->initializeItems();
	
	sf::Event Event;

	//Boucler tant que la fenêtre n'est pas fermée
    while (true)
    {
		bool usernameIsValid = username.size() > 3;

        //Vérifier les événements
        while (mainWin.pollEvent(Event))
        {
            if (Event.type == sf::Event::Closed)
            {
                return (-1);
            }

			if (Event.type == sf::Event::TextEntered) //On veut seulement les caracteres ASCII
			{	
				bool isANumber = (Event.text.unicode >= 48 && Event.text.unicode <= 57);
				bool isALetter = (Event.text.unicode >= 65 && Event.text.unicode <= 90) || (Event.text.unicode >= 97 && Event.text.unicode <= 122);

				if (username.size() < 13 && (isANumber || isALetter))
				{
					username += static_cast<char>(Event.text.unicode);
				}
			}

			if (Event.type == sf::Event::KeyReleased && Event.key.code == sf::Keyboard::BackSpace)
			{
				username = "";
			}

            if (Event.type == sf::Event::MouseButtonReleased)
            {
				sf::Vector2f mouse_coords = sf::Vector2f(Event.mouseButton.x, Event.mouseButton.y);

				if (intersects(mouse_coords, start.getGlobalBounds()) && usernameIsValid)
				{
					return (MENU);        
				}
				else if	(intersects(mouse_coords, nameContainer.getGlobalBounds()))
				{
					username = "";
				}
			}

            if (Event.type == sf::Event::MouseMoved)
            {
				sf::Vector2f mouse_coords = sf::Vector2f(Event.mouseMove.x, Event.mouseMove.y);
				if (intersects(mouse_coords, start.getGlobalBounds()))
				{
					start.setColor(sf::Color::Red);
					start.setScale(1.05, 1.05);
				}
				else
				{
					start.setColor(sf::Color::White);
					start.setScale(1,1);
				}
			}

			nameDisplay.setString(username);
        }

        mainWin.clear();

		mainWin.draw(spriteLogo);

		mainWin.draw(text);

		if (!usernameIsValid)
		{
			nameContainer.setOutlineColor(sf::Color::Red);
		}
		else
		{
			nameContainer.setOutlineColor(sf::Color::White);
		}

		mainWin.draw(nameContainer);

		mainWin.draw(nameDisplay);

		mainWin.draw(start);

        mainWin.display();
    }

    //On ne devrait jamais se rendre jusqu'à ce point, mais juste au cas, on ferme l'application.
    return (-1);
}
bool exit()
{
	int x, y, size;
	bool exit1 = false;

	if(addHighScores() == true)
		return true;

	if(!font.loadFromFile("ethnocentric rg.ttf"))
	{
		//error...
	};
	gameOver.setString("Game Over");
	gameOver.setFont(font);
	gameOver.setCharacterSize(50);
	gameOver.setColor(sf::Color(200, 200, 0));
	gameOver.setPosition(85, 200);

	sf::RectangleShape exit2(sf::Vector2f(80, 40));
	exit2.setFillColor(sf::Color(255, 0, 0));
	exit2.setOutlineThickness(4);
    exit2.setOutlineColor(sf::Color(255, 100, 0));
	exit2.setPosition(250, 350);

	sf::RectangleShape menu(sf::Vector2f(80, 40));
	menu.setFillColor(sf::Color(255, 0, 0));
	menu.setOutlineThickness(4);
    menu.setOutlineColor(sf::Color(255, 100, 0));
	menu.setPosition(100, 350);

	exitButton.setString("Exit");
	exitButton.setFont(font);
	exitButton.setCharacterSize(20);
	exitButton.setColor(sf::Color(30, 30, 30));
	exitButton.setPosition(260, 357);

	menuButton.setString("Menu");
	menuButton.setFont(font);
	menuButton.setCharacterSize(20);
	menuButton.setColor(sf::Color(30, 30, 30));
	menuButton.setPosition(100, 357);
	        window.draw(gameOver);
			  window.draw(exit2);
			  window.draw(menu);
			  window.draw(gameOver);
			  window.draw(exitButton);
			  window.draw(menuButton);
		      window.display();
	while(true){
		  while(window.pollEvent(event)) {
			  if(event.type == sf::Event::Closed)
				  window.close();

			 if (sf::Mouse::isButtonPressed(sf::Mouse::Left)){
				 sf::Vector2i localPosition = sf::Mouse::getPosition(window);
				 x = localPosition.x;
				 y = localPosition.y;
				 if((x > 100) && (x < 180) && (y < 390) && (y > 350)){
					exit1 = true;
				 }
				 else if((x > 250) && (x < 330) && (y < 390) && (y > 350)){
			size = Robjects.size();
			Robjects.erase(Robjects.begin()+0, Robjects.begin()+size);
				 window.close();
				 }
		       }
	         }

			  if(exit1 == true)
				  break;

			  size = Robjects.size();
			Robjects.erase(Robjects.begin()+0, Robjects.begin()+size);
	}
	window.clear();
	return true;
}
示例#30
0
void GameBoardDrawer::drawBoard()
{
	int positionX, positionY;
	Ball ball;
	Player player[2];

	// draw ball
	sharedMemory.getCurrentState(ball, player[0], player[1]);

	ball.getPosition(positionX, positionY);
	window->clear(sf::Color::Black);

	sf::CircleShape circle(circleRadius);
	circle.setPosition(positionX - circleRadius, positionY - circleRadius);
	circle.setFillColor(sf::Color::Green);
	window->draw(circle);

	// draw bottom platform
	player[0].getPosition(positionX, positionY);

	sf::RectangleShape bottomPlayer(sf::Vector2f(platformWidth, platformHeight));
	bottomPlayer.setPosition(positionX - platformWidth/2, positionY - platformHeight/2);
	bottomPlayer.setFillColor(sf::Color::Red);
	window->draw(bottomPlayer);

	// draw top platform
	player[1].getPosition(positionX, positionY);

	sf::RectangleShape topPlayer(sf::Vector2f(platformWidth, platformHeight));
	topPlayer.setPosition(positionX - platformWidth/2, positionY - platformHeight/2);
	topPlayer.setFillColor(sf::Color::Yellow);
	window->draw(topPlayer);

	// draw borders
	sf::RectangleShape topBorder(sf::Vector2f(windowWidth, 2*borderSize));
	topBorder.setPosition(0, -borderSize);
	topBorder.setFillColor(sf::Color::Blue);
	window->draw(topBorder);

	sf::RectangleShape leftBorder(sf::Vector2f(2*borderSize, windowHeight));
	leftBorder.setPosition(-borderSize, 0);
	leftBorder.setFillColor(sf::Color::Blue);
	window->draw(leftBorder);

	sf::RectangleShape bottomBorder(sf::Vector2f(windowWidth, 2*borderSize));
	bottomBorder.setPosition(0, windowHeight-borderSize);
	bottomBorder.setFillColor(sf::Color::Blue);
	window->draw(bottomBorder);

	sf::RectangleShape rightBorder(sf::Vector2f(2*borderSize, windowHeight));
	rightBorder.setPosition(windowWidth-borderSize, 0);
	rightBorder.setFillColor(sf::Color::Blue);
	window->draw(rightBorder);

	static sf::Font font;
	static bool fontLoaded = false;
	if (!fontLoaded && font.loadFromFile("dreamorphans.ttf"))
	{
		std::cout << "font loaded" << std::endl;
		fontLoaded = true;
	}
	
	if (fontLoaded)
	{
		static int score = 0;
		player[0].getScore(score);

		char buffer[32];
		sprintf(buffer, "score: %d", score);

		static sf::Text text;
		text.setFont(font);
		text.setString(buffer);
		text.setCharacterSize(24);
		text.setColor(sf::Color::White);
		text.setStyle(sf::Text::Bold);
		text.setPosition(20,20);
		window->draw(text);

		player[1].getScore(score);
		sprintf(buffer, "score: %d", score);
		text.setString(buffer);
		text.setPosition(20,windowHeight-54);
		window->draw(text);
	}

	window->display();
}