1

Even though I have no errors showing from the compiler, the program starts a console and shows the following:

Process returned -1073741819 (0xC0000005)   execution time : 2.266 s
Press any key to continue.

I don't know why. I have tried Visual Studio, Codeblocks, and DEV C++

#include <GL/glut.h>
#define RATIO 1.2
#define WW 100
#define WH (WW/RATIO)
#define HALFX ((int)(WW/2))
#define HALFY ((int)(WH/2))
#define deltat 0.001
int WindowWidth;
int WindowHeight;
void Display() {
    glLineWidth(4.0);
    float StartShape[12][2] = { {-15,-15},{-5,-15},{0,-5},{5,-15},{15,-15},{15,25},{5,25}, 
    {5,-5},{0,0},{-5,-5},{-5,25},{-15,25} };
    float EndShape[12][2] = { {-15,-15},{-5,-15},{-5,10},{0,0},{5,10},{5,-15},{15,-15}, 
    {15,25},{5,25},{0,15},{-5,25},{-15,25} };
    float IntermediateShape[12][2];
    float VertexColors[12][3] = { {1,0,0},{1,1,0},{1,0,1},{0,1,0},{0,1,1},{0,0,1},{1,0.5,0}, 
    {1,0,0.5},{0.5,1,0},{0.5,0,1},{1,0,0.5},{0,1,0.5} };
    static float Tween = 0.0 - deltat;

    if (Tween < 1) {
        Tween += deltat;
    }
    for (int i = 0; i < 12; i++) {
        IntermediateShape[i][0] = (1 - Tween) * StartShape[i][0] + Tween * EndShape[i][0];
        IntermediateShape[i][1] = (1 - Tween) * StartShape[i][1] + Tween * EndShape[i][1];
    }
    glVertexPointer(2, GL_FLOAT, 0, IntermediateShape);
    glColorPointer(3, GL_FLOAT, 0, VertexColors);
    for (int i = 0; i < 1000000; i++) {
        glClear(GL_COLOR_BUFFER_BIT);
        glDrawArrays(GL_LINE_LOOP, 0, 12);
        glutSwapBuffers();
        glutPostRedisplay();

    }
}

void InitGL() {
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(-HALFX, HALFX, -HALFY, HALFY);
    glMatrixMode(GL_MODELVIEW);
    glClearColor(0, 0, 0, 0);
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_COLOR_ARRAY);
    glShadeModel(GL_SMOOTH);
    glViewport(0, 0, WindowWidth, WindowHeight);
}
void Reshape(int w, int h) {
    glutReshapeWindow(w, (int)(w / RATIO));
    WindowWidth = w;
    WindowHeight = (int)(w / RATIO);
    InitGL();
}

int main(int& argc, char** argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
    WindowWidth = (int)(glutGet((GLenum)GLUT_SCREEN_WIDTH) * 0.4);
    WindowHeight = (int)(WindowWidth / RATIO);
    glutInitWindowSize(WindowWidth, WindowHeight);
    glutInitWindowPosition(((int)glutGet((GLenum)GLUT_SCREEN_WIDTH) * 0.1), 
       (glutGet((GLenum)GLUT_SCREEN_WIDTH) / 2) - (WindowHeight / 2));
    glutCreateWindow("Bilguun Erdenebaatar Tweening Midterm Exam");
    glutDisplayFunc(Display);
    glutReshapeFunc(Reshape);
    InitGL();
    glutMainLoop();
    return 0;
}
genpfault
  • 51,148
  • 11
  • 85
  • 139
  • 1
    `0xC0000005` means Access Violation. (I've seen this often enough to remember the value.) ;-) The most prominent reason is probably accessing an invalid pointer, i.e. it's a sign for Undefined Behavior in your code. Run this in the debugger and have a look for which function calls are involved when this happens. This might give you a hint where to look closer at. – Scheff's Cat Oct 28 '21 at 08:14
  • Btw. if I remember right, `Display()` (registered by `glutDisplayFunc(Display);`) is a function which is periodically called in the `glutMainLoop()`. To embed a rendering loop by yourself (`for (int i = 0; i < 1000000; i++) {`) is counter-productive. – Scheff's Cat Oct 28 '21 at 08:20
  • FYI: [SO: Understanding the relationship between glutDisplayFunc and glutPostRedisplay](https://stackoverflow.com/a/4206504/7478597) (I consider this as easier to understand than the "official" doc. [glutDisplayFunc](https://www.opengl.org/resources/libraries/glut/spec3/node46.html).) – Scheff's Cat Oct 28 '21 at 08:24

0 Answers0