I see, thanks for the information - I will take a look at that book list and see if I can find some good ones to buy.
Also, instead of creating a new thread, I might as well ask here about a problem I am having with a blinking window. Here is my (far from complete) code:
// Brownian_Motion_Test.cpp
// Program to simulate Brownian motion of particles in 2d
// Rendered using SFML Graphics ®
// Preprocessor directives (include headers) for program
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <cmath>
#include <iostream>
#include <vector>
#include <sstream>
#include <string>
// Namespaces to be used by program
using namespace std; // standard namespace (std)
using namespace sf; // Simple and Fast Multimedia Library namespace (sf)
// Global Declaration of Mathematical/Physical Constants
const double pi = 4*atan(1); // Value of pi
const double k_B = 1.38E-23; // Boltzmann Constant
// Global Declaration of Rendered Window Sizes
unsigned int width = 800; // width of rendered 2d window (in px)
unsigned int height = 600; // height of rendered 2d window (in px)
// Global Declaration of Particle Properties
const float r = 5.0F; // radius of particle (in px)
const int NParticles= 3; // number of particles to be produced in simulation
// Declare time value and utility class to measure elapsed time
Time thetime;
Clock elapsedtime;
// Function to return string from integer type
string IntToString(int d) {
stringstream ss;
ss << d;
return ss.str();
}
// Function to calculate frame rate (fps)
std::string getFrameRate() {
static int frameCounter = 0; // (re-)Initialise frame counter variable
static int fps = 0; // (re-)Initialise frames per second variables
frameCounter++; // Increment frameCounter by 1 for each call to function GetFrameRate
thetime = elapsedtime.getElapsedTime(); // Returns the elapsed time since the last call to .restart()
// if-statement to process code if thetime reaches 1 second (or greater)
if(thetime.asMilliseconds() > 999) {
fps = frameCounter; // assign frameCounter to fps if thetime > 999ms
frameCounter = 0; // reinitialise frame counter to 0
elapsedtime.restart(); // reinitialise the clock back to zero time
}
return IntToString(fps); // Returns string for fps value
}
// Function to render animation sequences for each particle
void animationSequence ( RenderWindow& window, CircleShape& circle, float x, float y ) {
window.clear(Color(255,255,255,255)); // Fill the window with even white colour
circle.setPosition(x, y); // Update position of circle in window
window.draw(circle); // Draw circle in rendered window
window.display(); // Display on screeen what has been rendered to the window so far
}
void displayFrameRate ( RenderWindow& window, Text& FPS ) {
window.clear(Color(255,255,255,255)); // Fill the window with even white colour
FPS.setString(getFrameRate()); // Set FPS to string returned by call to GetFrameRate function
window.draw(FPS); // Draw FPS text to screen
window.display(); // Display on screen what has been rendered to window so far
}
int main(void) {
RenderWindow window(VideoMode(width, height), "Brownian Motion Animation"); // Render 2d Window for Animation; width x height px
vector<CircleShape> particle(NParticles); // Create a vector of circles called particle() of size NParticles
float RandomNumberWidth[100] = {0}; // RNG Variables for...
float RandomNumberHeight[100] = {0}; //... X and Y positions
const unsigned IM = 1664525; // Integer constants
const unsigned IC = 1013904223; //... for the RNG algorithm
const float scale = 1.0/0xFFFFFFFF; //Scaling factor for random double between 0 and 1
unsigned iran = time(0); //Seeds the random-number generator from the system time
const int MAX(100); // RNG for loop maximum limit
for ( int i = 0 ; i < MAX ; i++ ) {
RandomNumberWidth[i] = width * scale * float ( iran = IM * iran + IC ); // Random Number Generator for X position
RandomNumberHeight[i] = height * scale * float ( iran = IM * iran + IC ); // Random Number Generator for Y position
//cout << RandomNumberWidth[i] << endl; // UNCOMMENT TO PRINT RANDOM NUMBERS FOR TESTING
//cout << RandomNumberHeight[i] << endl << endl;
}
Font font; // Load character font
if (!font.loadFromFile("arial.ttf")) { // If the font arial.ttf cannot be loaded, print error message to console
// error...
}
Text FPS("0", font, 25); // text variable to store Frame Rate, initialise to 0...
//... set Font type to font, set character size to 25
FPS.setOrigin(-10,-10); // set original position of FPS text in 2d window
FPS.setColor(Color::Red); // set colour of FPS text
for ( int i = 0 ; i < NParticles ; i++ ) {
particle[i].setRadius(r); // Set radius of particle[i] to r
particle[i].setFillColor(Color::Blue); // Set fill colour of particle[i] to blue
particle[i].setOrigin(-RandomNumberWidth[i],-RandomNumberHeight[i]); // Set origin position of particle[i] to random position
}
float x[NParticles] = {0}; // horizontal displacement of particles
float y[NParticles] = {0}; // vertical displacement of particles
int k = 0; // animation loop iterator
while (window.isOpen()) {
if ( k >= NParticles ) // reset k to 0 when it reaches NParticles value
k = 0;
x[k] = float(pow(-1,k)) * (10 * scale * float ( iran = IM * iran + IC )); //Update X position of particle[k] - THIS IS TEST EXPRESSION, NOT PHYSICALLY ACCURATE
y[k] = float(pow(-1,k)) * (10 * scale * float ( iran = IM * iran + IC )); //Update Y position of particle[k] - THIS IS TEST EXPRESSION, NOT PHYSICALLY ACCURATE
Event event; // Defines a system event and its parameters
while (window.pollEvent(event)) {
if (event.type == Event::Closed) // close the window and destroy all attached resources if user quits animation window
window.close();
}
animationSequence(window, particle[k], x[k], y[k]); // call to function animationSequence() to simulate particles
displayFrameRate (window, FPS); // call to function displayFrameRate() to print fps to window
k++; // increment k for each while loop iteration
}
return 0;
} // end of main()
/*
YET TO UPDATE:
- **PRIORITY** SOLVE GRAPHICAL GLITCH PROBLEM OF EACH PARTICLE AND FRAME RATE TEXT FLASHING RAPIDLY **PRIORITY**
- DEVISE SIMPLE COLLISION DETECTION ALGORITHM (CDA) FOR PARTICLE COLLISIONS
- INVESTIGATE USE OF .SETPOSITION FOR USE IN CDA
- LINES 162,163: REPLACE COEFFICIENT OF SCALE WITH ENERGY DISTRIBUTIONS OF PARTICLES - i.e. E[k], where E[] is random distribution array/vector
*/
As far as I can tell, I have utilised the principle of events -> draw -> display outlined in the tutorials and I cannot find the source of the rapid blinking (of the circles and the text in the window) in my code.
I would be very much appreciate if somebody could point out the problem and also explain how and why what I have done is incorrect or bad practice.
Thanks.