Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your function is a member of a class. When you do something like <code>Car c; c.drive()</code>, that <code>drive()</code> function needs a car to work with. That is the <code>this</code> pointer. So glut can't call that function if it doesn't have a car to work on, it's expecting a free function.</p> <p>You could make your function <code>static</code>, which would mean the function does not operate on a car. glut will then be able to call it, however I assume you want to manipulate a car. The solution is to make the function pass it's call onto an object, like this:</p> <pre><code>void key_press(int key, int x, int y) { activeCar-&gt;KeyPress(key, x, y); } </code></pre> <p>Where <code>activeCar</code> is some globally accessible pointer to car. You can do this with some sort of <code>CarManager</code> singleton.</p> <p>CarManager keeps track of the active car being controlled, so when a key is pressed you can pass it on: <code>CarManager::reference().active_car().KeyPress(key, x, y)</code>.</p> <p>A singleton is an object that has only one globally accessible instance. It is outside the scope of the answer, but you can Google for various resources on creating one. Look up Meyers Singleton for a simple singleton solution.</p> <p>A different approach is to have a sort of InputManager singleton, and this manager will keep track of a list of objects it should notify of key presses. This can be done in a few ways, the easiest would be something like this:</p> <pre><code>class InputListener; class InputManager { public: // ... void register_listener(InputListener *listener) { _listeners.push_back(listener); } void unregister_listener(InputListener *listener) { _listeners.erase(std::find(_listeners.begin(), _listeners.end(), listener)); } // ... private: // types typedef std::vector&lt;InputListener*&gt; container; // global KeyPress function, you can register this in the constructor // of InputManager, by calling glutSpecialFunc static void KeyPress(int key, int x, int y) { // singleton method to get a reference to the instance reference().handle_key_press(key, x, y); } void handle_key_press(int key, int x, int y) const { for (container::const_iterator iter = _listeners.begin(); iter != _listenders.end(), ++iter) { iter-&gt;KeyPress(key, x, y); } } container _listeners; }; class InputListener { public: // creation InputListener(void) { // automatically add to manager InputManager::reference().register_listener(this); } virtual ~InputListener(void) { // unregister InputManager::reference().unregister_listener(this); } // this will be implemented to handle input virtual void KeyPress(int key, int x, int y) = 0; }; class Car : public InputListener { // implement input handler void KeyPress(int key, int x, int y) { // ... } }; </code></pre> <p>Of course feel free to ask more questions if this doesn't make sense.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload