Jump to content

  • Log In with Google      Sign In   
  • Create Account


Matt-D

Member Since 21 Oct 2011
Offline Last Active Private
-----

#5132612 SFML - Can't even compile tutorial code

Posted by Matt-D on 19 February 2014 - 06:55 AM

Try simply following the official tutorials: http://www.sfml-dev.org/tutorials/2.1/




#5122255 C++ templates, are they worth it?

Posted by Matt-D on 08 January 2014 - 04:40 PM

These are some pretty good starting points ("old-code-can-call-new-code" is a pretty succinct description):
http://isocpp.org/wiki/faq/templates
http://isocpp.org/wiki/faq/big-picture#generic-paradigm
http://isocpp.org/wiki/faq/big-picture#multiparadigm-programming
http://isocpp.org/wiki/faq/big-picture#old-code-can-call-new-code
http://isocpp.org/wiki/faq/cpp11-language-templates

One other thing -- they go hand in hand with "auto" and genericity:
http://isocpp.org/wiki/faq/cpp11-language#auto

For instance, instead of writing:

int add(int a, int b) { return a + b; }
float add(float a, float b) { return a + b; }
double add(double a, double b) { return a + b; }

int x = add(1, 2);
float y = add(1.f, 2.f);
double z = add(1., 2.);

You can simply write:

template <typename T>
T add(T a, T b) { return a + b; }

auto x = add(1, 2);
auto y = add(1.f, 2.f);
auto z = add(1., 2.);

So they help you achieve generic, reusable code that doesn't just work for a particular type, but for a bunch of types that happen to satisfy a concept: http://en.cppreference.com/w/cpp/concept

// This concept is implicit here -- operator "+" has to exist. You can make it explicit with type-traits, static_assert, and enable_if.

 

This is useful -- say, instead of "add" taking two numbers, think of "will_first_monster_win" taking two monsters:

template <typename FirstMonsterType, typename SecondMonsterType>
bool will_first_monster_win(const FirstMonsterType & first_monster, const SecondMonsterType & second_monster)
{
  return strength(first_monster) >= strength(second_monster); // super-fancy combat algorithm!
}

