【发布时间】:2013-03-22 06:03:24
【问题描述】:
我的目标是一款游戏,稍后也会在 MacOS 和 iOS 上运行。
第一种方法是在主循环中使用 GLUT:
int main ( int argc, char * argv [] )
{
// initialize glut
glutInit ( &argc, argv );
glutInitDisplayMode ( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
glutInitWindowSize ( 500, 500 );
// create window
glutCreateWindow ( "Simple GLUT Example" );
// register handlers
glutDisplayFunc ( display );
glutReshapeFunc ( reshape );
glutKeyboardFunc ( key );
glutMouseFunc ( mouse );
glutMotionFunc ( motion );
glutIdleFunc ( animate );
glutMainLoop ();
return 0;
}
这可能是个不错的决定,但我现在没有经验,我如何捕捉用户交互(触摸、鼠标移动等)
我正在研究的第二种方法是使用 NSOpenGLView 的子类:
- (void) setupRenderTimer
{
NSTimeInterval timeInterval = 0.005;
renderTimer = [ NSTimer scheduledTimerWithTimeInterval:timeInterval
target:self
selector:@selector( updateGLView: )
userInfo:nil repeats:YES ];
[ [ NSRunLoop currentRunLoop ] addTimer:renderTimer
forMode:NSEventTrackingRunLoopMode ];
[ [ NSRunLoop currentRunLoop ] addTimer:renderTimer
forMode:NSModalPanelRunLoopMode ];
}
/*
* Called by the rendering timer.
*/
- (void) updateGLView:(NSTimer *)timer
{
if( glView != nil )
[ glView drawRect:[ glView frame ] ];
}
- (void) awakeFromNib
{
[ NSApp setDelegate:self ]; // We want delegate notifications
renderTimer = nil;
//[self makeFirstResponder:self ];
glView = [ [ MyOpenGLView alloc ] initWithFrame:[ self.myGLview frame ]
colorBits:16 depthBits:16 fullscreen:FALSE ];
if( glView != nil )
{
[ self.window.contentView addSubview:glView ];
[ self.window makeKeyAndOrderFront:self ];
[ self setupRenderTimer ];
}
else
[ self createFailed ];
}
最后,我喜欢保留 Cocoa IB 的优势(在 IB 中构建所有控件并保持它在 OpenGL 上下文中的优先级)。在这种情况下,直到现在我还没有找到一个解决方案,它在 MacOS 下运行良好,我可以在 OpenGL 上下文之前使用可可 IB 元素。在 IB 上下文中,总是预先考虑所有元素,使用以下方法破解:
const GLint aValue = 1;
[[self openGLContext] setValues:&aValue
forParameter:NSOpenGLCPSurfaceOpacity];
给我很好的结果设置背景颜色:
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f ); // Black background
不起作用:
我想问什么: - 以任何方式以第二种方式取得成功(性能原因可能对我未来至关重要,游戏将拥有大的 3d 世界)。如果是,您能否分享一些有用的示例或编写我当前错误的修复程序。
如果第一种方式只有一种方式,我是一个很好的代码组织,可以在一个代码中支持 iOS 和 osx 平台(也写一些代码示例)。 GLUT 是否可以很好地控制 iOS 的触摸,难以取代所有可可触摸功能,如长按 e.t.c
我很清楚,我需要做一个特定于平台的更改,但也许你可以分享一些经验,如何更好、更简单地完成它。
附:我知道我可以使用 GLKit,但不幸的是现在它在 MacOS 上工作得非常糟糕,有已知的错误。
【问题讨论】:
-
您可能在gamedev.stackexchange.com 上运气更好,但是您的问题范围相当广泛,您需要将其缩小到更简洁的范围。
-
感谢建议,我可以保留相同的详细信息吗?我猜有些用户只能回答问题的一部分,我会批准只回答那部分。
-
如果您想提出多个问题,请将它们分成较短的独立问题。见:sscce.org
-
"第一种方法是在主循环中使用 GLUT:" 如果您正在制作游戏,则不应使用 GLUT。
-
@NicolBolas 我必须使用什么?我目前正在使用书中的示例