Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 05 Jan 2009
Offline Last Active Feb 12 2016 06:32 PM

#5270043 Criticism of C++

Posted by Zlodo on 08 January 2016 - 07:27 AM

I just want to point out that it is not really fair to characterize the c creators as lazy. You have to keep in mind that they wrote the first c compiler in assembly, on a pdp-11 (https://en.m.wikipedia.org/wiki/PDP-11), which had memory measured in kilo bytes and CPUs that were probably slower than the cheapest micro controller on the market nowadays.

A lot of thing that may sound trivial for us nowadays with our multi core cpus running at frequencies measured in gigahertz might have been incredibly expensive back then.

#5269214 Criticism of C++

Posted by Zlodo on 04 January 2016 - 10:18 AM

If that is something that makes the language unviable for you, use a different one.

Can we not have a civil discourse on the pros/cons of a language without resorting to this?

I fully agree with that sentiment. "Use a different language" is a defeatist approach that seem to assume that languages are immutable things that can never be improved, or that what we have now is as good as it gets.

Complex applications such as games often have many different parts that, with the "use the right language for the job" mantra would call at a minimum for using one language for the tools, one for the engine, one for the high level scripting. Perhaps even yet another one for ui scripting such as action script or JavaScript.

The downside is that you end up writing heaps of glue code to interface together things written in some different languages. Or at least, heaps of interface descriptions for tools to generate the glue. Or ugly macros, or whatever else you use to work around the lack of a good introspection system.

I wrote a lot of tools to do this kind of things over the years, I have came up with solutions that I find sufficiently elegant, but I still find it generally clunky.

And regardless of how much you automate it, it is always much uglier than necessary. The impedance mismatch between all the languages result in interface APIs whose semantics are always unnatural to some extent for the language in which they are exposed.

It also makes refactoring much harder. Want to move this bit of logic from here to there? Oops, you moved to a country that speaks a different programming language. Time to rewrite it.

There's something to be said for the simplicity of using the same language for everything. Or, at least fewer different languages.

#5243795 Pass anything as constructor without varargs

Posted by Zlodo on 31 July 2015 - 08:08 AM

You can do this natively in c++ using rtti.

There is an operator called typeid (it's an operator that looks like a function call, like sizeof).

It returns a type_info object. It implements a "before" function that defines an ordering, so you can make a wrapper class to use it as a map index with an overloaded < operator.

C++11 directly provides such a wrapper class, called type_index, that can be constructed from a type_info and can also be used as a key in unordered_map and unordered_set, as there is a specialization of std::hash for it.

typeid can be given a type directly as in typeid( int ) or an expression. If the expression is an object instance with a vtable (such as an interface class), the type will be determined at runtime (by reading a pointer in a special slot of the vtable, so there's virtually no memory overhead). In other cases it is resolved at compilation, just giving a poiner to the appropriate type_info object directly.

Note that it is a common belief that rtti is evil and costly and should be disabled in games. I never understood the rationale for it, in particular in cases where people then go out of their way to reimplement a similar but unwieldy type identification system by hand.

#5237739 Easy source control for educational uses?

Posted by Zlodo on 30 June 2015 - 03:20 PM

I've always wanted an excuse for using Fossil smile.png


Besides having a cool name, it is designed to be self-contained and simple to use, while still being powerful enough to be called a full featured versioning system.

A comparison between Fossil and Git here: http://www.fossil-scm.org/index.html/doc/trunk/www/fossil-v-git.wiki

((Totally biased comparison, of course, but nonetheless...))


I especially like that Fossil has wiki and issue tracking built right into it.

And that great care has been taken to ensure that you can't shoot yourself in your feet so easily.


I think for educational use, Fossil is probably going to be an excellent choice.

I have a feeling that it has been - and still is - developed and used by primarily academics. smile.png


I use fossil on my hobby projects at home and I can attest that it is excellent. It is very easy to deploy and use, and have very few surprising behaviors. I only used it on single person projects though, I don't know how it looks like in day to day use with multiple developers. The repositories being contained in single files is useful for quick backups on for instance google drive.


It is also very robust. I actually have installed in as a server on my nas along with a script that automatically pulls and recompile it from the latest trunk version from fossil's official repos a couple times a day, which sounds dumb and suicidal but i've been running this for years and never once pulled a version with any serious bug, and never had to use my backups.

#5221490 GCC -malign-double

Posted by Zlodo on 05 April 2015 - 11:40 AM

As Bacterius alluded to..this flag can turn out to be very viral.


You could say that it is a malign flag.

#5221167 preventing system crash or power outage from wiping savegame: best methods

Posted by Zlodo on 03 April 2015 - 12:36 PM


A bit of explanation is probably called for.


The data in question is the bitmap masks for unexplored sections of the local maps in a FPS/RPG. Right now, they are overwritten on save, with no backup. if the power goes out, they get wiped, and the entire 25 square mile area of the game world becomes "unexplored " again.


Power outages are a special concern as i'm off grid running off a gen-batt system, and the low voltage alarm on the power inverter is not very loud.  So unexpected power outages are a multiple times a day occurrence here. i suspect laptop users might face similar issues. And since the game will run on laptops (i'm developing it on a laptop board in a mini case) i'd like to make it as robust as possible against power outage.


And its not like being on grid is much better - at least around here.  The power goes out in EVERY storm (almost all power lines are above ground in woods in this area). Its quite common for me to be the the only person in the neighborhood with power during bad weather. In fact the power went out two days ago just because of the normal March winds.



while the game has a built-in cheat to reveal all unexplored areas on the world and local maps, having to use it every few hours because the battery died makes it hard to reproduce the non-cheating player's experience in long term play testing.



Why load and save the entire map monolithically? Divide it into fixed size chunks and only overwrite those that have been touched since the last save (keep a boolean for each chunk that you set to true whenever you modify that chunk).


You could even save them on the fly in a background thread in a temporary file/directory and rename them into an actual game save file(s) when the player wants to save his progress. This way you "save game" function is almost instant (which is nice for the player), and when restarting the game after an unexpected power loss, you can offer the player to restore his "unsaved" progress from the temporary files.


Also this is exactly the kind of "mission critical" stuff where i'd definitely trust sqlite over any homegrown solution, by the way.

#5218884 preventing system crash or power outage from wiping savegame: best methods

Posted by Zlodo on 24 March 2015 - 01:58 PM


You can have a look at how sqlite achieves atomic commits using a rollback or a write ahead journal:


that was about the only thing i found from googling this




it helped refresh my memory about how such things work.  I was into systems programming before I got into game programming, but that was a LONG time ago, back in the DOS 3.0 and DOS 4.0 days.


from the wikipedia description, recovery seemed rather non-trivial, especially compared to round robin saves or new save each time.


going to SQLite to save what is essentially a header-less 264x264 monochrome bitmap is probably overkill. also, these must page in real time, so performance is "mission critical code". in my mind, "mission critical code" and SQLanything don't belong in the same universe. 



All "SQLanything" aren't created equal - I suggested using SQLite, not Oracle. It's server-less and writes everything into a single binary file. You could easily store your bitmaps in there as blobs and get the atomic updates, resilience to crashes etc for free. If you use it like just as a key/value store for binary blobs you don't even need it to parse any sql (not that it would matter because parsing a few sql statements when initializing your app wouldn't kill you anyway)


As an aside, SQLite and "mission critical" do belong in the same universe, unless you don't consider airliners flight software to be mission critical:


#5218622 preventing system crash or power outage from wiping savegame: best methods

Posted by Zlodo on 23 March 2015 - 03:56 PM

You can have a look at how sqlite achieves atomic commits using a rollback or a write ahead journal:



Of course, as said above it relies on the hard disks not lying about flushing their caches onto the physical media. But even in that case it would be a good protection against crashes.


You may also consider just using sqlite to store your game save and let it deal with that.

#5218575 Programmatic Pixel Art

Posted by Zlodo on 23 March 2015 - 01:57 PM

Well the thing is that things like gimp can directly load and save XPM images so you can just edit them like normal images and then just #include them in your code. You still have to decode them into an RGB format before you can use them anyway, so I'm not sure it's really very useful.


If you really want to embed images directly into your executable, you might be better off just including images in PNG or JPG in your code using something like bin2c and using stb_image (a full featured image loader that fits entirely in a header file) to decode them.

#5218540 Programmatic Pixel Art

Posted by Zlodo on 23 March 2015 - 12:42 PM

Has anyone ever tried to do that, you ask? Oh yeah. I can guarantee you that in the medieval ages of computing people were routinely hardcoding graphics directly in their source code like that.


You may want to have a look at the XPM format, a text based bitmap format that is also actually valid C code defining palette and pixels as arrays. Some editors even recognize it and can turn into bizarre text editor / image editor hybrids:


#5217043 is it possible to prevent a proggram(specialy a game) to get decompiled

Posted by Zlodo on 17 March 2015 - 04:37 AM

Doing things client side in a mmo is only "fundamentally wrong" if cheating is your only concern.

More realistically it depends on many factors, including your business model. Having dumb servers and letting clients do most of the work can be pretty justified for a non subscription based game - especially if the gameplay is computationally demanding, for instance because it involves a detailed physic model.

Making the game hard enough to hack can suffice. It's an engineering compromise like everything else.

#5216632 is it possible to prevent a proggram(specialy a game) to get decompiled

Posted by Zlodo on 15 March 2015 - 09:19 AM

A lot of people see this as an all-or-nothing issue, aka "it's always theoretically possible to defeat a client side protection so there's no use in doing it at all", but it really all depends on the exposure of your game and what type of hackers you end up with.


On the game I work on, the only hackers we've had so far have limited abilities: they basically know how to poke half blindly around memory with cheatengine and that's it. So despite a lot of things being done client side and our game using peer to peer networking we've had some good success with relatively simple client-side protection schemes. Those could be defeated by good hackers, but the hackers that have been active in our game so far are mediocre.

#5215628 Questions about GPGPU

Posted by Zlodo on 10 March 2015 - 05:15 AM

In a similar vein to C++ AMP, there's SYCL, a khronos standard to embed opencl code directly into c++ code.

It's also worth noting that opencl 2.1 adopted a subset of c++14 as a compute kernel language (basically all of c++ except what you'd expect not to be possible on a GPU)

