Testing some event stuff
This commit is contained in:
		@@ -3,10 +3,10 @@
 | 
			
		||||
# This software is released under the MIT License.
 | 
			
		||||
# https://opensource.org/licenses/MIT
 | 
			
		||||
 | 
			
		||||
# target_sources(${DAWN_TARGET_NAME}
 | 
			
		||||
#   PRIVATE
 | 
			
		||||
#     Game.cpp
 | 
			
		||||
# )
 | 
			
		||||
target_sources(${DAWN_TARGET_NAME}
 | 
			
		||||
  PRIVATE
 | 
			
		||||
    SimpleComponent.cpp
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
# Subdirs
 | 
			
		||||
add_subdirectory(display)
 | 
			
		||||
							
								
								
									
										27
									
								
								src/dawn/component/SimpleComponent.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								src/dawn/component/SimpleComponent.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,27 @@
 | 
			
		||||
// Copyright (c) 2023 Dominic Masters
 | 
			
		||||
// 
 | 
			
		||||
// This software is released under the MIT License.
 | 
			
		||||
// https://opensource.org/licenses/MIT
 | 
			
		||||
 | 
			
		||||
#include "SimpleComponent.hpp"
 | 
			
		||||
 | 
			
		||||
using namespace Dawn;
 | 
			
		||||
 | 
			
		||||
void SimpleComponent::onInit() {
 | 
			
		||||
  this->initMethod(*this, events);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void SimpleComponent::onDispose() {
 | 
			
		||||
  for(auto &event : events) {
 | 
			
		||||
    event();
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
std::shared_ptr<SimpleComponent> Dawn::addSimpleComponent(
 | 
			
		||||
  std::shared_ptr<SceneItem> item,
 | 
			
		||||
  std::function<void(SceneComponent&, std::vector<std::function<void()>>&)> init
 | 
			
		||||
) {
 | 
			
		||||
  auto cmp = item->addComponent<SimpleComponent>();
 | 
			
		||||
  cmp->initMethod = init;
 | 
			
		||||
  return cmp;
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										31
									
								
								src/dawn/component/SimpleComponent.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										31
									
								
								src/dawn/component/SimpleComponent.hpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,31 @@
 | 
			
		||||
// Copyright (c) 2023 Dominic Masters
 | 
			
		||||
// 
 | 
			
		||||
// This software is released under the MIT License.
 | 
			
		||||
// https://opensource.org/licenses/MIT
 | 
			
		||||
 | 
			
		||||
#pragma once
 | 
			
		||||
#include "scene/Scene.hpp"
 | 
			
		||||
 | 
			
		||||
namespace Dawn {
 | 
			
		||||
  class SimpleComponent final : public SceneComponent {
 | 
			
		||||
    private:
 | 
			
		||||
      std::vector<std::function<void()>> events;
 | 
			
		||||
 | 
			
		||||
    public:
 | 
			
		||||
      std::function<void(
 | 
			
		||||
        SceneComponent&,
 | 
			
		||||
        std::vector<std::function<void()>>&
 | 
			
		||||
      )> initMethod;
 | 
			
		||||
 | 
			
		||||
      void onInit() override;
 | 
			
		||||
      void onDispose() override;
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  std::shared_ptr<SimpleComponent> addSimpleComponent(
 | 
			
		||||
    std::shared_ptr<SceneItem> item,
 | 
			
		||||
    std::function<void(
 | 
			
		||||
      SceneComponent&,
 | 
			
		||||
      std::vector<std::function<void()>>&
 | 
			
		||||
    )> init
 | 
			
		||||
  );
 | 
			
		||||
}
 | 
			
		||||
@@ -10,11 +10,11 @@
 | 
			
		||||
using namespace Dawn;
 | 
			
		||||
 | 
			
		||||
void Camera::onInit() {
 | 
			
		||||
  if(renderTarget == nullptr) {
 | 
			
		||||
    this->setRenderTarget(
 | 
			
		||||
      getGame()->renderHost.backBufferRenderTarget
 | 
			
		||||
    );
 | 
			
		||||
  }
 | 
			
		||||
  this->onResizeListener = this->getRenderTarget()->onResize.listen([&](
 | 
			
		||||
    float_t width, float_t height
 | 
			
		||||
  ) {
 | 
			
		||||
    this->onResize.emit(this->getRenderTarget(), width, height);
 | 
			
		||||
  });
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void Camera::onDispose() {
 | 
			
		||||
@@ -22,7 +22,8 @@ void Camera::onDispose() {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
std::shared_ptr<RenderTarget> Camera::getRenderTarget() {
 | 
			
		||||
  return this->renderTarget;
 | 
			
		||||
  if(this->renderTarget) return this->renderTarget;
 | 
			
		||||
  return getGame()->renderHost.getBackBufferRenderTarget();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
glm::mat4 Camera::getProjection() {
 | 
			
		||||
@@ -52,13 +53,15 @@ glm::mat4 Camera::getProjection() {
 | 
			
		||||
 | 
			
		||||
float_t Camera::getAspect() {
 | 
			
		||||
  auto rt = this->getRenderTarget();
 | 
			
		||||
  if(rt == nullptr) rt = getGame()->renderHost.getBackBufferRenderTarget();
 | 
			
		||||
  return rt->getWidth() / rt->getHeight();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void Camera::setRenderTarget(std::shared_ptr<RenderTarget> renderTarget) {
 | 
			
		||||
  if(this->renderTarget != nullptr) {
 | 
			
		||||
 | 
			
		||||
  }
 | 
			
		||||
  onResizeListener();
 | 
			
		||||
  this->renderTarget = renderTarget;
 | 
			
		||||
  this->onResizeListener = this->getRenderTarget()->onResize.listen([&](
 | 
			
		||||
    float_t width, float_t height
 | 
			
		||||
  ) {
 | 
			
		||||
    this->onResize.emit(this->getRenderTarget(), width, height);
 | 
			
		||||
  });
 | 
			
		||||
}
 | 
			
		||||
@@ -16,8 +16,10 @@ namespace Dawn {
 | 
			
		||||
  class Camera final : public SceneComponent {
 | 
			
		||||
    private:
 | 
			
		||||
      std::shared_ptr<RenderTarget> renderTarget;
 | 
			
		||||
      std::function<void()> onResizeListener;
 | 
			
		||||
 | 
			
		||||
    public:
 | 
			
		||||
      Event<std::shared_ptr<RenderTarget>, float_t, float_t> onResize;
 | 
			
		||||
      float_t clipNear = 0.01f;
 | 
			
		||||
      float_t clipFar = 1000.0f;
 | 
			
		||||
      enum CameraType type = CameraType::PERSPECTIVE;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user