-
Notifications
You must be signed in to change notification settings - Fork 2
Swarm.h and .cpp
Hannupekka Sormunen edited this page Dec 29, 2018
·
6 revisions
//Header guards for SWARM_H_
#ifndef SWARM_H_
#define SWARM_H_
//Include local headers
#include "Screen.h"
#include "Particle.h"
//Create a namespace for this the use of this class
namespace particlefire {
//Create a class Swarm inside namespace ParticleFire. This class devires from Screen-class.
class Swarm : public Screen {
//Private data members of a Swarm-class:
private:
Particle *m_particles;
int m_lastTime;
static int nparticles_;
static double red_speed_;
static double green_speed_;
static double blue_speed_;
//Public data members of a Swarm-class:
public:
//Constructors and destructor of Swarm-class
public:
Swarm();
virtual ~Swarm();
//Private data methods of a Swarm-class:
private:
bool ReadSettings();//Method for getting settings from a file
//Public data methods of a Swarm-class:
public:
void Update(int elapsed);//Method for updating particles
const Particle *const GetParticles() { return m_particles; };//Method for returning the pointer m_pParticles
void SetParticleColorValue(int elapsed);//Method for setting particle's color value
};
}/* end of namespace ParticleFire */
#endif /* SWARM_H_ */ Swarm::Swarm() {
//Constructor that creates array of Particle-objects and saves their location to pointer m_pParticles.
ReadSettings();
m_particles = new Particle[nparticles_];
//Initialize variable to zero
m_lastTime = 0;
} //Method for getting settings from a file
bool Swarm::ReadSettings() {
//Access the file "settings.cfg" and store it's position to variable file
std::ifstream file("settings.cfg");
//Variable where to save each line of the file
std::string line;
//Variables for storing the values from file
int nparticles(0);
double red_speed(0.0);
double green_speed(0.0);
double blue_speed(0.0);
//Loop that goes throught the file line by line
while (std::getline(file, line)) {
std::istringstream sin(line.substr(line.find("=") + 1));
//If any of the following text snippets are found from the file these if-else statements are true and values are saved to variables
if (line.find("#") != -1) {
//do nothing, since lines starting with #-character are ignored
}
else if (line.find("NPARTICLES") != -1) {
sin >> nparticles;
}
else if (line.find("redSpeed") != -1) {
sin >> red_speed;
}
else if (line.find("greenSpeed") != -1) {
sin >> green_speed;
}
else if (line.find("blueSpeed") != -1) {
sin >> blue_speed;
}
}
//Check that the number of particles from the file are greater than zero
if (nparticles > 0) {
//Save values to data members
nparticles_ = nparticles;
red_speed_ = red_speed;
green_speed_ = green_speed;
blue_speed_ = blue_speed;
//Close the file "settings.cfg"
file.close();
}
//If values are negative or zero file is closed and function returns false.
else {
//Close the file "settings.cfg"
file.close();
return false;
}
return true;
}//Method for updating particles
void Swarm::Update(int elapsed) {
//Calculate the the value between last time and elapsed time
//This is used to make particles move constantly on every setting
int interval = elapsed - m_lastTime;
//Run update method for each Particle object.
for (int i = 0; i < particlefire::Swarm::nparticles_; i++) {
m_particles[i].UpdatePosition(interval);
}
//Store the elapsed time to variabe so that next run it can be calculated again with correct values.
m_lastTime = elapsed;
} //Method for setting particle's color value
void Swarm::SetParticleColorValue(int elapsed)
{
//These three variables are used to store value from algorithm that vary the number between 0 - 255 where 0 is complete black and 255 is complete white.
//Their value changes during programs run depending on the setting from the each _speed_-variable.
Uint8 red((unsigned char)((1 + sin(elapsed * red_speed_)) * 128));
Uint8 green((unsigned char)((1 + sin(elapsed * green_speed_)) * 128));
Uint8 blue((unsigned char)((1 + sin(elapsed * blue_speed_)) * 128));
//Loop that goes through every particle on the screen and sends the values to SetPixel-function so that they can be drawn on the screen
for (int i = 0; i < nparticles_; i++) {
//Store particles position to variables x and y in pixel format
int x((m_particles[i].GetXpos() + 1) * particlefire::Screen::screen_width_ / 2);
int y(m_particles[i].GetYpos() * particlefire::Screen::screen_width_ / 2 + particlefire::Screen::screen_height_ / 2);
//Call SetPixel on each loop on each particle
particlefire::Screen::SetPixel(x, y, red, green, blue);
}
}