From 325d49270db0bc19f92b4b669205c5f368ecdd02 Mon Sep 17 00:00:00 2001 From: NaiJi Date: Tue, 27 Jul 2021 21:18:37 +0300 Subject: [PATCH] Implement test main menu state --- CMakeLists.txt | 30 +++---- include/gui/state.h | 3 +- src/application.cpp | 13 +++- .../classicdyinganimationscenario.cpp | 1 + src/gui/button.cpp | 48 ------------ src/gui/mainmenu.cpp | 41 ++++++++++ src/gui/mainmenu.h | 20 +++++ src/gui/widget.cpp | 13 ---- src/gui/widgets/button.cpp | 78 +++++++++++++++++++ src/gui/{ => widgets}/button.h | 6 +- src/gui/widgets/group.cpp | 32 ++++++++ src/gui/widgets/group.h | 18 +++++ src/gui/widgets/widget.cpp | 25 ++++++ src/gui/{ => widgets}/widget.h | 10 +-- 14 files changed, 251 insertions(+), 87 deletions(-) delete mode 100644 src/gui/button.cpp create mode 100644 src/gui/mainmenu.cpp create mode 100644 src/gui/mainmenu.h delete mode 100644 src/gui/widget.cpp create mode 100644 src/gui/widgets/button.cpp rename src/gui/{ => widgets}/button.h (76%) create mode 100644 src/gui/widgets/group.cpp create mode 100644 src/gui/widgets/group.h create mode 100644 src/gui/widgets/widget.cpp rename src/gui/{ => widgets}/widget.h (71%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5d63dc0..72319ce 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,30 +4,30 @@ project(project-kyoku LANGUAGES CXX) set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD_REQUIRED ON) -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -g") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -Wextra -Wpedantic -g") set(CMAKE_THREAD_LIBS_INIT "-lpthread") SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread") set(CMAKE_HAVE_THREADS_LIBRARY 1) set(CMAKE_USE_WIN32_THREADS_INIT 0) set(CMAKE_USE_PTHREADS_INIT 1) set(THREADS_PREFER_PTHREAD_FLAG ON) -file(GLOB SOURCES "src/*.cpp" "src/classicgame/*.*" "src/classicgame/classicnotestate/*") +file(GLOB_RECURSE SOURCES "src/*.cpp" "src/*.h") # STATIC # # You need to build SFML from sources with cmake -#set(SFML_LIB_DIR -# ${CMAKE_SOURCE_DIR}/SFML-2.5.1/lib/libsfml-graphics.so.2.5 -# ${CMAKE_SOURCE_DIR}/SFML-2.5.1/lib/libsfml-system.so.2.5 -# ${CMAKE_SOURCE_DIR}/SFML-2.5.1/lib/libsfml-window.so.2.5 -# ${CMAKE_SOURCE_DIR}/SFML-2.5.1/lib/libsfml-audio.so.2.5) -#set(SFML_INCL_DIR ${CMAKE_SOURCE_DIR}/SFML-2.5.1/include) -#include_directories(${SFML_INCL_DIR} ${CMAKE_SOURCE_DIR}/include) -#add_executable(project-kyoku ${SOURCES}) -#target_link_libraries(project-kyoku ${SFML_LIB_DIR}) +set(SFML_LIB_DIR + ${CMAKE_SOURCE_DIR}/SFML-2.5.1/lib/libsfml-graphics.so.2.5 + ${CMAKE_SOURCE_DIR}/SFML-2.5.1/lib/libsfml-system.so.2.5 + ${CMAKE_SOURCE_DIR}/SFML-2.5.1/lib/libsfml-window.so.2.5 + ${CMAKE_SOURCE_DIR}/SFML-2.5.1/lib/libsfml-audio.so.2.5) +set(SFML_INCL_DIR ${CMAKE_SOURCE_DIR}/SFML-2.5.1/include) +include_directories(${SFML_INCL_DIR} ${CMAKE_SOURCE_DIR}/include) +add_executable(project-kyoku ${SOURCES}) +target_link_libraries(project-kyoku ${SFML_LIB_DIR}) # DYNAMIC # # You only need to install SFML from your package manager -find_package(SFML REQUIRED graphics window system) -add_executable(project-kyoku ${SOURCES}) -include_directories(${SFML_INCL_DIR} ${CMAKE_SOURCE_DIR}/include) -target_link_libraries(project-kyoku sfml-system sfml-audio sfml-graphics sfml-network) +#find_package(SFML REQUIRED graphics window system) +#add_executable(project-kyoku ${SOURCES}) +#include_directories(${SFML_INCL_DIR} ${CMAKE_SOURCE_DIR}/include) +#target_link_libraries(project-kyoku sfml-system sfml-audio sfml-graphics sfml-network) diff --git a/include/gui/state.h b/include/gui/state.h index f0f3610..398be7e 100644 --- a/include/gui/state.h +++ b/include/gui/state.h @@ -7,9 +7,10 @@ class GUIState : public sf::Drawable { +public: virtual ~GUIState() = default; - virtual void input(const sf::Event& event, std::stack>& states) = 0; + virtual void input(const sf::Event& event) = 0; virtual void update() = 0; virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const = 0; }; diff --git a/src/application.cpp b/src/application.cpp index 4a426d5..c5f59a9 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -4,6 +4,8 @@ #include "classicgame/classicgame.h" #include "classicgame/classicgraphicsmanager.h" +#include "gui/mainmenu.h" + #include const sf::Time TIME_PER_FRAME = sf::seconds(1.f / 90.f); @@ -16,12 +18,13 @@ Application::Application() : _game_window.setKeyRepeatEnabled(false); _game_window.setMouseCursorGrabbed(false); _game_window.setVerticalSyncEnabled(true); + + _states.push(std::make_shared(_game_window)); } void Application::run() { _game_window.display(); - _game->run(); exec(); } @@ -60,9 +63,11 @@ void Application::input() case sf::Event::KeyPressed: case sf::Event::KeyReleased: + case sf::Event::MouseButtonReleased: + case sf::Event::MouseButtonPressed: if (event.key.code == sf::Keyboard::Escape) _game_window.close(); - _game->input(PlayerInput{0, event}); + _states.top()->input(event); break; default: @@ -73,12 +78,12 @@ void Application::input() void Application::update() { - _game->update(); + _states.top()->update(); } void Application::draw() { _game_window.clear(); - _game->draw(); + _game_window.draw(*_states.top()); _game_window.display(); } diff --git a/src/classicgame/classicdyinganimationscenario.cpp b/src/classicgame/classicdyinganimationscenario.cpp index cae1aee..926611c 100644 --- a/src/classicgame/classicdyinganimationscenario.cpp +++ b/src/classicgame/classicdyinganimationscenario.cpp @@ -12,6 +12,7 @@ void ClassicDyingAnimationScenario::launch(const std::shared_ptr void ClassicDyingAnimationScenario::update(const microsec& music_offset) { + (void) music_offset; _sprite->update(); } diff --git a/src/gui/button.cpp b/src/gui/button.cpp deleted file mode 100644 index 49cc11c..0000000 --- a/src/gui/button.cpp +++ /dev/null @@ -1,48 +0,0 @@ -#include "button.h" - -Button::Button(const std::string &text) -{ - _button_text.setString(text); - _button_text.setFillColor(sf::Color::Black); - _button_content.setFillColor(sf::Color::White); -} - -void Button::input(const sf::Event& event) -{ - switch (event.type) - { - case sf::Event::MouseButtonPressed: - break; - } -} - -void Button::update() -{ - -} - -void Button::draw(sf::RenderTarget& target, sf::RenderStates states) const -{ - -} - -void Button::setRect(const sf::IntRect& rect) -{ - -} - -void Button::setPosition(const sf::Vector2f& position) -{ - -} - -void Button::setText(const std::string& text) -{ - -} - -void Button::setCallback(std::function callback) -{ - -} - diff --git a/src/gui/mainmenu.cpp b/src/gui/mainmenu.cpp new file mode 100644 index 0000000..818b217 --- /dev/null +++ b/src/gui/mainmenu.cpp @@ -0,0 +1,41 @@ +#include "mainmenu.h" +#include "widgets/button.h" +#include "widgets/group.h" + +MainMenu::MainMenu(sf::RenderWindow& game_window) : + _buttons(std::make_shared()), + _game_window(game_window) +{ + std::shared_ptr