Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


Trienco

Member Since 22 Aug 2001
Offline Last Active Yesterday, 10:02 PM

#5232089 Defning a namespace class in a header file

Posted by Trienco on 31 May 2015 - 10:13 PM

I doubt it even gets to the point of worrying about a constructor. Without knowing the actual size of the class, it doesn't even know how much space the instance will need on the stack. In the same way, if it was a member variable, it couldn't determine the size of the enclosing class.




#5229257 Basic 2D array C problem

Posted by Trienco on 15 May 2015 - 10:38 PM

I've worked with too many people that never managed to completely transfer from C to C++ and still stick to all their bad C-habits. You can often tell which code was written by a former C programmer. Examples (vaguely based supposed _C++_ on code at work):

 

typedef MyStruct {...} MyStruct_t;
 
int myFunc(void) { ... if (success) return 0; }
 
 
void stringStuff() {  if (str.compare(str2) == 0) {...} }
 
void func()
{
    int a;
    int b;
    int c;
    a = 0;
    b = 1;
 
   ...
   if (blah)
   {
       if (moreBlah)
       {
            c = func();
            otherFunc(c);
       }
   }
}
 
 
class MyClass
{
public:
    MyClass() {
        member1 = 0;
        member2 = 0;
        member3 = 0;
    }
};

 

So, unless your future career is pure C89, do yourself and any future coworkers a favor: skip C and don't pick up all those habits in the first place.




#5227882 Trying to write my own GUI with SFML

Posted by Trienco on 07 May 2015 - 09:59 PM

Just as a quick example what you can do with a relatively simple approach described by AnnaMarie and me: 

 

While I wouldn't use the code as example for great coding, the GUI code is relatively well separated (the gui directory has the classes with the logic, the renderer has the functions to draw the elements).

 

http://festini.device-zero.de/source.7z




#5227232 Trying to write my own GUI with SFML

Posted by Trienco on 04 May 2015 - 10:12 PM

I usually go with a base class that contains a child container. Child elements are sorted (either top to bottom). To draw elements, I disable the z-test (too annoying to offset each element) and draw bottom to top (you can use the parent element dimensions as viewport to get automatic clipping).

 

When processing clicks, you go through the children top to bottom, so clicks are automatically processed by the right window and you don't need to care about overlapping. You might also want to have some kind of mechanism that allows "useless" elements to forward clicks to their parent element (a button might contain a label, yet when you click the label you generally want the button to trigger). Again, the easiest way is to avoid special handling and make the default event handler call the parents event handler.

 

Also, after you figured out which element was clicked on, loop over it and all parents and move them to the front (or back, whichever is "top") of their own parents respective child list.

 

Getting a basic ("static") GUI to work is usually pretty trivial, up to the point where you add drag & drop (which is unfortunately often the most convenient way to do stuff).




#5220616 c++ Instantiating a class in another class

Posted by Trienco on 01 April 2015 - 01:18 AM

@Wooh,

 

It may not be necessary but it's probably good practice, just like putting return 0, at the end of main.

 

Mike

 

One is explicitly returning the exit status instead of relying on an implicitly generated default, the other is changing global state after it has any meaning or use and right before it gets destroyed anyway.

 

So I would rather compare it to setting a member variable to 0 in your destructor.




#5193196 Why only rotation in degrees?

Posted by Trienco on 16 November 2014 - 11:11 PM

Take a long look at the actual vectors that make up your rotation matrix. Eventually you should notice (depending on how your game defines its axes) that the rotation matrix is nothing else but the forward, right and up direction vectors. No point in additionally storing angles. If you need to apply the rotation, just use the matrix, if you need to move, just use the vectors from the matrix. That's basically the benefit for using a representation that is more wasteful than others (angles, quaternions, two vectors).

 

One major downside if you keep accumulating rotations in a matrix is that small errors will also accumulate and force you to re-orthonormalize the matrix every once in a while.




#5190084 weird header behavior in gcc and llvm

Posted by Trienco on 29 October 2014 - 11:21 PM

Header guards do exactly zero to help with circular includes. In fact, they are causing the problem in the first place (kind of, otherwise you'd get endless recursion and are still screwed). You use them to prevent getting multiple declarations of the same stuff for one source file. You have the opposite problem where your circular includes result in one header trying to use something from the other header before it was ever declared (no matter which order you include them).

 

A says "copy everything from B to here, because I need it declared before my stuff", then B says "copy everything from A to here, because I need it declared before my stuff". But A already declared the header guards and nothing is included. You end up with a file in the order B - A - source. B is trying to use stuff from A that is only declared further down and you get errors.

 

The two steps when you have circular includes is a) ask yourself if you screwed up the design when you introduced circular dependencies in the first place and if your answer is "no" then b) break them with forward declarations.

 

Okay, technically you should prefer forward declarations anyway (at least if you just need a single class or two... but then, if you find yourself needing declarations for dozens of functions from somewhere else in a header file, you probably got bigger problems).




#5189335 Error: unique_ptr constructed with null deleter pointer

Posted by Trienco on 26 October 2014 - 10:45 PM

Since the error is about the default constructor, I'd blame it on the map creating a default constructed object if you call the [] operator with a new key. Are you getting the same error for simpler containers like a vector or if you try using a shared_ptr? Maybe some detail about the operator [] implementation is screwing you over and tries to copy a default constructed unique_ptr?

 

emplace is definitely more likely to work than insert. Does the error move somewhere else if you comment out the code in add_resource? If not, the problem really seems to be limited to adding it the proper way. If it does, the problem is probably more fundamental than that.




#5187575 What a web I Weave !

Posted by Trienco on 16 October 2014 - 10:04 PM

Once you reduced the size of a file from 3.5MB to 2.5MB by replacing space indent with tab or simply appreciating the fact that you can set the tab width in your editor according to your own preference, you will start wondering why anyone in their right mind would even consider spamming spaces to indent stuff.

 

"But then it won't look the same in all editors". Yes. Exactly. I can tailor the readability to my personal preference without impacting anybody else.

 

"But then stuff on different lines might not be properly aligned anymore". Well. True. Except we're writing code, not ASCII art. So stop creating those neatly aligned variable declaration blocks (unless you really ARE still using an ancient C compiler that enforces bad programming practices).

 

Now, mixing tabs and spaces is just awful, horrible and completely ruins your formatting pretty much everywhere except in the editor of origin...




#5185448 Lines of code language comparisons

Posted by Trienco on 06 October 2014 - 10:34 PM

Well, here's an empirical fun fact I basically get to observe every day: a bad programmer solves trivial problems in less time and with more lines of code than a good programmer. Why? Because rather than putting some thought into it, he simply brute forces it. Because he doesn't bother with refactoring his result for better readability and maintainability. Because rather than refactoring, he just copy/pastes existing blocks of code all over the place.

 

So by producing way more code in less time, he's clearly more productive, right? If your answer is 'yes', you are probably a project manager...




#5183240 Source control with git hub/lab?

Posted by Trienco on 26 September 2014 - 11:04 PM

Really? A new branch for every change? Never heard anyone doing that.

 

That's pretty much the basic rule when using git. You could even go as far as saying "master is for merging, not for developing". Just make it your goal that master is always in a decent state, builds and isn't a messy construction site.

 

Of course the usefulness of heavy branching varies. If you're working alone and have a habit of finishing on thing before you start working on something else, then branching is somewhat pointless. If you work on a team or keep working on multiple things at once, then you absolutely should get into the habit of using on branch per "thing" (if you say "feature", keep in mind that a bug fix for something that was already merged is a "feature" as well).

 

We're using Gerrit at work, which requires all changes that get pushed to be reviewed before they get merged to master (plus, a build is automatically started to verify the project still compiles... in the future, we might make unit tests still passing a requirement as well). Since every commit turns into a review, you will quickly learn to appreciate keeping every feature separate and tidying up your branch history with interactive rebases before pushing. Of course some people just dump several changes into one, creating an absurd number of changed files, making reviews seriously annoying (because you never know which changes actually belong together) and then complain if they need to fix something, because it's all just one big mess.

 

If you find working (and especially branching) with git unwieldy, I'd suggest GitExtensions. It lets you do most tasks without needing a command line and gives you a very good overview of your repository structure. Alternatively, SourceTree looks extremely similar.




#5181188 reading float from binary file (endian problem?)

Posted by Trienco on 17 September 2014 - 09:39 PM

Even if you're now looking at the correct container, all these "if's" look like a horrible idea. At this point your code should abort and return an error, not happily continue on to create a corrupt and broken file, so you can then spend days debugging your file reading function or wonder about endian issues.

 

Alternatively, at least write some kind of marker that will tell your reading function if a block exists or not.




#5179004 C++ starter

Posted by Trienco on 08 September 2014 - 09:38 PM

Somewhere in Visual Studio there's the well hidden property manager and from there you can find property sheets for your user. That's where I dump all paths to headers and libraries I use a lot (boost, etc.). In some Express versions it's hidden even more behind first having to change to some "expert view".




#5178795 How difficult(or not) do you find these C++ tests?

Posted by Trienco on 07 September 2014 - 10:15 PM

Is it weird that my answer to most questions about the output of the program was: "the responsible programmer losing his job"?




#5174919 Destructor in vector called too often

Posted by Trienco on 19 August 2014 - 10:12 PM

There was a known bug when using the initializer list for containers (especially visible when storing smart pointers) which always screwed up the first element. If you didn't already, make sure you have the latest updates installed (I'm relatively certain that the problem was fixed in one of them).

 

Not necessarily your problem, but I spent all afternoon debugging why it's always the first element in my map that was invalid immediately after it's definition. Turned out plenty of other people saw similar bugs with other container types.






PARTNERS