project-kyoku/src/impl/sfml/application/applicationsfml.cpp

188 lines
4.7 KiB
C++
Raw Normal View History

2021-12-29 15:59:18 +01:00
#include "applicationsfml.h"
#include "sfml/corefactorysfml.h"
#include "sfml/classicmode/classicfactorysfml.h"
#include <SFML/System/Clock.hpp>
const sf::Time TIME_PER_FRAME = sf::seconds(1.f / 90.f);
ApplicationSFML::ApplicationSFML(const std::shared_ptr<sf::RenderWindow>& render_window) :
_render_window(render_window)
{
_render_window->setFramerateLimit(60);
_render_window->setKeyRepeatEnabled(false);
_render_window->setMouseCursorGrabbed(false);
_render_window->setVerticalSyncEnabled(true);
_core_factory = std::make_shared<CoreFactorySFML>(_render_window);
_game_factory = std::make_shared<ClassicFactorySFML>(_render_window);
}
void ApplicationSFML::display()
{
_render_window->clear();
for (const auto& state : _state_stack)
state->display();
_render_window->display();
}
void ApplicationSFML::run()
{
sf::Clock timer;
sf::Time time_since_last_update = sf::Time::Zero;
while (_render_window->isOpen())
{
time_since_last_update += timer.restart();
sf::Event event;
while (_render_window->pollEvent(event))
{
switch(event.type)
{
case sf::Event::Closed:
_render_window->close();
break;
default:
Application::input(convert(event));
break;
}
}
bool isOneFramePassed = time_since_last_update >= TIME_PER_FRAME;
if (isOneFramePassed)
{
time_since_last_update -= TIME_PER_FRAME;
update(time_since_last_update.asMicroseconds());
display();
}
}
}
kku::SystemEvent ApplicationSFML::convert(const sf::Event& event) const
{
switch (event.type)
{
default:
break;
case sf::Event::Resized:
return kku::SystemEvent
{
kku::SystemEvent::Type::Resize,
kku::SystemEvent::Size
{
event.size.width,
event.size.height
}
};
case sf::Event::KeyPressed:
return kku::SystemEvent
{
kku::SystemEvent::Type::KeyPress,
kku::SystemEvent::Key
{
' ', // converter
event.key.alt,
event.key.control,
event.key.shift
}
};
case sf::Event::KeyReleased:
return kku::SystemEvent
{
kku::SystemEvent::Type::KeyRelease,
kku::SystemEvent::Key
{
' ', // converter
event.key.alt,
event.key.control,
event.key.shift
}
};
case sf::Event::MouseWheelScrolled:
return kku::SystemEvent
{
kku::SystemEvent::Type::MouseWheelScroll,
kku::SystemEvent::Mouse
{
kku::Point
{
event.mouseWheelScroll.x,
event.mouseWheelScroll.y
},
(event.mouseWheelScroll.delta > 0),
kku::SystemEvent::Mouse::Button::Wheel
}
};
case sf::Event::MouseMoved:
return kku::SystemEvent
{
kku::SystemEvent::Type::MouseMove,
kku::SystemEvent::Mouse
{
kku::Point
{
event.mouseMove.x,
event.mouseMove.y
},
false,
kku::SystemEvent::Mouse::Button::None
}
};
case sf::Event::MouseButtonPressed:
return kku::SystemEvent
{
kku::SystemEvent::Type::MousePress,
kku::SystemEvent::Mouse
{
kku::Point
{
event.mouseButton.x,
event.mouseButton.y
},
false,
((event.mouseButton.button == sf::Mouse::Button::Left)
? kku::SystemEvent::Mouse::Button::Left
: kku::SystemEvent::Mouse::Button::Right)
}
};
case sf::Event::MouseButtonReleased:
return kku::SystemEvent
{
kku::SystemEvent::Type::MouseRelease,
kku::SystemEvent::Mouse
{
kku::Point
{
event.mouseButton.x,
event.mouseButton.y
},
false,
((event.mouseButton.button == sf::Mouse::Button::Left)
? kku::SystemEvent::Mouse::Button::Left
: kku::SystemEvent::Mouse::Button::Right)
}
};
}
return kku::SystemEvent
{
kku::SystemEvent::Type::None,
kku::SystemEvent::None{}
};
}