mirror of
https://github.com/SFML/SFML.git
synced 2024-11-24 20:31:05 +08:00
Add example raw_input
This commit is contained in:
parent
92bba1ed6f
commit
1e1c13b51d
@ -31,9 +31,11 @@ if(SFML_BUILD_GRAPHICS)
|
||||
|
||||
if(SFML_OS_WINDOWS)
|
||||
add_subdirectory(win32)
|
||||
add_subdirectory(raw_input)
|
||||
elseif(SFML_OS_LINUX OR SFML_OS_FREEBSD)
|
||||
if(NOT SFML_USE_DRM)
|
||||
add_subdirectory(X11)
|
||||
add_subdirectory(raw_input)
|
||||
endif()
|
||||
elseif(SFML_OS_MACOS)
|
||||
add_subdirectory(cocoa)
|
||||
|
7
examples/raw_input/CMakeLists.txt
Normal file
7
examples/raw_input/CMakeLists.txt
Normal file
@ -0,0 +1,7 @@
|
||||
# all source files
|
||||
set(SRC RawInput.cpp)
|
||||
|
||||
# define the raw_input target
|
||||
sfml_add_example(raw_input GUI_APP
|
||||
SOURCES ${SRC}
|
||||
DEPENDS SFML::Graphics)
|
72
examples/raw_input/RawInput.cpp
Normal file
72
examples/raw_input/RawInput.cpp
Normal file
@ -0,0 +1,72 @@
|
||||
////////////////////////////////////////////////////////////
|
||||
// Headers
|
||||
////////////////////////////////////////////////////////////
|
||||
#include <SFML/Graphics.hpp>
|
||||
|
||||
#include <vector>
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
/// Entry point of application
|
||||
///
|
||||
/// \return Application exit code
|
||||
///
|
||||
////////////////////////////////////////////////////////////
|
||||
int main()
|
||||
{
|
||||
// Create the main window
|
||||
sf::RenderWindow window(sf::VideoMode({800u, 600u}), "SFML Raw Mouse Input", sf::Style::Titlebar | sf::Style::Close);
|
||||
window.setVerticalSyncEnabled(true);
|
||||
|
||||
// Load the application font and pass it to the Effect class
|
||||
const auto font = sf::Font::loadFromFile("resources/tuffy.ttf").value();
|
||||
|
||||
// Create the mouse position text
|
||||
sf::Text mousePosition(font, "", 20);
|
||||
mousePosition.setPosition({400.f, 300.f});
|
||||
mousePosition.setFillColor(sf::Color::White);
|
||||
|
||||
// Create the mouse raw movement text
|
||||
sf::Text mouseRawMovement(font, "", 20);
|
||||
mouseRawMovement.setFillColor(sf::Color::White);
|
||||
|
||||
std::vector<std::string> log;
|
||||
|
||||
while (window.isOpen())
|
||||
{
|
||||
while (const auto event = window.pollEvent())
|
||||
{
|
||||
if (event.is<sf::Event::Closed>())
|
||||
{
|
||||
window.close();
|
||||
break;
|
||||
}
|
||||
|
||||
static const auto vec2ToString = [](const sf::Vector2i& vec2)
|
||||
{ return '(' + std::to_string(vec2.x) + ", " + std::to_string(vec2.y) + ')'; };
|
||||
|
||||
if (const auto* const mouseMoved = event.getIf<sf::Event::MouseMoved>())
|
||||
mousePosition.setString("Mouse Position: " + vec2ToString(mouseMoved->position));
|
||||
|
||||
if (const auto* const mouseMovedRaw = event.getIf<sf::Event::MouseMovedRaw>())
|
||||
{
|
||||
log.emplace_back("Mouse Movement: " + vec2ToString(mouseMovedRaw->delta));
|
||||
|
||||
if (log.size() > 24u)
|
||||
log.erase(log.begin());
|
||||
}
|
||||
}
|
||||
|
||||
window.clear();
|
||||
window.draw(mousePosition);
|
||||
|
||||
for (std::size_t i = 0; i < log.size(); ++i)
|
||||
{
|
||||
mouseRawMovement.setPosition({50.f, static_cast<float>(i * 20) + 50.f});
|
||||
mouseRawMovement.setString(log[i]);
|
||||
window.draw(mouseRawMovement);
|
||||
}
|
||||
|
||||
window.display();
|
||||
}
|
||||
}
|
BIN
examples/raw_input/resources/tuffy.ttf
Normal file
BIN
examples/raw_input/resources/tuffy.ttf
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user