c++ - OpenGL code Works in Windows but not Mac -
my school uses windows computers have mac @ home. code runs fine in windows reason errors running in xcode. wasn't sure frameworks include added glkit, cocoa, glut, , opengl. missing something?? xcode setup can work on homework same code work on windows. here 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; }
why work in visual studio , not in xcode??
the errors compiler seem pretty clear. superficial reason doesn't work because code contains windows-isms not portable.
c++ doesn't allow typedef of
void
indicate empty parameter list. have usevoid
itself. so, changedglvoid
svoid
s.sprintf_s()
windows-specific function. can replacesnprintf()
, have add parameter after buffer size of buffer. so, replace this:sprintf_s(title, ...);
with:
snprintf(title, sizeof(title), ...);
the
exit()
function available cross-platform, have include right header. add#include <stdlib.h>
near top of file.
Comments
Post a Comment