Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 16 Mar 2005
Offline Last Active Yesterday, 11:23 AM

#4846123 Declaring class objects as a pointer or not

Posted by rip-off on 08 August 2011 - 04:50 AM

I thought, that he as a beginner should just allocate all of them on the heap, so that he doesn't get problems like for example stack overflows. I simply should've said that

Even still, no.

Stack overflow is a rare problem, mostly caused by excessive recursion or placing large arrays on the stack (or objects that contain large arrays). It is also generally easy to fix.

Allocating data on the heap can lead to lots of common problems, including memory leaks and bad pointers. These problems can be difficult to track down and fix. It also is a bad idea to do dynamic allocation when it is unnecessary from a performance standpoint, because each allocation/deallocation involves traversing some data structure. Stack allocation is virtually free in comparison.

#4845932 Exit game from another class (Input)

Posted by rip-off on 07 August 2011 - 04:19 PM

If you don't mask out the bits, you may erroneously treat a just-released key as a pressed key for a frame. This may or may not be noticeable, depending on what you do when a key is released.

#4845283 Exit game from another class (Input)

Posted by rip-off on 05 August 2011 - 06:14 PM

I havn't tried anything as I have no idea what a SHORT is and what the method will return. I guess that a SHORT is somekind of string or "datatext", but I have no idea what it will contain when the method has been called. There's no info about this in the MSDN :/

My link was supposed to inspire you to do some independent research. Throwing "GetASyncKeyState" or "GetASyncKeyState example" into Google yields lots of results. They may be of varying quality, but you should get a good idea how to call this function by viewing a few examples.

So if the button is pressed, the return value should be 1? And if it isn't pressed, it should be 0?

No. If it were that simple, it would be documented as doing that.

#4844637 Exit game from another class (Input)

Posted by rip-off on 04 August 2011 - 01:18 PM

sizeof() is a compile time construct. You cannot use it to determine the size of a dynamically allocated array. The result of the expression is the sizeof of the pointer variable. Instead, consider using std::vector<char> as your buffer. It won't leak, unlike your current code.

Your current code sets "left" or "right" to "true" when any activity occurs on the keyboard. Instead you should inspect the members of the RAWKEYBOARD structure, and determine if the key has been pressed or released.

Another option is to use GetKeyboardState() or GetASyncKeyState(), rather than using raw input.

#4844505 SDL Surface not appearing

Posted by rip-off on 04 August 2011 - 08:47 AM

Because you used the "int" keyword, you are creating new, unused variables and giving them values. If you want to assign to your member variables, instead write:
xPos = 490;
health = 100;

#4844439 Review My Code? [OBJ and JSON Converter Open Source Project.]

Posted by rip-off on 04 August 2011 - 05:10 AM

Use an actual license, don't make up your own.

Console utility programs should be silent unless they have something interesting to say. You can add a "--verbose" command line switch to enable stuff like the statistics and printing data to screen, and a "--debug" switch to enable things like "reading from file X". Console programs that operate on files usually can handle an arbitrary number. You can loop over argv to do this (obviously you must skip any switches you might add).

You might want to abort the operation and warn the user if the output file already exists. You can add a "--force" switch to allow the user to override this.

It would be a good idea to split the code into three components:
  • A model class
  • A class or pair of functions that loads/saves OBJ files
  • A class or pair of functions that loads/saves JSON files
It would then be easy to add a new file format - you don't need to write FooToOBJ(), OBJToFoo(), FooToJSON(), JSONToFoo() (etc, etc), you just need Model loadFooFormat(std::istream &) and void saveFooFormat(std::ostream &, const Model &).

As for code style itself:

  • Consider breaking your functions into smaller helper functions.
  • Your converter probably shouldn't be a class.

    • It maintains no useful state that couldn't be trivially transformed into a function parameter.
    • Maintaining this state is a bad idea, because it could lead to bugs if you don't reset everything
  • The class name is dubious, "Manager"? What does it manage?
  • You've included a virtual destructor, but I don't think the class is designed for inheritance.
  • Error handling is poor to non-existent.
  • If the file is corrupt your parser doesn't seem to contain much code to detect this.

#4844409 Exit game from another class (Input)

Posted by rip-off on 04 August 2011 - 03:05 AM

Crashes on build? Be clear - is the problem at compile time or run time. "Crash" is a verb used to describe a running program terminating unexpectedly - so "crashes on build" would imply the compiler crashed, which is hard to believe.

You don't have enough error checking. GetRawInputData() has an error code, you should be checking it. When dealing with external APIs, you should check every single function call - otherwise you'll get strange behaviour as your code will continue trying when the API has signaled something is wrong. Your code seems to assume there is raw input ready, I don't know if this is safe.

Your symptoms sound like a classic case of a buffer overflow. Have you taken a look at the sample code?

I tried using the basic windows message for input before, but it didn't work for some buttons, like the Esc button, and then I was recommended to use the Raw Input.

Raw input is more complicated that basic windows messages. If you can't get the basics to work, then the chances of getting raw input to work are slimmer still.

#4844252 Exit game from another class (Input)

Posted by rip-off on 03 August 2011 - 05:07 PM

0x00000000 is null, yes. There is no GetInput() method in the last version of Input.cpp you posted. Are you sure it is in the GetInput() function? The line mentions creating a vertex buffer, which means it probably is in your graphics code (or should be - decouple input from rendering if not).

I don't really see why you are using raw input for reading from the keyboard, what is wrong with reading windows messages in a window procedure? It is probably the simplest solution here.

How can my d3dd3d and hWnd be null depending on the code...

I have no idea how these variables are being passed/referenced. Use your debugger to inspect these values at various critical points in the code (the point where they are first assigned, points where they are passed to subsystems, etc). You should look for any times the pointer is incorrect.

This article might help, depending on the root problem.

#4844221 Exit game from another class (Input)

Posted by rip-off on 03 August 2011 - 03:40 PM

That is a null pointer access. One of the values in that expression involves dereferencing a null pointer. Use your debugger to check the values and check whether they are all valid.

#4844009 should i use multithreading in my game ?

Posted by rip-off on 03 August 2011 - 05:48 AM

Ah, you have a problem I forgot to mention:

Problem #3: No problem

Your processor is many, many times more powerful than the top end ones available in the height of 2D gaming. Orders of magnitude higher. 3.X gigahertz per core compared to maybe some hundred megahertz at most. Surely you're going to struggle to actually find enough work to do to keep all these cores busy. The chances are that a 2D game that actually is doing that much work - simulating that many active objects - is either written poorly in terms of the algorithms used, or is simply simulating far too much stuff to be actually playable and fun, or both *.

Multi-threading is hard. Not impossible, but it has a high up-front cost in design time and continued costs when extending and maintaining the code. It is totally not worth it for such a non-problem.

I recommend you actually write a fun game first. That is, in many ways, an equally hard problem. The good thing though is that when you're done at least you have a fun game. When you finish putting multi-threading into your code, you still have a long way to go to make a fun game.

Example solution #1: Drop this worry as unfounded

Example solution #2:
#include "some-thread-library.h"

void busy()
    while(true) { } 

int main()
    int n = library::countProcessors();
    for(int i = 0 ; i < n - 1 ; ++i)

    return runGame();
Being a good programmer is about solving actual problems, not inventing non-problems. Don't be a poor programmer.

* Exceptions: Games making heavy use of something like fluid mechanics as a core gameplay mechanic

#4843953 App developer gets screwed out of $55,000

Posted by rip-off on 03 August 2011 - 02:15 AM

I don't think the original blog post was particularly whiny. They weren't complaining about the money itself, they had written off that. They were illustrating/complaining about how the Amazon app store is portrayed versus how it actually works.

What makes us mad though is the public perception that Amazon pays developers to be featured. Every single person we asked on Twitter or via email thought they were helping developers out, and getting a free application. Amazon does nothing to dispel these rumours, in fact they put really restrictive clauses at the bottom of their emails, saying that no one is even allowed to discuss these back door deals they are doing.

This and the bullet points that follow are things which one wouldn't necessarily discern from reading the contract.

I believe they are offering their story as a cautionary tale. The linked blog post is a poor overreaction to the original.

#4843511 [C++] Why .exe contains class/struct names?

Posted by rip-off on 02 August 2011 - 05:46 AM

I have two structures defined: managerHeightmap and managerTextures. They both are used in same way: global variables, never passed as pointer/reference, never typecasted/copied, they aren't polymorphic. However exe contains managerHeightmap but not managerTextures.

Are they both actually used? Unused global variables may be discarded - I believe even if their constructors have side effects.

If they are used, does either or both have a constructor or destructor? If one does and the other doesn't might explain why one is mentioned and the other isn't.

#4843504 [C++] Why .exe contains class/struct names?

Posted by rip-off on 02 August 2011 - 05:11 AM

... it didn't store my structure's name, but stored string structures.

I cannot parse this. Can you give an example of what you mean by "stored string structures"?

One reason I can think of is RTTI, e.g. when using exceptions.

#4842828 Cross reference in the same header

Posted by rip-off on 31 July 2011 - 08:16 AM

I would avoid doing that. Designing headers such that they cannot be included in isolation is brittle. Split the file into two headers if you wish, but ensure they work in isolation - e.g. they have the correct namespace declarations.

#4842057 [C++] When a class doesn't need to be a class

Posted by rip-off on 29 July 2011 - 03:42 AM

If the caching is purely an optimisation, I generally wouldn't mind hiding it from the interface.

Better ensure there are no bugs in it though! They say there are two hard problems in computer science:
  • Naming things
  • Caching things
  • Off by one errors