forked from NaiJi/project-kyoku
Implement test main menu state
This commit is contained in:
parent
686107b215
commit
325d49270d
|
@ -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)
|
||||
|
|
|
@ -7,9 +7,10 @@
|
|||
|
||||
class GUIState : public sf::Drawable
|
||||
{
|
||||
public:
|
||||
virtual ~GUIState() = default;
|
||||
|
||||
virtual void input(const sf::Event& event, std::stack<std::shared_ptr<GUIState>>& 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;
|
||||
};
|
||||
|
|
|
@ -4,6 +4,8 @@
|
|||
#include "classicgame/classicgame.h"
|
||||
#include "classicgame/classicgraphicsmanager.h"
|
||||
|
||||
#include "gui/mainmenu.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
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<MainMenu>(_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();
|
||||
}
|
||||
|
|
|
@ -12,6 +12,7 @@ void ClassicDyingAnimationScenario::launch(const std::shared_ptr<ClassicSprite>
|
|||
|
||||
void ClassicDyingAnimationScenario::update(const microsec& music_offset)
|
||||
{
|
||||
(void) music_offset;
|
||||
_sprite->update();
|
||||
}
|
||||
|
||||
|
|
|
@ -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<void(void)> callback)
|
||||
{
|
||||
|
||||
}
|
||||
|
|
@ -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<Group>()),
|
||||
_game_window(game_window)
|
||||
{
|
||||
std::shared_ptr<Button> button_start = std::make_shared<Button>("Start");
|
||||
button_start->setRect(sf::FloatRect(140, 140, 500, 100));
|
||||
button_start->setCallback([&]()
|
||||
{
|
||||
_game_window.close();
|
||||
});
|
||||
|
||||
std::shared_ptr<Button> button_exit = std::make_shared<Button>("Exit");
|
||||
button_exit->setRect(sf::FloatRect(140, 140, 400, 100));
|
||||
button_exit->setPosition({240, 340});
|
||||
button_exit->setCallback([&]()
|
||||
{
|
||||
_game_window.close();
|
||||
});
|
||||
|
||||
_buttons->addChild(button_start);
|
||||
_buttons->addChild(button_exit);
|
||||
}
|
||||
|
||||
void MainMenu::input(const sf::Event& event)
|
||||
{
|
||||
_buttons->input(event);
|
||||
}
|
||||
|
||||
void MainMenu::update()
|
||||
{
|
||||
_buttons->update();
|
||||
}
|
||||
|
||||
void MainMenu::draw(sf::RenderTarget& target, sf::RenderStates states) const
|
||||
{
|
||||
target.draw(*_buttons, states);
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
#pragma once
|
||||
|
||||
#include "gui/state.h"
|
||||
#include <SFML/Graphics/RenderWindow.hpp>
|
||||
|
||||
class Group;
|
||||
|
||||
class MainMenu : public GUIState
|
||||
{
|
||||
public:
|
||||
MainMenu(sf::RenderWindow& game_window);
|
||||
virtual void input(const sf::Event& event) override;
|
||||
virtual void update() override;
|
||||
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
|
||||
|
||||
private:
|
||||
std::shared_ptr<Group> _buttons;
|
||||
sf::RenderWindow& _game_window;
|
||||
};
|
||||
|
|
@ -1,13 +0,0 @@
|
|||
#include "widget.h"
|
||||
|
||||
Widget::Widget(const std::shared_ptr<Widget>& parent) :
|
||||
_parent(parent)
|
||||
{
|
||||
if (_parent)
|
||||
_parent->addChild(shared_from_this());
|
||||
}
|
||||
|
||||
void Widget::addChild(const std::shared_ptr<Widget> &child)
|
||||
{
|
||||
_children.emplace_back(child);
|
||||
}
|
|
@ -0,0 +1,78 @@
|
|||
#include "button.h"
|
||||
|
||||
Button::Button(const std::string &text) :
|
||||
_pressed(false)
|
||||
{
|
||||
setText(text);
|
||||
_button_text.setFillColor(sf::Color::Black);
|
||||
_button_content.setFillColor(sf::Color::White);
|
||||
}
|
||||
|
||||
void Button::input(const sf::Event& event)
|
||||
{
|
||||
switch (event.type)
|
||||
{
|
||||
default:
|
||||
break;
|
||||
|
||||
case sf::Event::MouseButtonPressed:
|
||||
if (isUnderMouse(event.mouseButton.x, event.mouseButton.y))
|
||||
{
|
||||
_pressed = true;
|
||||
_button_content.setFillColor(sf::Color(155, 155, 155));
|
||||
}
|
||||
break;
|
||||
|
||||
case sf::Event::MouseButtonReleased:
|
||||
if (_pressed)
|
||||
{
|
||||
_button_content.setFillColor(sf::Color::White);
|
||||
_pressed = false;
|
||||
if (isUnderMouse(event.mouseButton.x, event.mouseButton.y))
|
||||
_on_click_callback();
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
Widget::input(event);
|
||||
}
|
||||
|
||||
void Button::update()
|
||||
{
|
||||
Widget::update();
|
||||
}
|
||||
|
||||
void Button::draw(sf::RenderTarget& target, sf::RenderStates states) const
|
||||
{
|
||||
target.draw(_button_content, states);
|
||||
target.draw(_button_text, states);
|
||||
|
||||
Widget::draw(target, states);
|
||||
}
|
||||
|
||||
void Button::setRect(const sf::FloatRect& rect)
|
||||
{
|
||||
_button_content.setPosition(rect.left, rect.top);
|
||||
_button_content.setSize({rect.width - rect.left, rect.height - rect.top});
|
||||
}
|
||||
|
||||
void Button::setPosition(const sf::Vector2f &position)
|
||||
{
|
||||
_button_content.setPosition(position);
|
||||
}
|
||||
|
||||
bool Button::isUnderMouse(int mouse_x, int mouse_y) const
|
||||
{
|
||||
return _button_content.getGlobalBounds().contains(mouse_x, mouse_y);
|
||||
}
|
||||
|
||||
void Button::setText(const std::string& text)
|
||||
{
|
||||
_button_text.setString(text);
|
||||
}
|
||||
|
||||
void Button::setCallback(std::function<void(void)> callback)
|
||||
{
|
||||
_on_click_callback = callback;
|
||||
}
|
||||
|
|
@ -13,8 +13,9 @@ public:
|
|||
virtual void input(const sf::Event& event) override;
|
||||
virtual void update() override;
|
||||
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
|
||||
virtual void setRect(const sf::IntRect& rect) override;
|
||||
virtual void setRect(const sf::FloatRect& rect) override;
|
||||
virtual void setPosition(const sf::Vector2f& position) override;
|
||||
virtual bool isUnderMouse(int mouse_x, int mouse_y) const override;
|
||||
|
||||
void setText(const std::string& text);
|
||||
void setCallback(std::function<void(void)> callback);
|
||||
|
@ -22,5 +23,8 @@ public:
|
|||
private:
|
||||
sf::RectangleShape _button_content;
|
||||
sf::Text _button_text;
|
||||
bool _pressed;
|
||||
|
||||
std::function<void(void)> _on_click_callback;
|
||||
};
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
#include "group.h"
|
||||
|
||||
void Group::input(const sf::Event& event)
|
||||
{
|
||||
Widget::input(event);
|
||||
}
|
||||
|
||||
void Group::update()
|
||||
{
|
||||
Widget::update();
|
||||
}
|
||||
|
||||
void Group::draw(sf::RenderTarget& target, sf::RenderStates states) const
|
||||
{
|
||||
Widget::draw(target, states);
|
||||
}
|
||||
|
||||
void Group::setRect(const sf::FloatRect& rect)
|
||||
{
|
||||
_rect = rect;
|
||||
}
|
||||
|
||||
void Group::setPosition(const sf::Vector2f& position)
|
||||
{
|
||||
_rect.top = position.y;
|
||||
_rect.left = position.x;
|
||||
}
|
||||
|
||||
bool Group::isUnderMouse(int mouse_x, int mouse_y) const
|
||||
{
|
||||
return _rect.contains(mouse_x, mouse_y);
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
#pragma once
|
||||
|
||||
#include "widget.h"
|
||||
|
||||
class Group : public Widget
|
||||
{
|
||||
public:
|
||||
virtual void input(const sf::Event& event) override;
|
||||
virtual void update() override;
|
||||
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
|
||||
virtual void setRect(const sf::FloatRect& rect) override;
|
||||
virtual void setPosition(const sf::Vector2f& position) override;
|
||||
virtual bool isUnderMouse(int mouse_x, int mouse_y) const override;
|
||||
|
||||
private:
|
||||
sf::FloatRect _rect;
|
||||
};
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
#include "widget.h"
|
||||
|
||||
void Widget::input(const sf::Event &event)
|
||||
{
|
||||
for (auto& child : _children)
|
||||
child->input(event);
|
||||
}
|
||||
|
||||
void Widget::update()
|
||||
{
|
||||
for (auto& child : _children)
|
||||
child->update();
|
||||
}
|
||||
|
||||
void Widget::draw(sf::RenderTarget& target, sf::RenderStates states) const
|
||||
{
|
||||
for (auto& child : _children)
|
||||
child->draw(target, states);
|
||||
}
|
||||
|
||||
void Widget::addChild(const std::shared_ptr<Widget> &child)
|
||||
{
|
||||
child->_parent = shared_from_this();
|
||||
_children.emplace_back(child);
|
||||
}
|
|
@ -4,12 +4,11 @@
|
|||
#include <memory>
|
||||
#include <SFML/Window/Event.hpp>
|
||||
#include <SFML/Graphics/Drawable.hpp>
|
||||
#include <SFML/Graphics/RenderTarget.hpp>
|
||||
|
||||
class Widget : public sf::Drawable, std::enable_shared_from_this<Widget>
|
||||
class Widget : public sf::Drawable, public std::enable_shared_from_this<Widget>
|
||||
{
|
||||
public:
|
||||
Widget(const std::shared_ptr<Widget>& parent = nullptr);
|
||||
|
||||
virtual ~Widget() = default;
|
||||
|
||||
virtual void input(const sf::Event& event) = 0;
|
||||
|
@ -17,11 +16,12 @@ public:
|
|||
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const = 0;
|
||||
virtual void setRect(const sf::FloatRect& rect) = 0;
|
||||
virtual void setPosition(const sf::Vector2f& position) = 0;
|
||||
virtual bool isUnderMouse(int mouse_x, int mouse_y) const = 0;
|
||||
|
||||
protected:
|
||||
void addChild(const std::shared_ptr<Widget>& child);
|
||||
|
||||
protected:
|
||||
std::vector<std::shared_ptr<Widget>> _children;
|
||||
const std::shared_ptr<Widget> _parent;
|
||||
std::shared_ptr<Widget> _parent;
|
||||
};
|
||||
|
Loading…
Reference in New Issue