views:

298

answers:

3

I made a new project and pretty much copied this guide, but whenever I call any OpenGL function it the spot marked // Drawing code here it crashes. I have this there :

glViewport(0, 0, [self bounds].size.width, [self bounds].size.height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, [self bounds].size.width, [self bounds].size.height, 0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.375, 0.375, 0.0);

glClearColor(1, 1, 1, 1);
glClear(GL_COLOR_BUFFER_BIT);

glColor3f(0, 0, 0);

glBegin(GL_LINES);
glVertex2f(10, 10);
glVertex2f(300, 300);
glEnd();

glFlush();

But just calling one OpenGL function crashes it, reporting GDB: Program received signal: "EXC_BAD_ACCESS". First of all, if it did work, is that code good? Doing all that stuff at the top every time it gets called? Its supposed to be for 2d, with origin at top-left. And why is it crashing? I have CoreVideo and OpenGL linked and imported... I want to get a Cocoa application set up to use OpenGL so I can focus on the game code.

Thanks.

A: 

You could try replacing '[self bounds].size.width' with hard-coded numbers to see if that is part of the problem.

William Knight
No difference, it still crashes.
Mk12
I have done limited OpenGL development on the Mac, and it works. However, I use straight C/C++ and avoid all of the Objective-C and Mac OS/X GUI stuff as much as possible. I don't even build with the XCode IDE, just do generic command-line builds, specifying only '-framework AGL' and '-framework Carbon' args for the linker. I use SDL to do all the window and OpenGL initialization and it's a nice cross-platform KISS solution. Don't know if that might work better for you, you could try it.
William Knight
How could I do that except with Cocoa instead of Carbon? Isn't Carbon becoming deprecated (and no 64 bit)?
Mk12
A: 

The article you linked isn't really a complete project -- it's just a discussion and sample of using CVDisplayLink to drive the drawing loop. You'll probably do better by starting with Apple's OpenGL Programming Guide.

dodgio
A: 

Check out MyOpenGLView.m in Apple's sample code for GLFullScreen. They use CVDisplayLink successfully. The thing missing in "Driving OpenGL Rendering Loops" is the fact that you need to add a mutex to avoid two threads accessing the same opengl context.

Nick Sonneveld