iEpic
iEpic

Reputation: 95

OpenGL code Works in Windows but not Mac

My school uses windows computers but I only have a mac at home. The code runs fine in windows but for some reason I get errors running it in xCode. I wasn't sure which frameworks to include so I added GLKit, Cocoa, GLUT, and OpenGL. Am I missing something?? I would like to get Xcode setup so I can work on homework with the same code that will work on windows. Here is the code

#include <stdio.h>
#include <stdarg.h>
#include <math.h>
#define GL_GLEXT_PROTOTYPES
#ifdef __APPLE__
    #include <GLUT/glut.h>
#else
    #include <GL/glut.h>
#endif

#define GLUT_KEY_ESCAPE 27

const GLsizei windowWidth = 500;
const GLsizei windowHeight = 500;

GLfloat cubeRotateX = 45.0f;
GLfloat cubeRotateY = 45.0f;
bool keys[255];

GLvoid establishProjectionMatrix(GLsizei width, GLsizei height)
{
    glViewport(0, 0, width, height);

    glMatrixMode(GL_PROJECTION);

    glLoadIdentity();

    gluPerspective(45.0f, (GLfloat)width / (GLfloat)height, 0.1f, 200.0f);
}

GLvoid initGL(GLsizei width, GLsizei height)
{
    establishProjectionMatrix(width, height);

    glShadeModel(GL_SMOOTH);

    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);

    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
    glEnable(GL_PERSPECTIVE_CORRECTION_HINT);
}

GLvoid displayFPS(GLvoid)
{
    static long lastTime = glutGet(GLUT_ELAPSED_TIME);
    static int loops = 0;
    static GLfloat fps = 0.0f;

    int newTime = glutGet(GLUT_ELAPSED_TIME);

    if (newTime - lastTime > 100)
    {
        float newFPS = (float)loops / float(newTime - lastTime) * 1000.0f;
        fps = (fps + newFPS) / 2.0f;

        char title[80];
        sprintf_s(title, "OpenGL Demo - %.2f", fps);
        glutSetWindowTitle(title);

        lastTime = newTime;
        loops = 0;
    }
    loops++;
}

GLvoid drawScene(GLvoid)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glTranslatef(0, 0, -5.0f);
    glRotatef(cubeRotateX, 1.0, 0.0, 0.0);
    glRotatef(cubeRotateY, 0.0, 1.0, 0.0);

    glBegin(GL_QUADS);
    glColor3f(0.0f, 1.0f, 1.0f);
    glVertex3f( 1.0f,  1.0f, -1.0f);
    glVertex3f(-1.0f,  1.0f, -1.0f);
    glVertex3f(-1.0f,  1.0f,  1.0f);
    glVertex3f( 1.0f,  1.0f,  1.0f);

    glColor3f(0.0f, 1.0f, 0.0f);
    glVertex3f( 1.0f, -1.0f, -1.0f);
    glVertex3f(-1.0f, -1.0f, -1.0f);
    glVertex3f(-1.0f, -1.0f,  1.0f);
    glVertex3f( 1.0f, -1.0f,  1.0f);

    glColor3f(1.0f, 0.0f, 0.0f);
    glVertex3f( 1.0f,  1.0f, 1.0f);
    glVertex3f(-1.0f,  1.0f, 1.0f);
    glVertex3f(-1.0f, -1.0f, 1.0f);
    glVertex3f( 1.0f, -1.0f, 1.0f);

    glColor3f(1.0f, 1.0f, 0.0f);
    glVertex3f( 1.0f,  1.0f, -1.0f);
    glVertex3f(-1.0f,  1.0f, -1.0f);
    glVertex3f(-1.0f, -1.0f, -1.0f);
    glVertex3f( 1.0f, -1.0f, -1.0f);

    glColor3f(0.0f, 0.0f, 1.0f);
    glVertex3f(-1.0f,  1.0f,  1.0f);
    glVertex3f(-1.0f,  1.0f, -1.0f);
    glVertex3f(-1.0f, -1.0f, -1.0f);
    glVertex3f(-1.0f, -1.0f,  1.0f);

    glColor3f(1.0f, 0.0f, 1.0f);
    glVertex3f( 1.0f,  1.0f,  1.0f);
    glVertex3f( 1.0f,  1.0f, -1.0f);
    glVertex3f( 1.0f, -1.0f, -1.0f);
    glVertex3f( 1.0f, -1.0f,  1.0f);
    glEnd();

    glFlush();

    glutSwapBuffers();

    displayFPS();
}
GLboolean checkKeys(GLvoid)
{
    const GLfloat speed = 1.0f;

    if ( keys[GLUT_KEY_ESCAPE] )
        return true;

    if (keys[GLUT_KEY_LEFT])
        cubeRotateY -= speed;
    if (keys[GLUT_KEY_RIGHT])
        cubeRotateY += speed;
    if (keys[GLUT_KEY_UP])
        cubeRotateX -= speed;
    if (keys[GLUT_KEY_DOWN])
        cubeRotateX += speed;

    return false;
}
GLvoid timerLoop(int value)
{
    if (checkKeys())
        exit(0);
    glutPostRedisplay();

    glutTimerFunc(1, timerLoop, 0);
}



GLvoid keyboardCB(unsigned char key, int x, int y)
{
    keys[key] = true;
}
GLvoid keyboardUpCB(unsigned char key, int x, int y)
{
    keys[key] = false;
}
GLvoid keyboardSpecialCB(int key, int x, int y)
{
    keys[key] = true;
}
GLvoid keyboardSpecialUpCB(int key, int x, int y)
{
    keys[key] = false;
}




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

    glutInitDisplayMode(GLUT_DOUBLE);

    int windowID = glutCreateWindow("OpenGL Cube Demo");
    glutReshapeWindow(windowWidth, windowHeight);

    initGL(windowWidth, windowHeight);

    glutDisplayFunc(drawScene);

    glutKeyboardFunc(keyboardCB);
    glutKeyboardUpFunc(keyboardUpCB);
    glutSpecialFunc(keyboardSpecialCB);
    glutSpecialUpFunc(keyboardSpecialUpCB);

    glutTimerFunc(1, timerLoop, 0);

    glutMainLoop();
    return 0;

}

enter image description here enter image description here

Why does it work in visual studio and not in Xcode??

Upvotes: 0

Views: 1118

Answers (1)

Ken Thomases
Ken Thomases

Reputation: 90521

The errors from the compiler seem pretty clear. The superficial reason it doesn't work is because this code contains Windows-isms that are not portable.

  • C++ doesn't allow a typedef of void to indicate an empty parameter list. You have to use void itself. So, just changed those GLvoids to voids.

  • sprintf_s() is a Windows-specific function. You can replace it with snprintf(), but you have to add a parameter after the buffer which is the size of the buffer. So, replace this:

    sprintf_s(title, ...);
    

    with:

    snprintf(title, sizeof(title), ...);
    
  • The exit() function is available cross-platform, but you have to include the right header. Add #include <stdlib.h> near the top of your file.

Upvotes: 1

Related Questions