I am completely new to programming and I am following a C++ tutorial for beginners. I am using Eclipse IDE 2020 06 with MinGW-64, SDL2 on a Windows 10 machine.
We’ve been building the project in stages. The first part was to create a window, then to create particles and then have the particles move. I successfully completed the first two parts, but I’m stuck with the particle movement. The project builds and runs without error, but the particles will not move. I have been googling and searching YouTube for two days and I just cannot find anything to help. And since there are no error messages, I have no idea why it won’t work.
The project includes 3 separate classes. I’m posting the main.cpp, Particle and Swarm classes. I’m thinking the problem has to be in there somewhere, since everything worked beautifully up to the point where I tried to get the particles to move.
I apologize in advance for posting so much code, but I just don’t know what else to do.
Animating_Particles.cpp: (main)
#include "Screen.h"
#include "Swarm.h"
#include "Particle.h"
#include <math.h>
#include <time.h>
#include <stdlib.h>
using namespace std;
using namespace screenanimation;
int main(int argc, char **argv) {
srand (time (NULL));
Screen screen;
if (screen.init () == false) {
cout << "SDL_Init failed." << endl;
}
Swarm swarm;
while (true) {
int elapsed = SDL_GetTicks ();
screen.clear ();
swarm.update ();
unsigned char red = (1 + sin (elapsed * 0.0011)) * 128;
unsigned char green = (1 + sin (elapsed * 0.0011)) * 128;
unsigned char blue = (1 + sin (elapsed * 0.0011)) * 128;
const Particle *const pParticles = swarm.getParticles ();
for (int i = 0; i < Swarm::NPARTICLES; i++) {
Particle particle = pParticles [i];
int x = (particle.m_x + 1) * Screen::SCREEN_WIDTH / 2;
int y = (particle.m_y + 1) * Screen::SCREEN_HEIGHT / 2;
screen.setPixel(x, y, red, green, blue);
}
screen.update();
if (screen.processEvents () == false) {
break;
}
}
screen.close ();
return 0;
}
Particle.cpp
#include "Particle.h"
#include <stdlib.h>
namespace screenanimation {
Particle::Particle() {
m_x = ((2.0 * rand ()) / RAND_MAX) -1;
m_y = ((2.0 * rand ()) / RAND_MAX) -1;
m_xspeed = 0.001 * ((2.0 * rand ()) / RAND_MAX) -1;
m_yspeed = 0.001 * ((2.0 * rand ()) / RAND_MAX) -1;
}
Particle::~Particle() {
}
void Particle::update () {
m_x += m_xspeed; m_y += m_yspeed;
if (m_x <= -1.0 || m_x >= 1.0) {
m_xspeed = -m_xspeed;
}
if (m_y <= -1.0 || m_y >= 1.0) {
m_yspeed = -m_yspeed;
}
}
} /* namespace screenanimation */
Particle.h
#ifndef PARTICLE_H_
#define PARTICLE_H_
#include <math.h>
namespace screenanimation {
struct Particle {
double m_x; double m_y;
double m_xspeed; double m_yspeed;
public:
Particle();
virtual ~Particle();
void update ();
};
} /* namespace screenanimation */
#endif /* PARTICLE_H_ */
Swarm.cpp
#include "Swarm.h"
namespace screenanimation {
Swarm::Swarm() {
m_pParticles = new Particle [NPARTICLES];
}
Swarm::~Swarm() {
delete [] m_pParticles;
}
void Swarm::update() {
for (int i = 0; i < Swarm::NPARTICLES; i++) {
m_pParticles [i].update ();
}
}
} /* namespace screenanimation */
Swarm.h
#ifndef SWARM_H_
#define SWARM_H_
#include <iostream>
#include "Particle.h"
namespace screenanimation {
class Swarm {
public:
const static int NPARTICLES = 2000;
private:
Particle * m_pParticles;
public:
Swarm();
virtual ~Swarm();
void update ();
const Particle * const getParticles () { return m_pParticles; };
};
} /* namespace screenanimation */