Initial commit

This commit is contained in:
Robert 2020-09-24 19:06:57 +02:00
parent 70e0a1729e
commit 5c2d5f5454
11 changed files with 279 additions and 0 deletions

78
src/Screen.cpp Normal file
View file

@ -0,0 +1,78 @@
#include "Screen.hpp"
#include <time.h>
#include <QPainter>
#include <QPaintEvent>
#include <QMouseEvent>
#include <QTimer>
#include <QtMath>
Screen::Screen(QWidget* parent) :
QOpenGLWidget(parent)
{
srand(time(NULL));
background = QBrush(Qt::black);
renderTimer = new QTimer(this);
connect(
renderTimer, &QTimer::timeout,
this, &Screen::Render
);
renderTimer->start(1000.f / 60.0f);
}
void Screen::mouseMoveEvent(QMouseEvent* event)
{
if (mouseDown)
{
Planet* planet = planets.back();
QPointF distance = event->localPos() - mouseClickPos;
planet->Resize(qSqrt(distance.x()*distance.x() + distance.y() *distance.y()));
}
}
void Screen::mousePressEvent(QMouseEvent* event)
{
if (event->button() == Qt::MouseButton::LeftButton)
{
planets.push_back(new Planet(event->localPos(), 0,
QColor(rand() % 256, rand() % 256, rand() % 256)
));
mouseClickPos = event->localPos();
mouseDown = true;
}
if (event->button() == Qt::MouseButton::RightButton)
{
if (mouseDown)
{
planets.pop_back();
mouseDown = false;
}
}
}
void Screen::mouseReleaseEvent(QMouseEvent* event)
{
if (event->button() == Qt::MouseButton::LeftButton)
mouseDown = false;
}
void Screen::Render()
{
update();
}
void Screen::paintEvent(QPaintEvent* event)
{
QPainter painter;
painter.begin(this);
painter.fillRect(rect(), background);
for (Planet* planet : planets)
planet->Draw(&painter);
painter.end();
}