#5212763 How to store data for reflection in preprocessor stage

Posted by Zlodo on 24 February 2015 - 01:37 PM

Olof Hedman have the right idea. C++ can let you approach reflection in a unique way: do (almost) everything at compilation time.
I've experimented and played around a lot with that in a couple of pet projects as well as in some production code at my previous company. The solution I've ended up with that I liked the most is to describe everything at compilation time using template specialization, much like type traits. From various proposals for compilation time reflection for the C++ standard that I've seen, I think other people have used similar approaches to reflection in c++ as they are proposing things that work generally in a similar way.
I'm going to oversimplify this, but this approach is very extensible. I've used it to automatically generate language bindings completely at compilation time, to serialize graphs of objects, and a few other things.

Basically, I have a reflection::traits template that I specialize to describe every item I want my reflection system to describe. It is defined like so:

namespace reflection
    template< typename T > struct traits {};

I then have a specialization of it for each thing I want to describe, and which contain static methods, typedefs, etc. depending on what kind of thing I'm describing.

For instance if I have a class named Foo, I'll have a specialization of the reflection template that looks like this:
template<> struct reflection::traits< Foo >
    static const char* Name() { return "Foo"; }
At runtime, I now can now the name of class Foo by calling this: reflection::traits< Foo >::Name()
Of course, just getting the name isn't really useful. What I really want to do is to enumerate all the members. I do it using a compilation time visitor pattern. I guess it could be possible to use a more functional programming style, using tuples or something similar but I find the visitor approach less daunting.
In my previous company we only used this to serialize things, so I was doing something like to describe member variables:
template<> struct reflection::traits< Foo >
  static const char* Name() { return "Foo"; }

  template< typename V > accept( V& visitor )
    visitor.template memberVar( "Blah", &Foo::m_Blah );
    visitor.template memberVar( "Thing", &Foo::m_Thing );

