Untitled

 avatar
user_1601351
plain_text
a year ago
4.9 kB
2
Indexable
Never
//menu.h
#ifndef _MENU_H
#define _MENU_H
#include <SFML/Graphics.hpp>
#include "chessGame.h"
class Menu {
private:
	sf::RenderWindow& m_window;
	sf::Font m_font;
	sf::Text m_newGameText;
	sf::Text m_playWithAIText;
	sf::Text m_exitText;
	sf::Text m_lanTEXT;
	sf::RectangleShape backGround;
	sf::Texture mainTexture;

	void handleEvents();
	void draw();
	bool isGameStarted;
public:
	int x;
	Menu(sf::RenderWindow& window);
	void run();

};

#endif 

//menu.cpp
#include "menu.h"

#include<iostream>
using namespace std;

Menu::Menu(sf::RenderWindow& window) : m_window(window), isGameStarted(false) {

    
    if (!m_font.loadFromFile("Textures//arial.ttf")) {
        std::cout << "Handle font loading error" << std::endl;
    }

    m_newGameText.setString("TWO PLAYER");
    m_playWithAIText.setString("PLAY COMPUTER");
    m_lanTEXT.setString("LAN");
    m_exitText.setString("EXIT");

    m_newGameText.setFont(m_font);
    m_playWithAIText.setFont(m_font);
    m_lanTEXT.setFont(m_font);
    m_exitText.setFont(m_font);


    m_newGameText.setCharacterSize(30);
    m_playWithAIText.setCharacterSize(30);
    m_lanTEXT.setCharacterSize(30);
    m_exitText.setCharacterSize(30);

    m_newGameText.setPosition(300, 200);
    m_playWithAIText.setPosition(300, 250);
    //m_lanTEXT.setPosition(300,270);
    m_exitText.setPosition(300, 300);
}

void Menu::run() {
    x = 0; // chua con mode, 2 la exit
    sf::Event event;
    ChessGame chess(sf::Color(0xf3bc7aff), sf::Color(0xae722bff));
    backGround.setSize(sf::Vector2f(841, 1500));
    mainTexture.loadFromFile("Textures//chess2.png");
    backGround.setTexture(&mainTexture);


    while (m_window.isOpen()) {
        m_window.draw(backGround);
        if (x == 0) {
            handleEvents();
            
            draw();
        }
        
        if (isGameStarted) {
            m_window.close();
            x = 1;
        }
    }
    sf::RenderWindow window(sf::VideoMode(768, 512), "Chess", sf::Style::Titlebar | sf::Style::Close);

    while (window.isOpen()) {
        if (x == 2) {
            window.close();
        }

        while (window.pollEvent(event)) {

            cout << event.type << " " << sf::Event::Closed << " " << window.pollEvent(event) << endl;

            if (event.type == sf::Event::MouseButtonPressed) {
                if (event.mouseButton.button == sf::Mouse::Left) {
                    if ((0 <= event.mouseButton.x) && (event.mouseButton.x <= 512) && (0 <= event.mouseButton.y) && (event.mouseButton.y <= 512)) {
                        unsigned int buttonPos((event.mouseButton.x / 64) + ((event.mouseButton.y / 64) * (8 * (512 / window.getSize().y))));

                        if (!chess.getSelected())
                            chess.selectPiece(buttonPos);
                        else
                            chess.moveSelected(buttonPos);
                    }
                    else if ((517 <= event.mouseButton.x) && (event.mouseButton.x <= 763) && (5 <= event.mouseButton.y) && (event.mouseButton.y <= 45)) {
                        chess.restart();
                    }
                }
                //window.pollEvent(event);
            }
            //

        }
        window.clear();
        window.draw(chess);
        window.display();
    }

}



void Menu::handleEvents() {
    sf::Event event;
    while (m_window.pollEvent(event)) {
        if (event.type == sf::Event::Closed) {
            m_window.close();
        }

        if (event.type == sf::Event::MouseButtonPressed) {
            sf::Vector2i mousePos = sf::Mouse::getPosition(m_window);

            if (m_newGameText.getGlobalBounds().contains(mousePos.x, mousePos.y)) {
                // Handle "Chơi mới" option clicked
                // Start a new game or transition to game setup screen
                isGameStarted = true;
                x = 4;
            }
            else if (m_playWithAIText.getGlobalBounds().contains(mousePos.x, mousePos.y)) {
                // Handle "Chơi với máy" option clicked
                // Transition to AI game mode setup screen
                isGameStarted = true;
                x = 3;
            }
            else if (m_exitText.getGlobalBounds().contains(mousePos.x, mousePos.y)) {
                m_window.close();
                x = 2;
            }
            /*else if (m_lanTEXT.getGlobalBounds().contains(mousePos.x, mousePos.y)) {
                isGameStarted = true;
                x = 5;
            }*/
        }
    }
}

void Menu::draw() {
    m_window.clear();

    // Draw menu options

    m_window.draw(backGround);
    m_window.draw(m_newGameText);
    m_window.draw(m_playWithAIText);
    m_window.draw(m_exitText);
    m_window.display();
}