Now, if you add new monster types to your game, your "will_first_monster_win" function will keep working unmodified -- as long as the relevant (new) "strength" function exists (let's call this particular existence requirement a "has-strength" concept -- in other words, FirstMonsterType & SecondMonsterType in the "will_first_monster_win" function will successfully match any types that satisfy the "has-strength" concept). In other words, you can extend your code (and add new code) without modifying the already written ("old") code: https://en.wikipedia.org/wiki/Open/closed_principle

// Note: the linked article happens to use inheritance/inclusion-run-time-polymorphism (typical OOP technique) to illustrate this; in many ways, however, templates/compile-time-polymorphism (typical Generic Programming) as in the "will_first_monster_win" example will work just as well if not better.

 

Without templates, you'd have to know the types of your monsters in advance / right at the moment when you're implementing "will_first_monster_win" -- and then rewrite it / provide overloads / etc. each time you add new monster type. Or just settle for a boring game with fixed monster types :-(

 

With templates you can save quite some time, speed up your experiments, and keep adding increasingly cruel monsters, so you can provide more fun for the players :-)




#5120180 When to use polymorphism

Posted by Matt-D on 30 December 2013 - 05:40 PM

To start with, C++ offers several distinct forms of polymorphism, each one with different use cases in mind (and different trade-offs).

To be able to make an informed choice, it's a good idea to become familiar with them all:

http://accu.org/index.php/journals/538

 

You'll learn some of the trade-offs the hard way, after discovering that working on your game (or other program) becomes harder over time, when adding more features forces you to go back to the old code that wasn't written with them in mind, etc. And that's OK, this is how you learn and improve -- as long as you Refactor Mercilessly!

 

It's especially worth noting that run-time polymorphism is very much possible (and arguably cleaner!) without inheritance -- I strongly recommend watching this:

http://channel9.msdn.com/Events/GoingNative/2013/Inheritance-Is-The-Base-Class-of-Evil




#5120177 gui/game librarys FOR C++

Posted by Matt-D on 30 December 2013 - 05:33 PM

I'd also recommend SFML: http://www.sfml-dev.org/

Don't forget the docs: http://www.sfml-dev.org/resources.php




#5095447 Does C++ <random> lib require seeding?

Posted by Matt-D on 20 September 2013 - 06:45 AM

Because reproducibility is desirable, while non-reproducibility is undesirable.

// See, for instance, http://openmd.org/?p=257

Think how hard debugging would be if you couldn't guarantee the same execution path of your program.

 

Besides, even if you were fine with non-reproducibility, seeding with time functions is often a bad idea -- at least, you'd need a high resolution timer.

As an alternative, you can do the following:

std::random_device rdev{}; // ASSUMPTION: your implementation / target platform actually guarantees this is random
std::default_random_engine e{rdev()}; // seed with the aforementioned source of randomness

 

For more, read this:

http://isocpp.org/files/papers/n3551.pdf




#5095438 Effective C++ (book)

Posted by Matt-D on 20 September 2013 - 06:26 AM

See also previous thread:

http://www.gamedev.net/topic/645967-effective-and-more-effective-stl/#entry5082624




#5087324 Generic Datatypes --Are Templates the Way to Go?

Posted by Matt-D on 19 August 2013 - 10:23 AM

The reason I wanted my own type of lists is mainly due to std::list iterators. Having to declare an iterator every time I want to get to a specific element is just plain annoying. It takes like 3 or 4 lines of code to get to that specific element I already know I want. I've thought about vectors since I'd be able to refer to my elements by index, but then everything's aligned in memory. Although memory alignment has its benefits, adding elements to a vector could cause the entire array to relocate in memory to ensure everything is memory-aligned. This can be slow when adding data --especially larger elements.

 

I would still consider using std::vector, see: Bjarne Stroustrup: Why you should avoid Linked Lists.

The cache benefits of std::vector using contiguous memory are so massive that they may in practice outweigh the benefits provided by the lists, EVEN when inserting/removing elements in the middle. It's a trade-off; what's your relative access-to-modification ratio? It's worth checking the performance with profiler (ideally, avoid synthetic benchmarks, and test with your actual gameplay, e.g., using automated replay feature (assuming you have that in your engine)).

 

See “Software Development for Infrastructure” for more details and the graph mentioned in the video.

We should prefer sequential access of compact structures, not thoughtlessly use linked structures, and avoid general memory allocators that scatter logically related data. We must measure to avoid being blind-sided by unexpected phenomena. Our systems are too complex for us to guess about efficiency and use patterns.

 

 

 

 




#5086073 EventBus - Possible with C++ templates?

Posted by Matt-D on 15 August 2013 - 05:23 AM

the event bus calls handleEvent using the engine pointer (the logfile says it is the engine) ... but it seems that the handleEvent of EventHandler is called, not Engine.

Why is that?

 

Doesn't virtual mean that it shouldn't matter that the EventBus is working with an EventHandler pointer?

 

Only if you store & call it via a pointer (or its variants, like std::unique_ptr) or a reference (or its variants, like std::reference_wrapper).

Otherwise, object slicing occurs.

 

Just like here, where you store by value:

std::list< EventHandler<E> > m_handlerList; // oops

 

and here, where you call your stored-by-value (and sliced) handler

for (typename std::list< EventHandler >::iterator it = m_handlerList.begin(); m_handlerList.end() != it ; ++it) // oops
{
EventHandler handler = *it; // oops

*pMessage << "Informing handler nr. " << i << ": " << handler.getName().c_str();
m_pLogger->log(pMessage, Logger::LOGLEVEL_DEBUG);
handler.handleEvent(pEvent); // this is sliced




#5085609 EventBus - Possible with C++ templates?

Posted by Matt-D on 13 August 2013 - 01:30 PM

(0) Would love to answer all your questions ... but I can't think straight right now.

(1) Can't wrap my head around static polymorphism and runtime polymorphism.

 

Basically what I want to do is avoid adding all modules to all modules. I want an event bus to couple them more loosely.

 

(2) I still don't see how a function can be more powerful than a class.

 

(0) I guess trying out a couple of alternative, throwaway but concrete (compilable) solutions might be your best course of action, then.

 

(1) Go through this and try out the examples, that should do it: http://accu.org/index.php/journals/538

 

Note that static forms of polymorphism (both parametric/templates and overloading) are somewhat more powerful in C++ than dynamic polymorphism (in form of inclusion polymorphism/inheritance), since they support multiple dispatch out-of-the-box (so there's no need for things like the visitor pattern in Java if you're fine with the compile-time world), see: http://www.codeproject.com/Articles/635264/Open-Multi-Methods-for-Cplusplus11-Part-1

To see how this comes up in game development, consider the asteroids-spaceships collisions example:

https://en.wikipedia.org/wiki/Multiple_dispatch#Examples

// Note that if you need dynamic/run-time polymorphism, you will also need to use something like the visitor pattern (or a library-based solution), even in C++.

 

(2) Things like std::function support type-erasure and directly support catamorphisms (again, completely eliminate the need for design patterns like the visitor pattern), so a lot of the things you'd normally need a lot of OOP boilerplate for (to implement the design patterns) are simply supported out of the box (no manually written boilerplate code) if you write in FP style:

http://lorgonblog.wordpress.com/2008/04/09/catamorphisms-part-three/

http://stackoverflow.com/questions/2527153/are-design-patterns-specific-to-language-or-technology

 

In particular, in OOP style you often have to waste your time coding some brittle/rigid boilerplate requiring inheritance, while in FP style with std::function you can completely decouple many of your components. Here's another example in the gamedev context: http://probablydance.com/2012/12/16/the-importance-of-stdfunction/

 

Consider the traditional (OOP/inheritance solution):

But you’ve got that inheritance in there, and that doesn’t scale. You will probably want to add a render loop and maybe a separate update loop for the editor. Once you’ve got that many base classes it makes sense to combine them into one. And now you’ve just started on the way of having a big base class that gets used everywhere. Soon enough adding something different to the update loop involves a whole lot of unnecessary work.

 

And compare with the FP one:
 

This has no inheritance. Meaning I can add anything to the update loop now. If I create a new object that is unrelated to anything I had before and is managed completely differently, I can still add it just as easily as anything else. And I can use the same idea for a render loop or for implementing an observer pattern. It makes everything immensely easier.

Perhaps this is exactly what you're looking for?

 

Just as with anything in programming, there ain't no such thing as a free lunch, so each solution has upsides and downsides -- it's your wonderful job as a designer to compare and contrast each and chose the least-imperfect one for the task at hand :-)

 

In particular, both std::function and OOP/inheritance will have a run-time cost related to type-erasure/virtual function call; in contrast, if it's possible for you to know the types at compile-time and you can design & code a GP solution with templates, you won't suffer any overhead of this type (although may have icache trade-offs to consider, etc.).




#5085228 Mathbook for dummies, any recomendation?

Posted by Matt-D on 12 August 2013 - 10:34 AM

This reminded me of EWD's comments:

 

But things have changed. These days it is quite common that students don't even know the name of the person who is lecturing to them, let alone that they feel proud to be his student! And it is equally common that faculty talk about students, in particular undergraduates, as if they were animals from another planet. Students are no longer seen as part of the solution, but as part of the problem, and textbooks and lecturing techniques have become so condescending that, if I were a student, I would take offence. I don't think that I could stand a lecturer that assumes that my attention span is no more than 7 minutes or who feels obliged to feed me a cartoon every 5 foils, and subjects me to multiple-choice tests because I am supposed to be functionally illiterate. I invite you to read carefully the catalogues of publishers of mathematical textbooks: obviously, colour math is better than B&W math, most books are recommended for being intuitive instead of formal, for being chatty instead of crisp, for being vague and sloppy instead of rigorous. It is clearly an article of the New Faith that teaching the real thing would be counter-productive: just as our students are supposed to live on junk food, they are supposed to thrive on junk science.

 

The loss of mutual respect has affected more than just the educational process, it has corroded publication as well. In 1975 I received a letter that objected to the style in which I had written an article for the Communications of the ACM (= Association for Computing Machinery). The complaint was that, by separating my concerns more strictly than usual, I had addressed my intended audience in a style they were not used to. The writer continued with the well-known quotation from P.T.Barnum that "No one ever got broke by underestimating the intelligence of the American people." and urged me to bear that in mind whenever I wrote for the programming community. So, 25 years ago, the rot had already set in; at the end of the century it would lead to an endless series of fat, yellow books titled "Such and such for dummies". Allow me to quote in contrast from "The elements of Style" by Strunk and White, because it reflects a much more inspiring spirit; "No one can write decently who is distrustful of the reader's intelligence, or whose attitude is patronizing.".




#5085212 Web Services Framework

Posted by Matt-D on 12 August 2013 - 09:46 AM

Add one more to the list: http://code.google.com/p/staff/

 

gSOAP looks more like C than C++: http://www.cs.fsu.edu/~engelen/calc.html

 

In comparison: http://code.google.com/p/staff/wiki/BriefExamples#Simple_service_and_client:_Calculator

// Although the use of deprecated std::auto_ptr is also worrying, nowadays one should use std::unique_ptr instead.

 

There's also Qt SOAP: http://doc.qt.digia.com/solutions/4/qtsoap/

 

See also http://stackoverflow.com/questions/1866080/is-there-any-standard-to-consume-a-webservice-inside-of-native-c




#5084815 sorting std::vector instead of array

Posted by Matt-D on 10 August 2013 - 06:31 PM

BTW, if you're using C++11, it's a good idea to use std::begin and std::end:

http://en.cppreference.com/w/cpp/iterator/begin

http://en.cppreference.com/w/cpp/iterator/end

 

These will work both with the containers and C-style (fixed) arrays -- useful for situations just like this, when you're switching between the two.

// Although some tweaking is needed for the C-style dynamic arrays: http://stackoverflow.com/questions/15904896/range-based-for-loop-on-a-dynamic-array




#5084813 EventBus - Possible with C++ templates?

Posted by Matt-D on 10 August 2013 - 06:21 PM

OP, perhaps we could try it the other way round: could you tell us how would you prefer to *use* the code?

Forget about the design, interfaces, classes *completely* and then think of the ideal "dream code" (as in "dream team") you'd like to have at your disposal at the end -- just thinking out loud, perhaps you're unnecessarily constraining yourself with the particular OOP solution?

 

Next step, consider creating a spike solution (at least one, they're throw-away design explorations anyway).

What are your spike solutions? // See also Spike Described;




#5083124 Used old C++, Sudden feeling overwhelmed.

Posted by Matt-D on 04 August 2013 - 07:02 PM

The books you've chosen are quite good!

 

Regarding the reading order, I'd advise the following:

0. I'd start with "C++ Primer", 5th Edition by Stanley B. Lippman, Josée LaJoie, Barbara E. Moo

// http://www.informit.com/store/c-plus-plus-primer-9780321714114

This will get you up to speed with modern C++11, including giving you a very good, thorough introduction both to the core language and to the standard library.

 

1. Then, consider "The C++ Standard Library", 2nd Edition" by Nicolai M. Josuttis

// http://www.cppstdlib.com/

This will give you more practice with the standard library and drill deeper into more details.

 

2. If you still feel like a more complete, reference-style overview of the language and the library, the way to go is "The C++ Programming Language", 4th Edition (TC++PL4) by Bjarne Stroustrup

// http://www.stroustrup.com/4th.html

// http://www.informit.com/store/c-plus-plus-programming-language-9780321563842

IMHO it's not the first book to read if you're feeling "overwhelmed". Very good for the "big picture" understanding ("how does it all tie together").

 

That being said, the drafts of the first part of TC++PL4, known as "A Tour of C++", are available for free, so you may as well add these to your reading list:

http://isocpp.org/tour

 

Note that every single book in the above list is modern C++11. I would NOT advise to start with Eckel's "Thinking in C++" -- while it was actually a good book in its time (there are a lot of poor books on C++ even on the first day of their release -- Eckel's books were not among them, they were actually pretty good), it will not get you up to speed with modern best practices. You may consider it as a future reading list TODO item, but definitely not the one to start with.

 

BTW, Scott Meyers is currently working on "Effective C++11/14" (so you may want to consider ordering these, should be out by the time you're done with the others):

http://scottmeyers.blogspot.com/2013/08/two-videos-coming-many-videos-past.html

There's also link to videos by Scott Meyers there -- http://www.aristeia.com/videos.html

 

This reminds me of Channel 9 :-)

http://channel9.msdn.com/Tags/c++

 

I can DEFINITELY recommend videos by Stephan T. Lavavej (yes, his initials really are STL, how awesome is that ;]) to everyone -- 100% AWESOME!

