Original Post
Dont you just hate it?
quote:
Original post by Eskhan
Especially people that comment every other line, ugh...
quote:
Original post by CpMan
Commenting is good because it explains what''s going on. When you''re looking at their code, doesn''t it make sense that it will make more sense if they had the sense to put sensible comments in it?
Gamedev for learning.
libGDN for putting it all together.
An opensource, cross platform, cross API game development library.
int SomeFunction(int parameter) //This is a function that
//returns a integer
{
int Counter; //A counter variable
int Result; //stores the Result of the function in a variable
for(Counter = 1; Counter > parameter; Counter++) // loop that //runs the number of times specified in the parameter variable
{
Result = Result + Counter; //Adds the Counter to the result
}
return Result; //returns the data in the result variable
}
quote:
Original post by 987th Yak
Yes, but it is very annoying when their code looks like this:
snip
quote:
Yes, especially when it has bugs like that =)
quote:No complaint was never raised against "sensible comments".
Original post by CpMan
...that it will make more sense if they had the sense to put sensible comments in it?
/***************************************************
* Particle Management. This is a proxy from the ParticleSet class
*/
//!Returns a vector of all of the particles
std::vector<Particle>& GetParticles() { return PSet; }
//Returns a particle at the given index. If the index is out of range, std::out_of_range is thrown.
Particle& operator[](std::size_t index) {
if(index < 0 || index >= Particles.size()) {
throw std::out_of_range("Particle with the given index does not exist");
}
return Particles[index];
}
//!Retrieves a pointer to an available particle and updates any counters.
/*!
If no particles are available, the system attempts a resize of the vector. If the ResizeFactor is 0, it throws std::out_of_range()
*/
Particle& AddParticle() {
if(ParticlesAvailable > 0) {
ParticlesAvailable--;
ParticlesInUse++;
return Particles[AvailableParticles.top()];
}
//gotta resize, but first make sure resize factor is correct
if(ResizeFactor == 0) {
throw std::out_of_range("Particle could not be created, out of available particles and resize factor is zero");
}
//now, unfortunately, InUseIsMaxParticles is not true anymore
InUseIsMaxIndex = false;
for(std::size_t i = 0; i < ResizeFactor; i++) {
Particles.push_back(Particle(ParticleProperties));
ParticlesAvailable.push(MaxParticles+i);
}
ParticlesAvailable+=ResizeFactor-1;
MaxParticles+=ResizeFactor-1;
//now grab one
return Particles[AvailableParticles.top()];
}
//!Adds a Particle to the set and returns a pointer to it, setting it's value to particle. If the particle cannot be created, std::out_of_range is thrown
Particle& AddParticle(Particle& particle) {
//attempt to get a new particle
Particle newparticle;
try {
newparticle = NewParticle();
}
catch(std::out_of_range outofrange) {
throw std::out_of_range("Could not add particle, no particles available");
}
newparticle = particle;
return newparticle;
}
//!Adds multiple particles. If not all of the particles could be created, returns false;
bool AddParticles(std::size_t numnewparticles) {
//attempt to add all of the particles
for(std::size_t i = 0; i < numnewparticles; i++) {
try {
AddParticle();
}
catch(std::out_of_range outofrange) {
//could not add this one, return, since no further particle creation will succeed;
return false;
}
}
return true;
}
//!Kills a single particle
void KillParticle(std::size_t index) {
if(index < 0 || index > Particles.size())
return;
if(Particles[index].IsAlive()) {
Particles[index].Kill();
TotalParticlesAvailable++;
ParticlesInUse--;
AvailableParticles.push(index);
}
}
//!Kills a range of particles, inclusive. If lastindex is (std::size_t)-1
void KillParticleRange(std::size_t firstindex, std::size_t lastindex = (std::size_t)-1) {
//simply kill() each of the particles in the range
std::size_t maxindex = (lastindex != (std::size_t)-1) ? lastindex : Particles.size();
for(std::size_t i = firstindex; i < maxindex; i++) {
KillParticle(i);
}
}
//!Kills all particles
void KillAll() {
//simply loop through the entire vector and Kill each particle
for(std::size_t i = 0; i < Particles.size(); i++) {
KillParticle(i);
}
}
//!Now since we have the possibility with the resizing, that we're going to lose the ability to predict the max index, we need a function to restore this abilitiy
/*!
Please note that this is not an inexpensive operation. It involves removal of all of the spaces in the Partilces array, emptying
of the AvailableParticles stack, and readdition of all the avialable particles.
*/
void RestoreOrder() {
//recompress the vertices and rebuild the stack.
Compress();
RebuildStack();
//reset boolean
InUseIsMaxIndex = true;
}
private:
//!Compresses the vertices into contiguous sections, with no dead particles in between.
/*!
Please note this is not an inexpensive operation, but is useful because it restores the ability to do the
InUseIsMAxIndex optimization. However, please note that while Compress will restore this condition, doing this
will invalidate the AvailableParticles stack. In order for everything to be correct, the stack must also be rebuilt.
*/
void Compress() {
//The first step is recompression of the particle array into contigious live particles
int i = 0;
int count = 0;
while(i < Particles.size()) {
//check if the current one is alive
if(Particles[i].IsAlive()) {
i++;
continue;
}
else {
count = 0;
while(i < Particles.size()) {
i++;
count++;
if(Particles[i].IsAlive()) {
Particles[i-count] = Particles[i];
Particles[i].Kill();
break;
}
}
//this means we reached the end of the vector and everything is in order, break
break;
}
}
//the compression reenabled the optimization, so reset the boolean var.
InUseIsMaxIndex = true;
}
//!Rebuilds the AvailableParticles Stack
void RebuildStack() {
std::stack<std::size_t> a;
TotalAvailableParticles = 0;
//now we need to push all available particles onto the stack
for(std::size_t i = Particles.size(); i >= ParticlesInUse; i--) {
a.push(i);
AvailableParticles++;
}
AvailableParticles = a;
}
public:
//!Returns the maximum number of particles available
std::size_t GetMaximumParticles() { return Particles.size(); }
//!Sets the maximum number of particles.
/*!
If this group has already had an initialize call (i.e. is alive), you must recall Initialize()
in order for the changes to take effect.
*/
void SetMaximumParticles(std::size_t numparticles) { MaxParticles = numparticles; }
//!Returns the number of available particles
std::size_t GetParticlesAvailable() { return TotalParticlesAvailable; }
//!Returns the number of particles currently in use
std::size_t GetInUseParticles() { return ParticlesInUse; }
//!Returns whether InUseParticles() also implies the maximum index
bool InUseEqualsMaxIndex() { return InUseIsMaxIndex; }
//!Returns the resize factor
std::size_t GetResizeFactor() { return ResizeFactor; }
//!Sets the resize factor
void SetResizeFactor(std::size_t newfactor) { ResizeFactor = newfactor; }
/*
****************************************************/
This topic has been locked by a moderator. New replies are not allowed.
With your permission, GameDev.net uses analytics cookies to understand how people use the platform. You can accept analytics or continue with necessary cookies only. Learn more