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!


Member Since 24 Apr 2010
Offline Last Active Today, 06:43 PM

#5213021 Initialize vector of vectors after declaration.

Posted by Khatharr on Yesterday, 10:36 PM


std::vector<std::vector<int> > myArray;

You must realize that this is quite improper.

You have used template of a variable memory type as another vector template type.


I don't know if I'd call it "improper". OP wants member containers of static length, so a std::array is better than a std::vector. A vector of arrays will be contiguous in memory, which is nice. However, sometimes you actually do want a variable-length container of variable-length containers, so a vector of vectors would make sense. The templates don't really have much to do with it. It's perfectly legal to nest template types.

#5213017 Read data from specified adress of memory.

Posted by Khatharr on Yesterday, 10:04 PM

If it's a single value rather than an array of values then you can just cast it. In any case, he needs to put more effort into explaining what he's talking about before we should start trying to answer him.

#5213012 Read data from specified adress of memory.

Posted by Khatharr on Yesterday, 09:09 PM


He doesn't want to read and write, he wants to read and write.

He doesn't want to "he doesn't want to read and write, he wants to read and write", he wants to "he doesn't want to read and write, he wants to read and write" with variables.





(I think he's asking why he can't deref a void pointer.)

#5213010 Read data from specified adress of memory.

Posted by Khatharr on Yesterday, 09:07 PM

You are not making any sense. Perhaps you can give us some context, so we can understand what you are asking.


He doesn't want to read and write, he wants to read and write.

#5212074 Initialize vector of vectors after declaration.

Posted by Khatharr on 21 February 2015 - 12:26 AM

Addressing the adjacent issue, std::array allows for static-length containers.

#5212072 Easiest way to pass a 2D array?

Posted by Khatharr on 21 February 2015 - 12:20 AM

Generally in release build a vector of vectors will be just as fast as a raw array...

Double indirection is a lot harder on the cache than linear data. Unless the graph is unusually large, it's likely that linear storage will allow the whole thing to fit in the L1 cache at once. This is not the case with a vector of vectors, since the addresses of the storage in the member vectors are not necessarily cache-aligned, and even if they were, there's a chance of cache thrashing if two of them happen to be aligned to the same cache line.


There's a simple solution to this. Here's a quick-and-dirty implementation:

