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

4
.gitignore vendored Normal file
View file

@ -0,0 +1,4 @@
.vs
out
*.json

22
CMakeLists.txt Normal file
View file

@ -0,0 +1,22 @@
# CMakeList.txt : Top-level CMake project file, do global configuration
# and include sub-projects here.
#
cmake_minimum_required (VERSION 3.8)
project ("GraviSim")
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOUIC_SEARCH_PATHS ${CMAKE_AUTOUIC_SEARCH_PATHS} ${CMAKE_SOURCE_DIR}/ui)
if(CMAKE_VERSION VERSION_LESS "3.7.0")
set(CMAKE_INCLUDE_CURRENT_DIR ON)
endif()
find_package(Qt5 COMPONENTS Core REQUIRED)
find_package(Qt5 COMPONENTS Widgets REQUIRED)
# Include sub-projects.
add_subdirectory ("src")

21
src/CMakeLists.txt Normal file
View file

@ -0,0 +1,21 @@
# CMakeList.txt : CMake project for GraviSim, include source and define
# project specific logic here.
#
cmake_minimum_required (VERSION 3.8)
# Add source to this project's executable.
add_executable (GraviSim
"main.cpp"
"MainWindow.cpp"
"Screen.hpp" "Screen.cpp" "Planet.hpp" "Planet.cpp")
target_link_libraries(GraviSim
Qt5::Widgets
Qt5::Core
)
add_custom_command(TARGET GraviSim POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:Qt5::Core> $<TARGET_FILE_DIR:GraviSim>
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:Qt5::Gui> $<TARGET_FILE_DIR:GraviSim>
COMMAND ${CMAKE_COMMAND} -E copy_if_different $<TARGET_FILE:Qt5::Widgets> $<TARGET_FILE_DIR:GraviSim>
)

12
src/MainWindow.cpp Normal file
View file

@ -0,0 +1,12 @@
#include "MainWindow.hpp"
#include "Screen.hpp"
MainWindow::MainWindow(QWidget* parent) :
QMainWindow(parent)
{
ui.setupUi(this);
Screen* screen = new Screen(this);
ui.layout->addWidget(screen, 0, 0);
}

15
src/MainWindow.hpp Normal file
View file

@ -0,0 +1,15 @@
#pragma once
#include <QMainWindow>
#include "ui_MainWindow.h"
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget* parent = Q_NULLPTR);
private:
Ui::MainWindow ui;
};

19
src/Planet.cpp Normal file
View file

@ -0,0 +1,19 @@
#include "Planet.hpp"
Planet::Planet(float xPos, float yPos, float radius, QColor color) :
position{ xPos, yPos }, radius(radius)
{
circle = QBrush(color);
}
Planet::Planet(QPointF pos, float radius, QColor color) :
position(pos), radius(radius)
{
circle = QBrush(color);
}
void Planet::Draw(QPainter* painter)
{
painter->setBrush(circle);
painter->drawEllipse(position, radius, radius);
}

22
src/Planet.hpp Normal file
View file

@ -0,0 +1,22 @@
#pragma once
#include <QBrush>
#include <QPainter>
#include <QPaintEvent>
class Planet
{
public:
Planet(float xPos, float yPos, float radius, QColor color);
Planet(QPointF pos, float radius, QColor color);
void Resize(float newRadius) { radius = newRadius; }
void Draw(QPainter* painter);
private:
QPointF position;
float radius;
QBrush circle;
};

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();
}

32
src/Screen.hpp Normal file
View file

@ -0,0 +1,32 @@
#pragma once
#include <vector>
#include <QOpenGLWidget>
#include "Planet.hpp"
class Screen : public QOpenGLWidget
{
public:
Screen(QWidget* parent);
void mouseMoveEvent(QMouseEvent* event) override;
void mousePressEvent(QMouseEvent* event) override;
void mouseReleaseEvent(QMouseEvent* event) override;
private slots:
void Render();
protected:
void paintEvent(QPaintEvent* event) override;
private:
bool mouseDown = false;
QPointF mouseClickPos;
QBrush background;
QTimer* renderTimer;
std::vector<Planet*> planets;
};

12
src/main.cpp Normal file
View file

@ -0,0 +1,12 @@
#include <QApplication>
#include "MainWindow.hpp"
int main(int argc, char** argv)
{
QApplication app(argc, argv);
MainWindow window;
window.show();
return app.exec();
}

42
ui/MainWindow.ui Normal file
View file

@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>726</width>
<height>643</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QGridLayout" name="gridLayout">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item row="0" column="0">
<layout class="QGridLayout" name="layout">
<property name="sizeConstraint">
<enum>QLayout::SetDefaultConstraint</enum>
</property>
</layout>
</item>
</layout>
</widget>
</widget>
<resources/>
<connections/>
</ui>