Error when I close the OpenGL window

I use the following code with Borland C++ Builder 6. It works fine and does the following: When I press the button (Button1Click) it creates a separate OpenGL window, with background color blue. Now: When I want to close the window (no matter which) I get the following error : “access violation at 0x40009729: write of address 0x00030c0c. Process stopped” and the Compiler hangs up. Why is this and what`s wrong? When I run the .exe file of this Programm, I do not get a problem, when I quit it.


#include <vcl.h>
#pragma hdrstop
#include “Basic_U.h”
#include <GL/glut.h>
//-----------------------------------------------
#pragma package(smart_init)
#pragma resource “*.dfm”
TForm1 Form1;
//-----------------------------------------------
__fastcall TForm1::TForm1(TComponent
Owner)
: TForm(Owner)
{
}
//-----------------------------------------------
void RenderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}

void SetupRC(void)
{
glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
}
//-----------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutCreateWindow(“This is a OpenGL window”);
glutDisplayFunc(RenderScene);
SetupRC();
glutMainLoop();
}
//-----------------------------------------------

Is there any debugger with Borland products ? These typical memory craps aren’t a problem for a debugger of a compiler

Yes, I think so. But I have never used it (or another debugger). I am a beginner. What shall I try or do? Now I also have the same problem with the .exe file. Then I get this standard-window you`ll know, where I am asked to send the error message to microsoft.

By using the debugger you must run the application in debugging-mode, and just after shutdown program (and not debugging mode) the debugger will ask you if you want to breakpoint the bug that create crap-shutdown, I don’t know what’s possible to do with your debugger, but generally it show variables where values are strange and breakpoint function where it crack…

Understand. But I think the problem is somwhere else. If you look at the end of my code: You see GlutMainLoop(); This starts OpenGL in a neverending loop. So I guess, this is the problem. But what can I do, the GlutMainLoop() is needed to get OpenGL running? I start GlutMainLoop() with a Button Click from a window. Is this the way one does it?

Since you’re creating the OpenGL window at runtime, are you sure you’re deleting it? Usually, with glut, one uses exit(0) to exit the program, as glutMainLoop() never returns. I also think that glut redefines this function in glut.h.
So maybe you can call it on the form’s OnClose event or the OnCanClose (or something).

ok, I tried exit(0); with another Button-Click. It shuts down the whole Program. What does it realy in the program flow?

What does your code do ? I can’t see what you mean inside it.
Nevertheless, I’m almost sure you create a first opengl window, then on a click on this window you create another gl window. Isn’t it ?

You encounter context problems doing like that. You might encounter other problems depending on how you wrote your code. Ensure first the good contexts are current when you use one window or another one.

Well, indeed I am struggeling with these first steps. I figured out, that I reate new windows, when I put all the functions (glutCreateWindow(“foo”)…) in my ButtonCall. Then I tried to use one window and update the scenes. Lets say I have one Scene with Points and another one with Linse. I can bring either one in the one window. But I have to change sizes first, bevor the second one is drawn. What do I need to change? I thought glFlush() is all I need to update a scene. This seems to be wrong. Thats my code :

void Szene(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(7.0);
glBegin(GL_POINTS);
glColor3f(1.0, 0.0, 0.0);
glVertex2f(-0.5, -0.5);
glColor3f(0.0, 1.0, 0.0);
glVertex2f(-0.5, 0.5);
glColor3f(0.0, 0.0, 1.0);
glVertex2f(0.5, 0.5);
glColor3f(1.0, 1.0, 0.0);
glVertex2f(0.5, -0.5);
glEnd();
glFlush();
}

void Scene2(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON);
glColor3f(1.0, 0.0, 0.0);
glVertex2f(-0.4, -0.4);
glColor3f(0.0, 1.0, 0.0);
glVertex2f(-0.4, 0.4);
glColor3f(0.0, 0.0, 1.0);
glVertex2f(0.4, 0.4);
glColor3f(1.0, 1.0, 0.0);
glVertex2f(0.4, -0.4);
glEnd();
glFlush();
}

void InitOpenGL(void)
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); //black
glColor3f(1.0, 1.0, 1.0); //white
//View : Standard orthogonal / default:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-1.0, 1.0, -1.0, 1.0);
}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0,0);
glutCreateWindow(“OpenGL active”);
glutDisplayFunc(Scene);
InitOpenGL();
glutMainLoop();
}
//-----------------------------------------------

void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
exit(0);
}
//-----------------------------------------------

void __fastcall TForm1::Button2Click(TObject *Sender)
{
glutDisplayFunc(Scene2);
}
//-----------------------------------------------

Trivial, my dear Watson, trivial (Holmes grins)
Put a glutPostRedisplay(); after your glFlush();
It tells glut that the display has changed and needs to be redrawn.
Also if you move things around in your scene, you will need double buffers. You only need to change
glutInit and instead of GLUT_SINGLE aks for GLUT_DOUBLE. Normally you would need to swap buffers by hand, but I’m 99% sure that glutPostRedisplay() is kind enough to provide this.

Nice! Thank you. That worked. If you could help me one more time, I would be at the point where I can really start:

I would like to write different scenes (points, linse, curves, whatever…). But I dont want it all in my .cpp file. So Id like to make a file for each scene, and the include it. I tried to put it (void szene()…) in header (.h) files and #include them. But it did not work. How does such a structure have to look like? In case you use ICQ, my # is : 153-349-956

I’d say you would want this:
//scene 1
scene1.cpp

#include "scene1.h"

void scene1Display() {
// blabla code etc.
}
void scene1Reshape etc. you get the idea.

scene1.h

// fucntion prototypes
void scene1Display();
void scene1Reshape(..);
etc. as required.

//scene 2
scene2.cpp

#include "scene2.h"

void scene2Display() {
// blabla code etc.
}
void scene2Reshape etc. you get the idea.

scene2.h

// fucntion prototypes
void scene2Display();
void scene2Reshape(..);
etc. as required.

Then in main.cpp:
#include “scene1.h”
#include “scene2.h”

I think that should get you starting.
Remember, code goes into cpp files, functions prototypes on .h and then you include the .h in your main.cpp

PS You might like to put this (changed appropriately) above and below your header files (e.g. scene1.h) to avoid cyclical declarations:
#ifndef _SCENE1_H
#define _SCENE1_H

// function prototypes, structures etc.

#endif