Skip to content

Commit

Permalink
Merge pull request #45 from fatonhoti/44-smart-pointers
Browse files Browse the repository at this point in the history
44 smart pointers
  • Loading branch information
eliskleen authored Feb 8, 2024
2 parents 88ae88a + 5719016 commit eeb2b04
Show file tree
Hide file tree
Showing 39 changed files with 370 additions and 393 deletions.
8 changes: 6 additions & 2 deletions Game/Game.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,24 @@
<ClInclude Include="include\Asteriod.h">
<Filter>include</Filter>
</ClInclude>
<ClInclude Include="include\Bullet.h">
<Filter>include</Filter>
</ClInclude>
<ClInclude Include="include\Spaceship.h">
<Filter>include</Filter>
</ClInclude>
<ClInclude Include="main.h" />
<ClInclude Include="include\Bullet.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.cpp" />
<ClCompile Include="src\Asteriod.cpp">
<Filter>src</Filter>
</ClCompile>
<ClCompile Include="src\Bullet.cpp">
<Filter>src</Filter>
</ClCompile>
<ClCompile Include="src\Spaceship.cpp">
<Filter>src</Filter>
</ClCompile>
<ClCompile Include="src\Bullet.cpp" />
</ItemGroup>
</Project>
16 changes: 12 additions & 4 deletions Game/include/Asteriod.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@
#include "PhysicsBody.h"
#include "InputEvent.h"
#include "InputManager.h"
#include "Logger.h"

