Fix on Mojave assignment-1
authorLuke Lau <luke_lau@icloud.com>
Sun, 30 Sep 2018 14:12:33 +0000 (15:12 +0100)
committerLuke Lau <luke_lau@icloud.com>
Sun, 30 Sep 2018 14:12:33 +0000 (15:12 +0100)
assignment1.cpp

index b3049f0583f956317d1b38e07ec0914aae1acb1f..887ffb0d9a2444a8dacce433606dca42b2aeed75 100644 (file)
@@ -17,6 +17,8 @@ using namespace std;
 GLuint* vaos;
 GLuint* progIds;
 
+bool hasDrawn = false;
+
 void display() {
        glClear(GL_COLOR_BUFFER_BIT);
        for (int i = 0; i < 2; i++) {
@@ -25,6 +27,10 @@ void display() {
                glDrawArrays(GL_TRIANGLES, 0, 3);
        }
        glutSwapBuffers();
+       if (!hasDrawn) {
+               glutPostRedisplay();
+               hasDrawn = true;
+       }
 }
 
 void attachShader(GLuint progId, const char* filePath, GLenum type) {
@@ -38,7 +44,8 @@ void attachShader(GLuint progId, const char* filePath, GLenum type) {
        ifstream file(filePath);
        stringstream buffer;
        buffer << file.rdbuf();
-       const char* contents = buffer.str().c_str();
+       string str = buffer.str();
+       const char* contents = str.c_str();
 
        glShaderSource(shader, 1, (const GLchar**)&contents, NULL);
        glCompileShader(shader);
@@ -155,17 +162,23 @@ void init() {
        validateProgram(progId2);
 }
 
+void keyboard(unsigned char key, int x, int y) {
+       glutPostRedisplay();
+}
+
 int main(int argc, char** argv) {
        glutInit(&argc, argv);
-       glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_3_2_CORE_PROFILE);
+       glutInitDisplayMode(GLUT_STENCIL|GLUT_SINGLE|GLUT_RGB|GLUT_3_2_CORE_PROFILE);
        glutInitWindowSize(800, 600);
-       glutCreateWindow("Hello Triangle");
+       int win = glutCreateWindow("Hello Triangle");
        glutDisplayFunc(display);
 
        glewInit();
        
        init();
 
+       glutKeyboardFunc(keyboard);
+
        glutMainLoop();
 
        return 0;