Untitled
unknown
plain_text
a year ago
5.6 kB
8
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::Texture backGroundTexture; sf::Sprite backGroundSprite; 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) { backGroundSprite.setTexture(backGroundTexture); if(!backGroundTexture.loadFromFile("Textures//chess_background.png")){ std::cout << "Handle font loading error" <<std::endl; } backGroundTexture.loadFromFile("C:/SCS/Chess_Game/chess/chess/Textures/chess_background.png"); 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)); while (m_window.isOpen()) { 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(backGroundSprite); m_window.draw(m_newGameText); m_window.draw(m_playWithAIText); m_window.draw(m_exitText); m_window.display(); } //main /* This code file (main.cpp) contains the main function that runs the game all libraries that must be used have to be included here. */ #include <iostream> #include <SFML/Graphics.hpp> #include <SFML/Audio.hpp> #include <SFML/Network.hpp> #include "menu.h" #pragma comment (lib, "sfml-system-d.lib") #pragma comment (lib, "sfml-window-d.lib") #pragma comment (lib, "sfml-graphics-d.lib") #pragma comment (lib, "sfml-network-d.lib") int main(){ sf::RenderWindow window_menu(sf::VideoMode(800, 600), "Chess Game Menu"); Menu menu(window_menu); menu.run(); //setup audio /*sf::Music musicBackGround; if (!musicBackGround.openFromFile("Audio//Aoe.ogg")) { std::cout << "ERROR" << std::endl; } musicBackGround.setVolume(50);*/ //musicBackGround.play(); // window.setVerticalSyncEnabled(true); return 0; }