如何创建一个包装GLUT的类? [英] How to create a class to wrap GLUT?

查看:119
本文介绍了如何创建一个包装GLUT的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用OpenGL编写学校游戏。由于还会有更多类似的任务,因此我想为在OpenGL中做常见的事情制作一个小框架。我之前做过一些简单的游戏,通常将其分解为IO类以处理输入和绘制到屏幕上,主要游戏循环/逻辑的Game类,以及游戏中任何对象的类。

I'm writing a game for school in OpenGL. Since there will be several more similar assignments I want to make a small framework for doing common things in OpenGL. I have made a few simple games before and I usually break it down into an IO class to handle input and drawing to the screen, Game class for the main game loop/logic, and classes for whatever objects there are in the game.

在我使用SDL之前,我的问题是,这是在OpenGL中进行处理的正确方法吗?我已经遇到了一些麻烦。我希望我的IO类可以处理窗口的初始化,绘制场景以及单击鼠标。因此,构造函数如下所示:

Before I was using SDL, so my question is, is this the right way of going about this in OpenGL? I've already ran into some trouble. I want my IO class to handle initializing the window, drawing the scene, and mouse clicks. So the constructor looked like this:

IO::IO()
{
    currWindowSize[0] = DEF_WIDTH;
    currWindowSize[1] = DEF_HEIGHT;

    glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA );
    glutInitWindowPosition( INIT_WINDOW_POSITION[0], INIT_WINDOW_POSITION[1] );
    glutInitWindowSize( currWindowSize[0], currWindowSize[1] );
    glutCreateWindow( "TEST" );

    setUp();

    glutDisplayFunc(drawScene);
    glutMainLoop();
}

但是, drawScene 是一个类方法。有没有一种方法可以将类方法传递给 glutDisplayFunc()而不使其保持静态?

However, drawScene is a class method. Is there a way to pass a class method to glutDisplayFunc() without making it static?

推荐答案

不幸的是, glutDisplayFunc()不会采用 void * 指针,因此您可以伪造对象上下文。您将必须创建一个静态函数,该函数可以使用静态变量来调用正确的IO实例。

Unfortunately the glutDisplayFunc() doesn't take a void* pointer so you could've fake an object context. You will have to make a static function that can call into the correct IO instance using a static variable.

不过,我也发现您的模式有些麻烦。据我所知, glutMainLoop()在终止GLUT上下文之前永远不会返回,因此,实际上有一个构造器永远不会返回,因此您的程序流程是不合理的。您应该将该调用移到班级中单独的 run()方法中。

I see some slight trouble with your pattern also, though. As far as I know, glutMainLoop() never returns until you terminate the GLUT context, therefore you have a constructor that practically never returns, so your program flow is unreasonable. You should move that call into a separate run() method in your class.

(我个人将使用GLFW ,即使您必须编写mainloop,也可以避免GLUT整个回调过程混乱。)

(Personally I would use GLFW, which avoids the entire callback mess with GLUT, although you have to write your mainloop.)

这篇关于如何创建一个包装GLUT的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