From 87dbf88c7baccc7fb7dceff6a291ebc498c41fd7 Mon Sep 17 00:00:00 2001 From: wqking Date: Wed, 15 Feb 2017 17:44:54 +0800 Subject: [PATCH] Added basic camera, not completely finished yet. --- include/gincu/gcomponentcamera.h | 1 + include/gincu/gcomponentmanager.h | 33 +- include/gincu/gcomponentrender.h | 2 +- include/gincu/gcomponenttransform.h | 1 - include/gincu/gentityutil.h | 6 +- include/gincu/gscene.h | 8 +- include/gincu/gutil.h | 6 + readme.md | 2 +- src/gcomponentmanager.cpp | 459 ++++++++++++++++++++-------- src/gcomponenttouchhandler.cpp | 2 +- src/gcomponenttransform.cpp | 6 +- src/gentityutil.cpp | 6 +- src/gscene.cpp | 30 +- src/sfml/gcamera.cpp | 6 +- 14 files changed, 420 insertions(+), 148 deletions(-) diff --git a/include/gincu/gcomponentcamera.h b/include/gincu/gcomponentcamera.h index 4c1459d..86049b8 100644 --- a/include/gincu/gcomponentcamera.h +++ b/include/gincu/gcomponentcamera.h @@ -36,6 +36,7 @@ class GComponentCamera : public GComponent uint32_t getMask() const { return this->camera.getMask(); } bool belongs(const unsigned int cameraId) const { return this->camera.belongs(cameraId); } + const GCamera & getCamera() const { return this->camera; } private: void onEntityEvent(GComponent * component, const GEntityEventType eventType); diff --git a/include/gincu/gcomponentmanager.h b/include/gincu/gcomponentmanager.h index bf08c36..52b21d0 100644 --- a/include/gincu/gcomponentmanager.h +++ b/include/gincu/gcomponentmanager.h @@ -5,6 +5,7 @@ #include #include +#include namespace gincu { @@ -18,6 +19,35 @@ class GComponentManager { private: typedef std::vector ComponentListType; + + struct CameraInfo + { + CameraInfo(); + explicit CameraInfo(GComponentCamera * camera); + + void loadRootTransformList(const std::vector & componentList); + void loadTouchHandlerList(const std::vector & componentList); + + void zOrderChanged(GComponentTransform * transform); + void cameraIdChanged(GComponentTransform * transform, const unsigned int oldCameraId); + + void addTransform(GComponentTransform * component); + void removeTransform(GComponentTransform * component); + void addTouchHandler(GComponent * component); + void removeTouchHandler(GComponent * component); + + bool belongs(const GComponent * component); + + void render(); + void findTouchHandlers(const GPoint & position, std::vector * outputResult); + + GComponentCamera * camera; + std::vector rootTransformList; + bool needSortRootTransformList; + std::vector touchHandlerList; + }; + + typedef std::shared_ptr CameraInfoPointer; public: GComponentManager(); @@ -28,7 +58,7 @@ class GComponentManager void parentChanged(GComponentLocalTransform * localTransform); void zOrderChanged(GComponentTransform * transform); // this can be either a global or local transform - void cameraIdChanged(GComponentTransform * transform); + void cameraIdChanged(GComponentTransform * transform, const unsigned int oldCameraId); void cameraMaskChanged(GComponentCamera * camera); void updateAnimation(); @@ -51,6 +81,7 @@ class GComponentManager std::map componentListColdMap; mutable std::vector rootTransformList; mutable bool needSortRootTransformList; + mutable std::vector cameraInfoList; mutable bool needSortCameraList; }; diff --git a/include/gincu/gcomponentrender.h b/include/gincu/gcomponentrender.h index e767d5e..e8d7b20 100644 --- a/include/gincu/gcomponentrender.h +++ b/include/gincu/gcomponentrender.h @@ -102,7 +102,7 @@ class GComponentRenderCommon : public GComponentRender virtual void doDraw() override { GComponentTransform * transform = this->getEntity()->template getComponentByType(); if(transform->isVisible()) { - this->render.draw(computeRenderableTransform(transform, this), this->getRenderInfo()); + this->render.draw(computeRenderableMatrix(transform, this), this->getRenderInfo()); } } diff --git a/include/gincu/gcomponenttransform.h b/include/gincu/gcomponenttransform.h index ce66112..dc4df10 100644 --- a/include/gincu/gcomponenttransform.h +++ b/include/gincu/gcomponenttransform.h @@ -45,7 +45,6 @@ class GComponentTransform : public GComponent GComponentTransform * setZOrder(const int zOrder); const GTransform & getTransform() const { return this->transform; } - GTransform & getTransform() { return this->transform; } GComponentTransform * setTransform(const GTransform & transform) { this->transform = transform; return this; } private: diff --git a/include/gincu/gentityutil.h b/include/gincu/gentityutil.h index 4016854..1cc7b7a 100644 --- a/include/gincu/gentityutil.h +++ b/include/gincu/gentityutil.h @@ -19,7 +19,7 @@ void enumerateEntityChildren(GComponentLocalTransform * localTransform, const Ca } template -T * getComponentByTypeFromComponent(GComponent * component) +T * getComponentByTypeFromComponent(const GComponent * component) { if(component != nullptr) { GEntity * entity = component->getEntity(); @@ -35,8 +35,8 @@ class GComponentRender; class GEntity; class GComponentManager; -GMatrix44 computeRenderableTransform(GComponentTransform * componentTransform, const GSize & size); -GMatrix44 computeRenderableTransform(GComponentTransform * componentTransform, GComponentRender * render = nullptr); +GMatrix44 computeRenderableMatrix(GComponentTransform * componentTransform, const GSize & size); +GMatrix44 computeRenderableMatrix(GComponentTransform * componentTransform, GComponentRender * render = nullptr); GComponentManager * getComponentManagerFromEntity(const GEntity * entity); GComponentLocalTransform * getParentLocalTransform(const GEntity * entity); diff --git a/include/gincu/gscene.h b/include/gincu/gscene.h index a02409e..1cb0dae 100644 --- a/include/gincu/gscene.h +++ b/include/gincu/gscene.h @@ -2,7 +2,6 @@ #define GSCENE_H #include "gincu/ggeometry.h" -#include "gincu/gcomponentmanager.h" #include "cpgf/tween/gtweenlist.h" @@ -11,6 +10,7 @@ namespace gincu { +class GComponentManager; class GEntity; struct GEvent; @@ -39,13 +39,17 @@ class GScene cpgf::GTweenList * getTweenList() { return &this->tweenList; } +private: + void initializePrimaryCamera(); + private: virtual void doOnEnter(); virtual void doOnExit(); private: - GComponentManager componentManager; + std::unique_ptr componentManager; std::vector entityList; + GEntity * primaryCamera; GEntity * touchCapture; cpgf::GTweenList tweenList; }; diff --git a/include/gincu/gutil.h b/include/gincu/gutil.h index e444b22..190cf9d 100644 --- a/include/gincu/gutil.h +++ b/include/gincu/gutil.h @@ -34,6 +34,12 @@ void removeValueFromContainer(C & container, const V & value) container.erase(std::remove(container.begin(), container.end(), value), container.end()); } +template +void removeIfValueFromContainer(C & container, const V & predication) +{ + container.erase(std::remove_if(container.begin(), container.end(), predication), container.end()); +} + inline bool isEqual(const int a, const int b) { diff --git a/readme.md b/readme.md index 69b9162..e0850b8 100644 --- a/readme.md +++ b/readme.md @@ -17,7 +17,7 @@ Now there is a match-three game in the project. In the future we may add more ga ## Version -1.0.1 +0.0.1 ## Supported platform diff --git a/src/gcomponentmanager.cpp b/src/gcomponentmanager.cpp index 36e326b..b54fe15 100644 --- a/src/gcomponentmanager.cpp +++ b/src/gcomponentmanager.cpp @@ -6,107 +6,16 @@ #include "gincu/gcomponentanimation.h" #include "gincu/gcomponenttouchhandler.h" #include "gincu/grenderengine.h" +#include "gincu/gentityutil.h" #include "gincu/gutil.h" #include +#include #include "gincu/gcamera.h" // test namespace gincu { -GComponentManager::GComponentManager() - : - componentListHotArray(componentTypeId_PrimaryCount), - needSortRootTransformList(false), - needSortCameraList(false) -{ -} - -void GComponentManager::add(GComponent * component) -{ - this->doGetComponentList(component->getTypeId())->push_back(component); - - if(component->getTypeId() == componentTypeId_Transform) { - if(getParentLocalTransform(component->getEntity()) == nullptr) { - this->rootTransformList.push_back(static_cast(component)); - this->needSortRootTransformList = true; - } - } - if(component->getTypeId() == componentTypeId_Camera) { - this->needSortCameraList = true; - } -} - -void GComponentManager::remove(GComponent * component) -{ - ComponentListType * componentList = this->doGetComponentList(component->getTypeId()); - removeValueFromContainer(*componentList, component); - - if(component->getTypeId() == componentTypeId_Transform) { - if(getParentLocalTransform(component->getEntity()) == nullptr) { - removeValueFromContainer(this->rootTransformList, component); - } - } -} - -void GComponentManager::clear() -{ - this->componentListHotArray.clear(); - this->componentListColdMap.clear(); - this->rootTransformList.clear(); - this->needSortRootTransformList = false; -} - -void GComponentManager::parentChanged(GComponentLocalTransform * localTransform) -{ - GComponentTransform * transform = getComponentByTypeFromComponent(localTransform); - if(transform != nullptr) { - if(localTransform->getParent() == nullptr) { - this->rootTransformList.push_back(transform); - this->needSortRootTransformList = true; - - if(! this->needSortCameraList) { - if(getComponentByTypeFromComponent(localTransform) != nullptr) { - this->needSortCameraList = true; - } - } - } - else { - removeValueFromContainer(this->rootTransformList, transform); - } - } -} - -void GComponentManager::zOrderChanged(GComponentTransform * transform) -{ -} - -void GComponentManager::cameraIdChanged(GComponentTransform * transform) -{ -} - -void GComponentManager::cameraMaskChanged(GComponentCamera * camera) -{ -} - -void GComponentManager::updateAnimation() -{ - ComponentListType * componentList = this->doGetComponentList(GComponentAnimation::getComponentType()); - for(GComponent * component : *componentList) { - static_cast(component)->update(); - } -} - -void GComponentManager::updateLocalTransforms() -{ - ComponentListType * componentList = this->doGetComponentList(GComponentLocalTransform::getComponentType()); - for(GComponent * component : *componentList) { - if(component != nullptr && static_cast(component)->getParent() == nullptr) { - static_cast(component)->applyGlobal(); - } - } -} - namespace { void doRenderEntity(GEntity * entity); @@ -148,6 +57,8 @@ void doRenderEntity(GEntity * entity) void doSortTransformList(std::vector & transformList) { std::stable_sort(transformList.begin(), transformList.end(), [](const GComponentTransform * a, const GComponentTransform * b) { + return a->getZOrder() < b->getZOrder(); +/* int zOrderA, zOrderB; GComponentLocalTransform * localTransform; @@ -168,39 +79,15 @@ void doSortTransformList(std::vector & transformList) } return zOrderA < zOrderB; - }); -} - -} //unnamed namespace - -void GComponentManager::render() -{ -/* - const float w = 900, h = 600; - GCamera camera; - GTransform transform; - transform.setProjectionMode(true); - camera.setViewport({ 0, 0, w, h }); - transform.setOrigin({ w, h }); - GComponentAnchor anchor(GRenderAnchor::leftTop); - transform.setPosition({ w / 2, h / 2 }); -// anchor.setFlipX(true); - anchor.apply(transform, { w, h }); - camera.apply(transform); - GRenderEngine::getInstance()->switchCamera(camera); */ - - if(this->needSortRootTransformList) { - this->needSortRootTransformList = false; - doSortTransformList(this->rootTransformList); - } - - for(GComponentTransform * transform : this->rootTransformList) { - doRenderEntity(transform->getEntity()); - } + }); } -void GComponentManager::findTouchHandlers(const GPoint & position, std::vector * outputResult) +void doFindTouchHandlers( + const std::vector & touchHandlerList, + const GPoint & position, + std::vector * outputResult + ) { struct Item { GComponentTouchHandler * touchHandler; @@ -209,8 +96,7 @@ void GComponentManager::findTouchHandlers(const GPoint & position, std::vector itemList; - ComponentListType * componentList = this->doGetComponentList(GComponentTouchHandler::getComponentType()); - for(GComponent * component : *componentList) { + for(GComponent * component : touchHandlerList) { if(static_cast(component)->canHandle(position)) { itemList.push_back({ static_cast(component), std::vector() }); } @@ -280,6 +166,329 @@ void GComponentManager::findTouchHandlers(const GPoint & position, std::vector & componentList) +{ + this->rootTransformList.clear(); + + std::copy_if( + componentList.begin(), + componentList.end(), + std::back_inserter(this->rootTransformList), + [=](const GComponentTransform * component) { + return this->camera->belongs(component->getCameraId()); + } + ); + this->needSortRootTransformList = true; +} + +void GComponentManager::CameraInfo::loadTouchHandlerList(const std::vector & componentList) +{ + this->touchHandlerList.clear(); + + std::copy_if( + componentList.begin(), + componentList.end(), + std::back_inserter(this->touchHandlerList), + [=](const GComponent * component) { + return this->belongs(component); + } + ); +} + +void GComponentManager::CameraInfo::zOrderChanged(GComponentTransform * transform) +{ + if(this->belongs(transform)) { + this->needSortRootTransformList = true; + } +} + +void GComponentManager::CameraInfo::cameraIdChanged(GComponentTransform * transform, const unsigned int oldCameraId) +{ + if(this->camera->belongs(oldCameraId)) { + if(this->camera->belongs(transform->getCameraId())) { + return; + } + + this->removeTransform(transform); + } + else { + if(this->camera->belongs(transform->getCameraId())) { + this->addTransform(transform); + } + } +} + +void GComponentManager::CameraInfo::addTransform(GComponentTransform * component) +{ + if(this->camera->belongs(component->getCameraId())) { + this->rootTransformList.push_back(component); + this->needSortRootTransformList = true; + } +} + +void GComponentManager::CameraInfo::removeTransform(GComponentTransform * component) +{ + if(this->camera->belongs(component->getCameraId())) { + removeValueFromContainer(this->rootTransformList, component); + } +} + +void GComponentManager::CameraInfo::addTouchHandler(GComponent * component) +{ + if(this->belongs(component)) { + this->touchHandlerList.push_back(component); + } +} + +void GComponentManager::CameraInfo::removeTouchHandler(GComponent * component) +{ + if(this->belongs(component)) { + removeValueFromContainer(this->touchHandlerList, component); + } +} + +bool GComponentManager::CameraInfo::belongs(const GComponent * component) +{ + GComponentTransform * transform = getComponentByTypeFromComponent(component); + if(transform != nullptr) { + return this->camera->belongs(transform->getCameraId()); + } + else { + return false; + } +} + +void GComponentManager::CameraInfo::render() +{ + if(this->needSortRootTransformList) { + this->needSortRootTransformList = false; + doSortTransformList(this->rootTransformList); + } + + GCamera c = this->camera->getCamera(); + GComponentTransform * transform = getComponentByTypeFromComponent(this->camera); + if(transform != nullptr) { + c.apply(transform->getTransform()); + } + GRenderEngine::getInstance()->switchCamera(c); + + for(GComponentTransform * transform : this->rootTransformList) { + doRenderEntity(transform->getEntity()); + } +} + +void GComponentManager::CameraInfo::findTouchHandlers(const GPoint & position, std::vector * outputResult) +{ + doFindTouchHandlers(this->touchHandlerList, position, outputResult); +} + + +GComponentManager::GComponentManager() + : + componentListHotArray(componentTypeId_PrimaryCount), + needSortRootTransformList(false), + needSortCameraList(false) +{ +} + +void GComponentManager::add(GComponent * component) +{ + this->doGetComponentList(component->getTypeId())->push_back(component); + + switch(component->getTypeId()) { + case componentTypeId_Transform: + if(getParentLocalTransform(component->getEntity()) == nullptr) { + this->rootTransformList.push_back(static_cast(component)); + this->needSortRootTransformList = true; + + for(CameraInfoPointer & cameraInfo : this->cameraInfoList) { + cameraInfo->addTransform(static_cast(component)); + } + } + break; + + case componentTypeId_TouchHandler: + for(CameraInfoPointer & cameraInfo : this->cameraInfoList) { + cameraInfo->addTouchHandler(component); + } + break; + + case componentTypeId_Camera: + this->needSortCameraList = true; + GComponentCamera * camera = static_cast(component); + this->cameraInfoList.push_back(std::make_shared(camera)); + this->cameraInfoList.back()->loadRootTransformList(this->rootTransformList); + this->cameraInfoList.back()->loadTouchHandlerList(*this->doGetComponentList(GComponentTouchHandler::getComponentType())); + break; + } +} + +void GComponentManager::remove(GComponent * component) +{ + ComponentListType * componentList = this->doGetComponentList(component->getTypeId()); + removeValueFromContainer(*componentList, component); + + switch(component->getTypeId()) { + case componentTypeId_Transform: + if(getParentLocalTransform(component->getEntity()) == nullptr) { + removeValueFromContainer(this->rootTransformList, component); + + for(CameraInfoPointer & cameraInfo : this->cameraInfoList) { + cameraInfo->removeTransform(static_cast(component)); + } + } + break; + + case componentTypeId_TouchHandler: + for(CameraInfoPointer & cameraInfo : this->cameraInfoList) { + cameraInfo->removeTouchHandler(component); + } + break; + + case componentTypeId_Camera: + removeIfValueFromContainer(this->cameraInfoList, [=](const CameraInfoPointer & a) { + return a->camera == component; + }); + break; + } +} + +void GComponentManager::clear() +{ + this->componentListHotArray.clear(); + this->componentListColdMap.clear(); + this->rootTransformList.clear(); + this->needSortRootTransformList = false; +} + +void GComponentManager::parentChanged(GComponentLocalTransform * localTransform) +{ + GComponentTransform * transform = getComponentByTypeFromComponent(localTransform); + if(transform != nullptr) { + if(localTransform->getParent() == nullptr) { + this->rootTransformList.push_back(transform); + this->needSortRootTransformList = true; + + if(! this->needSortCameraList) { + if(getComponentByTypeFromComponent(localTransform) != nullptr) { + this->needSortCameraList = true; + } + } + } + else { + removeValueFromContainer(this->rootTransformList, transform); + } + } +} + +void GComponentManager::zOrderChanged(GComponentTransform * transform) +{ + if(transform->getTypeId() == componentTypeId_Transform + && getParentLocalTransform(transform->getEntity()) == nullptr) { + for(CameraInfoPointer & cameraInfo : this->cameraInfoList) { + cameraInfo->zOrderChanged(transform); + } + } +} + +void GComponentManager::cameraIdChanged(GComponentTransform * transform, const unsigned int oldCameraId) +{ + for(CameraInfoPointer & cameraInfo : this->cameraInfoList) { + cameraInfo->cameraIdChanged(transform, oldCameraId); + } +} + +void GComponentManager::cameraMaskChanged(GComponentCamera * camera) +{ + auto it = std::find_if(this->cameraInfoList.begin(), this->cameraInfoList.end(), + [=](const CameraInfoPointer & info) { return info->camera == camera; } + ); + if(it != this->cameraInfoList.end()) { + (*it)->loadRootTransformList(this->rootTransformList); + (*it)->loadTouchHandlerList(*this->doGetComponentList(GComponentTouchHandler::getComponentType())); + } +} + +void GComponentManager::updateAnimation() +{ + ComponentListType * componentList = this->doGetComponentList(GComponentAnimation::getComponentType()); + for(GComponent * component : *componentList) { + static_cast(component)->update(); + } +} + +void GComponentManager::updateLocalTransforms() +{ + ComponentListType * componentList = this->doGetComponentList(GComponentLocalTransform::getComponentType()); + for(GComponent * component : *componentList) { + if(component != nullptr && static_cast(component)->getParent() == nullptr) { + static_cast(component)->applyGlobal(); + } + } +} + +void GComponentManager::render() +{ + if(this->needSortCameraList) { + this->needSortCameraList = false; + // TODO + } + for(CameraInfoPointer & cameraInfo : this->cameraInfoList) { + cameraInfo->render(); + } + return; + +/* + const float w = 900, h = 600; + GCamera camera; + GTransform transform; + transform.setProjectionMode(true); + camera.setViewport({ 0, 0, w, h }); + transform.setOrigin({ w, h }); + GComponentAnchor anchor(GRenderAnchor::leftTop); + transform.setPosition({ w / 2, h / 2 }); +// anchor.setFlipX(true); + anchor.apply(transform, { w, h }); + camera.apply(transform); + GRenderEngine::getInstance()->switchCamera(camera); +*/ + + if(this->needSortRootTransformList) { + this->needSortRootTransformList = false; + doSortTransformList(this->rootTransformList); + } + + for(GComponentTransform * transform : this->rootTransformList) { + doRenderEntity(transform->getEntity()); + } +} + +void GComponentManager::findTouchHandlers(const GPoint & position, std::vector * outputResult) +{ + for(CameraInfoPointer & cameraInfo : this->cameraInfoList) { + cameraInfo->findTouchHandlers(position, outputResult); + } + return; + + doFindTouchHandlers(*this->doGetComponentList(GComponentTouchHandler::getComponentType()), position, outputResult); +} + GComponentManager::ComponentListType * GComponentManager::doGetComponentList(const unsigned int typeId) { if(typeId < this->componentListHotArray.size()) { diff --git a/src/gcomponenttouchhandler.cpp b/src/gcomponenttouchhandler.cpp index 29379a3..0b69285 100644 --- a/src/gcomponenttouchhandler.cpp +++ b/src/gcomponenttouchhandler.cpp @@ -52,7 +52,7 @@ bool GComponentRendererTouchHandler::doCanHandle(const GPoint & point) const return false; } - const GMatrix44 matrix = computeRenderableTransform(transform); + const GMatrix44 matrix = computeRenderableMatrix(transform); const GSize size = getEntity()->getComponentByType()->getSize(); auto normalizedPoint = transformPoint(inverseMatrix(matrix), { point.x, point.y }); diff --git a/src/gcomponenttransform.cpp b/src/gcomponenttransform.cpp index 5e84389..2f11013 100644 --- a/src/gcomponenttransform.cpp +++ b/src/gcomponenttransform.cpp @@ -30,11 +30,13 @@ GComponentTransform::~GComponentTransform() GComponentTransform * GComponentTransform::setCameraId(const unsigned int cameraId) { if(this->cameraId != cameraId) { + const unsigned int oldCameraId = this->cameraId; + this->cameraId = cameraId; GComponentManager * componentManager = getComponentManagerFromEntity(this->getEntity()); if(componentManager != nullptr) { - componentManager->cameraIdChanged(this); + componentManager->cameraIdChanged(this, oldCameraId); } } @@ -148,7 +150,7 @@ void GComponentLocalTransform::applyGlobal() } else { globalTransform->setVisible(this->isVisible()); - + globalTransform->setZOrder(this->getZOrder()); globalTransform->setTransform(this->getTransform()); } } diff --git a/src/gentityutil.cpp b/src/gentityutil.cpp index 2fade6e..be6c33d 100644 --- a/src/gentityutil.cpp +++ b/src/gentityutil.cpp @@ -5,7 +5,7 @@ namespace gincu { -GMatrix44 computeRenderableTransform(GComponentTransform * componentTransform, const GSize & size) +GMatrix44 computeRenderableMatrix(GComponentTransform * componentTransform, const GSize & size) { GComponentAnchor * anchor = componentTransform->getEntity()->template getComponentByType(); if(anchor != nullptr) { @@ -18,12 +18,12 @@ GMatrix44 computeRenderableTransform(GComponentTransform * componentTransform, c } } -GMatrix44 computeRenderableTransform(GComponentTransform * componentTransform, GComponentRender * render) +GMatrix44 computeRenderableMatrix(GComponentTransform * componentTransform, GComponentRender * render) { if(render == nullptr) { render = componentTransform->getEntity()->getComponentByType(); } - return computeRenderableTransform(componentTransform, render->getSize()); + return computeRenderableMatrix(componentTransform, render->getSize()); } GComponentManager * getComponentManagerFromEntity(const GEntity * entity) diff --git a/src/gscene.cpp b/src/gscene.cpp index 0c7d475..605d76b 100644 --- a/src/gscene.cpp +++ b/src/gscene.cpp @@ -1,14 +1,20 @@ #include "gincu/gscene.h" #include "gincu/gentity.h" #include "gincu/gcomponenttouchhandler.h" +#include "gincu/gcomponenttransform.h" +#include "gincu/gcomponentcamera.h" #include "gincu/gapplication.h" #include "gincu/gscenemanager.h" +#include "gincu/gcomponentmanager.h" #include "gincu/gevent.h" namespace gincu { GScene::GScene() - : touchCapture(nullptr) + : + componentManager(new GComponentManager()), + primaryCamera(nullptr), + touchCapture(nullptr) { } @@ -16,8 +22,22 @@ GScene::~GScene() { } +void GScene::initializePrimaryCamera() +{ + if(this->primaryCamera != nullptr) { + this->removeEntity(this->primaryCamera); + } + + this->primaryCamera = new GEntity(); + this->primaryCamera->addComponent(createComponent()); + this->primaryCamera->addComponent(createComponent()); + this->addEntity(this->primaryCamera); +} + void GScene::onEnter() { + this->initializePrimaryCamera(); + this->doOnEnter(); } @@ -41,7 +61,7 @@ void GScene::renderScene() { this->tweenList.tick((cpgf::GTweenNumber)GApplication::getInstance()->getRenderMilliseconds()); - this->componentManager.updateDuringRender(); + this->componentManager->updateDuringRender(); } void GScene::setTouchCapture(GEntity * touchCapture) @@ -61,7 +81,7 @@ void GScene::handleTouchEvent(const GEvent & touchEvent) { std::vector handlerList; - this->componentManager.findTouchHandlers(touchEvent.touch.position, &handlerList); + this->componentManager->findTouchHandlers(touchEvent.touch.position, &handlerList); GEvent tempEvent = touchEvent; @@ -96,7 +116,7 @@ GEntity * GScene::addEntity(GEntity * entity) { if(entity != nullptr) { this->entityList.push_back(EntityPointer(entity)); - entity->setComponentManager(&this->componentManager); + entity->setComponentManager(this->componentManager.get()); } return entity; @@ -118,7 +138,7 @@ void GScene::removeEntity(GEntity * entity) void GScene::removeAllEntities() { - this->componentManager.clear(); + this->componentManager->clear(); this->entityList.clear(); this->touchCapture = nullptr; this->tweenList.clear(); diff --git a/src/sfml/gcamera.cpp b/src/sfml/gcamera.cpp index 4302c7f..5829981 100644 --- a/src/sfml/gcamera.cpp +++ b/src/sfml/gcamera.cpp @@ -24,9 +24,9 @@ GCamera::GCamera() void GCamera::apply(const GTransform & transform) { transform.setProjectionMode(true); - const GMatrix44 & sfmlTransform = transform.getMatrix(); - GINCU_ACCESS_HACK(this->data->view, sfmlView_m_transform) = matrixToSfml(sfmlTransform); - GINCU_ACCESS_HACK(this->data->view, sfmlView_m_inverseTransform) = matrixToSfml(inverseMatrix(sfmlTransform)); + const GMatrix44 & matrix = transform.getMatrix(); + GINCU_ACCESS_HACK(this->data->view, sfmlView_m_transform) = matrixToSfml(matrix); + GINCU_ACCESS_HACK(this->data->view, sfmlView_m_inverseTransform) = matrixToSfml(inverseMatrix(matrix)); GINCU_ACCESS_HACK(this->data->view, sfmlView_m_transformUpdated) = true; GINCU_ACCESS_HACK(this->data->view, sfmlView_m_invTransformUpdated) = true;