Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 24 Apr 2010
Offline Last Active Private

#5263075 Random errors in Release builds

Posted by on 21 November 2015 - 09:13 PM

Please don't mark threads as "solved", as it discourages further discussion.

#5262909 MyClass myClass VS myClass = new MyClass()

Posted by on 20 November 2015 - 01:09 PM

I've never overflowed the stack outside of bugs and intentional tomfoolery. In fact, I find the default stack length on Win7/VS2015 to be uncomfortably large as it takes too long to overflow when I screw up a recursion or something. (Looking it up, apparently it's only 1MB, lol)
The rule of thumb is to use pointers when you need nullable, reassignable, or late-binding references, use references when you need simple references, and just do everything else by value/on the stack. Raw resource management, including 'new' and 'delete', in the application layer are officially considered an abomination. Use some kind of RAII instead, preferably the native smart pointers and containers for memory.
As you get used to this model you'll find that unique_ptr is nice to have, shared_ptr is a little bit scary (it's not hard to use, but it can sometimes mean that you're doing something silly), and doing everything in-place makes life a lot simpler.
I'm not sure whether you know it already or not, but one thing I see people get stuck on at this level is how to initialize member objects that need constructor arguments. When you see someone using pointers for all their class members it's because they don't know about this. You pass constructor args to member objects with the initializer list on the constructor:
class Foo { //Construction requires an argument!
public: Foo(int val) { value = val; }
  int value;

class Bar {
  Foo a; //How can you initialize this? It needs a ctor argument!
  Foo b;
  Foo c;

Bar::Bar() : a(42), b(16), c(-1) { //You do it like this. 
  //^ The initializers happen before the function body.

You can pass arguments from the owning ctor as well. That is, if Bar::Bar() was Bar::Bar(int arg) then you could say:
Bar::Bar(int arg) : a(arg), b(arg), c(42) { /* stuff */ }

#5262126 Why isn't this working?

Posted by on 15 November 2015 - 09:58 AM

Correct, however, I'd caution you regarding the use of member references. They can get really cantankerous if you start moving objects around. Dangling references are no fun at all. They're legal C++, but if you use them just be very careful and if you encounter a "weird" bug, suspect them immediately.

#5261349 How to do a color reduction calculation?

Posted by on 10 November 2015 - 08:40 AM

Assuming you're referring to a single color channel you just cast it to float and then divide it by 255.

#5261230 Can a full program be stored in a photo image

Posted by on 09 November 2015 - 04:19 PM

Can anyone suggest a better approach?


Digital storage.

#5260992 Possible to forward declare a sub class?

Posted by on 08 November 2015 - 06:02 AM

You don't (necessarily) need to pimpl here. The reason you were having a problem is not that you had a pointer to an incomplete type. It's that you were trying to make use of that incomplete type.
This works:

#pragma once

class One {
	int doSomething();

	class Two; //forward declaration =/= full declaration

  /* full declaration would work
  class Two {
    int test();

	Two* cTwo;

  Two* createTwo();

class One::Two { //this is a full declaration (you could also just nest it in One)
  int test();

//you could put the One::Two declaration in a different header, but you'd need to include that header anywhere that One::Two is used.

#include "derp.h"

int One::doSomething() {
  cTwo = createTwo();
  return 0;

One::Two* One::createTwo() {
   return new Two();

int One::Two::test() {
  return 0;


#include <iostream>

#include "derp.h"

int main() {
  One foo;

  std::cout << foo.cTwo->test() << "\n";

Nested types can be very handy in places where they're appropriate, but understanding where those places are is a bit advanced in terms of design. Why are you trying to nest this type?

#5260533 Smash Bros Style Camera

Posted by on 04 November 2015 - 11:58 AM

First, avoid putting long statements in switches as it quickly gets too messy. Break the subroutine out into a new function and just call it from the switch. It will make life easier for you.
Abs the vector components instead of the max value. If the zoom is inverted then it means the fractions for the ratios are inverted, so just flip them over (invert the comparison as well).
There's a cleaner way of doing this though, since you're already iterating the characters to find the camera position. Instead of finding the position and then the zoom, just find the bounding box which contains everything you want and then derive what you need from that:

Rectangle rect = new Rectangle(playerList[0].Position.X, playerList[0].Position.Y, 0, 0);

foreach (Player P in playerList)
    int x = (int)P.position.X;
    int y = (int)P.position.Y;
    if (x < rect.Left  ) { rect.X      = x; }
    if (x > rect.Right ) { rect.Width  = x - rect.X; }
    if (y < rect.Top   ) { rect.Y      = y; }
    if (y > rect.Bottom) { rect.Height = y - rect.Y; }

//You may want to increase the rect size at this point by subtracting 
//some from the x and y and adding to the width and height. Alternatively
//you could just find some factor very close to 1.0f to multiply the 'zoom'
//by before applying it.

Vector2 cameraPos = new Vector2((float)rect.X, (float)rect.Y);
cameraPos.X += (float)rect.Width  / 2;
cameraPos.Y += (float)rect.Height / 2;

//this part may be inverted??? if so flip the divisions and use max instead of min
float xZoomReq = (float)Game1.screenWidth  / rect.Width;
float yZoomReq = (float)Game1.screenHeight / rect.Height;
float zoom = Math.Min(xZoomReq, yZoomReq);

//At this point you can compare the result against min/max zoom values.
//These are numbers you should determine yourself through experimentation,
//as would be the buffer size mentioned above.

For reasons unknown to any sane person, there's no float rectangle struct, so the math is a bit rough and I may have missed some casting. You don't need super-high precision here though, since you're probably going to monkey with the final result a little anyway.

#5260464 Smash Bros Style Camera

Posted by on 04 November 2015 - 02:49 AM

Do you have zoom functionality available? For 2D this is actually quite simple to calculate.


Once you're centered you should have an x/y world coordinate that you're placing the camera at. You need to do a little (very little) vector math on the players to find out which one is farthest from that point in the x dimension and which one is farthest in the y dimension.


To find the relative coordinate of a player from the center-point, just:


relative_pos = player.pos - centerpoint


using 2D vectors. Create max_abs_x and max_abs_y variables and set them both to zero (or some minimum value that you decide). Iterate through the players and determine their relative positions, then abs the x and y and if either of them is larger than the value in your max variables then replace that value with the new larger one. You'll end up with a pair of values representing the half-width of the minimum area you need to represent. You can add a buffer at this point if you want so that the players aren't at the absolute screen edge.


Once you have that you just need to consider the size of the viewport compared to the result.


Say you have a viewport that's 800x600. The half-width dimensions of the viewport are 400 and 300. If your max_abs_x is 500 and your max_abs_y is 500 then you can get the ratios:


x_ratio = 500 / 400 = 1.25

y_ratio = 500 / 300 = 1.66~


So you can see that the larger of the two is the y, so you need to zoom to a factor of 1.66~.


Depending on how your zoom is implemented you may be able to just plug that in directly.


You'd only need to get a little more complicated if you're working with a 3D'ish camera and need to move the camera back and forth or play with the field of view.

#5259974 How do I re-size a binary file?

Posted by on 01 November 2015 - 05:54 AM

Still can't just post the whole thing?


That is so frustrating

#5259952 NonCopyable everywhere ?

Posted by on 31 October 2015 - 08:30 PM

I use NonCopyable thoroughly in my engine for a few reasons
- it's possible to implement copying but YAGNI, so I'll only do it when I need it rather than needlessly writing unused code. I find YAGNI to be an important way to not end up in permanent engine-development hell.
- It's possible to implement but would be a significant performance cost, and there's probably a better way to do whatever it is that is making you think that you need a copy, so I'm discouraging you.
- multiple copy semantics are equally valid (e.g. deep/shallow), and I don't want an automatic opetator to have vague or unexpected behaviour, so copying via explicit named functions is available instead.
- Copying *could* be possible, but by assuming that it can't happen, I can simplify the code design (e.g. maybe it means I can avoid reference counting), plus YAGNI.

FWIW, I also use a NoCreate class, which has private, non-implemented constructors and destructors smile.png


Sure, it doesn't bother me in application code, but when I find it in APIs it makes my eye twitch.

#5259951 Trying to initialize an array created with 'new'

Posted by on 31 October 2015 - 08:24 PM


The difference is that [3] would cause a compile error if the literal didn't have three members (stops you from making mistakes).

It results in a compile error if you have too many, but not if you have too few.


Wow, that's gross.

It would appear that VS2015 also ignores the braced initializer for array new, and does not indicate a syntax problem. I'm sending a report presently.

#5259838 NonCopyable everywhere ?

Posted by on 31 October 2015 - 12:48 AM

I keep finding non-copy and non-move on things that should have one or both. It's like shorthand for "I don't know how to write proper copy/move ctors".


If you want a class which owns a resource to be copyable then you have to copy the resource. If you want it to be movable then you have to transfer the resource and then break it's ownership ties in the original object.

#5259538 Tree Node Deletion

Posted by on 29 October 2015 - 05:00 AM

Your "tree" is actually a linked list with a kink, which is weird. A node in a tree should add a certain number of children to itself recursively, using some rule to determine what child to recurse to or take ownership of the element as a new child. When this tree has children added to its root, it just appends them as siblings to its first child.
Anyway, when you remove an item from a doubly linked list it's important to reset the 'next' pointer of the previous node. That's not happening here, so you're getting undefined behavior.
Get a sheet of paper and try the following exercise:
Use a pencil (with an eraser) to draw the list as a "train" of squares. Each square should have the words "next" and "prev" written in it. If either of them is null then put an X through it. Otherwise draw an arrow from it to the car it points to.
Update your drawing of the "train" for every action that does any of the following:
  • Creates a node
  • Deletes a node
  • Changes the value of a "next"
  • Changes the value of a "prev"
Now, using your Node class, do the following operations within main:

  • Create three new nodes and store them in pointers called "first", "second", and "third".
  • Pass "second" to "first" using AddSibling().
  • Pass "third" to "first" using AddSibling().
  • Delete "second".
  • Examine the state of the train carefully. You should see a serious problem with your root node.
Incidentally, while it's very important to be comfortable with the concept of pointers and indirection, modern C++ makes a tremendous effort to avoid this kind of code by providing a lot of new tools. The reason is that it's really easy to screw this stuff up and get undefined behavior, which may or may not appear until much later. Once you feel comfortable with the language you should check out some articles on what's new in C++11 and C++14.

#5259221 Excuse me < How do I create Models/Objects for my game?

Posted by on 26 October 2015 - 07:48 PM

Blender is actually on par with the ridiculously expensive competitors, and personally I felt it has better documentation than Max. Krita is another Photoshop alternative I've been looking at lately. It takes a little getting used to but it has some interesting tools. You can get it for free if you agree to GPL rules, so it depends on what you're doing with it.

#5259219 The wrong way to count lines of code

Posted by on 26 October 2015 - 07:22 PM

[server hamsters duplicated my message. It wasn't my fault. promise.]


We all know you were just trying to sneak in an extra Line of Convo.