From 271997905babc1ec84ea99542fa5a6d876a02a06 Mon Sep 17 00:00:00 2001 From: Kay Gonschior Date: Fri, 9 Oct 2015 13:29:17 +0200 Subject: [PATCH] changes --- kgEngine-2.0/TileMapGame/Camera.cpp | 29 +++++++++++++++---- kgEngine-2.0/TileMapGame/Camera.h | 10 +++++-- kgEngine-2.0/TileMapGame/GameController.cpp | 23 ++++++++------- kgEngine-2.0/TileMapGame/GameController.h | 3 ++ kgEngine-2.0/TileMapGame/SpriteBatch.cpp | 3 +- .../TileMapGame/TileMapGame.vcxproj.filters | 4 --- kgEngine-2.0/TileMapGame/Transformation.cpp | 24 +++++++-------- kgEngine-2.0/TileMapGame/Transformation.h | 3 +- 8 files changed, 61 insertions(+), 38 deletions(-) diff --git a/kgEngine-2.0/TileMapGame/Camera.cpp b/kgEngine-2.0/TileMapGame/Camera.cpp index 2c8f64e9..fe7742a3 100644 --- a/kgEngine-2.0/TileMapGame/Camera.cpp +++ b/kgEngine-2.0/TileMapGame/Camera.cpp @@ -14,8 +14,8 @@ namespace kg { r_transformation = thisEntity.getComponent(); - m_connectToSignal( r_transformation->s_positionChanged, &Camera::onPositionChanged ); - m_connectToSignal( r_transformation->s_sizeChanged, &Camera::onSizeChanged ); + m_connectToSignal( r_transformation->s_positionChanged, &Camera::m_onPositionChanged ); + m_connectToSignal( r_transformation->s_sizeChanged, &Camera::m_onSizeChanged ); } void Camera::update( Engine& engine, World& world, ComponentManager& thisEntity, const sf::Time& frameTime ) @@ -43,17 +43,22 @@ namespace kg return ( int )id::ComponentPluginId::CAMERA; } - void Camera::onPositionChanged( const sf::Vector2i& newPosition ) + void Camera::m_onPositionChanged( const sf::Vector2i& newPosition ) { m_viewMutex.lock(); m_view.setCenter( sf::Vector2f( newPosition ) ); m_viewMutex.unlock(); } - void Camera::onSizeChanged( const sf::Vector2i& newSize ) + void Camera::m_onSizeChanged( const sf::Vector2i& newSize ) + { + m_setViewSize( newSize, m_zoomFactor ); + } + + void Camera::m_setViewSize( const sf::Vector2i& size, const float& zoomFactor ) { m_viewMutex.lock(); - m_view.setSize( sf::Vector2f( newSize ) ); + m_view.setSize( (( double )size.x)*zoomFactor, (( double )size.y)*zoomFactor ); m_viewMutex.unlock(); } @@ -72,6 +77,18 @@ namespace kg return retVal; } + void Camera::setZoomFactor( const float& zoomFactor ) + { + m_zoomFactor = zoomFactor; + auto size = r_transformation->getSize(); + m_setViewSize( size, m_zoomFactor ); + } + + const float& Camera::getZoomFactor() const + { + return m_zoomFactor; + } + std::shared_ptr Camera::EMPLACE_TO_WORLD( Engine& engine, World& world, boost::mutex& drawDistanceMutex, unsigned int* drawDistancePointer ) { auto camera = world.createNewTemporaryEntity( engine, world ); @@ -110,7 +127,7 @@ namespace kg auto thisPosition = r_transformation->getPosition(); auto distanceVec = sf::Vector2i( spritePosition.x - thisPosition.x, spritePosition.y - thisPosition.y ); - if(length(distanceVec)<=*r_drawDistance ) + if( length( distanceVec ) <= *r_drawDistance ) get<2>( el )->drawToSpriteBatch( m_spriteBatch ); } diff --git a/kgEngine-2.0/TileMapGame/Camera.h b/kgEngine-2.0/TileMapGame/Camera.h index 6975a443..086bd2db 100644 --- a/kgEngine-2.0/TileMapGame/Camera.h +++ b/kgEngine-2.0/TileMapGame/Camera.h @@ -18,11 +18,14 @@ namespace kg sf::View m_view; sf::Vector2u m_finalSize; - void onPositionChanged( const sf::Vector2i& newPosition ); - void onSizeChanged( const sf::Vector2i& newSize ); + void m_onPositionChanged( const sf::Vector2i& newPosition ); + void m_onSizeChanged( const sf::Vector2i& newSize ); + void m_setViewSize( const sf::Vector2i& size, const float& zoomFactor ); batch::SpriteBatch m_spriteBatch; + float m_zoomFactor = 1; + public: virtual void preInit( Engine& engine, const std::map& blueprintValues )override; @@ -45,6 +48,9 @@ namespace kg void setViewport( const sf::FloatRect& viewport ); sf::FloatRect getViewport()const; + void setZoomFactor( const float& zoomFactor ); + const float& getZoomFactor()const; + void drawSpritesToRenderWindow( sf::RenderWindow& renderWindow, const std::vector, Graphics*>>& toDrawSorted ); diff --git a/kgEngine-2.0/TileMapGame/GameController.cpp b/kgEngine-2.0/TileMapGame/GameController.cpp index 246ffade..c325a91d 100644 --- a/kgEngine-2.0/TileMapGame/GameController.cpp +++ b/kgEngine-2.0/TileMapGame/GameController.cpp @@ -69,6 +69,13 @@ namespace kg return type_hash; } + std::shared_ptr GameController::m_getValidCamera() + { + if( m_camera.expired() ) + m_camera = r_graphicsSystem->getCamera( 0 ); + return m_camera.lock(); + } + void GameController::saveOpenSavegame( Engine& engine, World& world, SaveManager& saveManager ) { world.getSystem()->saveAllLoadedChunks( engine, world, saveManager ); @@ -77,9 +84,7 @@ namespace kg void GameController::movePlayer( sf::Vector2i distance ) { - if( m_camera.expired() ) - m_camera = r_graphicsSystem->getCamera( 0 ); - auto camera = m_camera.lock(); + auto camera = m_getValidCamera(); camera->getComponent()->move( distance ); } @@ -243,28 +248,24 @@ namespace kg void GameController::zoomIn() { - if( m_camera.expired() ) - m_camera = r_graphicsSystem->getCamera( 0 ); - auto camera = m_camera.lock(); + auto camera = m_getValidCamera(); m_cameraZoomFactor -= 0.01*lastFrameTimeInMilliseconds; if( m_cameraZoomFactor < 0 ) m_cameraZoomFactor = 0; - camera->getComponent()->setSize( Vector2i( (double)1280.0 * m_cameraZoomFactor, (double)720.0 * m_cameraZoomFactor ) ); + camera->getComponent()->setZoomFactor( m_cameraZoomFactor ); } void GameController::zoomOut() { - if( m_camera.expired() ) - m_camera = r_graphicsSystem->getCamera( 0 ); - auto camera = m_camera.lock(); + auto camera = m_getValidCamera(); m_cameraZoomFactor += 0.01*lastFrameTimeInMilliseconds; if( m_cameraZoomFactor < 0 ) m_cameraZoomFactor = 0; - camera->getComponent()->setSize( Vector2i( (double)1280.0 * m_cameraZoomFactor, (double)720.0 * m_cameraZoomFactor ) ); + camera->getComponent()->setZoomFactor( m_cameraZoomFactor ); } } diff --git a/kgEngine-2.0/TileMapGame/GameController.h b/kgEngine-2.0/TileMapGame/GameController.h index f261b4f1..ed9e6ed3 100644 --- a/kgEngine-2.0/TileMapGame/GameController.h +++ b/kgEngine-2.0/TileMapGame/GameController.h @@ -15,6 +15,7 @@ namespace kg std::weak_ptr m_camera; float m_cameraZoomFactor = 1; + std::shared_ptr m_getValidCamera(); sf::Int32 lastFrameTimeInMilliseconds = 0; @@ -67,5 +68,7 @@ namespace kg void zoomIn(); void zoomOut(); + + }; } diff --git a/kgEngine-2.0/TileMapGame/SpriteBatch.cpp b/kgEngine-2.0/TileMapGame/SpriteBatch.cpp index 7b386a0b..c7512d1d 100644 --- a/kgEngine-2.0/TileMapGame/SpriteBatch.cpp +++ b/kgEngine-2.0/TileMapGame/SpriteBatch.cpp @@ -111,7 +111,7 @@ namespace kg glBufferData( GL_ARRAY_BUFFER, sizeof( Vertex )*MaxCapacity, NULL, GL_STREAM_DRAW ); m_bufferPtr = ( Vertex* )glMapBuffer( GL_ARRAY_BUFFER, GL_WRITE_ONLY ); - m_isBufferBound = true; + m_isBufferBound = false; m_isVBOinit = true; } @@ -180,6 +180,7 @@ namespace kg if( !m_isBufferBound ) { glBindBuffer( GL_ARRAY_BUFFER, m_vbo ); + glBufferData( GL_ARRAY_BUFFER, sizeof( Vertex )*MaxCapacity, NULL, GL_STREAM_DRAW ); m_bufferPtr = ( Vertex* )glMapBuffer( GL_ARRAY_BUFFER, GL_WRITE_ONLY ); m_isBufferBound = true; } diff --git a/kgEngine-2.0/TileMapGame/TileMapGame.vcxproj.filters b/kgEngine-2.0/TileMapGame/TileMapGame.vcxproj.filters index 9c9ecb8c..1721ff2e 100644 --- a/kgEngine-2.0/TileMapGame/TileMapGame.vcxproj.filters +++ b/kgEngine-2.0/TileMapGame/TileMapGame.vcxproj.filters @@ -9,10 +9,6 @@ {93995380-89BD-4b04-88EB-625FBE52EBFB} h;hh;hpp;hxx;hm;inl;inc;xsd - - {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} - rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms - {8022208d-41e3-46ec-946d-bbefa46221fd} diff --git a/kgEngine-2.0/TileMapGame/Transformation.cpp b/kgEngine-2.0/TileMapGame/Transformation.cpp index 0d33bfec..ff82048b 100644 --- a/kgEngine-2.0/TileMapGame/Transformation.cpp +++ b/kgEngine-2.0/TileMapGame/Transformation.cpp @@ -58,31 +58,31 @@ namespace kg sf::FloatRect Transformation::getGlobalBounds() const { - m_globalBoundsMutex.lock(); + m_mutex.lock(); auto retVal = m_globalBounds; - m_globalBoundsMutex.unlock(); + m_mutex.unlock(); return retVal; } bool Transformation::intersects( const sf::FloatRect& rect )const { - m_globalBoundsMutex.lock(); + m_mutex.lock(); bool retVal = m_globalBounds.intersects( rect ); - m_globalBoundsMutex.unlock(); + m_mutex.unlock(); return retVal; } sf::Vector3i Transformation::getXYZValues() const { - m_globalBoundsMutex.lock(); + m_mutex.lock(); auto retVal = Vector3i( m_globalBounds.left, m_globalBounds.top + m_globalBounds.height,//feet position m_zValue ); - m_globalBoundsMutex.unlock(); + m_mutex.unlock(); return retVal; } @@ -186,7 +186,7 @@ namespace kg void Transformation::recalculateGlobalBounds() { - m_globalBoundsMutex.lock(); + m_mutex.lock(); RectangleShape shape; @@ -197,27 +197,27 @@ namespace kg m_globalBounds = shape.getGlobalBounds(); - m_globalBoundsMutex.unlock(); + m_mutex.unlock(); } int Transformation::getZValue() const { - m_zValueMutex.lock(); + m_mutex.lock(); auto retVal = m_zValue; - m_zValueMutex.unlock(); + m_mutex.unlock(); return retVal; } void Transformation::setZValue( int zValue ) { - m_zValueMutex.lock(); + m_mutex.lock(); m_zValue = zValue; - m_zValueMutex.unlock(); + m_mutex.unlock(); } const std::string Transformation::BLUEPRINT_ZVALUE = "zValue"; diff --git a/kgEngine-2.0/TileMapGame/Transformation.h b/kgEngine-2.0/TileMapGame/Transformation.h index 2ce94d46..a9597c0c 100644 --- a/kgEngine-2.0/TileMapGame/Transformation.h +++ b/kgEngine-2.0/TileMapGame/Transformation.h @@ -5,8 +5,7 @@ namespace kg { class Transformation : public Component, public CallbackReciever { - mutable boost::mutex m_globalBoundsMutex; - mutable boost::mutex m_zValueMutex; + mutable boost::mutex m_mutex; sf::FloatRect m_globalBounds;