It was all done using a bunch of macros so it looked something like this:


The reflection::traits specialization had to be declared friend, so I had another macro for that. It is the only thing I need to insert into the definition of my classes, other than that this approach is non-intrusive, all the reflection stuff lives completely on the side.


It is possible to do much more complicated things, though: just create a dummy type for each member variable / property and create specific specializations of reflection::traits for those where you can then put whatever you need, like pointers to setter/getter member functions).

Likewise, macros are just one way to go about it. On my pet project I do more complicated things so I have a generator that generate all those templates from descriptions written in a simple language (I just don't like to insert annotations in the C++ code itself, I think it's both more complicated and less neat).


Then I can for instance print the member variables of any class described using this system by doing something like this:
template< class C >
class PrintObjectVisitor
    WriteVisitor( const C& object, ostream& output ) :
        m_object( object ),
        m_output( output )

    template< typename T > void memberVar( const char* pName, T C::* mpVar )
        output << "  " << pName << ": " << m_object.*mpVar;

    const C& m_object;
    ostream& m_output;

template< typename C >
void PrintObject( const C& obj )
    PrintObjectVisitor v( obj, cout );
    reflection::traits< C >::accept( v );
The visitor can have methods besides "memberVar" to describe other aspects of the class, using template functions to pass along the required type (so that the visitor can then use reflection on that type in the same way and so on). For instance, you could have a method to tell the visitor about the superclasses of the class. It can then recursively visit them to print their member variables too.
You can use this to attach reflection information and implement visitor patterns for other things than classes. For namespaces, I just create a dummmy type in the namespace:
namespace Bar
    struct reflection_tag {}
