Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 08 Aug 2000
Offline Last Active Today, 12:48 PM

#5255091 which program I should use for a text adventure

Posted by BitMaster on 02 October 2015 - 02:37 AM

There is also Inform and TADS. You might also want to take a look at a more specialized forum.

#5254775 vector push_back creates error when multi threading

Posted by BitMaster on 30 September 2015 - 07:48 AM

No. Every time you call doit, the function creates its own mutex. As written, it is completely useless and just wastes CPU cycles. No blocking will ever happen.

You need to do something like this:

std::mutex my_global_doit_mutex;

void doit(std::vector<int>& d)
   std::lock_guard<std::mutex> guard(my_global_doit_mutex);
   for (int i = 0; i < 10000;i++)
This is the simplest possible example.

#5254771 Problem with glew.h

Posted by BitMaster on 30 September 2015 - 07:41 AM

LPRECT is just a typedef for a RECT*. You need to do something like this:
RECT rect;
if (GetWindowRect(m_Hwnd, &rect))
	m_Width = rect.right - rect.left;
	m_Height = rect.bottom - rect.top;

// rect is now freed automatically
// you never leak memory, you never need to manually delete it.

#5254344 Don't want to iterate the cells in order to find one?

Posted by BitMaster on 28 September 2015 - 01:55 AM

You can gain O(1) access if there is a suitable formula to map positions to grid identifiers. For example if your grid consists of unit length cells, starting at at (0, 0, 0) and extending in the direction of the positive axis you can trivially extract intermediate X, Y, Z integer identifiers for that cell. Mapping those three identifiers to a grid identifier should be easy then (for example Z * width * height + Y * width + X but there are other possibilities).

Depending on what exactly you are doing that might not be possible and you rather want to look at spatial partitioning, for example via octrees which gives you O(log n).

#5253987 sprite render is very slow

Posted by BitMaster on 25 September 2015 - 07:27 AM

Also, when dealing with problems like this it can be extremely helpful to add units to your values (at least in your head, although Boost has a framework to have the compiler do it), like you do in physics.

A speed has (for example) the unit pixel/second. A position has the unit pixel. The assignment
myPosition = myPosition + mySpeedPerSecond;
then should immediately give you pause because you try to add something with the unit pixel and pixel/second together. You cannot do that.

Let's check do a unit check for
myPosition = myPosition + secondsPerUpdateStep * mySpeedPerSecond;
[pixel] = [pixel] + [seconds] * [pixel/seconds] = [pixel] + [seconds * pixel / seconds] = [pixel] + [pixel]
That checks out.

#5253979 sprite render is very slow

Posted by BitMaster on 25 September 2015 - 06:45 AM

@Aardvajk I use almost the same code with yours and it can only move very slowly at 60fps. I have to raise fps to 500 to speed it up. for example, the velocity is 0.75f per second at 500fps, and it perform good. But when at 60fps, I have to increase the speed to 8.f or even more, which produce a very unsmooth movement(the object seems to be jumping on the screen).

Setting aside that your code is not very much like Aardvajk's in important details (for example you process only a single message per frame, quite unlike the original), I strongly suspect your speeds are not 'per second' as you think but 'per frame'. It's rather telling you decrease your target frame rate by a factor of approximately 8 and then have to increase your speed by a factor of approximately 8 to get similar results.

Edit: some pseudo code:

int   updatesPerSecond     = 60; 
float secondsPerUpdateStep = 1.0f / static_cast<float>(updatesPerSecond);
float mySpeedPerSecond     = 10.0f;

// you appear to be doing this every update:
myPosition = myPosition + mySpeedPerSecond;

// you should be doing this:
myPosition = myPosition + secondsPerUpdateStep * mySpeedPerSecond;

#5253807 Is it possible to optimize this? (keeping a small array in registers)

Posted by BitMaster on 24 September 2015 - 06:34 AM

Stack/heap are just language constructs to help manage the lifetime of memory allocations (resource management).
At a low level, they're both just memory addresses. Movind data onto "the stack" will do absolutely nothing here - except add the aditional cost of copying the data from it's original location to this new temporary location. It wont affect the compiler's code generation or use of registers.

