how does GLUT do function pointers?

This topic is 4827 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

i want to implement a console into my engine. so far its going good and im trying to optimize the development of it. my question is how does GLUT make function pointers? for example you create your own rendering function named "display". to put it into glut all you have to do is call glutDisplayFunc(display). now when i pass commands through my console, how can i run a function from it? does my question even make sense?

Share on other sites
glutDisplayFunc is simply a function that takes a single function pointer as an argument (or at least that's what it looks like from what you've said). There's a lot of info on function pointers here that would be worth taking a look at.

For a console you need to map commands (strings) to functions. If you're using C++ std::map with an std::string as the key and a function pointer as the value would be a simple solution. Simply lookup a typed command in the map and call the matching function pointer.

Share on other sites
Or you can embed an actual scripting language into your application like Python or Lua. (It's a lot less painful than it sounds.) Then you can pass the string from your console to the scripting environment and have it do the magic.

Share on other sites
i appreciate the help guys. now that i look back, i didnt make my question very clear (that and i have a better idea for building the console). im going to make a vector of console commands/items. im making a class which will be in each element of the vector.

heres what i have so far
class _Console_Item{   std::string item_name;   /*some data type*/ func_pointer;}

im just not sure what the /*some data type*/ needs to be. im not totally sure yet, but each of the functions may or may not return values. would that make a difference?

Share on other sites
Well you can only store function pointers of one type (or at least this is the case with the current setup you propose) and a function pointer of a certain type corresponds to one function signature (i.e. the return type and arguments have to be the same) so yes return type does matter.

Share on other sites
The glut display function pointer is:

void (*display)()

Nice and simple

Also as it appears you will be taking keyboard input the keyboard function pointer looks like:

void (*keyboard)(unsigned char key, int x, int y)

Share on other sites
Quote:
 Original post by MonderWell you can only store function pointers of one type (or at least this is the case with the current setup you propose) and a function pointer of a certain type corresponds to one function signature (i.e. the return type and arguments have to be the same) so yes return type does matter.

ok ive narrowed it down to using only functions of type void. i found the article on how to make a console and im a little lost on how they add functions.

click

Share on other sites
Quote:
 class _Console_Item

This is a reserved keyword.

Quote:
 From the MSDN: Lexical ConventionsThe first character of an identifier must be an alphabetic character, either uppercase or lowercase, or an underscore ( _ ). Because C++ identifiers are case sensitive, fileName is different from FileName.Identifiers cannot be exactly the same spelling and case as keywords. Identifiers that contain keywords are legal. For example, Pint is a legal identifier, even though it contains int, which is a keyword.Use of two sequential underscore characters ( __ ) at the beginning of an identifier, or a single leading underscore followed by a capital letter, is reserved for C++ implementations in all scopes. You should avoid using one leading underscore followed by a lowercase letter for names with file scope because of possible conflicts with current or future reserved identifiers.

Quote:
Quote:
 Original post by MonderWell you can only store function pointers of one type (or at least this is the case with the current setup you propose) and a function pointer of a certain type corresponds to one function signature (i.e. the return type and arguments have to be the same) so yes return type does matter.

ok ive narrowed it down to using only functions of type void. i found the article on how to make a console and im a little lost on how they add functions.

click

Since you're using C++ I would highly recommend the boost librarys, specifically Boost.Function.

Assuming the console mentioned is like a console interface, here's an example (just using good old cout):

#include <map>#include <sstream>#include <string>#include <vector>#include <iostream>#include <boost/function.hpp>#include <boost/lexical_cast.hpp>using namespace std;using namespace boost;void console_exit( const vector< string > & args ) {    cout << "Bye!" << endl;    exit( 0 );}void console_add( const vector< string > & args ) {    double sum = 0.0;    for ( unsigned int i = 0 ; i < args.size() ; ++i ) {        //lexical cast simply turns "2.0" into 2.0        sum += lexical_cast< double >( args );    }    cout << sum << endl;}map< string , function< void ( const vector< string > & args ) > > console_functions;//note: console_functions can be pointed at a function that returns void and has an argument of (or similar) to: "const vector< string > &"int main () {    console_functions[ "exit" ] = & console_exit;    console_functions[ "quit" ] = & console_exit;    console_functions[ "add" ] = & console_add;    while ( true ) { //loop forever (until we quit)        cout << "Simple example console input thing :-)" << endl;        string line;        getline( line , cin ); //gets a full line from cin        string command;        vector< string > arguments;        stringstream ss;        ss << line; //fills our stringstream with a single line        //gets only the first word:        ss >> command;        //pushes each remaining word in the stringstream back into it's own argument:        copy( istream_iterator< string >( ss ) , istream_iterator< string >() , back_inserter( arguments ) );        /* if the user entered "add 2.0 3.0" our important variables would now be:         *          * command == "add"         * arguments[0] == "2.0"         * arguments[1] == "3.0"         *          * we're now going to use these to call a function:         */        if ( console_functions[ command ].empty() ) {            cout << "Command not found" << endl;        } else {            console_functions[ command ]( arguments );            /* the above calls the function pointed to by:             *             * console_functions[ command ]             *             * with a single argument, the variable "arguments".             */        }    }}

Try it out :-).

Share on other sites
i like the fact that there are tons of consoles that i can just plug and play into my engine, but im trying to learn how to make one on my own, with a little help on the side

1. 1
Rutin
26
2. 2
3. 3
4. 4
5. 5

• 11
• 10
• 13
• 20
• 14
• Forum Statistics

• Total Topics
632950
• Total Posts
3009380
• Who's Online (See full list)

There are no registered users currently online

×

Important Information

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!