123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- #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;
- tickWelt = 0;
- ref = 1;
- }
- Kamera2D::~Kamera2D()
- {
- if( welt )
- welt->release();
- }
- void Kamera2D::lookAtWorldPos( int x, int y )
- {
- wPos.x = (float)x;
- wPos.y = (float)y;
- }
- void Kamera2D::lookAtWorldArea( int width, int height )
- {
- zoom = (float)getBreite() / (float)width;
- // TODO have two scaling factors
- }
- void Kamera2D::setDrehung( float rotation )
- {
- this->rotation = rotation;
- }
- void Kamera2D::setZoom( float zoom )
- {
- this->zoom = zoom;
- }
- void Kamera2D::setWelt( Welt2D *welt, bool tick )
- {
- if( this->welt )
- this->welt->release();
- this->welt = welt;
- tickWelt = tick;
- }
- bool Kamera2D::tick( double time )
- {
- bool ret = rend;
- rend = 0;
- if( welt && tickWelt )
- 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 )
- {
- Vertex wKoord = ( Mat3< float >::translation( wPos ) * Mat3< float >::scaling( 1 / zoom ) * Mat3< float >::rotation( -rotation ) * Mat3< float >::translation( (Vertex)gr / -2 ) ) * (Vertex)screenPos;
- if( welt->getWorldInfo().circular && welt->getWorldInfo().hasSize && welt->getWorldInfo().size.x && welt->getWorldInfo().size.y )
- {
- while( wKoord.x < 0 )
- wKoord.x += (float)welt->getWorldInfo().size.x;
- while( wKoord.x > welt->getWorldInfo().size.x )
- wKoord.x -= (float)welt->getWorldInfo().size.x;
- while( wKoord.y < 0 )
- wKoord.y += (float)welt->getWorldInfo().size.y;
- while( wKoord.y > welt->getWorldInfo().size.y )
- wKoord.y -= (float)welt->getWorldInfo().size.y;
- }
- return wKoord;
- }
- Vertex Kamera2D::getWorldDirection( Vertex dir )
- {
- return Vertex( dir.x, dir.y ).rotation( -rotation ) * ( 1 / zoom );
- }
- Kamera2D *Kamera2D::getThis()
- {
- ref++;
- return this;
- }
- Kamera2D *Kamera2D::release()
- {
- if( !--ref )
- delete this;
- return 0;
- }
- bool TestKamera2D::tick( double time )
- {
- bool ret = rend;
- rend = Kamera2D::tick( time );
- 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;
- }
- return ret;
- }
|