• Advertisement

Archived

This topic is now archived and is closed to further replies.

Console?

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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 - |

Share this post


Link to post
Share on other sites

  
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

Share this post


Link to post
Share on other sites
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 command

class Command
{
public:
virtual ~Command() {}
// this is called by the console handler

virtual void execute(vector<string>& params){};
};

// command function type

typedef 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 handler

template<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 case

string toLower(const string& text)
{
string result = text;
for (int n = 0; n < result.size(); ++n)
result[n] = tolower(result[n]);

return result;
}

// parse params

vector<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 function

void onQuit(vector<string>& params)
{
PostQuitMessage(0);
}

// example class

class Test
{
public:

//

void onCall(vector<string>& params)
{
// do something

}
};

Console con;
Test test;

// make a few commands

CommandFun cmdQuit(onQuit);
CommandMemFun<Test> cmdTest(&test, &Test::onCall);

// register them to console

con.registerCommand("quit", &cmdQuit);
con.registerCommand("test", &cmdTest);


Share this post


Link to post
Share on other sites

  • Advertisement