Then specialize reflection::traits for "Bar::reflection_tag" to describe reflection informations about that namespace, including a function that goes through all the classes and nested namespace that it contains using a compile-time visitor like above.
Likewise, I create dummy structs in the class reflection traits to identify methods, properties and so on and then specialize the reflection::traits class for those to describe everything I need to describe about those things, depending on what I need to do.
The nice thing is that for most things, you pay no runtime cost for all that. That PrintObject function above, for instance, gets compiled into completely straight forward code that will just print each variable of the object without performing any lookup through a container at runtime. Furthermore, you don't get a bunch of data you don't need compiled into your binaries. If you only need to serialize a bunch of data in a binary blob, you don't need the class and variable names as long as you can identify the format version (I was doing it by using that same system to generate a CRC of all the classes description - it was enough for us since we used this only for network communication and it allowed to make sure that both the client and server were able to serialize and unserialize the exact same types). By the way in a modenr C++ compiler, things like computing CRCs such as this could be also done completely at compilation time using constexpr.
Another plus of this method is that you don't need to add stuff into the classes themselves, it lives completely on the side. You can make macros to build all those template specializations, I've did that at my prevoous company. However, in my personal projects I'm doing more sophisticated stuff using this approach than just serialization (like scripting language bindings), so I wrote a tool that generate those. I didn't want to use a Qt or unreal like approach of inserting special annotations through my c++ classes though, just a matter of taste but I find this messy. Instead, I have a simple interface description living in their own files, using a simple description language that ressembles a very simplified subset of c++, where I describe namespaces, classes, methods and properties. Then I have a tool that spits out a bunch of header files containing all those reflection::traits specialization, and from that I can generate  serialization, language bindings and such entirely through a bunch of templates.
Its also possible to use all this to construct a more classical system that allows introspection at runtime, but I'd only use that for things like property editor UIs and the like.

#5176175 Game content source repository?

Posted by Zlodo on 26 August 2014 - 05:43 AM

We use perforce here, on a large project with multiple teams around the world, to store both the code and the data.

Some things that make perforce good for this are:

- the possibility to have different locking policies per file type (you want to allow multiple checkout for sources, but exclusive checkout only for binary data such as art assets)

- the possibility (again, per file type) to limit the number of revision for which you keep the actual data. For instance, you can get it to store only the last 5 revisions of PNG files and discard earlier ones. This is vital for very large project s that really deal with a lot of data, to keep the size of the repository under control.

- perforce allows to setup proxy servers, and it works really well and allows the dozens or hundreds of people working at each studio to just talk with their local proxy, which in turn incrementally synchronize themselves with the central repository. This way the (large) data being committed elsewhere in the world is only downloaded once by your local proxy, and then everyone gets them on their PC through the lan. Imagine if a team of 100 persons had to download the same latest art assets through the internet directly...

Despite of all this it is very responsive in practice, someone on the other side of the world pushes a commit through their local proxy and you see it almost immediately. Of course when large operations are underway such as branching or large commits it tends to create some slow downs but nothing really crippling.