http://channel9.msdn.com/Tags/stephan-t-lavavej

I may sound too positive here ;-), but it's so rare to find high quality videos on C++ on the Internet, that it makes the exceptions all the more praiseworthy :-)

 

In particular, you just HAVE to watch the following series:

- C9 Lectures: Stephan T. Lavavej - Standard Template Library (STL): http://channel9.msdn.com/Series/C9-Lectures-Stephan-T-Lavavej-Standard-Template-Library-STL-

- C9 Lectures: Stephan T. Lavavej - Advanced STL: http://channel9.msdn.com/Series/C9-Lectures-Stephan-T-Lavavej-Advanced-STL

- C9 Lectures: Stephan T. Lavavej - Core C++: http://channel9.msdn.com/Series/C9-Lectures-Stephan-T-Lavavej-Core-C-

 

In fact, it may be a good idea to start watching the STL series alongside reading a book #0 on these topics, this will give you even more examples/details/explanation.

(For instance, right there in the first lecture he talks about capacity-vs-size in std::vector and the practical allocation details, which is pretty good when trying to understand what to expect and what not to worry about).

// Perhaps do it after doing the exercises in the respective chapter(s), though -- note that these videos are not introductory themselves.

 

Herb Sutter is also rewriting the Guru of the Week (GotW) series for C++11/14:

