Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 5 of 5

Thread: Problem with mainloop.

  1. #1
    Junior Member Newbie
    Join Date
    Jan 2013
    Posts
    8

    [SOLVED]Problem with mainloop.

    Hello guys.
    I'm a newbie in OpenGL and i'm trying to make a program with keyboard events, just to make some tests.
    I'm using Linux Debian e g++ to compile the code which i'm making with c++.
    The problem is when i run my program, what i see is:
    Object created.
    Object killed with success.
    I thought the "main" method would be waiting i push a button on the keyboard. I'm saying this because of "glutMainLoop();".
    Why it doesn't happen, and the "glutMainLoop();" method does not enter in loop?
    Code :
    //keyboard.h
    #ifndef __keyboard_h__
    #define __keyboard_h__
     
    #include<iostream>
    #include<GL/gl.h>
    #include<GL/glut.h>
    #include<GL/glx.h>
     
    using namespace std;
     
    class Keyboard
    {
    	public:
    		Keyboard(void);
    		~Keyboard(void);
    		void keyPressed (unsigned char key, int x, int y);
    };
    #endif
    Code :
    //keyboard.cpp
    #include"keyboard.h"
     
    Keyboard :: Keyboard(void)
    {
    	cout<<"Object created."<<endl;
    }
     
    Keyboard :: ~Keyboard(void)
    {
    	cout<<"Object killed with success."<<endl;
    }
     
    void Keyboard :: keyPressed(unsigned char key, int x, int y)
    {
    	switch(key)
    	{
    		case 'j': cout<<"Nothing, just testing."<<endl;
    			break;
    	}
    }
    Code :
    //main.cpp
    #include"keyboard.h"
     
    Keyboard global_keyboard;
     
    void keypress_wrapper(unsigned char key, int x, int y)
    {
       	global_keyboard.keyPressed(key, x, y);
    }
     
    int main(int argc, char **argv)
    {
    	glutInit(&argc, argv);
    	glutKeyboardFunc(keypress_wrapper); 
    	glutMainLoop();
    }

    To compile:
    g++ -o test main.cpp keyboard.cpp -lglut
    What i'm doing wrong?
    Last edited by robgeek; 01-23-2013 at 07:23 AM.

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Jan 2012
    Location
    Australia
    Posts
    1,097
    You haven't created a window. I would also look at swapping to FreeGlut rather than Glut which is quite old

  3. #3
    Junior Member Newbie
    Join Date
    Jan 2013
    Posts
    8
    But is really necessary to create a window just to test a simple callback or a keyboard event?
    I've tried to registrate a idle function, but still doesn't work!
    Code :
    //main.cpp
    #include"keyboard.h"
     
    Keyboard global_keyboard;
     
    void keypress_wrapper(unsigned char key, int x, int y)
    {
       	global_keyboard.keyPressed(key, x, y);
    }
     
    void render_scene(void) {
    	//this is where you'll be drawing later
    }
     
    int main(int argc, char **argv)
    {
    	glutInit(&argc, argv);
    	glutKeyboardFunc(keypress_wrapper); 
    	glutIdleFunc(render_scene);
    	glutMainLoop();
    }

  4. #4
    Senior Member OpenGL Guru
    Join Date
    May 2009
    Posts
    4,948
    But is really necessary to create a window just to test a simple callback or a keyboard event?
    Yes. Until you have a window, you don't have a window message processing loop. And without that, you can't have idle time (ie: when you're not processing messages). You can't process keyboard messages because you can't process messages.

    GUI programming is generally built around a window of some sort. In Win32 especially, the Window is what gets keyboard messages, not the application. That's why different windows of the same application can process keyboard messages differently.

  5. #5
    Junior Member Newbie
    Join Date
    Jan 2013
    Posts
    8
    Yes. Until you have a window, you don't have a window message processing loop. And without that, you can't have idle time (ie: when you're not processing messages). You can't process keyboard messages because you can't process messages.

    GUI programming is generally built around a window of some sort. In Win32 especially, the Window is what gets keyboard messages, not the application. That's why different windows of the same application can process keyboard messages differently.
    Ok, i did what you said to do, and now everything is working fine.

    Thank you very much.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •