Jump to content

  • Log In with Google      Sign In   
  • Create Account


fastcall22

Member Since 04 Feb 2008
Online Last Active Today, 11:35 PM

#5001388 storing random unique values in a vector

Posted by fastcall22 on 15 November 2012 - 05:46 PM

How do I shuffle a sequence of random numbers that do not repeat

Hi Phil,

By default, the seed used by the random number generator is set to a default value each time the program starts. This causes the same sequence of random numbers. You will need to reseed the random number generator before using it.

Use the time function under the ctime header to get the current time (number of seconds since the epoch), then use that to seed the random number generator using srand:
srand( (unsigned)time(0) );



#5000363 "Frames" per second in Win32 console application

Posted by fastcall22 on 12 November 2012 - 04:55 PM

Off the top of my head:
timeGetTime (winmm.lib, millisecond resolution, and deprecated?)
GetPerformanceFrequency and GetPerformanceCounter (highest resolution timer, may be inconsistent across multiple cores)

There may be others...



#4997653 How to avoid singleton

Posted by fastcall22 on 05 November 2012 - 12:07 PM

I know I shouldn't use singletons a lot. How can I avoid this?

You can use a factory, then pass the factory around:

class Factory {
private:
    map<string,Entity*> prototypes;

public:

    Entity* create(const string&amp; name) {
        if ( prototypes[name] )
            return prototypes[name];
    }

    // Or:
    // template<class T>
    // T* create() { }
    // template<> Fireball* create() { return (Fireball*)prototypes["fireball"] /*or etc...*/; }
};

struct Game {
    Factory f;
};

void test( Character&amp; c, Factory&amp; f ) {
    c.doSkill(f.create("fireball"));
    // or: f.create<Fireball>()
}
Or etc...

You can also use const char* over std::string if you need to avoid frequent memory allocations there..


#4995188 TicTacToe v.1 - Test and Feedback.

Posted by fastcall22 on 29 October 2012 - 05:05 PM

What I am looking for now is some feedback from this wonderful community as to what I can improve before I enter my final stage of development.

If you want quality feedback, you might want to consider releasing the source code as well.


#4993536 Merge Sort

Posted by fastcall22 on 24 October 2012 - 02:15 PM

You cannot return m in merge because merge is defined not to return a value and m is an array on the stack. As soon as merge returns, the memory that contained the array for m is destroyed/trashed/undefined/etc. You will either need to use dynamic memory allocation (malloc and free) to create and return the array m, or use the in-place flavor of merge sort.

Also, watch your ipos and jpos carefully, as a[jpos] is an invalid element when jpos == k == ARRAY_SIZE.


#4993465 Favorite little known or underused C++ features

Posted by fastcall22 on 24 October 2012 - 10:04 AM

My favorite hardly ever used part of C++ is pointers to members, if for no other reason than the fact that they're generally a counter example for memset() to 0 being the same as C++ assignment with 0.


Pointer-to-members are also my favorite part of the standard, though not for the same reason. I recently found myself doing this:
template<class T>
const Point2_t<T> Rect2_t<T>::corner( int idx ) const {
	assert( 0 <= idx && idx < 4 );
	T Rect2_t<T>::* const (&out)[2] = corners[idx];
	return Point2_t<T>( this->*out[0], this->*out[1] );
}

template<class T>
T Rect2_t<T>::* const Rect2_t<T>::corners[][2] = {
	{ &Rect2_t<T>::left, &Rect2_t<T>::bottom },
	{ &Rect2_t<T>::right, &Rect2_t<T>::bottom },
	{ &Rect2_t<T>::right, &Rect2_t<T>::top },
	{ &Rect2_t<T>::left, &Rect2_t<T>::top }
};

It's... It's beautiful (in a sadomasochism sort of way...)


#4991328 [SFML] I Finished Breakout! It's So Awesome!

Posted by fastcall22 on 17 October 2012 - 09:58 PM

Good job, that was quick. I was unable to play it though, it says MSVCP100D.dll is missing.

I'll fix that, it seems you must have Microsoft visual studio installed to run it. I'll fix it tomorrow. Try downloading the source code and then compiling

You'll need to compile your project in the "Release" configuration before distributing.


#4990786 New to C++ from C#, is my pointer usage correct?

Posted by fastcall22 on 16 October 2012 - 10:31 AM

I can't comment much about the "correct" way to do an entity-component system. Though, I can comment on your C++. Overall, it's pretty well written and solid, though there's a few things missing.

1) Entity will need a destructor to cleanup its components.
2) Entity and EntityManager will either need the Rule of Three satisfied or made copyable in order to prevent object-ownership issues on (accidental or otherwise) copying Entities or EntityManagers.