class Asteroid : public Shard::GameObject, public Shard::InputListener, public Shard::CollisionHandler {
#include <memory>

class Asteroid
: public Shard::GameObject
, public Shard::InputListener
, public Shard::CollisionHandler
// , public std::enable_shared_from_this<Asteroid>
{
public:
int torque_counter{10};

Expand All @@ -21,9 +29,9 @@ class Asteroid : public Shard::GameObject, public Shard::InputListener, public S
void update() override;
void killMe() override;

void onCollisionEnter(Shard::PhysicsBody* body) override;
void onCollisionExit(Shard::PhysicsBody* body) override ;
void onCollisionStay(Shard::PhysicsBody* body) override;
void onCollisionEnter(std::shared_ptr<Shard::PhysicsBody> body) override;
void onCollisionExit(std::shared_ptr<Shard::PhysicsBody> body) override ;
void onCollisionStay(std::shared_ptr<Shard::PhysicsBody> body) override;


};
10 changes: 4 additions & 6 deletions Game/include/Bullet.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,17 @@
class Bullet : public Shard::GameObject, public Shard::CollisionHandler {
public:
Bullet();
void setupBullet(Spaceship* origin_spaceship, float x, float y);
void setupBullet(float x, float y);
void initialize() override;
void physicsUpdate() override;
void prePhysicsUpdate() override;
void update() override;

void onCollisionEnter(Shard::PhysicsBody* body) override;
void onCollisionExit(Shard::PhysicsBody* body) override;
void onCollisionStay(Shard::PhysicsBody* body) override;

void onCollisionEnter(std::shared_ptr<Shard::PhysicsBody> body) override;
void onCollisionExit(std::shared_ptr<Shard::PhysicsBody> body) override;
void onCollisionStay(std::shared_ptr<Shard::PhysicsBody> body) override;

void killMe() override;

private:
Spaceship* origin;
};
19 changes: 10 additions & 9 deletions Game/include/Spaceship.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ class Spaceship : public Shard::InputListener, public Shard::GameObject, public
bool up, down, turn_left, turn_right;

Spaceship();
Spaceship(const Spaceship* src) : Shard::GameObject(this) {
this->up = src->up;
this->down = src->down;
this->turn_left = src->turn_left;
this->turn_right = src->turn_right;
}

//Spaceship(std::shared_ptr<Spaceship> src) : Shard::GameObject(shared_from_this()) {
// this->up = src->up;
// this->down = src->down;
// this->turn_left = src->turn_left;
// this->turn_right = src->turn_right;
//}

//Spaceship(const Spaceship* src) {
// this->body_ = src->body_;
Expand All @@ -31,9 +32,9 @@ class Spaceship : public Shard::InputListener, public Shard::GameObject, public
//}

void fireBullet();
void onCollisionEnter(Shard::PhysicsBody* body) override;
void onCollisionExit(Shard::PhysicsBody* body) override;
void onCollisionStay(Shard::PhysicsBody* body) override;
void onCollisionEnter(std::shared_ptr<Shard::PhysicsBody> body) override;
void onCollisionExit(std::shared_ptr<Shard::PhysicsBody> body) override;
void onCollisionStay(std::shared_ptr<Shard::PhysicsBody> body) override;

// inherited from InputListener
void handleEvent(Shard::InputEvent ev, Shard::EventType et);
Expand Down
31 changes: 17 additions & 14 deletions Game/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
#include "Logger.h"
#include "Bootstrap.h"
#include "Spaceship.h"

#include "GameObjectManager.h"
#include <iostream>

#undef main
#define FAST 100000000000;
#define SLOW 50;

GameTest::GameTest() {
// Empty constructor.
}
Expand All @@ -26,27 +27,28 @@ int GameTest::getTargetFrameRate() {

void GameTest::createShip() {
Shard::Logger::log("Creating spaceship");
// shared_ptr<A> a( new A );
spaceship = std::make_unique<Spaceship>();
Shard::Bootstrap::getInput()->addListeners(spaceship.get());
spaceship->initialize();
Shard::GameObjectManager::getInstance().addGameObject(spaceship->shared_from_this());
Shard::Bootstrap::getInput().addListeners(spaceship);
}

void GameTest::createAsteroid(float x, float y) {
Asteroid* asteroid = new Asteroid();
asteroid->transform_->x = x;
asteroid->transform_->y = y;
auto asteroid = std::make_shared<Asteroid>();
asteroid->initialize();
Shard::GameObjectManager::getInstance().addGameObject(asteroid->shared_from_this());
asteroid->body_->trans->x = x;
asteroid->body_->trans->y = y;
asteroids.push_back(asteroid);

//Shard::Logger::log("Creating asteroid");
//asteroid = std::make_unique<Asteroid>(x, y);
//Shard::Bootstrap::getInput()->addListeners(asteroid.get());
}

void GameTest::initalize() {
Shard::Logger::log("Initializing game");
createShip();
//for(int i = 0; i < 100; i++)
// createAsteroid(i%10, i%10);
Shard::Bootstrap::getInput()->addListeners(this);
Shard::Bootstrap::getInput().addListeners(shared_from_this());
}

void GameTest::handleEvent(Shard::InputEvent ie, Shard::EventType et) {
Expand All @@ -55,7 +57,8 @@ void GameTest::handleEvent(Shard::InputEvent ie, Shard::EventType et) {


if (ie.button == SDL_BUTTON_LEFT) {
createAsteroid(ie.x, ie.y);
for(int i = 0; i < 1; i++)
createAsteroid(ie.x, ie.y);
}
else if (ie.button == SDL_BUTTON_RIGHT) {
for (auto astr : asteroids) {
Expand All @@ -71,9 +74,9 @@ int main() {
Shard::Logger::log("Hello from game?!?!?");

Shard::Logger::log("Creating 'GameTest' object");
GameTest game;

Shard::Bootstrap::setRunningGame(&game);

auto game = std::make_shared<GameTest>();
Shard::Bootstrap::setRunningGame(game);

Shard::Logger::log("Runnning Bootstrap::Main");
Shard::Bootstrap::Main({});
Expand Down
8 changes: 3 additions & 5 deletions Game/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <vector>
#include <memory>

class GameTest : public Shard::Game, public Shard::InputListener {
class GameTest : public Shard::Game, public Shard::InputListener, public std::enable_shared_from_this<GameTest> {
public:
GameTest();

Expand All @@ -23,8 +23,6 @@ class GameTest : public Shard::Game, public Shard::InputListener {

void handleEvent(Shard::InputEvent ev, Shard::EventType et);

std::vector<Asteroid*> asteroids{};
//std::vedctor<Asteroid*> asteroids{ nullptr };
//std::unique_ptr<Asteroid> asteroid{ nullptr };
std::unique_ptr<Spaceship> spaceship{ nullptr };
std::vector<std::shared_ptr<Asteroid>> asteroids{};
std::shared_ptr<Spaceship> spaceship;
};
38 changes: 26 additions & 12 deletions Game/src/Asteriod.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
#include "Asteriod.h"

#include "GameObjectManager.h"
#include "Bootstrap.h"
#include "Logger.h"
#include <memory>

void handleAsteroidInput(Shard::InputEvent ev, Shard::EventType et) {

}

Asteroid::Asteroid() : GameObject() {
initialize();

}


void Asteroid::handleEvent(Shard::InputEvent ie, Shard::EventType et) {
if (et == Shard::EventType::MouseDown && ie.button == SDL_BUTTON_MIDDLE) {
if ((body_->checkCollisions(glm::vec2{ ie.x, ie.y })).has_value()) {
Expand All @@ -22,10 +24,10 @@ void Asteroid::handleEvent(Shard::InputEvent ie, Shard::EventType et) {
void Asteroid::initialize()
{
setPhysicsEnabled();
transform_->x = 300.0f;
transform_->y = 300.0f;
auto path = Shard::Bootstrap::getAssetManager()->getAssetPath("asteroid.png");
transform_->sprite_path = path;
body_->trans->x = 300.0f;
body_->trans->y = 300.0f;
auto path = Shard::Bootstrap::getAssetManager().getAssetPath("asteroid.png");
body_->trans->sprite_path = path;

body_->max_torque = 50;
body_->max_force = 100;
Expand All @@ -37,17 +39,29 @@ void Asteroid::initialize()
//body_->pass_through = false;

// TODO: Add randomness to initial force
//body_->addForce(transform_->right, 2.0f);
//body_->addForce(body_->trans.right, 2.0f);

body_->addRectCollider();

Shard::Bootstrap::getInput()->addListeners(this);
/*
getSharedPtr() -> std::shared_ptr<GameBoject>
asteroid->getSharedPtr();
std::shared_ptr<GameObject>
*/

Shard::Bootstrap::getInput().addListeners(
// shared_ptr<GOBJ> (ptr -> asteroid (which is listener))
//shared_from_this()
std::dynamic_pointer_cast<Shard::InputListener>(shared_from_this())
);

GameObject::addTag("Asteroid");

}
void Asteroid::update(){
Shard::Bootstrap::getDisplay()->addToDraw(this);
Shard::Bootstrap::getDisplay()->addToDraw(shared_from_this());
}

void Asteroid::physicsUpdate() {
Expand All @@ -58,7 +72,7 @@ void Asteroid::physicsUpdate() {
torque_counter -= 1;
}

void Asteroid::onCollisionEnter(Shard::PhysicsBody* body) {
void Asteroid::onCollisionEnter(std::shared_ptr<Shard::PhysicsBody> body) {

Shard::Logger::log("INSIDE ONCOLLISIONENTER ASTEROOOOOOOOID@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@", Shard::LoggerLevel::LOG_LEVEL_ALL);

Expand All @@ -70,11 +84,11 @@ void Asteroid::onCollisionEnter(Shard::PhysicsBody* body) {
Shard::Logger::log("Bang!");
}

void Asteroid::onCollisionExit(Shard::PhysicsBody* body) {
void Asteroid::onCollisionExit(std::shared_ptr<Shard::PhysicsBody> body) {
Shard::Logger::log("Anti bang!");
}

void Asteroid::onCollisionStay(Shard::PhysicsBody* body) { }
void Asteroid::onCollisionStay(std::shared_ptr<Shard::PhysicsBody> body) { }
void Asteroid::killMe(){
}
void Asteroid::prePhysicsUpdate(){}
38 changes: 18 additions & 20 deletions Game/src/Bullet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,16 @@

#include <cstdlib>

Bullet::Bullet() : GameObject(), origin(nullptr) {
Bullet::Bullet() : GameObject() {
initialize();
}

void Bullet::setupBullet(Spaceship* origin_spaceship, float x, float y) {
void Bullet::setupBullet(float x, float y) {
setPhysicsEnabled();
transform_->x = x;
transform_->y = y;
transform_->w = 10;
transform_->h = 10;

this->origin = origin_spaceship;
body_->trans->x = x;
body_->trans->y = y;
body_->trans->w = 10;
body_->trans->h = 10;

body_->mass = 100.0f;
body_->max_force = 50.0f;
Expand All @@ -31,7 +29,7 @@ void Bullet::initialize() {
}

void Bullet::physicsUpdate() {
body_->addForce(transform_->forward, 100.0f);
body_->addForce(body_->trans->forward, 100.0f);
}

void Bullet::update() {
Expand All @@ -41,23 +39,23 @@ void Bullet::update() {
Shard::Display* disp = Shard::Bootstrap::getDisplay();

disp->drawLine(
transform_->x,
transform_->y,
transform_->x + 10,
transform_->y + 10,
body_->trans->x,
body_->trans->y,
body_->trans->x + 10,
body_->trans->y + 10,
color
);

disp->drawLine(
transform_->x + 10,
transform_->y,
transform_->x,
transform_->y + 10,
body_->trans->x + 10,
body_->trans->y,
body_->trans->x,
body_->trans->y + 10,
color
);
}

void Bullet::onCollisionEnter(Shard::PhysicsBody* body) {
void Bullet::onCollisionEnter(std::shared_ptr<Shard::PhysicsBody> body) {

Shard::Logger::log("INSIDE ONCOLLISIONENTER BULLETtt tttttttt@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@", Shard::LoggerLevel::LOG_LEVEL_ALL);

Expand All @@ -66,7 +64,7 @@ void Bullet::onCollisionEnter(Shard::PhysicsBody* body) {
to_be_destroyed_ = true;
}
}
void Bullet::onCollisionExit(Shard::PhysicsBody* body) {}
void Bullet::onCollisionStay(Shard::PhysicsBody* body) {}
void Bullet::onCollisionExit(std::shared_ptr<Shard::PhysicsBody> body) {}
void Bullet::onCollisionStay(std::shared_ptr<Shard::PhysicsBody> body) {}
void Bullet::killMe() {}
void Bullet::prePhysicsUpdate(){}
Loading

0 comments on commit eeb2b04

Please sign in to comment.