c++ - C++ SFML 操纵杆输入
问题描述
我正在使用 SFML 和 C++ 进行大学结对编程项目,以制作两人 2D 游戏。
TL;博士:
如何让Xbox360Controller
游戏中的任何地方都可以访问实例?[请记住,如果您建议Singleton Pattern
将连接两个控制器]。
我们有一个从大学课程中编写的引擎,我们制作了诸如ResourceManager
, SceneGraph
, CommandQueue
, StateStack
,Input Action bindings
等之类的东西。
引擎功能分为update()
,draw()
和handleEvent()
单独handleRealtimeInput()
的Player.cpp
.
我在我的Github中编写并重新编写了一个Xbox360Controller
类(暂时忽略方法)。static GetController()
最初,dustinfreeman 的 GitHub中的原始类使用了一种Singleton
模式,但我们需要允许连接 2 个控制器。
我不确定我是否应该仍然使用 Singleton 类型的类,还是应该Xbox360Controller
为特定玩家创建一个实例。
目前,我正在尝试使用第二个实现,但是Application.cpp
当我在构造函数中创建实例时,我希望整个应用程序访问控制器事件,如下所示:
Application::Application()
:mWindow(sf::VideoMode(1024, 768), "Audio", sf::Style::Close)
, mTextures()
, mFonts()
, mPlayer()
, mMusic()
, mSounds()
, mStateStack(State::Context(mWindow, mTextures, mFonts, mPlayer, mMusic, mSounds))
, mStatisticsText()
, mStatisticsUpdateTime()
, mStatisticsNumFrames(0)
{
mWindow.setKeyRepeatEnabled(false);
mFonts.load(FontIDs::Main, "Media/Sansation.ttf");
mTextures.load(TextureIDs::TitleScreen, "Media/Textures/TitleScreen.png");
mTextures.load(TextureIDs::Buttons, "Media/Textures/Buttons.png");
registerStates();
mStateStack.pushState(StateIDs::Title);
// This line creates an instance for a controller if a controller is connected
if (sf::Joystick::Count > 0)
{
std::cout << "Joystick Connected." << std::endl;
// This instance should be available everywhere
Xbox360Controller controller(0);
}
}
// This runs in the game-loop
void Application::processInput()
{
sf::Event event;
while (mWindow.pollEvent(event))
{
if (sf::Joystick::isConnected(0))
{
// Do something [For testing], eventually, pass the event into StateStack
}
mStateStack.handleEvent(event);
if (event.type == sf::Event::Closed)
{
mWindow.close();
}
}
}
圣诞快乐。
解决方案
推荐阅读
- reactjs - 使用 CSS 隐藏反应选择菜单
- c++ - 实例化抽象类,然后用特定的派生类重新声明?
- python - 无法理解 write() 格式(它是如何工作的)
- xml - Mulesoft 3 DataWeave - 按任意长度分割字符串
- javascript - 我怎样才能使这个函数确保返回的坐标与其他坐标的距离最小?
- python-3.x - 如何使用停在边缘的相机使视差滚动正常工作pygame
- performance - Mariadb 10.0 和 10.4 之间的性能问题
- vba - 在 VBA 中使用 Selenium 选择/取消选择网页上的一组复选框
- javascript - 使用变量名访问对象中的属性时出现问题
- powershell - 使用可变源名称的 Powershell 复制项