diff --git a/GravitySim/src/Game.cpp b/GravitySim/src/Game.cpp index bec242b..15983c4 100644 --- a/GravitySim/src/Game.cpp +++ b/GravitySim/src/Game.cpp @@ -57,13 +57,6 @@ Game::Game(const char* title, int xpos, int ypos, int width, int height, bool fu this->play = SDL_CreateTextureFromSurface(renderer, tempSurface); SDL_FreeSurface(tempSurface); - // 4 Planets in a circle - /*universe.addPlanet(new Planet(100000, Vector(200, 400), Vector(0, 1), renderer, this->planet)); - universe.addPlanet(new Planet(100000, Vector(600, 400), Vector(0, -1), renderer, this->planet)); - universe.addPlanet(new Planet(100000, Vector(400, 200), Vector(-1, 0), renderer, this->planet)); - universe.addPlanet(new Planet(100000, Vector(400, 600), Vector(1, 0), renderer, this->planet));//*/ - - // Random planets srand(time(NULL)); for (int i = 0; i < 100; i++) { @@ -75,7 +68,6 @@ Game::Game(const char* title, int xpos, int ypos, int width, int height, bool fu 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));//*/ - // Buttons 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)); @@ -106,24 +98,23 @@ void Game::update() if(!isPaused) this->universe.update(); UIElement* e = this->ui->getElementByID(ID::PAUSE); - switch (this->isPaused) + if (!this->isPaused) { - case 0: if (e->getTexture() == this->play) e->updateTexture(this->pause); - break; - - case 1: + } + else + { if (e->getTexture() == this->pause) e->updateTexture(this->play); - break; - } int x, y; SDL_GetMouseState(&x, &y); Vector mousePos = Vector(x, y); + this->ui->update(mousePos); + if (this->tempPlanet) { if (editingM) @@ -139,18 +130,14 @@ void Game::render() { SDL_RenderClear(renderer); - // Planets this->universe.render(this->renderer, this->zoom); - // UI 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); - // Temp Planet if (this->tempPlanet) this->tempPlanet->render(renderer, this->zoom); if (this->editingV && this->tempPlanet) { - // Rendering the velocity vector int x, y; SDL_GetMouseState(&x, &y); @@ -166,7 +153,6 @@ void Game::render() void Game::handleEvents() { SDL_Event event; - SDL_PollEvent(&event); switch (event.type) @@ -213,7 +199,6 @@ void Game::handleEvents() if (event.button.button == SDL_BUTTON_MIDDLE) { this->editingPos = true; - //isPaused = true; int x, y; SDL_GetMouseState(&x, &y); @@ -228,7 +213,6 @@ void Game::handleEvents() if (this->editingPos) { this->editingPos = false; - //isPaused = false; } break; @@ -254,13 +238,11 @@ void Game::handleEvents() } else if (editingV) { - // Adding velocity int x, y; SDL_GetMouseState(&x, &y); tempPlanet->velocity = Vector(x, y) - tempPlanet->position; tempPlanet->velocity = tempPlanet->velocity / 10; - // Adding the planet this->universe.addPlanet(new Planet(*tempPlanet)); delete tempPlanet; tempPlanet = nullptr; @@ -300,14 +282,13 @@ void Game::handleEvents() this->isPaused = true; int m; - //std::cin >> m; m = 10000; if (this->tempPlanet) { delete tempPlanet; } - // + this->tempPlanet = new Planet(m, Vector(0, 0), Vector(0, 0), renderer, this->planet); skipM = false; break; diff --git a/GravitySim/src/Game.h b/GravitySim/src/Game.h index 4332e1e..f849540 100644 --- a/GravitySim/src/Game.h +++ b/GravitySim/src/Game.h @@ -1,13 +1,9 @@ #ifndef GAME_H #define GAME_H -#include "SDL.h" -#include "SDL_image.h" #include "Universe.h" #include "UIManager.h" -#include - class Game { public: diff --git a/GravitySim/src/Planet.cpp b/GravitySim/src/Planet.cpp index 8ba4044..abaafbd 100644 --- a/GravitySim/src/Planet.cpp +++ b/GravitySim/src/Planet.cpp @@ -5,21 +5,6 @@ #include -//const double MOUSE = -1000; - -Planet::Planet(double mass, Vector position, Vector velocity, SDL_Renderer* renderer) -{ - this->radius = std::cbrt(4 * mass / DENSITY / PI / 3); - this->acceleration = Vector(); - this->mass = mass; - this->position = position; - this->velocity = velocity; - - SDL_Surface* tempSurface = IMG_Load("planet.png"); - this->texture = SDL_CreateTextureFromSurface(renderer, tempSurface); - SDL_FreeSurface(tempSurface); -} - Planet::Planet(double mass, Vector position, Vector velocity, SDL_Renderer* renderer, SDL_Texture* texture) { this->radius = std::cbrt(4 * mass / DENSITY / PI / 3); @@ -31,19 +16,6 @@ Planet::Planet(double mass, Vector position, Vector velocity, SDL_Renderer* rend this->texture = texture; } -Planet::Planet(double mass, Vector position, Vector velocity, SDL_Renderer* renderer, const char* fileName) -{ - this->radius = std::cbrt(4 * mass / DENSITY / PI / 3); - this->acceleration = Vector(); - this->mass = mass; - this->position = position; - this->velocity = velocity; - - SDL_Surface* tempSurface = IMG_Load(fileName); - this->texture = SDL_CreateTextureFromSurface(renderer, tempSurface); - SDL_FreeSurface(tempSurface); -} - void Planet::destroyTexture() { SDL_DestroyTexture(this->texture); @@ -55,17 +27,14 @@ void Planet::updateVelocity(std::vector& others) for (auto& other : others) { - //cant collide with itself if (other == this) continue; - //collision 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 (this->mass > other->mass) { this->mass += other->mass; this->velocity += other->velocity * other->mass / this->mass; - //this->radius = std::cbrt(4 * mass / DENSITY / PI / 3); Planet* p = other; delete p; @@ -76,14 +45,13 @@ void Planet::updateVelocity(std::vector& others) { other->mass += this->mass; other->velocity += this->velocity * this->mass / other->mass; - //other->radius = std::cbrt(4 * mass / DENSITY / PI / 3); Planet* p = this; delete p; others.erase(std::remove(others.begin(), others.end(), this), others.end()); return; } - }//*/ + } Vector posV = other->position - this->position; double distance = posV.Lenght(); @@ -94,25 +62,6 @@ void Planet::updateVelocity(std::vector& others) this->radius = std::cbrt(4 * mass / DENSITY / PI / 3); } - //mouse - /*POINT p; - HWND handle; - handle = FindWindowA(NULL, "Gravity Simulation"); - if (GetCursorPos(&p)) - { - if (ScreenToClient(handle, &p)) - { - Vector mouse = Vector(p.x, p.y); - - Vector posV = mouse - this->position; - double distance = posV.Lenght(); - Vector mag = posV / distance; - - double force = this->mass * MOUSE * G / pow(distance, 2); - this->acceleration += mag * force / this->mass; - } - }//*/ - this->velocity += this->acceleration; } diff --git a/GravitySim/src/Planet.h b/GravitySim/src/Planet.h index eb353a4..c71aeac 100644 --- a/GravitySim/src/Planet.h +++ b/GravitySim/src/Planet.h @@ -7,7 +7,7 @@ constexpr auto PI = 3.1415; constexpr auto DENSITY = 1; -constexpr auto G = 0.1; //6.67408e-11; +constexpr auto G = 0.1; struct Planet { @@ -17,9 +17,7 @@ struct Planet SDL_Rect destRect; SDL_Texture* texture; - Planet(double mass, Vector position, Vector velocity, SDL_Renderer* renderer); Planet(double mass, Vector position, Vector velocity, SDL_Renderer* renderer, SDL_Texture* texture); - Planet(double mass, Vector position, Vector velocity, SDL_Renderer* renderer, const char* fileName); void destroyTexture(); diff --git a/GravitySim/src/UIManager.cpp b/GravitySim/src/UIManager.cpp index 9f4773e..e25f727 100644 --- a/GravitySim/src/UIManager.cpp +++ b/GravitySim/src/UIManager.cpp @@ -66,4 +66,6 @@ UIElement* UIManager::getElementByID(ID identifier) return button; } } + + return nullptr; }