struct TwoDee {
  TwoDee(int rows, int cols) : data(rows * cols), WIDTH(cols), HEIGHT(rows) {

  TwoDee(const TwoDee& other) : data(other.data), WIDTH(other.WIDTH), HEIGHT(other.HEIGHT) {

  int& operator()(int row, int col) {
    return data[(WIDTH*row) + col]; //<< This kind of lookup is specifically optimized by the CPU!

  const int WIDTH;
  const int HEIGHT;

  std::vector<int> data;

Then again, there's no telling what's actually being stored in the graph, so it's possible that we're talking about the difference between double and triple indirection, at which point the matter starts to become slightly less relevant.

#5212066 Curious about Save Protection Methods

Posted by Khatharr on 20 February 2015 - 11:41 PM

You're going to need to bundle a GPS dongle and make your game require a clear and constant signal in order to run. When the game loads a save you need to run a twelve-way hash/CRC/checksum on it. If any modifications are detected, immediately wipe the hard disk and email the GPS coordinates to your 'enforcement' department so that they can travel to the indicated location and deploy napalm.


This should give you about 24 hours of protection. The someone will bypass the dongle detection.

#5210783 Just starting out: Opinions on things like GameMaker?

Posted by Khatharr on 14 February 2015 - 11:56 PM

RPGMaker isn't really bad for what it's designed for. It's just overpriced (unless you get it on sale) and people seem to use it to publish half-cooked games more often than games worth buying. I consider it to be a learning aid, since it gives you a vague idea of what a tool set should look like and if you study the default RGSS scripts you can learn a decent amount about flow control and general game architecture, as well as most of what you'd ever want to know about how 2D JRPGs work.


It's actually a really great starting point, but not so good as a professional development platform, since RM games have earned a poor reputation in stores. Game Maker is a slightly more capable version of the same thing, really. GM seems really nice at first, but you'll soon find that dragging and dropping is amazingly slow compared to learning the GML commands that correspond to the available options. Once you're using GML you'll start wanting to make more complex systems and you'll suddenly find yourself constrained by the fact that GML is a very poorly designed language. (Seriously. If you're comfortable with RGSS then you will quickly come to hate GML.)


Rather than Game Maker, if you're looking to move forward from RPG Maker, I'd suggest SFML. Unity is another alternative, but I'm not really fond of it for personal reasons, so I'll leave that to other people to comment on. In any case, GM is not a step forward from RM. At best it would be a step sideways.

#5210780 Modern C++ Book?

Posted by Khatharr on 14 February 2015 - 11:39 PM


#5210411 Operator Overloading C++

Posted by Khatharr on 12 February 2015 - 11:23 PM


MyObject MyObject::operator+(const MyObject &obj)
  MyObject tmp;
  tmp.val += obj.val;
  return tmp;

This is off-topic, but this does not look correct. I believe you would want tmp.val to contain (this->val + obj.val).

I'm going in... Cover me...
//Operator+ (refactored to trip up JPs)

MyObject MyObject::operator+(MyObject obj) {
  obj.val += val;
  return obj;

#5209591 Clean OOP programming question

Posted by Khatharr on 09 February 2015 - 08:14 AM

(try using unique_ptr for automatic cleanup without handwriting copy constructors/operators and destructors)

Or just don't use pointers for ownership when it can be avoided. I suspect that this may be one of those cases.

My version of OP's class would probably look something like this:
class App {
public: //people reading headers usually care more about the public section, so I usually put it on top
  App(); //ctors are for initialization - exceptions are for error handling
  //There should probably be other methods here, or else something is very wrong.
  ProjectWizardWindow wizWnd; //avoid using pointers when you don't need to
  MainWindow mainWnd; //ctor args for these objects can be passed in the initializer list of App::App()
Elaborating on the use of initializer lists, if ProjectWizardWindow::ProjectWizardWindow() takes (int x, int y) as parameters, then...

App::App() : wizWnd(640, 480) {
  //ctor body
And wizWnd will construct with 640 and 480 as its arguments. You can also use args passed into the App() ctor.

App::App(int wizwinx, int wizwiny) : wizWnd(wizwinx, wizwiny) {}
Multiple items to be initialized are separated with commas:

App::App() : wizWnd(arg, arg), mainWnd(arg, arg, arg) {
  //stuff (or - more often - nothing, since you can often do everything in the list ^)

Using initializer lists will help you get away from pointer issues with member objects, which can save you a ton of grief. It also lets you have things like const members, which may cause you a little more grief if you're not up to speed on copy and move constructors.

#5209533 How do I fix this stupid linker error?

Posted by Khatharr on 08 February 2015 - 11:47 PM

Did you try turning it off and turning it back on again?

#5209243 SDL Collision wont let me collide and move another direction

Posted by Khatharr on 07 February 2015 - 04:26 AM

Looks like you're responding to collisions by disallowing motion to take place, so any time you're making a collision happen you should lose the ability to move. You don't really want to drive collision response that way. It's better to use input to apply forces to the entity and do some lightweight physics. That is, the entity has a position and a velocity; every update the position has the velocity added to it. The input influences the velocity rather than the position. This gives you access to smoother control and gets you away from the input vs collision problem.


When colliding with 'hard' objects, such as immovable walls, instead of just undo-ing the movement for the frame, determine the voronoi region of interception and change the position to resolve the collision. This will allow you to slide along walls gracefully, etc. This must be done simultaneously for all objects that the entity is colliding with, so...


* move object to new position according to velocity


* create a 'correction' vector and initialize it to 0,0


* for each 'hard' collidable object

** determine the depth of collision along the separating axis

** reverse the penetration vector to get a correction vector for that specific collision

** add it to the overall correction vector


* apply the correction vector directly to the entity's position


In order to detect the separating axis you'll want to use voronoi tesselation. If your walls are orthogonal (square) and axis-aligned then this is quite simple:






4 is 'inside' the wall, the other numbers represent individual voronoi regions for the wall. You can determine the region your entity is in by testing its centerpoint against the edges of the wall:


int region = 0;

if(center.x > wall.left) {region += 1;}

if(center.x > wall.right) {region += 1;}

if(center.y > wall.top) {region += 3;}

if(center.y > wall.bottom) {region += 3;}


In regions 0,2,6, and 8, push directly away from the matching vertex. In region 4 push away from the center of the wall. In the other regions just push in the direction that they represent. For instance, in region 1 you want to push straight upward, etc.


If your walls are not orthogonal and axis-aligned then you'll need to work out the tesselation differently before you can use SAT testing and response. If that's the case just let us know and I'll try to explain the technique. Or if any of this doesn't make sense, I can explain it in more depth.

#5202157 Looking for a good way to make games in C/C++ on linux for cross-platform

Posted by Khatharr on 05 January 2015 - 11:43 PM

AFAIK both SDL and SFML support multiple platforms.

#5201716 [WinAPI] How can I set the title of my tray icon?

Posted by Khatharr on 04 January 2015 - 05:48 AM

It turned out to be the FileDescription field in the version resource.