Advertisement

Console?

Started by January 30, 2003 02:16 PM
3 comments, last by Skalariak 22 years, 1 month ago
Hello everbody? I habe a big question. Maybe someone before asked this but the search is still disabled. My Question: Is there any tutorial for implement & programming a console, because I want to add one in my modified Nehe-Basecode 2. I haven´t fount anything like that. Thanky foru your attenation Skalariak
you don''t really need a tut for something like a console.
Just find yourself a good string handler (ie, stl string) and code away.
you will learn more this way.

| - Project-X - my mega project.. big things comming soon - | - adDeath - an ad blocker I made - | - email me - |
Advertisement
I have no idea how to start codeing a console. Thats my big problem

  std::vector<std::string> lines;std::string currentLine;onKeyDown { if(isChar(key)) currentLine += std::string(key); if(key == ''\n'') {  lines.push_back(currentLine);  processLine(currentLine); }}  



  processLine(std::string line) { if(line=="quit") {  PostQuitMessage(0); }}  



  onDrawConsole { for(std::size_t i=0;i<lines.size();++i) {  glPrint(0,i*16,lines[i].c_str()); }}  


basic, buggy, not very useful, but the idea is given now..

btw..
#include <string>#include <vector> 


"take a look around" - limp bizkit
www.google.com
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia

My Page davepermen.net | My Music on Bandcamp and on Soundcloud

For command processing, std::hash_map comes in handy.
Consider this (warning! long, but it actually works):


  #include <map>#include <hash_map>#include <vector>#include <string>using namespace std;// console commandclass Command{public:	virtual ~Command() {}	// this is called by the console handler	virtual void execute(vector<string>& params){};};// command function typetypedef void (*command_function)(vector<string>& params);class CommandFun   : public Command{     command_function _fun;public:     // create a command, takes command     CommandFun(void (*Fun)(vector<string>& params))          : _fun(Fun)     {}     // execute console command     void execute(vector<string>& params)     {         _fun(params);     }};// member function command handlertemplate<class T>class CommandMemFun   : public Command{	T* _obj;     void (T::*_memFun)(vector<string>& params);public:     // create a command, takes command     CommandMemFun(T* Obj, void (T::*MemFun)(vector<string>& params))          : _obj(Obj), _memFun(MemFun)     {}     // execute console command     void execute(vector<string>& params)     {         (_obj->*_memFun)(params);     }};// make lower casestring toLower(const string& text){	string result = text;	for (int n = 0; n < result.size(); ++n)		result[n] = tolower(result[n]);	return result;}// parse paramsvector<string> parseParams(const string& line){	static char temp[256];	vector<string> vResult;	copy(line.begin(), line.end(), temp);	temp[line.size()] = 0;	char* pToken = strtok(temp, " \t");	while (pToken != 0) {		vResult.push_back(toLower(pToken));		pToken = strtok(0, " \t");	}	return vResult;}class Console{	typedef hash_map<string, Command*> command_map_t;	// command map	command_map_t _commandMap;	vector<string> _lines;public:	// register a new console command	void registerCommand(const string& command, Command* function)	{		_commandMap[toLower(command)] = function;	}	void processLine (const string& line) 	{ 		// only if input is valid		if (line.size()) {			// parse command line			vector<string> params = parseParams(line);			// find  command			command_map_t::iterator cmd = _commandMap.find(params[0]);			if (cmd != _commandMap.end()) {				// execute if found				cmd->second->execute(params);			} else {				_lines.push_back("unknown command");			}		}	}};// Example:// example functionvoid onQuit(vector<string>& params){	PostQuitMessage(0);}// example classclass Test{public:		// 	void onCall(vector<string>& params)	{		// do something	}};Console con;Test    test;// make a few commandsCommandFun	    cmdQuit(onQuit);CommandMemFun<Test> cmdTest(&test, &Test::onCall);// register them to consolecon.registerCommand("quit", &cmdQuit);con.registerCommand("test", &cmdTest);  

This topic is closed to new replies.

Advertisement