http://herbsutter.com/gotw/

 

See also "The Definitive C++ Book Guide and List": http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list

During learning, you may also find cppreference.com helpful: http://en.cppreference.com/w/

// Note "6 June 2013: The C++03 and C++11 standard libraries have been completely documented."

// There are plenty of not-so-maintained references for C++ on-line, so it's important to be picky ;-)

 

// EDIT: I've also noticed you've asked about the exceptions, etc.

// While unfortunately it hasn't been updated to C++11 yet, C++ FAQ is a good place to reach for whenever you have questions like this, if anything to get additional information to form an informed perspective: http://www.parashift.com/c++-faq/

 

TL;DR:

http://herbsutter.com/elements-of-modern-c-style/

http://klmr.me/slides/modern-cpp/

// OK, this is tongue in cheek, seriously, go read the books ;D




#5083098 List of C++11 compliant compilers?

Posted by Matt-D on 04 August 2013 - 03:54 PM

It would be nice if there was some real effort to get Clang on Windows. I'd be real tempted to switch to it from GCC considering what I've seen with error messages and C++11/14 support.

 

There is one, although it's non-free: Embarcadero C++ Builder XE3, http://isocpp.org/blog/2012/12/embarcadero-c-builder-xe3

Haven't had any experience with it, though.

 

 

