Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Feeeeddmmmeee committed May 2, 2022
1 parent 886b78a commit 1c55f48
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 85 deletions.
33 changes: 7 additions & 26 deletions GravitySim/src/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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++)
{
Expand All @@ -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));
Expand Down Expand Up @@ -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)
Expand All @@ -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);

Expand All @@ -166,7 +153,6 @@ void Game::render()
void Game::handleEvents()
{
SDL_Event event;

SDL_PollEvent(&event);

switch (event.type)
Expand Down Expand Up @@ -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);
Expand All @@ -228,7 +213,6 @@ void Game::handleEvents()
if (this->editingPos)
{
this->editingPos = false;
//isPaused = false;
}
break;

Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 0 additions & 4 deletions GravitySim/src/Game.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
#ifndef GAME_H
#define GAME_H

#include "SDL.h"
#include "SDL_image.h"
#include "Universe.h"
#include "UIManager.h"

#include <iostream>

class Game {

public:
Expand Down
53 changes: 1 addition & 52 deletions GravitySim/src/Planet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,6 @@

#include <Windows.h>

//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);
Expand All @@ -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);
Expand All @@ -55,17 +27,14 @@ void Planet::updateVelocity(std::vector<Planet*>& 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;
Expand All @@ -76,14 +45,13 @@ void Planet::updateVelocity(std::vector<Planet*>& 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();
Expand All @@ -94,25 +62,6 @@ void Planet::updateVelocity(std::vector<Planet*>& 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;
}

Expand Down
4 changes: 1 addition & 3 deletions GravitySim/src/Planet.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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();

Expand Down
2 changes: 2 additions & 0 deletions GravitySim/src/UIManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,6 @@ UIElement* UIManager::getElementByID(ID identifier)
return button;
}
}

return nullptr;
}

0 comments on commit 1c55f48

Please sign in to comment.