More standard pointers.
This commit is contained in:
		@@ -10,8 +10,7 @@
 | 
			
		||||
 | 
			
		||||
using namespace Dawn;
 | 
			
		||||
 | 
			
		||||
Scene::Scene(DawnGame *game) {
 | 
			
		||||
  assertNotNull(game, "Scene::Scene: Game cannot be null");
 | 
			
		||||
Scene::Scene(std::weak_ptr<DawnGame> game) {
 | 
			
		||||
  this->game = game;
 | 
			
		||||
  this->nextId = 0;
 | 
			
		||||
  this->physics = new ScenePhysicsManager(this);
 | 
			
		||||
@@ -26,18 +25,13 @@ void Scene::update() {
 | 
			
		||||
    ++it;
 | 
			
		||||
  }
 | 
			
		||||
  this->itemsNotInitialized.clear();
 | 
			
		||||
 | 
			
		||||
  #if DAWN_DEBUG_BUILD
 | 
			
		||||
    this->debugGrid();
 | 
			
		||||
    this->debugOrigin();
 | 
			
		||||
    this->debugHitboxes();
 | 
			
		||||
  #endif
 | 
			
		||||
  
 | 
			
		||||
  // TODO: Cleanup old scene items
 | 
			
		||||
 | 
			
		||||
  // TODO: Tick scene items(?)
 | 
			
		||||
  auto game = this->game.lock();
 | 
			
		||||
  this->eventSceneUpdate.invoke(game->timeManager.delta);
 | 
			
		||||
  if(!this->game->timeManager.isPaused) this->eventSceneUnpausedUpdate.invoke(game->timeManager.delta);
 | 
			
		||||
  if(!game->timeManager.isPaused) this->eventSceneUnpausedUpdate.invoke(game->timeManager.delta);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
SceneItem * Scene::createSceneItem() {
 | 
			
		||||
 
 | 
			
		||||
@@ -25,14 +25,14 @@ namespace Dawn {
 | 
			
		||||
  template<class T>
 | 
			
		||||
  std::vector<std::shared_ptr<T>> _sceneForwardGetComponents(SceneItem *item);
 | 
			
		||||
 | 
			
		||||
  class Scene : public StateOwner {
 | 
			
		||||
  class Scene : public StateOwner, public std::enable_shared_from_this<Scene> {
 | 
			
		||||
    private:
 | 
			
		||||
      sceneitemid_t nextId;
 | 
			
		||||
      std::map<sceneitemid_t, SceneItem*> items;
 | 
			
		||||
      std::map<sceneitemid_t, SceneItem*> itemsNotInitialized;
 | 
			
		||||
    
 | 
			
		||||
    public:
 | 
			
		||||
      DawnGame *game;
 | 
			
		||||
      std::weak_ptr<DawnGame> game;
 | 
			
		||||
      ScenePhysicsManager *physics;
 | 
			
		||||
      StateEvent<float_t> eventSceneUpdate;
 | 
			
		||||
      StateEvent<float_t> eventSceneUnpausedUpdate;
 | 
			
		||||
@@ -42,7 +42,7 @@ namespace Dawn {
 | 
			
		||||
       * 
 | 
			
		||||
       * @param game Reference to the game that this scene belongs to.
 | 
			
		||||
       */
 | 
			
		||||
      Scene(DawnGame *game);
 | 
			
		||||
      Scene(std::weak_ptr<DawnGame> game);
 | 
			
		||||
 | 
			
		||||
      /**
 | 
			
		||||
       * Perform a one frame synchronous tick on the current scene. This may
 | 
			
		||||
@@ -58,7 +58,7 @@ namespace Dawn {
 | 
			
		||||
      template<class T>
 | 
			
		||||
      T * createSceneItemOfType() {
 | 
			
		||||
        sceneitemid_t id = this->nextId++;
 | 
			
		||||
        auto item = new T(this, id);
 | 
			
		||||
        auto item = new T(weak_from_this(), id);
 | 
			
		||||
        assertNotNull(item, "Scene::createSceneItemOfType: Failed to create SceneItem (Memory Filled?)");
 | 
			
		||||
        this->itemsNotInitialized[id] = item;
 | 
			
		||||
        return item;
 | 
			
		||||
@@ -137,18 +137,6 @@ namespace Dawn {
 | 
			
		||||
        }
 | 
			
		||||
        return components;
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      // Scene debugging functions
 | 
			
		||||
      #if DAWN_DEBUG_BUILD
 | 
			
		||||
        std::vector<struct SceneDebugLine> debugLines;
 | 
			
		||||
        void debugLine(struct SceneDebugLine line);
 | 
			
		||||
        void debugRay(struct SceneDebugRay ray);
 | 
			
		||||
        void debugCube(struct SceneDebugCube cube);
 | 
			
		||||
        void debugOrigin();
 | 
			
		||||
        void debugGrid();
 | 
			
		||||
        void debugHitboxes();
 | 
			
		||||
      #endif
 | 
			
		||||
 | 
			
		||||
      /**
 | 
			
		||||
       * Destroys a previously initialized Scene.
 | 
			
		||||
       */
 | 
			
		||||
 
 | 
			
		||||
@@ -9,12 +9,10 @@
 | 
			
		||||
 | 
			
		||||
using namespace Dawn;
 | 
			
		||||
 | 
			
		||||
SceneItem::SceneItem(Scene *scene, sceneitemid_t id)  :
 | 
			
		||||
SceneItem::SceneItem(std::weak_ptr<Scene> scene, sceneitemid_t id)  :
 | 
			
		||||
  transformLocal(1.0f),
 | 
			
		||||
  transformWorld(1.0f)
 | 
			
		||||
{
 | 
			
		||||
  assertNotNull(scene, "SceneItem::SceneItem: Scene cannot be null");
 | 
			
		||||
  
 | 
			
		||||
  this->id = id;
 | 
			
		||||
  this->scene = scene;
 | 
			
		||||
  this->updateLocalValuesFromLocalTransform();
 | 
			
		||||
@@ -89,7 +87,7 @@ void SceneItem::updateLocalTransformFromLocalValues() {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SceneItem::updateWorldTransformFromLocalTransform() {
 | 
			
		||||
  auto parent = this->getParent().lock();
 | 
			
		||||
  auto parent = this->getParent();
 | 
			
		||||
  if(parent != nullptr) {
 | 
			
		||||
    auto newWorld = parent->getWorldTransform();
 | 
			
		||||
    this->transformWorld = newWorld * transformLocal;
 | 
			
		||||
@@ -101,7 +99,7 @@ void SceneItem::updateWorldTransformFromLocalTransform() {
 | 
			
		||||
 | 
			
		||||
void SceneItem::updateLocalTransformFromWorldTransform() {
 | 
			
		||||
  glm::mat4 parentMat(1.0f);
 | 
			
		||||
  auto parent = this->getParent().lock();
 | 
			
		||||
  auto parent = this->getParent();
 | 
			
		||||
  if(parent != nullptr) parentMat = parent->getWorldTransform();
 | 
			
		||||
  this->transformLocal = parentMat / this->transformWorld;
 | 
			
		||||
  this->updateLocalValuesFromLocalTransform();
 | 
			
		||||
@@ -203,7 +201,7 @@ void SceneItem::setParent(std::shared_ptr<SceneItem> parent) {
 | 
			
		||||
    "SceneItem::setParent: Cannot set parent to self"
 | 
			
		||||
  );
 | 
			
		||||
  
 | 
			
		||||
  auto currentParent = this->getParent().lock();
 | 
			
		||||
  auto currentParent = this->getParent();
 | 
			
		||||
  if(currentParent == parent) return;
 | 
			
		||||
 | 
			
		||||
  if(currentParent != nullptr) {
 | 
			
		||||
@@ -226,14 +224,14 @@ void SceneItem::setParent(std::shared_ptr<SceneItem> parent) {
 | 
			
		||||
  this->eventTransformUpdated.invoke();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
std::weak_ptr<SceneItem> SceneItem::getParent() {
 | 
			
		||||
  return this->parent;
 | 
			
		||||
std::shared_ptr<SceneItem> SceneItem::getParent() {
 | 
			
		||||
  return this->parent.lock();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool_t SceneItem::isChildOf(std::shared_ptr<SceneItem> parent) {
 | 
			
		||||
  auto current = this->getParent();
 | 
			
		||||
  std::shared_ptr<SceneItem> currentLocked;
 | 
			
		||||
  while(currentLocked = current.lock()) {
 | 
			
		||||
  while(currentLocked = current) {
 | 
			
		||||
    if(currentLocked == parent) return true;
 | 
			
		||||
    current = currentLocked->getParent();
 | 
			
		||||
  }
 | 
			
		||||
 
 | 
			
		||||
@@ -41,7 +41,7 @@ namespace Dawn {
 | 
			
		||||
      void updateChildrenTransforms();
 | 
			
		||||
 | 
			
		||||
    public:
 | 
			
		||||
      Scene *scene;
 | 
			
		||||
      std::weak_ptr<Scene> scene;
 | 
			
		||||
      sceneitemid_t id;
 | 
			
		||||
      // I have no idea if I'm keeping this
 | 
			
		||||
      StateEvent<> eventTransformUpdated;
 | 
			
		||||
@@ -53,7 +53,7 @@ namespace Dawn {
 | 
			
		||||
       * @param scene Weak pointer to the Scene that this SceneItem belongs to.
 | 
			
		||||
       * @param id Scene Item ID that the Scene assigned this SceneItem.
 | 
			
		||||
       */
 | 
			
		||||
      SceneItem(Scene *scene, sceneitemid_t id);
 | 
			
		||||
      SceneItem(std::weak_ptr<Scene> scene, sceneitemid_t id);
 | 
			
		||||
      
 | 
			
		||||
      /**
 | 
			
		||||
       * Called by the Scene the frame after we were constructed so we can begin
 | 
			
		||||
@@ -310,7 +310,7 @@ namespace Dawn {
 | 
			
		||||
       * no parent for this transform.
 | 
			
		||||
       * @return Pointer to the parent transform, or nullptr.
 | 
			
		||||
       */
 | 
			
		||||
      std::weak_ptr<SceneItem> getParent();
 | 
			
		||||
      std::shared_ptr<SceneItem> getParent();
 | 
			
		||||
 | 
			
		||||
      /**
 | 
			
		||||
       * Returns true if this transform is a child of the given transform, this
 | 
			
		||||
 
 | 
			
		||||
@@ -24,16 +24,20 @@ std::vector<std::shared_ptr<SceneItemComponent>> SceneItemComponent::getDependen
 | 
			
		||||
  return {};
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
Scene * SceneItemComponent::getScene() {
 | 
			
		||||
  return this->item->scene;
 | 
			
		||||
std::shared_ptr<Scene> SceneItemComponent::getScene() {
 | 
			
		||||
  auto scene = this->item->scene.lock();
 | 
			
		||||
  assertNotNull(scene, "Scene cannot be null!");
 | 
			
		||||
  return scene;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
DawnGame * SceneItemComponent::getGame() {
 | 
			
		||||
  return this->item->scene->game;
 | 
			
		||||
std::shared_ptr<DawnGame> SceneItemComponent::getGame() {
 | 
			
		||||
  auto game = this->getScene()->game.lock();
 | 
			
		||||
  assertNotNull(game, "Game cannot be null!");
 | 
			
		||||
  return game;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
ScenePhysicsManager * SceneItemComponent::getPhysics() {
 | 
			
		||||
  return this->item->scene->physics;
 | 
			
		||||
  return this->getScene()->physics;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SceneItemComponent::onStart() {
 | 
			
		||||
 
 | 
			
		||||
@@ -43,13 +43,13 @@ namespace Dawn {
 | 
			
		||||
       * Shorthand to return the scene that this component's item belongs to.
 | 
			
		||||
       * @return The current scene.
 | 
			
		||||
       */
 | 
			
		||||
      Scene * getScene();
 | 
			
		||||
      std::shared_ptr<Scene> getScene();
 | 
			
		||||
 | 
			
		||||
      /**
 | 
			
		||||
       * Shorthand to return the game that this scene belongs to.
 | 
			
		||||
       * @return The current game.
 | 
			
		||||
       */
 | 
			
		||||
      DawnGame * getGame();
 | 
			
		||||
      std::shared_ptr<DawnGame> getGame();
 | 
			
		||||
 | 
			
		||||
      /**
 | 
			
		||||
       * Shorthand to return the physics manager that the scene this item 
 | 
			
		||||
 
 | 
			
		||||
@@ -20,5 +20,5 @@ void FPSLabelComponent::onStart() {
 | 
			
		||||
    std::string strTick = std::to_string((int32_t)(delta * 1000.0f));
 | 
			
		||||
    assertUnreachable("FPSLabelComponent::onStart: Not yet implemented");// Needs updating to new UI Label
 | 
			
		||||
    // label->text = strFps + "FPS (" + strTick + "ms)";
 | 
			
		||||
  }, this->item->scene->eventSceneUnpausedUpdate);
 | 
			
		||||
  }, getScene()->eventSceneUnpausedUpdate);
 | 
			
		||||
}
 | 
			
		||||
@@ -10,7 +10,7 @@ using namespace Dawn;
 | 
			
		||||
 | 
			
		||||
Camera::Camera(SceneItem *item) :
 | 
			
		||||
  SceneItemComponent(item),
 | 
			
		||||
  renderTarget(item->scene->game->renderManager->getBackBuffer()),
 | 
			
		||||
  renderTarget(item->scene.lock()->game.lock()->renderManager->getBackBuffer()),
 | 
			
		||||
  fov(0.785398f),// 45 degrees,
 | 
			
		||||
  type(CAMERA_TYPE_PERSPECTIVE),
 | 
			
		||||
  orthoLeft(-0.5f),
 | 
			
		||||
 
 | 
			
		||||
@@ -22,7 +22,7 @@ namespace Dawn {
 | 
			
		||||
      std::function<void()> evtResized;
 | 
			
		||||
 | 
			
		||||
    public:
 | 
			
		||||
      static std::shared_ptr<Camera> create(Scene *scene) {
 | 
			
		||||
      static std::shared_ptr<Camera> create(std::shared_ptr<Scene> scene) {
 | 
			
		||||
        auto item = scene->createSceneItem();
 | 
			
		||||
        auto cam = item->addComponent<Camera>();
 | 
			
		||||
        return cam;
 | 
			
		||||
 
 | 
			
		||||
@@ -22,7 +22,7 @@ UIComponent::UIComponent(SceneItem *item) :
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
std::shared_ptr<UIComponentDimensional> UIComponent::getParentDimensional() {
 | 
			
		||||
  auto parent = item->getParent().lock();
 | 
			
		||||
  auto parent = item->getParent();
 | 
			
		||||
  if(parent == nullptr) return nullptr;
 | 
			
		||||
  auto dimensional = parent->getComponent<UIComponentDimensional>();
 | 
			
		||||
  assertNotNull(dimensional, "UIComponent::getParentDimensional: Parent must have a UIComponentDimensional");
 | 
			
		||||
@@ -240,11 +240,11 @@ void UIComponent::calculateDimensions(
 | 
			
		||||
 | 
			
		||||
std::shared_ptr<UICanvas> UIComponent::getCanvas() {
 | 
			
		||||
  // TODO: Cache this on first hit.
 | 
			
		||||
  auto parent = item->getParent().lock();
 | 
			
		||||
  auto parent = item->getParent();
 | 
			
		||||
  while(parent) {
 | 
			
		||||
    auto canvas = parent->getComponent<UICanvas>();
 | 
			
		||||
    if(canvas != nullptr) return canvas;
 | 
			
		||||
    parent = parent->getParent().lock();
 | 
			
		||||
    parent = parent->getParent();
 | 
			
		||||
  }
 | 
			
		||||
  assertUnreachable("UIComponent::getCanvas: No canvas found");
 | 
			
		||||
  return nullptr;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user