我正在为我的 C++ 应用程序中的窗口使用 GLFW,并且我正在尝试使用 GLFW 的回调获取输入事件。例如,这是您获取关键事件的方式:
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods){
// Do something with event data.
}
int main(){
// initialize window (I have no problems with this step)
glfwSetKeyCallback(window, key_callback);
// Now when a key is pressed in the window it will call this function.
}
问题:
在我的 key_callback我想使用在 key_callback 之外声明的变量功能,因为我不能改变 key_callback 的参数我无法传递对变量的引用。
现在,在上面给出的示例中,我可以简单地在 int main() 之外声明我的变量和两个key_callback和 int main()将有权访问变量的相同实例。
我想要的用途:
我想要一个WindowWrapper创建和管理 glfwWindow 的生命周期的类,这将包括设置事件回调。
WindowWrapper.h
// Includes
class WindowWrapper{
private:
Centrum* g_centrum_;
GLFWwindow* window_;
std::thread thread_;
public:
WindowWrapper();
WindowWrapper(Centrum* g_centrum);
~WindowWrapper();
private:
// Callbacks
static void key_callback(
GLFWwindow* window, int key, int scancode, int action, int mods
);
};
WindowWrapper.cpp
WindowWrapper::WindowWrapper(Centrum* g_centrum){
g_centrum_ = g_centrum;
// Initialize window
glfwSetKeyCallback(window_, key_callback); // Problems
// Window loop and OpenGL stuff
}
WindowWrapper::~WindowWrapper(){
thread_.join(); // Don't worry about this, it works but, I will make it safer.
glfwDestroyWindow(window_);
printf("WindowWrapper Completely Destructed!\n"); // For testing purposes
}
void WindowWrapper::key_callback(
GLFWwindow* window, int key, int scancode, int action, int mods
){
// This function is declared static in the class declaration.
// And as a result I cannot use g_centrum_ since it is a non-static variable
// Essentially I want to be able to access g_centrum_ from this function
g_centrum_->input_eventmanager_->key_eventmanager_->
TriggerKeyEvent(key, action, mods);
}
我想到的第一种方法是传递对 g_centrum 的引用, 但 GLFW 不会对所有参数的任何偏差进行回调。
我的第二次尝试是在构造函数中声明和定义回调,但您不能那样做。
我的第三次尝试是制作 g_centrum_静态的,但我必须在构造函数之外给它引用,我认为这不是一个优雅的解决方案。
最佳答案
在注册回调之前,使用 glfwSetWindowUserPointer() 将包装器指针关联到窗口。当您的回调被调用时,您可以使用 glfwGetWindowUserPointer() 来检索它。 GLFW documentation 中描述了这些 API .
Window user pointer
Each window has a user pointer that can be set with glfwSetWindowUserPointer and fetched with glfwGetWindowUserPointer. This can be used for any purpose you need and will not modified by GLFW throughout the life-time of the window.
例如,您可以在 WindowWrapper 构造函数中执行此操作:
WindowWrapper::WindowWrapper(Centrum* g_centrum){
g_centrum_ = g_centrum;
// Initialize window first
...
// Now, associate the wrapper to the window
glfwSetWindowUserPointer(window_, this);
glfwSetKeyCallback(window_, key_callback); // Problems
// Window loop and OpenGL stuff
}
然后,在你的回调中:
void WindowWrapper::key_callback(
GLFWwindow* window, int key, int scancode, int action, int mods
){
void *data = glfwGetWindowUserPointer(window);
WindowWrapper *w = static_cast<WindowWrapper *>(data);
w->g_centrum_->input_eventmanager_->key_eventmanager_->
TriggerKeyEvent(key, action, mods);
}
关于c++ - 在不将数据作为参数传递的情况下授予静态函数访问数据的权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27596861/
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby中使用两个参数异步运行exe吗?我已经尝试过ruby命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何rubygems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除
我有一个包含模块的模型。我想在模块中覆盖模型的访问器方法。例如:classBlah这显然行不通。有什么想法可以实现吗? 最佳答案 您的代码看起来是正确的。我们正在毫无困难地使用这个确切的模式。如果我没记错的话,Rails使用#method_missing作为属性setter,因此您的模块将优先,阻止ActiveRecord的setter。如果您正在使用ActiveSupport::Concern(参见thisblogpost),那么您的实例方法需要进入一个特殊的模块:classBlah
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
这是在Ruby中设置默认值的常用方法:classQuietByDefaultdefinitialize(opts={})@verbose=opts[:verbose]endend这是一个容易落入的陷阱:classVerboseNoMatterWhatdefinitialize(opts={})@verbose=opts[:verbose]||trueendend正确的做法是:classVerboseByDefaultdefinitialize(opts={})@verbose=opts.include?(:verbose)?opts[:verbose]:trueendend编写Verb
我有一些Ruby代码,如下所示:Something.createdo|x|x.foo=barend我想编写一个测试,它使用double代替block参数x,这样我就可以调用:x_double.should_receive(:foo).with("whatever").这可能吗? 最佳答案 specify'something'dox=doublex.should_receive(:foo=).with("whatever")Something.should_receive(:create).and_yield(x)#callthere
我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以
我正在为一个项目制作一个简单的shell,我希望像在Bash中一样解析参数字符串。foobar"helloworld"fooz应该变成:["foo","bar","helloworld","fooz"]等等。到目前为止,我一直在使用CSV::parse_line,将列分隔符设置为""和.compact输出。问题是我现在必须选择是要支持单引号还是双引号。CSV不支持超过一个分隔符。Python有一个名为shlex的模块:>>>shlex.split("Test'helloworld'foo")['Test','helloworld','foo']>>>shlex.split('Test"
我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re