But that's becouse OP's done so already, or do you mean it generaly? Stack is a life saver!

No. For the purpose of this discussion what you are suggesting is completely irrelevant and all it shows is that you are not understanding the OPs situation (and what this thread is about) and/or what the difference between the free store (commonly implemented as a heap) and automatic storage (commonly implemented as the stack) actually is.

#5253609 C++ Graphics Library

Posted by BitMaster on 23 September 2015 - 02:22 AM

It can do that too, among other things. While I have read a bit about it in the past I have not actually used it so far. The closest analogue in a nutshell is probably "something like Windows GDI" independent of any OS. That includes different output devices (a simple memory bitmap, a PDF, an OpenGL texture which was created by rendering into a framebuffer, ...).

#5253441 Linker errors Eugh!

Posted by BitMaster on 22 September 2015 - 07:41 AM

I've even tried copying the .dll into the folder to bypass and when I put -lglew32 in the linker section it just dies. It's very frustrating:

This raises an immediate alarm. The DLL is completely irrelevant at link time, what you need to care about is the corresponding .lib file (or .a file, depending on which naming convention was followed during build).

#5252297 WinAPI Keyboard Input

Posted by BitMaster on 15 September 2015 - 12:30 AM

You don't appear to be creating a window at all. You won't receive practically all messages without a fully created window (that also means you need a message loop which correctly processes at least the WM_CREATE/WM_NC_CREATE-family of messages). I'm not sure about the rules for the console window but I have my doubts if retrieving it via GetForegroundWindow() works. Do you even get a non-NULL handle?

#5252186 HELP SFML

Posted by BitMaster on 14 September 2015 - 08:27 AM

According to the documentation sf::Vector2 is a template. Considering you used Vector2f above I would assume there is already a handy typedef for a float Vector2 available.

That said, none of your problems so far had anything to do with SFML but the very basics of C++. I would strongly suggest you forget about SFML for now and work on a significantly more solid foundation in your language of choice first.

#5244162 C++ Operator Overloading: Bitwise Operators & If-Check

Posted by BitMaster on 02 August 2015 - 10:01 AM

You might want to look at std::bitset. Either because it does everything you want already or as an inspiration.

#5243349 How to initialize a reference variable in a Quadtree?

Posted by BitMaster on 29 July 2015 - 04:45 AM

First, about your questions:
1. I don't really see where ultramailman suggested multiple entity managers.

2. If you know you need only one, you could as well make it global. My experience however says you don't know until the project is actually done, so my advice would be to avoid that path. Either you pass it along with every call that needs it or your quadtree needs to store the shared state and pass it down to every sub-quadtree it creates as well.

3. A static data member is a global with a slightly different name.

I feel you have modeled that in a slightly weird way. In my mind there should only be one Quadtree per actual quadtree. A Quadtree has a root Node. A Node has up to four child Node instances. Either the Node instances should know which Quadtree they belong to (allowing them access to any shared state), or perhaps better the Node instances are pure data and the visitor function always knows in which quadtree it is, again allowing access to shared state.

#5237049 delete file on Windows

Posted by BitMaster on 27 June 2015 - 01:25 AM


If you want a string containing "\\" you need to use "\\\\" in the source. If you want a string containing "\" you need to use "\\" in the source.

"\\\" in C++ is completely broken though. It means a string containing a \, followed by " and then the string ends without being ever probably terminated, hence the compile errors.

#5236875 c++11 lambdas: best practices for 0 dynamic heap allocation?

Posted by BitMaster on 26 June 2015 - 12:57 AM

The lambda itself will not allocate anything dynamically. If you store the lambda somewhere (for example an std::function<>) it might have to allocate memory (depending on the size of the lambda and std::function's implementation details). Note that if you transfer the lambda somewhere using a template, like

template <typename Functor> void doSomething(Functor f)
   { ... }
this will not require any dynamic memory allocations either (unless of course something you captured by value requires requires dynamic memory allocations for its copy construction).