The standard library is part of the language in any non-standalone implementation of the language. As per the language standard.


When I say "part of the language", what I mean is sitting down and writing code without including any headers. Anything that you can write in this case is "the language". Anything else is an add-on.

I do get that STL is part of the design standard and considered part of C++, however, this breaks what I have always considered a computer language. I don't think anyone would argue that "printf" is part of the C language; It is part of the standard IO library. However, somehow, std::cout is considered part of C++. This is the case even though you can write entire programs and never touch STL. Granted, doing so is akin to shooting yourself in the foot just for the heck of it.

 

 

Technically, what you're referring to is called "the C++ core language" or just "core C++" (standard C++, the core language).

Parts of STL have become incorporated into the C++ standard library (standard C++, the standard library).

This division is reflected in the ISO C++ standardization committee itself, with "core working group" (CWG) and "library working group" (LWG):

http://stackoverflow.com/questions/13221593/what-does-core-language-mean

// However, there are also WGs such as Evolution (aka EWG) or Library Evolution (LEWG) and further (sub)divisions: http://isocpp.org/std/the-committee

 

wg21-structure.png

 

Together, the C++ programming language itself is defined in the C++ standard (and a compliant implementation, whether just a compiler or an entire IDE, has to ship both the core language and the standard library in order to be considered a compliant C++ implementation).

 

Hope this clears things up ;-)






PARTNERS