#include "Kamera2D.h" #include "Welt2D.h" #include "Bild.h" #include "Globals.h" using namespace Framework; Kamera2D::Kamera2D() : ZeichnungHintergrund() { welt = 0; rotation = 0; matrix = Mat3< float >::identity(); zoom = 1; ref = 1; } Kamera2D::~Kamera2D() { if( welt ) welt->release(); } void Kamera2D::setWelt( Welt2D *welt ) { if( this->welt ) this->welt->release(); this->welt = welt; } bool Kamera2D::tick( double time ) { bool ret = rend; rend = 0; if( getTastenStand( 'q' ) ) { rotation += (float)( time * 0.5 ); ret = 1; } if( getTastenStand( 'e' ) ) { rotation -= (float)( time * 0.5 ); ret = 1; } Vertex move; if( getTastenStand( 'w' ) ) move = Vertex( 0, -100 * (float)time ); if( getTastenStand( 'a' ) ) move = Vertex( -100 * (float)time, 0 ); if( getTastenStand( 'd' ) ) move = Vertex( 100 * (float)time, 0 ); if( getTastenStand( 's' ) ) move = Vertex( 0, +100 * (float)time ); if( move != Vertex( 0, 0 ) ) { wPos += move.rotation( -rotation ) * ( 1 / zoom ); ret = 1; } if( getTastenStand( '+' ) ) { zoom += ( zoom ) * (float)time; ret = 1; } if( getTastenStand( '-' ) ) { zoom -= ( zoom ) * (float)time; if( zoom <= 0 ) zoom = 1; ret = 1; } if( welt ) ret |= welt->tick( time ); return ret | ZeichnungHintergrund::tick( time ); } void Kamera2D::render( Bild &zRObj ) { if( hatStyleNicht( Style::Sichtbar ) ) return; ZeichnungHintergrund::render( zRObj ); if( !welt ) return; lockZeichnung(); if( !zRObj.setDrawOptions( innenPosition, innenSize ) ) { unlockZeichnung(); return; } matrix = Mat3< float >::translation( (Vertex)gr / 2 ) * Mat3< float >::rotation( rotation ) * Mat3< float >::scaling( zoom ) * Mat3< float >::translation( -wPos ); welt->render( matrix, gr, zRObj ); zRObj.releaseDrawOptions(); unlockZeichnung(); } Vertex Kamera2D::getWorldCoordinates( Punkt screenPos ) { return ( Mat3< float >::translation( wPos ) * Mat3< float >::scaling( 1 / zoom ) * Mat3< float >::rotation( -rotation ) * Mat3< float >::translation( (Vertex)gr / -2 ) ) * (Vertex)screenPos; } Kamera2D *Kamera2D::getThis() { ref++; return this; } Kamera2D *Kamera2D::release() { if( !--ref ) delete this; return 0; }