🔒 Locked
how does GLUT do function pointers?
Advertisement
Original Post
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?
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.
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.
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
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?
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?
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.
Quote:
Original post by Monder
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.
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
Quote:
class _Console_Item
This is a reserved keyword.
Quote:
From the MSDN: Lexical Conventions
The 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:
Original post by adam17 Quote:
Original post by Monder
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.
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 :-).
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement