Can someone help a 'noob'? What's wrong with this?

Nibbie
Posts: 3
Joined: 2009.11
Post: #1
A crash occurs on the line I've indicated in bold - that's as much as I can figure out.
This is my first attempt at anything on the Mac - this would work okay in Windows but how come this doesn't work in xCode?
Someone please help, I'm going nuts!

Thanks

RagingAvatar

#include "defines.h"
#include <stdio.h>
#include <stdlib.h>

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glBegin(GL_QUADS);
glVertex2f( 0.0f, 0.0f);
glVertex2f(128.0f, 0.0f);
glVertex2f(128.0f, 128.0f);
glVertex2f( 0.0f, 128.0f);
glEnd();

glutSwapBuffers();
}

void reshape(int width, int height)
{
glViewport(0, 0, width, height);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, width, 0, height);
glMatrixMode(GL_MODELVIEW);
}

void idle(void)
{
glutPostRedisplay();
}

int main(int argc, char** argv)
{
glutInit(&argc, argv);

// Make a marker to indicate the start of output
printf("**********\n");

// Print out application name and version
printf(APP_NAME);
printf(" ");

char szTemp[100];
sprintf(szTemp, "%f", APP_VER);

printf(szTemp);
printf("\n");

// Print out OpenGL information
const GLubyte * glubTemp;
glubTemp = glGetString(GL_VERSION);
printf((char*)glubTemp);
//

glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);

glutInitWindowSize(APP_WIDTH, APP_HEIGHT);

glutCreateWindow(APP_NAME);

glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(idle);

glutMainLoop();

return EXIT_SUCCESS;
}
Quote this message in a reply
Oldtimer
Posts: 834
Joined: 2002.09
Post: #2
Hi, welcome to the forum and all that! Smile

You're hitting a subtle difference between Windows GLUT and Mac GLUT: on Mac, the GL context isn't created until the window is. So, when you call glGetString, the GL stack isn't active in your program. Move the glGetString calls to run after you call glutCreateWindow, and it runs. Smile

Second, please do avoid the word 'noob' and the like. Smile We're trying (yeah, right...) to keep a professional (yeah, right) façade and to be welcoming to new people (yeah, right...). Wink No harm done, you couldn't know - just wanted to point it out. Smile

Please post back if you need a clarification or more help! Smile
Quote this message in a reply
Moderator
Posts: 450
Joined: 2003.08
Post: #3
Stick with Ivan (Fenris), he can help you out a lot, he did me. Also, listen to all the advice people give here.

[offtopic]
We should make a script that changes anytime someone writes "noob" to "beginner". Noob has negative connotations, and beginner is a better word to use.
[/offtopic]
Quote this message in a reply
Nibbie
Posts: 3
Joined: 2009.11
Post: #4
Thank you for such a warm welcome!
I also agree that 'n00b' is a nasty term - I used it tongue-in-cheek - it's nice to find a serious forum!

Anyway, back to 'business'..

I changed the guts of Main() to look like this:
printf(szTemp);
printf("\n");

glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);

glutInitWindowSize(APP_WIDTH, APP_HEIGHT);

glutCreateWindow(APP_NAME);

// Note GL context isn't created until after glutCreateWindow under Mac OS X.
// Referencing the context before here will cause a crash under Mac OS X.

// Print out OpenGL information
const GLubyte * glubTemp;
glubTemp = glGetString(GL_VERSION);
printf((char*)glubTemp);
//


But still I get a crash of EXC_BAD_ACCESS..
What gives?

Thanks for all the help so far guys!
Quote this message in a reply
Nibbie
Posts: 3
Joined: 2009.11
Post: #5
Forgive me!
I indeed am a lowly beginner and have jumped the gun!

It works perfectly Smile

Thanks guys Smile
Quote this message in a reply
Oldtimer
Posts: 834
Joined: 2002.09
Post: #6
Quote:I used it tongue-in-cheek
I realized that you did. Wink

Quote:I indeed am a lowly beginner and have jumped the gun!
Study hard and eat your vegetables, and you'll be like me in no-time! Err... Ninja

Jokes aside, happy to help. Good luck with your endavours! Smile
Quote this message in a reply
spaceb
Unregistered
 
Post: #7
Avatar: This is a problem I've had with OpenGL in general. A lot of times, instead of giving you a nice error, it just crashes or acts as if no error happened. It can lead to a lot of headaches...
Quote this message in a reply
Oldtimer
Posts: 834
Joined: 2002.09
Post: #8
A good tip in that case is to end your rendering loop with a line that checks for glGetError and logs it if it is non-zero. That way, when you do something illegal your log will be filled with error messages. By doing it this way, you'll see right away that the error is caused by whatever you did during this compile-run cycle, instead of "somewhere in the program".

Also, launch the application through the OpenGL Profiler with the breakpoint "On Error" turned on. This will stop the application and tell you where and why an error occurred. Smile
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  What's wrong with my glutSolidTeapot? TomorrowPlusX 4 5,830 Aug 7, 2008 10:27 AM
Last Post: arekkusu
  Lighting a sphere with OpenGL lights (normals wrong?) cecilkorik 3 8,310 Dec 27, 2007 02:40 PM
Last Post: _ibd_
  What could be wrong here? Jones 5 2,934 Jul 1, 2006 08:52 PM
Last Post: Jones
  Texture Color is wrong DesertPenguin 7 4,170 Feb 14, 2006 09:04 PM
Last Post: DesertPenguin
  Whats wrong with my collision code? Jake 6 3,074 Sep 11, 2003 12:44 PM
Last Post: MacFiend