From 5c2d5f5454ad28c0a85610b332aaf2c6f0978cf9 Mon Sep 17 00:00:00 2001 From: Robert Date: Thu, 24 Sep 2020 19:06:57 +0200 Subject: [PATCH] Initial commit --- .gitignore | 4 +++ CMakeLists.txt | 22 +++++++++++++ src/CMakeLists.txt | 21 +++++++++++++ src/MainWindow.cpp | 12 +++++++ src/MainWindow.hpp | 15 +++++++++ src/Planet.cpp | 19 +++++++++++ src/Planet.hpp | 22 +++++++++++++ src/Screen.cpp | 78 ++++++++++++++++++++++++++++++++++++++++++++++ src/Screen.hpp | 32 +++++++++++++++++++ src/main.cpp | 12 +++++++ ui/MainWindow.ui | 42 +++++++++++++++++++++++++ 11 files changed, 279 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 src/CMakeLists.txt create mode 100644 src/MainWindow.cpp create mode 100644 src/MainWindow.hpp create mode 100644 src/Planet.cpp create mode 100644 src/Planet.hpp create mode 100644 src/Screen.cpp create mode 100644 src/Screen.hpp create mode 100644 src/main.cpp create mode 100644 ui/MainWindow.ui diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c8a4305 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.vs +out + +*.json diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..3cf74a0 --- /dev/null +++ b/CMakeLists.txt @@ -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") diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..b4abb30 --- /dev/null +++ b/src/CMakeLists.txt @@ -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 $ $ + COMMAND ${CMAKE_COMMAND} -E copy_if_different $ $ + COMMAND ${CMAKE_COMMAND} -E copy_if_different $ $ + ) \ No newline at end of file diff --git a/src/MainWindow.cpp b/src/MainWindow.cpp new file mode 100644 index 0000000..e3327d9 --- /dev/null +++ b/src/MainWindow.cpp @@ -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); +} \ No newline at end of file diff --git a/src/MainWindow.hpp b/src/MainWindow.hpp new file mode 100644 index 0000000..c7e476f --- /dev/null +++ b/src/MainWindow.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include +#include "ui_MainWindow.h" + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(QWidget* parent = Q_NULLPTR); + +private: + Ui::MainWindow ui; +}; diff --git a/src/Planet.cpp b/src/Planet.cpp new file mode 100644 index 0000000..80b3dbc --- /dev/null +++ b/src/Planet.cpp @@ -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); +} diff --git a/src/Planet.hpp b/src/Planet.hpp new file mode 100644 index 0000000..5b832af --- /dev/null +++ b/src/Planet.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include +#include +#include + +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; +}; \ No newline at end of file diff --git a/src/Screen.cpp b/src/Screen.cpp new file mode 100644 index 0000000..8ee2e80 --- /dev/null +++ b/src/Screen.cpp @@ -0,0 +1,78 @@ +#include "Screen.hpp" + +#include + +#include +#include +#include +#include +#include + +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(); +} \ No newline at end of file diff --git a/src/Screen.hpp b/src/Screen.hpp new file mode 100644 index 0000000..2e7fc4f --- /dev/null +++ b/src/Screen.hpp @@ -0,0 +1,32 @@ +#pragma once + +#include + +#include + +#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 planets; +}; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..2492cc0 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,12 @@ +#include +#include "MainWindow.hpp" + +int main(int argc, char** argv) +{ + QApplication app(argc, argv); + + MainWindow window; + window.show(); + + return app.exec(); +} \ No newline at end of file diff --git a/ui/MainWindow.ui b/ui/MainWindow.ui new file mode 100644 index 0000000..0a4df98 --- /dev/null +++ b/ui/MainWindow.ui @@ -0,0 +1,42 @@ + + + MainWindow + + + + 0 + 0 + 726 + 643 + + + + MainWindow + + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + QLayout::SetDefaultConstraint + + + + + + + + +