From 675e0d4a07b73f26bd2582425e4539a12474a896 Mon Sep 17 00:00:00 2001 From: Feeeeddmmmeee Date: Mon, 2 May 2022 15:25:36 +0200 Subject: [PATCH] removed unnecessary #include statements --- GravitySim/src/Game.cpp | 139 +++++++++++++++++------------------ GravitySim/src/Game.h | 2 + GravitySim/src/Planet.cpp | 9 +-- GravitySim/src/Planet.h | 4 +- GravitySim/src/UIElement.cpp | 6 +- GravitySim/src/UIElement.h | 4 +- GravitySim/src/UIManager.h | 1 - GravitySim/src/Universe.cpp | 10 +-- GravitySim/src/Universe.h | 4 - GravitySim/src/Vector.cpp | 4 +- GravitySim/src/main.cpp | 1 - 11 files changed, 86 insertions(+), 98 deletions(-) diff --git a/GravitySim/src/Game.cpp b/GravitySim/src/Game.cpp index 15983c4..53b1c99 100644 --- a/GravitySim/src/Game.cpp +++ b/GravitySim/src/Game.cpp @@ -1,9 +1,5 @@ #include "Game.h" -#include -#include -#include - Game::Game(const char* title, int xpos, int ypos, int width, int height, bool fullscreen) { int flags = 0; @@ -16,20 +12,20 @@ Game::Game(const char* title, int xpos, int ypos, int width, int height, bool fu { std::cout << "Subsystems Initialized!" << std::endl; - window = SDL_CreateWindow(title, xpos, ypos, width, height, flags); - if (window) + this->window = SDL_CreateWindow(title, xpos, ypos, width, height, flags); + if (this->window) { std::cout << "Window Created!" << std::endl; } - renderer = SDL_CreateRenderer(window, -1, 0); - if (renderer) + this->renderer = SDL_CreateRenderer(this->window, -1, 0); + if (this->renderer) { - SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); + SDL_SetRenderDrawColor(this->renderer, 0, 0, 0, 255); std::cout << "Renderer Created!" << std::endl; } - isRunning = true; + this->isRunning = true; } if (TTF_Init() == 0) @@ -60,24 +56,24 @@ Game::Game(const char* title, int xpos, int ypos, int width, int height, bool fu srand(time(NULL)); for (int i = 0; i < 100; i++) { - universe.addPlanet(new Planet(10 + std::rand() % 91, Vector(std::rand() % width, std::rand() % height), Vector((std::rand() % 10 - 5) / 10, (std::rand() % 10 - 5) / 10), renderer, this->planet)); + this->universe.addPlanet(new Planet(10 + std::rand() % 91, Vector(std::rand() % width, std::rand() % height), Vector((std::rand() % 10 - 5) / 10, (std::rand() % 10 - 5) / 10), this->renderer, this->planet)); }//*/ // Orbiting planets - /*universe.addPlanet(new Planet(100000, Vector(width/2, height/2), Vector(0 + 2, -0.3), renderer, this->planet)); - universe.addPlanet(new Planet(1000, Vector(width/2, height/2 - height/4), Vector(7 + 2, -0.3), renderer, this->planet)); - universe.addPlanet(new Planet(1000, Vector(width/2, height/2 + height/4), Vector(-7 + 2, -0.3), renderer, this->planet));//*/ - - ui = new UIManager(); - ui->buttons.push_back(new UIElement(Vector(width - 34 - 5, 10), this->renderer, "res/gfx/gui_exit.png", ID::EXIT)); - ui->buttons.push_back(new UIElement(Vector(width - 34 - 5, 10 + 34 + 5), this->renderer, this->pause, ID::PAUSE)); - ui->buttons.push_back(new UIElement(Vector(width - 34 - 5, 10 + 2*34 + 2*5), this->renderer, "res/gfx/gui_restart.png", ID::RESTART)); - ui->buttons.push_back(new UIElement(Vector(width - 34 - 5, 10 + 3*34 + 3*5), this->renderer, "res/gfx/gui_planet.png", ID::PLANET)); - ui->buttons.push_back(new UIElement(Vector(width - 34 - 5, 10 + 4*34 + 4*5), this->renderer, "res/gfx/gui_earth.png", ID::EARTH)); - ui->buttons.push_back(new UIElement(Vector(width - 34 - 5, 10 + 5*34 + 5*5), this->renderer, "res/gfx/gui_mars.png", ID::MARS)); + /*this->universe.addPlanet(new Planet(100000, Vector(width/2, height/2), Vector(0 + 2, -0.3), this->renderer, this->planet)); + this->universe.addPlanet(new Planet(1000, Vector(width/2, height/2 - height/4), Vector(7 + 2, -0.3), this->renderer, this->planet)); + this->universe.addPlanet(new Planet(1000, Vector(width/2, height/2 + height/4), Vector(-7 + 2, -0.3), this->renderer, this->planet));//*/ + + this->ui = new UIManager(); + this->ui->buttons.push_back(new UIElement(Vector(width - 34 - 5, 10), this->renderer, "res/gfx/gui_exit.png", ID::EXIT)); + this->ui->buttons.push_back(new UIElement(Vector(width - 34 - 5, 10 + 34 + 5), this->renderer, this->pause, ID::PAUSE)); + this->ui->buttons.push_back(new UIElement(Vector(width - 34 - 5, 10 + 2*34 + 2*5), this->renderer, "res/gfx/gui_restart.png", ID::RESTART)); + this->ui->buttons.push_back(new UIElement(Vector(width - 34 - 5, 10 + 3*34 + 3*5), this->renderer, "res/gfx/gui_planet.png", ID::PLANET)); + this->ui->buttons.push_back(new UIElement(Vector(width - 34 - 5, 10 + 4*34 + 4*5), this->renderer, "res/gfx/gui_earth.png", ID::EARTH)); + this->ui->buttons.push_back(new UIElement(Vector(width - 34 - 5, 10 + 5*34 + 5*5), this->renderer, "res/gfx/gui_mars.png", ID::MARS)); - isPaused = false; - zoom = 1; + this->isPaused = false; + this->zoom = 1; TTF_Font* font = TTF_OpenFont("res/font/its_me_sans_undertale.ttf", 24); this->ui->textInit(font, { 255, 255, 255 }); @@ -85,7 +81,7 @@ Game::Game(const char* title, int xpos, int ypos, int width, int height, bool fu void Game::update() { - if (editingPos) + if (this->editingPos) { int x, y; SDL_GetMouseState(&x, &y); @@ -95,7 +91,7 @@ void Game::update() this->previousMousePos = mouseVec; } - if(!isPaused) this->universe.update(); + if(!this->isPaused) this->universe.update(); UIElement* e = this->ui->getElementByID(ID::PAUSE); if (!this->isPaused) @@ -117,12 +113,13 @@ void Game::update() if (this->tempPlanet) { - if (editingM) + if (this->editingM) { - tempPlanet->radius = max(Vector(mousePos - tempPlanet->position).Lenght(), 1); - tempPlanet->mass = pow(tempPlanet->radius, 3) * 3 * DENSITY * PI / 4; + float len = Vector(mousePos - this->tempPlanet->position).Lenght(); + this->tempPlanet->radius = len > 1 ? len : 1; + this->tempPlanet->mass = pow(this->tempPlanet->radius, 3) * 3 * DENSITY * PI / 4; } - else if(!editingV) this->tempPlanet->position = mousePos; + else if(!this->editingV) this->tempPlanet->position = mousePos; } } @@ -135,19 +132,19 @@ void Game::render() std::string arr[3] = { "FPS: " + std::to_string(this->currentFPS), "Planets: " + std::to_string(this->universe.size()), "Paused: " + std::string(this->isPaused ? "true" : "false") }; this->ui->render(this->renderer, arr, 3); - if (this->tempPlanet) this->tempPlanet->render(renderer, this->zoom); + if (this->tempPlanet) this->tempPlanet->render(this->renderer, this->zoom); if (this->editingV && this->tempPlanet) { int x, y; SDL_GetMouseState(&x, &y); - SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); - SDL_RenderDrawLine(this->renderer, tempPlanet->position.x, tempPlanet->position.y, x, y); - SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0); + SDL_SetRenderDrawColor(this->renderer, 255, 0, 0, 255); + SDL_RenderDrawLine(this->renderer, this->tempPlanet->position.x, this->tempPlanet->position.y, x, y); + SDL_SetRenderDrawColor(this->renderer, 0, 0, 0, 0); } - SDL_RenderPresent(renderer); + SDL_RenderPresent(this->renderer); } void Game::handleEvents() @@ -158,25 +155,25 @@ void Game::handleEvents() switch (event.type) { case SDL_QUIT: - isRunning = false; + this->isRunning = false; break; case SDL_KEYDOWN: switch (event.key.keysym.sym) { case SDLK_ESCAPE: - isRunning = false; + this->isRunning = false; break; case SDLK_SPACE: - isPaused = !isPaused; + this->isPaused = !this->isPaused; if (this->tempPlanet) { - this->universe.addPlanet(new Planet(*tempPlanet)); - delete tempPlanet; - tempPlanet = nullptr; - editingV = false; - editingM = false; + this->universe.addPlanet(new Planet(*this->tempPlanet)); + delete this->tempPlanet; + this->tempPlanet = nullptr; + this->editingV = false; + this->editingM = false; } break; @@ -219,40 +216,40 @@ void Game::handleEvents() case SDL_BUTTON_RIGHT: if (this->tempPlanet) { - isPaused = !isPaused; - this->universe.addPlanet(new Planet(*tempPlanet)); - delete tempPlanet; - tempPlanet = nullptr; - editingV = false; - editingM = false; + this->isPaused = !this->isPaused; + this->universe.addPlanet(new Planet(*this->tempPlanet)); + delete this->tempPlanet; + this->tempPlanet = nullptr; + this->editingV = false; + this->editingM = false; } break; case SDL_BUTTON_LEFT: if (this->tempPlanet) { - if (editingM) + if (this->editingM) { - editingV = true; - editingM = false; + this->editingV = true; + this->editingM = false; } - else if (editingV) + else if (this->editingV) { int x, y; SDL_GetMouseState(&x, &y); - tempPlanet->velocity = Vector(x, y) - tempPlanet->position; - tempPlanet->velocity = tempPlanet->velocity / 10; + this->tempPlanet->velocity = Vector(x, y) - this->tempPlanet->position; + this->tempPlanet->velocity = this->tempPlanet->velocity / 10; - this->universe.addPlanet(new Planet(*tempPlanet)); + this->universe.addPlanet(new Planet(*this->tempPlanet)); delete tempPlanet; - tempPlanet = nullptr; - isPaused = false; - editingV = false; + this->tempPlanet = nullptr; + this->isPaused = false; + this->editingV = false; } else { - if (skipM) editingV = true; - else editingM = true; + if (this->skipM) this->editingV = true; + else this->editingM = true; } } @@ -261,13 +258,13 @@ void Game::handleEvents() { if (button->selected()) { - editingV = editingM = false; - skipM = true; + this->editingV = this->editingM = false; + this->skipM = true; switch (button->getID()) { case ID::EXIT: - isRunning = false; + this->isRunning = false; break; case ID::PAUSE: @@ -290,7 +287,7 @@ void Game::handleEvents() } this->tempPlanet = new Planet(m, Vector(0, 0), Vector(0, 0), renderer, this->planet); - skipM = false; + this->skipM = false; break; case ID::EARTH: @@ -299,7 +296,7 @@ void Game::handleEvents() { delete tempPlanet; } - this->tempPlanet = new Planet(EARTH_MASS, Vector(0, 0), Vector(0, 0), renderer, this->earth); + this->tempPlanet = new Planet(EARTH_MASS, Vector(0, 0), Vector(0, 0), this->renderer, this->earth); break; case ID::MARS: @@ -308,7 +305,7 @@ void Game::handleEvents() { delete tempPlanet; } - this->tempPlanet = new Planet(EARTH_MASS * 0.714, Vector(0, 0), Vector(0, 0), renderer, this->mars); + this->tempPlanet = new Planet(EARTH_MASS * 0.714, Vector(0, 0), Vector(0, 0), this->renderer, this->mars); break; } } @@ -321,15 +318,15 @@ void Game::handleEvents() Game::~Game() { - delete tempPlanet; - delete ui; + delete this->tempPlanet; + delete this->ui; SDL_DestroyTexture(this->planet); SDL_DestroyTexture(this->earth); SDL_DestroyTexture(this->mars); - SDL_DestroyWindow(window); - SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(this->window); + SDL_DestroyRenderer(this->renderer); SDL_Quit(); TTF_Quit(); } \ No newline at end of file diff --git a/GravitySim/src/Game.h b/GravitySim/src/Game.h index f849540..6c5048e 100644 --- a/GravitySim/src/Game.h +++ b/GravitySim/src/Game.h @@ -1,6 +1,8 @@ #ifndef GAME_H #define GAME_H +#include + #include "Universe.h" #include "UIManager.h" diff --git a/GravitySim/src/Planet.cpp b/GravitySim/src/Planet.cpp index abaafbd..c1b3bfc 100644 --- a/GravitySim/src/Planet.cpp +++ b/GravitySim/src/Planet.cpp @@ -1,10 +1,5 @@ #include "Planet.h" -#include "SDL_image.h" -#include "SDL.h" - -#include - Planet::Planet(double mass, Vector position, Vector velocity, SDL_Renderer* renderer, SDL_Texture* texture) { this->radius = std::cbrt(4 * mass / DENSITY / PI / 3); @@ -29,7 +24,7 @@ void Planet::updateVelocity(std::vector& others) { if (other == this) continue; - if (sqrt(pow((position.x + radius - other->position.x - other->radius), 2) + pow((position.y + radius - other->position.y - other->radius), 2)) < radius + other->radius) + if (sqrt(pow((this->position.x + this->radius - other->position.x - other->radius), 2) + pow((this->position.y + this->radius - other->position.y - other->radius), 2)) < this->radius + other->radius) { if (this->mass > other->mass) { @@ -59,7 +54,7 @@ void Planet::updateVelocity(std::vector& others) double force = this->mass * other->mass * G / pow(distance, 2); this->acceleration += mag * force / this->mass; - this->radius = std::cbrt(4 * mass / DENSITY / PI / 3); + this->radius = std::cbrt(4 * this->mass / DENSITY / PI / 3); } this->velocity += this->acceleration; diff --git a/GravitySim/src/Planet.h b/GravitySim/src/Planet.h index c71aeac..30a7250 100644 --- a/GravitySim/src/Planet.h +++ b/GravitySim/src/Planet.h @@ -1,10 +1,12 @@ #ifndef PLANET_H #define PLANET_H -#include "Vector.h" #include + #include "SDL_image.h" +#include "Vector.h" + constexpr auto PI = 3.1415; constexpr auto DENSITY = 1; constexpr auto G = 0.1; diff --git a/GravitySim/src/UIElement.cpp b/GravitySim/src/UIElement.cpp index d6a60ff..29621ae 100644 --- a/GravitySim/src/UIElement.cpp +++ b/GravitySim/src/UIElement.cpp @@ -32,12 +32,12 @@ UIElement::UIElement(Vector position, SDL_Renderer* renderer, SDL_Texture* textu void UIElement::update(Vector mousePos) { bool collision; - if (isSelected) collision = this->position.x < mousePos.x&& this->position.x + this->destRect.h > mousePos.x && this->position.y < mousePos.y&& this->destRect.h + this->position.y > mousePos.y; + if (this->isSelected) collision = this->position.x < mousePos.x&& this->position.x + this->destRect.h > mousePos.x && this->position.y < mousePos.y&& this->destRect.h + this->position.y > mousePos.y; else collision = this->position.x + 2 < mousePos.x&& this->position.x + 2 + this->destRect.h - 4 > mousePos.x && this->position.y + 2 < mousePos.y&& this->destRect.h - 4 + this->position.y + 2 > mousePos.y; if (collision) { - if (!isSelected) + if (!this->isSelected) { this->destRect.h -= 4; this->destRect.w -= 4; @@ -48,7 +48,7 @@ void UIElement::update(Vector mousePos) } else { - if (isSelected) + if (this->isSelected) { this->destRect.h += 4; this->destRect.w += 4; diff --git a/GravitySim/src/UIElement.h b/GravitySim/src/UIElement.h index 3196288..0d0a847 100644 --- a/GravitySim/src/UIElement.h +++ b/GravitySim/src/UIElement.h @@ -1,10 +1,8 @@ #ifndef UI_ELEMENT_H #define UI_ELEMENT_H -#include -#include +#include "SDL_image.h" -#include "Universe.h" #include "Vector.h" enum class ID diff --git a/GravitySim/src/UIManager.h b/GravitySim/src/UIManager.h index ff407b6..77c9c67 100644 --- a/GravitySim/src/UIManager.h +++ b/GravitySim/src/UIManager.h @@ -6,7 +6,6 @@ #include "SDL_ttf.h" #include "UIElement.h" -#include "Vector.h" class UIManager { diff --git a/GravitySim/src/Universe.cpp b/GravitySim/src/Universe.cpp index 4a172fe..81bdc7b 100644 --- a/GravitySim/src/Universe.cpp +++ b/GravitySim/src/Universe.cpp @@ -7,12 +7,12 @@ void Universe::addPlanet(Planet* planet) void Universe::update() { - for (auto& planet : planets) + for (auto& planet : this->planets) { - planet->updateVelocity(planets); + planet->updateVelocity(this->planets); } - for (auto& planet : planets) + for (auto& planet : this->planets) { planet->updatePosition(); } @@ -20,7 +20,7 @@ void Universe::update() void Universe::render(SDL_Renderer* renderer, float zoom) { - for (auto& planet : planets) + for (auto& planet : this->planets) { planet->render(renderer, zoom); } @@ -50,4 +50,4 @@ Vector Universe::getPlanetPosition(int& index) if (index > this->planets.size() - 1) index = 0; return this->planets[index]->position; -} +} \ No newline at end of file diff --git a/GravitySim/src/Universe.h b/GravitySim/src/Universe.h index 9ada6da..eee5fa9 100644 --- a/GravitySim/src/Universe.h +++ b/GravitySim/src/Universe.h @@ -2,10 +2,6 @@ #define UNIVERSE_H #include "Planet.h" -#include "Vector.h" - -#include -#include class Universe { diff --git a/GravitySim/src/Vector.cpp b/GravitySim/src/Vector.cpp index 35fa74d..2726b67 100644 --- a/GravitySim/src/Vector.cpp +++ b/GravitySim/src/Vector.cpp @@ -2,8 +2,8 @@ Vector::Vector() { - x = 0; - y = 0; + this->x = 0; + this->y = 0; } Vector::Vector(double x, double y) diff --git a/GravitySim/src/main.cpp b/GravitySim/src/main.cpp index 96ffa4d..9e8ce05 100644 --- a/GravitySim/src/main.cpp +++ b/GravitySim/src/main.cpp @@ -1,4 +1,3 @@ -#include "SDL.h" #include "Game.h" #include