Other things to consider:
1) Consider using smart pointers over raw pointers to give additional context to each pointer, add additional levels of safety, and provide automatic cleanup. (For comparison, an object reference in C# is equivalent to a shared_ptr in C++.) Since it appears your compiler has support for some C++11 or C++TR1 features (use of the auto keyword and the C++11 for-in syntax), include memory and you should be good to go!


#4988919 A strange issue with the scope of the << operator

Posted by fastcall22 on 10 October 2012 - 07:05 PM

The problem stems from the order in which arguments are evaluated. It's possible that (a copy of?) carry is evaluated first, before HalfAdder is called. It's also possible that the behavior of the statement is undefined, meaning that you may get different behavior depending on the compiler. For the sake of clarity and readability, I would suggest evaluating HalfAdder separately before writing the result:
bool result = HalfAdder(input[0], input[1], carry);
cout << "Result is " << result << " carry is " << carry << endl;



#4986418 Trouble resolving dependency properly

Posted by fastcall22 on 03 October 2012 - 09:25 AM

I'm not a graphics programmer (yet), but aren't vertex buffers dependent on the device you create them from? If this is the case, then #1 makes sense assuming you mean DXWrapper& and not ID3D11Device*. However, if this isn't an option, why not pass an intermediate class, which hides the device being used to create the vertex buffers from the interface? Something like the following:

class VertexBufferContext {
public:
    VertexBuffer create( ... );

private:
    DXWrapper& context;
};



#4984524 Why can't Bilinear/Bicubic resize transparent images properly?

Posted by fastcall22 on 27 September 2012 - 04:50 PM

I have not been able to reproduce the problem with the steps you've provided with GIMP. I have taken the first image, resized it to 50%, and added a magenta background layer, then collapsed the image onto the background, and the image has no artifacts. However, I noticed that I was able to reproduce your problem if I were to remove the alpha channel from the image then do a magenta flood-fill on the background.

Perhaps somewhere along the way the alpha channel is being lost somewhere along the way, perhaps being merged with a white background...


#4978942 My first public solution

Posted by fastcall22 on 11 September 2012 - 10:09 AM

extern unsigned int _gCurLine;
extern char* _gCurFile;

#define MEM_FUNC_CALL _gCurLine=__LINE__; _gCurFile=(char*)__FILE__;


Posted Image

For instance, try the following code:
#include "MemoryHandler.h"
#include <iostream>

class A { };

class B {
public:
    B() { MEM_FUNC_CALL a = new A(); }
    ~B() { MEM_FUNC_CALL delete a; }

private:
   B( const B& );
   B& operator= ( const B& );

private:
	A* a;
};

int main( int, char*[] ) {
    B* b = 0;
    if ( false )
        MEM_FUNC_CALL b = new B;

    EndOfProgramMemCheck();
}

You can also override new and new[] to include additional parameters and use a macro to hijack all calls to new and delete to call your overloaded version, without needing to remember to use MEM_FUNC_CALL. (Though, you do need to make sure that the macros are defined in each compilation unit.) Take a look at a demonstration of memory leak detection for Visual Studio on MSDN. It should give you some ideas.

Also:
Those aren't trees, those are linked lists :|


#4978678 Strange Memory Leaks...

Posted by fastcall22 on 10 September 2012 - 01:56 PM

Strange. Maybe here comes this? What is beeing used for?:

I've seen this code:
#define new new (_FILE_, _LINE_, _LINE);

Any suggestions?


This macro tags all of your "new" with additional information -- the file and the line wherein the allocation was made. (Though, it should be new (_NORMAL_BLOCK, __FILE__, __LINE__).) If you haven't already, take a look at the documentation on MSDN on the subject. It is also important to note that a #define in one header file isn't neccessarily applied everywhere else to your project. Remember, the contents of a header file are dumped into the source file that includes it and each source file is compiled separately. If you have one source file that includes a set of headers that eventually defines the new macro and another source file that does not, then allocations with new in the first file will get logged with the additional information and the other source will not.


#4977264 What improves better memory performance?

Posted by fastcall22 on 06 September 2012 - 10:45 AM

What imrpoves performance in memory management?

Pooling small frequent allocations into larger but infrequent allocations, favoring the stack over the heap whenever possible, minimizing the number of calls to malloc/new, and separating allocations into differnet heaps depending on expected object lifetimes*, utilizing separate heaps to improve cahce locality, and etc...

Cornstalks answered the rest...


*I'm not sure about this one; I've read somewhere -- can't remember where -- of a certain lifetime-based allocation algorithm that were used in some console games.


#4975253 C++ / C++ CLI

Posted by fastcall22 on 31 August 2012 - 04:15 PM

C++/CLI is .NET for C++. It is generally used for building applications to interface C# -- around here, I only hear of programs written in C++/CLI to interface editors written in C# and games built in C++. That is all I know. (I also hear that the implementation is frustratingly buggy, but that may have been long ago...)

C++11 ("Normal C++") is the new standard of C++ with many updated features. If you had a choice between the two, definately go for C++11. Most modern C++ compilers support C++11 natively, so pickup whichever one feel comfortable using.

However, I only recommend C++ because I am assuming you have some sort of prior programming experience. (If you do have prior programming expierience, please disregard.) It is strongly discouraged to learn programming through C++, simply because C++ assumes the programmer knows what he's doing, and that isn't always the case with beginners. Most of the time a beginner to programming spends his time fighting the C++ syntax and the idiosyncracies of C++, and that time could be better spent learning how to program with an easier language, such as Python or C#...




PARTNERS