Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxomann committed Oct 9, 2015
1 parent d2cbab1 commit 2719979
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 38 deletions.
29 changes: 23 additions & 6 deletions kgEngine-2.0/TileMapGame/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ namespace kg
{
r_transformation = thisEntity.getComponent<Transformation>();

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 )
Expand Down Expand Up @@ -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();
}

Expand All @@ -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<Entity> Camera::EMPLACE_TO_WORLD( Engine& engine, World& world, boost::mutex& drawDistanceMutex, unsigned int* drawDistancePointer )
{
auto camera = world.createNewTemporaryEntity<Transformation, Camera>( engine, world );
Expand Down Expand Up @@ -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 );
}

Expand Down
10 changes: 8 additions & 2 deletions kgEngine-2.0/TileMapGame/Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<blueprint::ComponentValue::Name, const blueprint::ComponentValue*>& blueprintValues )override;

Expand All @@ -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<std::tuple<sf::Vector3i, std::shared_ptr<Entity>, Graphics*>>& toDrawSorted );

Expand Down
23 changes: 12 additions & 11 deletions kgEngine-2.0/TileMapGame/GameController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ namespace kg
return type_hash;
}

std::shared_ptr<Entity> 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<ChunkSystem>()->saveAllLoadedChunks( engine, world, saveManager );
Expand All @@ -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<Transformation>()->move( distance );
}
Expand Down Expand Up @@ -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<Transformation>()->setSize( Vector2i( (double)1280.0 * m_cameraZoomFactor, (double)720.0 * m_cameraZoomFactor ) );
camera->getComponent<Camera>()->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<Transformation>()->setSize( Vector2i( (double)1280.0 * m_cameraZoomFactor, (double)720.0 * m_cameraZoomFactor ) );
camera->getComponent<Camera>()->setZoomFactor( m_cameraZoomFactor );
}

}
3 changes: 3 additions & 0 deletions kgEngine-2.0/TileMapGame/GameController.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace kg

std::weak_ptr<Entity> m_camera;
float m_cameraZoomFactor = 1;
std::shared_ptr<Entity> m_getValidCamera();

sf::Int32 lastFrameTimeInMilliseconds = 0;

Expand Down Expand Up @@ -67,5 +68,7 @@ namespace kg

void zoomIn();
void zoomOut();


};
}
3 changes: 2 additions & 1 deletion kgEngine-2.0/TileMapGame/SpriteBatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
Expand Down
4 changes: 0 additions & 4 deletions kgEngine-2.0/TileMapGame/TileMapGame.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions>
</Filter>
<Filter Include="Ressourcendateien">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
<Filter Include="Headerdateien\Systems">
<UniqueIdentifier>{8022208d-41e3-46ec-946d-bbefa46221fd}</UniqueIdentifier>
</Filter>
Expand Down
24 changes: 12 additions & 12 deletions kgEngine-2.0/TileMapGame/Transformation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -186,7 +186,7 @@ namespace kg

void Transformation::recalculateGlobalBounds()
{
m_globalBoundsMutex.lock();
m_mutex.lock();

RectangleShape shape;

Expand All @@ -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";
Expand Down
3 changes: 1 addition & 2 deletions kgEngine-2.0/TileMapGame/Transformation.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit 2719979

Please sign in to comment.