Jump to content

  • Log In with Google      Sign In   
  • Create Account

We need your help!

We need 1 more developer from Canada and 12 more from Australia to help us complete a research survey.

Support our site by taking a quick sponsored survey and win a chance at a $50 Amazon gift card. Click here to get started!


Member Since 26 Sep 2010
Offline Last Active Jul 09 2014 05:07 AM

Posts I've Made

In Topic: Object was not declared in this scope?

29 March 2013 - 09:08 AM

If you use the raw pointer solution SiCrane posted, don't forget to delete the pointer in the new bullet maker and after the end of the program so you won't leak memory!



Bullet * myBullet = 0; //nullptr instead of 0 if using C++11
while (!quit)
  if (shootNew)
    delete myBullet; //does nothing if myBullet is null
    myBullet = new Bullet();
  if (myBullet != 0)
delete myBullet;

If you don't, the old bullets are still in memory, so after pressing the fire button 2000 times, there are 2000 bullets in memory. Yuck.

In Topic: Vector MADNESS

18 March 2013 - 01:23 AM

As has been said, use std::vector::back() or vec[vec.size()-1], but ALWAYS check if the vector is not empty. Using either of those without checking for non-emptiness results in undefined behaviour and it will most likely overwrite sizeof(vectype) bytes before the start of vector data, corrupting the memory or causing a segmentation fault.


vec.at(vec.size()-1) throws an exception if the vector is empty, but it sacrifices speed and does not solve the problem except for alerting you instead of corrupting memory.


Paradigm Shifter: You, sir, have made my day.

In Topic: How to use a bitmask with flags that use more than one bit?

13 March 2013 - 03:55 PM

It can be done quite easily. There are many uses of this technique, e.g. packets.

When retrieving those data, bit AND them by the bits you want to retrieve and right shift them so the least significant bit you want is lowest in the retrieved value. Example: You want bits ...00xxxx00 in the int you have. You bit AND 00111100 (called the mask, this number is hex 0x3C) with your int and right shift it by 2 (the offset).



int result = (myInt & 0x3C) >> 2;


("I grab only the bits that interest me and shift them so I get a number with values starting from zero.")


Setting the value is quite similar - you bit AND with the inverse of 0x3C (that means 0x3C EXCLUSIVE OR (^) 0), so the bits that interest us are zeroed (not needed when you know all of those bits are already zeroed), and then OR the left-shifted setValue. Note that having the value larger than your storage limit overwrites upper bits, so be wary of what you are doing!



int modified = ((0x3C ^ 0) & (myInt)) | (setValue << 2)


("I decide which bits interest me, get the opposite (those that don't interest me) and leave only them, effectively zeroing the bits that interest me. Then I shift the setValue so I write it to right bits and add it to the modified number, effectively setting the bits that interest me and are 1 in the setValue.")


Also, you can compute the mask (0x3C) like this:




int mask = 0;
for (int i = 0; i < numInterestingBits; i++)
  mask << 1;
  mask += 1;
mask = mask << offset; //also written mask <<= offset;

And that is it. Be sure to make your own functions for it so you don't rewrite it for every value retrieval!

In Topic: Log text file. Need a tool that can keep my log file open and auto reload it...

03 March 2013 - 10:28 AM

I personally use PSPad. Its behaviour in these situations is similar to Programmer's Notepad mentioned by Servant of the Lord.

In Topic: Projectile Class Format

27 February 2013 - 08:01 PM

Will every projectile be different? I do not think so, we can say that every projectile behaves according to its type. Make every projectile have some kind of reference to its type (enum, pointer, reference, ID...). That is 8 bytes overhead at most, which is perfectly okay. You then modify the enemy using the projectile type and are okay.