I have a small game made in visual studio 2013, written in c++. It creates a window in which it draws several things and pops up the console I used to debug. The problem is: if I click on the "exit" button from the menu I introduced, all things good, the process is terminated as it should be; if I press the 'X' (close) button on top of the window, it closes the window, but the console still remains. As it should be excepted, not having the console enabled doesn't fix anything: there are no windows associated with the game, but the sound still plays and the process still exists in task manager. I've searched a lot about this and I couldn't find a solution for this issue.
I've tried with atexit, but the program doesn't go to the function written for atexit. Maybe I'm placing it wrong, I can't tell. This is what I've tried.
void close_win() {
if (engine)
engine->drop();
/*if (SC_CLOSE) {
cout << "sc_close";
_exit(0);
}*/
cout << "Ceva ";
exit(0);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(800, 600);
glutInitWindowPosition(100, 100);
glutCreateWindow("Aarghhh! O ramaaa !");
createMenu();
glClearColor(0.0, 0.0, 0.0, 0.0);
init();
glutReshapeFunc(reshape);
glutDisplayFunc(dreptunghi);
glutSpecialFunc(player);
glutMainLoop();
atexit(close_win);
return 0;
}
or
BOOL WINAPI ConsoleHandler(DWORD CEvent)
{
if (CEvent == CTRL_CLOSE_EVENT)
{
cout << "You are closing my program.";
}
return TRUE;
} if (SetConsoleCtrlHandler((PHANDLER_ROUTINE)ConsoleHandler, TRUE) == FALSE)
{
cout << "The handler is not going to work.";
}
or
if (glutGetWindow == 0)
exit(0);
The game was made without classes, using OpenGL and GLUT libraries, if it matters for anything.
Also, when I set a breakpoint to atexit and use step over, after 2 steps the debug stops with "sehprolg4.asm not found". Does any of this has to do with the fact that the function doesn't call close_win()?
Related
I want to create in OpenGL two differents windows but they should appears not together.
I mean: when I execute my code I visualize both of windows contemporarily.
My aim instead is to visualize at the beginning the first window, then the first window should be closed and the second will appear. My pseudocode is the following (this realeses only the two windows together ntemporarily):
int main(int argc, char** argv)
{
glutInit(&argc, argv);
initRendering();
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
//window1=
glutCreateWindow("First window");
glutDisplayFunc(drawScene1);
glutKeyboardFunc(handleKeypress);
glutReshapeFunc(handleResize);
cout << "Before inserting 'choice' I want to visualize the first window\n
After the insert of 'choice' I want to visualize the second window
and close the first one !";
cin >> choice;
//create the second window
//window2 =
glutCreateWindow("Second Window");
//define a window position for second window
glutPositionWindow(540, 40);
// register callbacks for second window, which is now current
glutReshapeFunc(handleResize);
glutDisplayFunc(drawScene2);
glutKeyboardFunc(handleKeypress);
glutMainLoop();
return 0;
}
The code is designed to load a .obj file, draw, rotate camera around mesh (this is ongoing) and display rotation value when the mouse button is clicked. I am using eclipse C++ IDE with FLTK. When the mouse button is clicked the window crashes. The error message when debugging is "no source available for gl_draw".
The code is as follows:
void onMouseButton(int button, int state, int x, int y)
{
switch(button){
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN){
// Print value on object
char s[40];
sprintf(s, "ROT=%.2f", g_rotation);
glRasterPos2f(60,40);
gl_draw(s, strlen(s));
}
}
}
The main code:
int main(int argc, char **argv)
{
Fl_Window win(WIDTH,HEIGHT, "Wavefront");
win.resizable(win);
win.show(argc, argv);
// Docs say to add glut subwindow /after/ calling win.show()
win.begin();
// initialize and run program
//glutInit(&argc, argv); // GLUT initialization
glutInitWindowSize(WIDTH-20, HEIGHT-20);
glutInitWindowPosition(10,10); // place inside parent window
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_MULTISAMPLE);
glutCreateWindow("Ear");
glutDisplayFunc(display); // register Display Function
glutIdleFunc( display ); // register Idle Function
glutMouseFunc( onMouseButton );
glutKeyboardFunc( keyboard ); // register Keyboard Handler
initialize();
obj.Load("Ear_obj.obj");
//gl_draw(s, strlen(s));
glutMainLoop();
win.end();// run GLUT mainloop
return 0;
}
If gl_draw(s, strlen(s)); is removed code run as expected , therefore loads the mesh, draws it, displays the mesh as the camera is rotated but would do nothing if the mouse is clicked.
Is the a way to display any text when the mouse is clicked or is there a step I am missing that is causing the error?
I've tried some OpenGL C++ training.
But I have a logic problem, how can I update my OpenGL Windows window.
It should draw text one, then delay 1-2sec, then draw text 2, but now it draws same time. Can anyone help or give a hint.
void text () {
wait(1);
Sleep(1000);
std::string text_one;
text_one = "Text 1";
glColor3f(1,01, 0);
drawText(text_one.data(), text_one.size(), 050, 150);
glutPostRedisplay();
wait (1)
std::string text_two;
text_two = "Text 2";
glColor3f(1,0, 0);
drawText(text_two.data(), text_two.size(), 250, 150);
}
and here the main
int main(int argc, char **argv) {
// init GLUT and create Window
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(640,640);
glutCreateWindow("Test 001");
// register callbacks
glutDisplayFunc(renderScene);
glutIdleFunc(text);
// enter GLUT event processing cycle
glutMainLoop();
return 1;
}
You should render in renderScene callback. It will be called automatically in you screen refresh rate. If you want some delay you need to implement it inside this callback (functions called from this callback).
So basically you need to re-render everything every 1/60 second.
If you want to implement easy delay you can do something like this:
void renderScene() {
time += deltaTime;
RenderText1();
if (time > delayTime)
RenderText2();
glutSwapBuffers();
}
I am trying to pause my GLUT program during its execution by pressing a key on the keyboard. It seems to not recognize my entry. Here are the relevant sections of my code:
static bool paused = false;
void handleKeypress(unsigned char key, //The key that was pressed
int x, int y) { //The current mouse coordinates
switch (key) {
case 27: //Escape key
exit(0); //Exit the program
case 'p':
paused = !paused;
break;
}
}
int main(int argc, char** argv) {
//Initialize GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(600, 400); //Set the window size
//Create the window
glutCreateWindow("Fractals in Motion");
initRendering(); //Initialize rendering
//Set handler functions for drawing, keypresses, and window resizes
if(!paused)
{
glutDisplayFunc(drawScene); //drawScene draws everything does the real work
glutTimerFunc(10, update, 0); //Add a timer
}
//glutKeyboardFunc(handleKeypress);
glutReshapeFunc(handleResize);
glutMainLoop(); //Start the main loop. glutMainLoop doesn't return.
return 0; //This line is never reached
}
I actually got the skeleton of this code from this very will written tutorial:
http://www.videotutorialsrock.com/opengl_tutorial/basic_shapes/home.php
However, I cannot seem to get the program to pause when I press the 'p' key. If you have better methods please let me know!
its not working because glutKeyboardFunc(handleKeypress) is commented out for some reason. uncomment and it should work.
Your program runs in two stages:
Initialization
Main loop
Everything before glutMainLoop is initialization, telling GLUT all the different settings and callbacks you want to use. During the main loop, GLUT will call all your callbacks and you can draw.
The problem is that you're setting paused during the main loop and checking it during the initialization. Since initialization always happens before the main loop, setting paused won't actually do anything.
The solution is to not rely on checking paused during initialization, but instead modify your callbacks to immediately return if paused is true.
# include<GL/glut.h>
void keys(unsigned char key, int x, int y)
{
if (key == 'a') paused = 1;
if (key == 'A') paused = 0;
glutPostRedisplay();
}
add this function in your program for keyboard function and wherever u are using glutPostRedisplay() in program
add
if(paused == 0)
above it
I have created a simple Visual Studio Express 2010 C++ project using GLUT and OpenGL,
it compiles and runs ok, except that the window it creates receives no events..
the close/minimize buttons don't do anything (not even mouseover) no context menu on the task bar on right click, and the window doesn't come to the foreground when clicked if partially covered.
The project is set up as a console application, I can close the program by closing the console.
I have this in main:
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitWindowSize(window_width, window_height);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutCreateWindow("MyApp");
glutIdleFunc(main_loop_function);
GLenum err = glewInit();
if (GLEW_OK != err)
{
/* Problem: glewInit failed, something is seriously wrong. */
fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
}
if (GLEW_VERSION_1_3)
{
fprintf(stdout, "OpenGL 1.3 is supported \n");
}
fprintf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
GL_Setup(window_width, window_height);
glutMainLoop();
}
You miss a display callback. Could you try:
void display();
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitWindowSize(window_width, window_height);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutCreateWindow("MyApp");
/// glutIdleFunc(main_loop_function);
glutDisplayFunc(display);
// ...
glutMainLoop();
}
void display(){
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glutSwapBuffers();
}
Also, your idle function seems bad, if you effectively loop inside that function. Glut is callback oriented. You don't need to create a loop, but rather rely on idle, mouse, keyboard, display and resize callbacks. If you don't do so, you are going to miss window manager events.
EDIT:
If you want to create an animation, you could use:
glutIdleFunc(idleFunc);
void idleFunc(){
glutPostRedisplay();
}