Skip to content
Snippets Groups Projects
Commit 8252118d authored by Armin Co's avatar Armin Co
Browse files

Scoring points is now available.

This DLC is free ^^
parent a47b39e3
No related branches found
No related tags found
No related merge requests found
...@@ -20,6 +20,9 @@ pkg_check_modules(GTKMM gtkmm-3.0) ...@@ -20,6 +20,9 @@ pkg_check_modules(GTKMM gtkmm-3.0)
link_directories(${GTKMM_LIBRARY_DIRS}) link_directories(${GTKMM_LIBRARY_DIRS})
include_directories(${GTKMM_INCLUDE_DIRS}) include_directories(${GTKMM_INCLUDE_DIRS})
# Use parallel for loops
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --openmp")
## link all necessary libs to the target ## link all necessary libs to the target
target_link_libraries(qpong.app target_link_libraries(qpong.app
qpong qpong
......
...@@ -9,8 +9,8 @@ ...@@ -9,8 +9,8 @@
namespace QPong namespace QPong
{ {
constexpr int arrayWidth = 312; constexpr int arrayWidth = 384;
constexpr int arrayHeight = 312; constexpr int arrayHeight = 384;
constexpr unsigned arraySize = arrayWidth * arrayHeight; constexpr unsigned arraySize = arrayWidth * arrayHeight;
......
...@@ -85,10 +85,11 @@ const double obstaclePotential(double x, double y, double x0, double y0) ...@@ -85,10 +85,11 @@ const double obstaclePotential(double x, double y, double x0, double y0)
return E; return E;
} }
double sumAtInit = 0.0;
void Particle::initMomentum() void Particle::initMomentum()
{ {
std::cout << "Initialising particle" << std::endl; std::cout << "Initialising particle" << std::endl;
sumAtInit = 0.0;
for (unsigned iy = 0; iy < QPong::arrayHeight; iy++) for (unsigned iy = 0; iy < QPong::arrayHeight; iy++)
{ {
double y = yAt(iy); double y = yAt(iy);
...@@ -104,18 +105,29 @@ void Particle::initMomentum() ...@@ -104,18 +105,29 @@ void Particle::initMomentum()
constexpr double lambda = 70.0; constexpr double lambda = 70.0;
constexpr double A0 = 2.5; constexpr double A0 = 2.5;
m_psi[index] = A0 * exp(Ci / hbar * (x * k_px0 + y * k_py0) - lambda * (sqr(x - k_x0) + sqr(y - k_y0))); m_psi[index] = A0 * exp(Ci / hbar * (x * k_px0 + y * k_py0) - lambda * (sqr(x - k_x0) + sqr(y - k_y0)));
sumAtInit += sqr(real(m_psi[index])) + sqr(imag(m_psi[index]));
}
} }
for (auto p : m_players)
{
m_players[p.first].score = 0;
} }
std::cout << sumAtInit << std::endl;//" " << m_players[Player::One].score << " " << m_players[Player::Two].score << std::endl;
} }
constexpr double playerOneStartX = -0.7;
constexpr double playerTwoStartX = 0.7;
Particle::Particle(ParticleImage *momentum, ParticleImage *position, ParticleImage *obstacles) Particle::Particle(ParticleImage *momentum, ParticleImage *position, ParticleImage *obstacles)
: m_bufMomentumRepresentation{momentum} : m_bufMomentumRepresentation{momentum}
, m_bufPositionRepresentation{position} , m_bufPositionRepresentation{position}
, m_obstacleImage{obstacles} , m_obstacleImage{obstacles}
, m_ready{false} , m_ready{false}
{ {
m_players.insert(std::make_pair(Player::One, Velocity {-0.7, 0.0})); m_players.insert(std::make_pair(Player::One, PlayerData {playerOneStartX, 0.0}));
m_players.insert(std::make_pair(Player::Two, Velocity { 0.7, 0.0})); m_players.insert(std::make_pair(Player::Two, PlayerData {playerTwoStartX, 0.0}));
fftw_init_threads(); fftw_init_threads();
...@@ -147,24 +159,54 @@ void Particle::propagate() ...@@ -147,24 +159,54 @@ void Particle::propagate()
{ {
updatePlayers(); updatePlayers();
constexpr double dt = 0.1; constexpr double dt = 0.1;
sumAtInit = 0;
#pragma omp parallel for
for (unsigned iy = 0; iy < QPong::arrayHeight; iy++) for (unsigned iy = 0; iy < QPong::arrayHeight; iy++)
{ {
double y = yAt(iy); double y = yAt(iy);
#pragma omp parallel for #pragma omp parallel for
for (unsigned long ix = 0; ix < QPong::arrayWidth; ix++) for (unsigned long ix = 0; ix < QPong::arrayWidth; ix++)
{ {
double x = xAt(ix); double x = xAt(ix);
int index = QPong::arrayWidth * iy + ix;
// destroy ^^
if (x < playerOneStartX) //< expexts p1.x < 0
{
double cx = -playerOneStartX + x;
cx = cx * (M_PI / 2) / (1.0 - playerOneStartX);
double e = cos(cx) * cos(cx);
std::complex<double> tmp = m_psi[index];
m_psi[index] *= e;
tmp -= m_psi[index];
m_players[Player::Two].score += sqr(real(tmp)) + sqr(imag(tmp));
}
if (x > playerTwoStartX) //< expects p2.x > 0
{
double cx = x - playerTwoStartX;
cx = cx * (M_PI / 2) / (1.0 - playerOneStartX);
double e = cos(cx) * cos(cx);
std::complex<double> tmp = m_psi[index];
m_psi[index] *= e;
tmp -= m_psi[index];
m_players[Player::One].score += sqr(std::real(tmp)) + sqr(std::imag(tmp));
}
// obstacles
double E = obstaclePotential(x, y, m_players[Player::One].x, m_players[Player::One].y); double E = obstaclePotential(x, y, m_players[Player::One].x, m_players[Player::One].y);
double E2 = obstaclePotential(x, y, m_players[Player::Two].x, m_players[Player::Two].y); double E2 = obstaclePotential(x, y, m_players[Player::Two].x, m_players[Player::Two].y);
double E3 = sqr(y) * sqr(y) * sqr(y) * 0.001; double E3 = sqr(y) * sqr(y) * sqr(y) * 0.001;
int index = QPong::arrayWidth * iy + ix;
std::complex<double> tmp = m_psi[index] * exp(-Ci * dt / hbar * (E + E2 + E3)); std::complex<double> tmp = m_psi[index] * exp(-Ci * dt / hbar * (E + E2 + E3));
m_psi[index] = tmp; m_psi[index] = tmp;
m_bufPositionRepresentation->updateBuffer(index, tmp, (E+E2+E3*0.99)*800); m_bufPositionRepresentation->updateBuffer(index, tmp, (E+E2+E3*0.99)*800);
} }
} }
fftw_execute (m_planForward); // transform into momentum repr. fftw_execute (m_planForward); // transform into momentum repr.
#pragma omp parallel for
for (unsigned iy = 0; iy < QPong::arrayHeight; iy++) for (unsigned iy = 0; iy < QPong::arrayHeight; iy++)
{ {
double py = pyAt(iy); double py = pyAt(iy);
...@@ -174,7 +216,7 @@ void Particle::propagate() ...@@ -174,7 +216,7 @@ void Particle::propagate()
double px = pxAt(ix); double px = pxAt(ix);
constexpr double m = 1.0; constexpr double m = 1.0;
double E = (sqr(px) + sqr(py)) / (2.0 * m); double E = (sqr(px) + sqr(py)) / (2.0 * m);
constexpr double N = 1.0 / (QPong::arrayWidth * QPong::arrayHeight); constexpr double N = 1.0 / QPong::arraySize;
int index = QPong::arrayWidth * iy + ix; int index = QPong::arrayWidth * iy + ix;
m_psi[index] *= exp(-Ci * dt / hbar * E) * N; m_psi[index] *= exp(-Ci * dt / hbar * E) * N;
} }
...@@ -190,6 +232,7 @@ void Particle::updateMomentumImage() ...@@ -190,6 +232,7 @@ void Particle::updateMomentumImage()
unsigned py = 0; unsigned py = 0;
unsigned long momentum_index = 0; unsigned long momentum_index = 0;
unsigned long array_index = 0; unsigned long array_index = 0;
#pragma omp parallel for
for (unsigned y = 0; y < QPong::arrayHeight; y++) for (unsigned y = 0; y < QPong::arrayHeight; y++)
{ {
for (unsigned x = 0; x < QPong::arrayWidth; x++) for (unsigned x = 0; x < QPong::arrayWidth; x++)
...@@ -278,6 +321,9 @@ void Particle::updatePlayers() ...@@ -278,6 +321,9 @@ void Particle::updatePlayers()
m_players[p.first].y = -maxY; m_players[p.first].y = -maxY;
} }
} }
}
double Particle::getScore(Player player)
{
return m_players[player].score;
} }
\ No newline at end of file
...@@ -28,12 +28,13 @@ enum class Move ...@@ -28,12 +28,13 @@ enum class Move
Down Down
}; };
struct Velocity struct PlayerData
{ {
double x; double x;
double y; double y;
double vx = 0; double vx = 0;
double vy = 0; double vy = 0;
double score = 0;
}; };
class Particle class Particle
...@@ -62,13 +63,17 @@ public: ...@@ -62,13 +63,17 @@ public:
/// ///
bool notReady(); bool notReady();
/// @brief Get the score of the given player
///
double getScore(Player player);
private: private:
std::complex<double> *m_psi; std::complex<double> *m_psi;
fftw_plan m_planForward; fftw_plan m_planForward;
fftw_plan m_planBackward; fftw_plan m_planBackward;
bool m_ready; bool m_ready;
std::map<Player, Velocity> m_players; std::map<Player, PlayerData> m_players;
ParticleImage *m_bufPositionRepresentation; ParticleImage *m_bufPositionRepresentation;
ParticleImage *m_bufMomentumRepresentation; ParticleImage *m_bufMomentumRepresentation;
......
...@@ -34,7 +34,7 @@ Glib::RefPtr<Gdk::Pixbuf> ParticleImage::getPixbuf() ...@@ -34,7 +34,7 @@ Glib::RefPtr<Gdk::Pixbuf> ParticleImage::getPixbuf()
} }
Glib::RefPtr<Gdk::Pixbuf> pb = Gdk::Pixbuf::create_from_data(m_pixbuf, Gdk::Colorspace::COLORSPACE_RGB, true, 8, QPong::arrayWidth, QPong::arrayHeight, QPong::arrayWidth * 4); Glib::RefPtr<Gdk::Pixbuf> pb = Gdk::Pixbuf::create_from_data(m_pixbuf, Gdk::Colorspace::COLORSPACE_RGB, true, 8, QPong::arrayWidth, QPong::arrayHeight, QPong::arrayWidth * 4);
pb = pb.get()->scale_simple(1024, 1024, Gdk::InterpType::INTERP_NEAREST); pb = pb.get()->scale_simple(768, 768, Gdk::InterpType::INTERP_NEAREST);
return pb; return pb;
} }
...@@ -49,7 +49,6 @@ const QColor ParticleImage::getValue(unsigned index) ...@@ -49,7 +49,6 @@ const QColor ParticleImage::getValue(unsigned index)
c.r = obs + r*r; c.r = obs + r*r;
c.g = r*r + i*i + obs / 2; c.g = r*r + i*i + obs / 2;
// c.b = 0;
c.b = i*i; c.b = i*i;
if (c.r >= 1.0) if (c.r >= 1.0)
......
...@@ -37,6 +37,8 @@ void render(QWindow *w) ...@@ -37,6 +37,8 @@ void render(QWindow *w)
QWindow::QWindow() QWindow::QWindow()
: m_button{"Initialise Particle"} : m_button{"Initialise Particle"}
, m_label("Hi, you are playing QPong and this is a label!") , m_label("Hi, you are playing QPong and this is a label!")
, m_scorePlayerOne("0")
, m_scorePlayerTwo("0")
{ {
set_border_width(10); set_border_width(10);
...@@ -53,9 +55,12 @@ QWindow::QWindow() ...@@ -53,9 +55,12 @@ QWindow::QWindow()
m_fpsBox.pack_start(m_proppergatingRate, Gtk::PACK_EXPAND_WIDGET); m_fpsBox.pack_start(m_proppergatingRate, Gtk::PACK_EXPAND_WIDGET);
m_vBox.pack_start(m_fpsBox, Gtk::PACK_SHRINK); m_vBox.pack_start(m_fpsBox, Gtk::PACK_SHRINK);
m_scoreBox.pack_start(m_scorePlayerOne, Gtk::PACK_EXPAND_WIDGET);
m_scoreBox.pack_start(m_scorePlayerTwo, Gtk::PACK_EXPAND_WIDGET);
m_vBox.pack_start(m_scoreBox, Gtk::PACK_SHRINK);
m_hBox.pack_start(m_positionArea, Gtk::PACK_EXPAND_WIDGET); m_hBox.pack_start(m_positionArea);
m_hBox.pack_start(m_momentumArea), Gtk::PACK_EXPAND_WIDGET; m_hBox.pack_start(m_momentumArea, Gtk::PACK_EXPAND_WIDGET);
m_vBox.pack_start(m_hBox); m_vBox.pack_start(m_hBox);
m_label.set_margin_top(5); m_label.set_margin_top(5);
...@@ -103,6 +108,16 @@ void QWindow::updateGui() ...@@ -103,6 +108,16 @@ void QWindow::updateGui()
ss << "simulation rate: "; ss << "simulation rate: ";
ss << 1000 / propRate; ss << 1000 / propRate;
m_proppergatingRate.set_text(ss.str()); m_proppergatingRate.set_text(ss.str());
ss = std::stringstream();
ss << "Score Player 1: ";
ss << m_particle->getScore(Player::One);
m_scorePlayerOne.set_text(ss.str());
ss = std::stringstream();
ss << "Score Player 2: ";
ss << m_particle->getScore(Player::Two);
m_scorePlayerTwo.set_text(ss.str());
} }
} }
......
...@@ -43,6 +43,10 @@ private: ...@@ -43,6 +43,10 @@ private:
Gtk::Label m_renderRate; Gtk::Label m_renderRate;
Gtk::Label m_proppergatingRate; Gtk::Label m_proppergatingRate;
Gtk::Box m_scoreBox;
Gtk::Label m_scorePlayerOne;
Gtk::Label m_scorePlayerTwo;
Glib::Dispatcher m_guiDispatcher; Glib::Dispatcher m_guiDispatcher;
Glib::Dispatcher m_areaDispatcher; Glib::Dispatcher m_areaDispatcher;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment