Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


C++ cvar system


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 Sepiantum   Members   -  Reputation: 102

Like
0Likes
Like

Posted 09 February 2012 - 04:43 PM

Valve's Source engine and the Quake engine both provide the user with a cool looking console system. In a little project of mine, I've decided that it would be helpful to implement a cvar system so that I could do some realtime debugging. So how does one go about designing such a system and not kill his FPS? I'm thinking an std::unordered_map<std::string, DWORD> would be cool and all, but then console functions would not really exists.
Follow and support my game engine (still in very basic development)? Link

Sponsor:

#2 Washu   Senior Moderators   -  Reputation: 5605

Like
0Likes
Like

Posted 09 February 2012 - 04:45 PM

Since the source to quake has been released, you can look at that to see how they managed it.

A simple parser for basic commands should run quite quickly, and needs to be executed in only select circumstances that would likely never have any noticeable effect on your frame rate.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
ScapeCode - Blog | SlimDX


#3 Hodgman   Moderators   -  Reputation: 31939

Like
2Likes
Like

Posted 09 February 2012 - 05:44 PM

Also, if the CVAR code from the Source engine isn't available, you can always look at the CVAR code in it's predecessor Goldsrc instead.

These days though, I'd probably just be lazy and hook my console up to my Lua interpreter, and tell Lua to eval any console inputs Posted Image

#4 Sepiantum   Members   -  Reputation: 102

Like
0Likes
Like

Posted 12 February 2012 - 12:01 AM

laziness ftw. anyways, i've decided to use a giant union of all supported types in vc++ to store cvar stuff because you really don't cvars for your directx renderer class.
Follow and support my game engine (still in very basic development)? Link

#5 Shael   Members   -  Reputation: 277

Like
0Likes
Like

Posted 12 February 2012 - 11:19 PM

These days though, I'd probably just be lazy and hook my console up to my Lua interpreter, and tell Lua to eval any console inputs


I second this. I use this method currently and it works beautifully!

#6 forb   Members   -  Reputation: 100

Like
0Likes
Like

Posted 29 February 2012 - 03:18 PM

I've attempted this topic just recently myself. My solution came from making a map<string, cvar_t> where cvar_t is a struct that has pointer storage for a few basic types I needed, int*, double*, string*, a flag for the type of cvar it is (int, double, string) and finally the cvar's name. This sidesteps polymorphism but at only the cost of a few extra address variables. I then have functions that return the addresses of the cvar value itself, not the struct but the data type address in use based on the type flag, and the calling functions should know what type of cvar they're dealing with thus knowing which call to make in order to retrieve the proper address.

By returning the address of the cvar's variable, you can reference it in multiple places. If it is changed in one place, its instantly changed in all the others (this is the primary advantage I see in doing it this way). An example would be if you had a renderer that had an int representing whether or not to draw polygons as wireframe or solids, and you change that variable in the console (assuming you did things correctly) the renderer will instantly start drawing wireframes based upon your input instead of having to query the cvar system for your variable on each pass to see whether it should or shouldn't draw wireframes.

As for the processing from the console, I pass everything around as a string and convert to its actual datatype at time of assignment since the particular cvars have type flags. If I'm not mistaken, this is similar to how the quake engine handles cvars.

#7 Washu   Senior Moderators   -  Reputation: 5605

Like
0Likes
Like

Posted 29 February 2012 - 04:39 PM

Its pretty easy to use boost::variant to come up with a typesafe cvar system.

#include <boost/variant.hpp>
#include <iostream>
#include <string>
#include <map>
#include <algorithm>

typedef boost::variant<int, double, std::string> variant;

struct cvar {
    std::string name;
    variant variable;
};

struct cvar_set_visitor : boost::static_visitor<> {
    cvar_set_visitor(variant const& value) : value(value) {}

    template<class T>
    void operator()(T& i) const {
        i = boost::get<T>(value);
    }

    variant value;
};

struct cvar_print_visitor : boost::static_visitor<> {
    void operator()(int& i) const {
        std::cout<<i<<std::endl;
    }

    void operator()(std::string const& s) const {
        std::cout<<s<<std::endl;
    }

    void operator()(double d) const {
        std::cout<<d<<std::endl;
    }
};

void printer(std::pair<std::string, cvar> const& p) {
    std::cout<<p.second.name<<": ";
    boost::apply_visitor(cvar_print_visitor(), p.second.variable);
};

int main() {
    std::map<std::string, cvar> vars;

    cvar someIntVar = {"someIntVar", 1};
    cvar someDoubleVar = {"someDoubleVar", 3.14};
    cvar someStringVar = {"someStringVar", "Hello world"};

    vars.insert(std::make_pair("someIntVar", someIntVar));
    vars.insert(std::make_pair("someDoubleVar", someDoubleVar));
    vars.insert(std::make_pair("someStringVar", someStringVar));

    std::for_each(vars.begin(), vars.end(), printer);
    boost::apply_visitor(cvar_set_visitor(2.1), vars["someDoubleVar"].variable);
    boost::apply_visitor(cvar_print_visitor(), vars["someDoubleVar"].variable);

    try {
        boost::apply_visitor(cvar_set_visitor(2.1), vars["someStringVar"].variable);
    } catch(std::exception& ex) {
        std::cout<<ex.what()<<std::endl;
    }

    std::for_each(vars.begin(), vars.end(), printer);
}

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
ScapeCode - Blog | SlimDX





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS