Browse Source

2D Funktionalität hinzugefügt

kolja 5 years ago
parent
commit
1758a26d5d
8 changed files with 167 additions and 51 deletions
  1. 2 2
      Array.h
  2. 63 42
      Kamera2D.cpp
  3. 13 4
      Kamera2D.h
  4. 58 2
      Model2D.cpp
  5. 10 0
      Model2D.h
  6. 1 1
      Vec2.h
  7. 18 0
      Welt2D.cpp
  8. 2 0
      Welt2D.h

+ 2 - 2
Array.h

@@ -68,7 +68,7 @@ namespace Framework
 
     public:
         // Erstellt eine neue Linked List
-        Array()
+        Array() noexcept
         {
 			entries = new ArrayEintrag< TYP >();
 			entries->set = 0;
@@ -383,7 +383,7 @@ namespace Framework
 
     public:
         // Erstellt eine neue Linked List
-        RCArray()
+        RCArray() noexcept
         {
             entries = new ArrayEintrag< TYP* >();
             entries->set = 0;

+ 63 - 42
Kamera2D.cpp

@@ -13,6 +13,7 @@ Kamera2D::Kamera2D()
     rotation = 0;
     matrix = Mat3< float >::identity();
     zoom = 1;
+    tickWelt = 0;
     ref = 1;
 }
 
@@ -22,59 +23,36 @@ Kamera2D::~Kamera2D()
         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::setZoom( float zoom )
 {
     this->zoom = zoom;
 }
 
-void Kamera2D::setWelt( Welt2D *welt )
+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( 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 )
+    if( welt && tickWelt )
         ret |= welt->tick( time );
     return ret | ZeichnungHintergrund::tick( time );
 }
@@ -104,13 +82,13 @@ Vertex Kamera2D::getWorldCoordinates( Punkt screenPos )
     if( welt->getWorldInfo().circular && welt->getWorldInfo().hasSize && welt->getWorldInfo().size.x && welt->getWorldInfo().size.y )
     {
         while( wKoord.x < 0 )
-            wKoord.x += welt->getWorldInfo().size.x;
+            wKoord.x += (float)welt->getWorldInfo().size.x;
         while( wKoord.x > welt->getWorldInfo().size.x )
-            wKoord.x -= welt->getWorldInfo().size.x;
+            wKoord.x -= (float)welt->getWorldInfo().size.x;
         while( wKoord.y < 0 )
-            wKoord.y += welt->getWorldInfo().size.y;
+            wKoord.y += (float)welt->getWorldInfo().size.y;
         while( wKoord.y > welt->getWorldInfo().size.y )
-            wKoord.y -= welt->getWorldInfo().size.y;
+            wKoord.y -= (float)welt->getWorldInfo().size.y;
     }
     return wKoord;
 }
@@ -131,4 +109,47 @@ 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;
 }

+ 13 - 4
Kamera2D.h

@@ -11,8 +11,9 @@ namespace Framework
 
     class Kamera2D : public ZeichnungHintergrund
     {
-    private:
-        Welt2D * welt;
+    protected:
+        Welt2D *welt;
+        bool tickWelt;
         Vertex wPos;
         float rotation;
         float zoom;
@@ -21,9 +22,11 @@ namespace Framework
 
     public:
         __declspec( dllexport ) Kamera2D();
-        virtual __declspec( dllexport ) ~Kamera2D();
+        __declspec( dllexport ) virtual ~Kamera2D();
+        __declspec( dllexport ) void lookAtWorldPos( int x, int y );
+        __declspec( dllexport ) void lookAtWorldArea( int width, int height );
         __declspec( dllexport ) void setZoom( float zoom );
-        __declspec( dllexport ) void setWelt( Welt2D *welt );
+        __declspec( dllexport ) void setWelt( Welt2D *welt, bool tick );
         __declspec( dllexport ) bool tick( double time );
         __declspec( dllexport ) void render( Bild &zRObj );
         __declspec( dllexport ) Vertex getWorldCoordinates( Punkt screenPos );
@@ -31,4 +34,10 @@ namespace Framework
         __declspec( dllexport ) Kamera2D *getThis();
         __declspec( dllexport ) Kamera2D *release();
     };
+
+    class TestKamera2D : public Kamera2D
+    {
+    public:
+        __declspec( dllexport ) bool tick( double time );
+    };
 }

+ 58 - 2
Model2D.cpp

@@ -769,7 +769,7 @@ void Model2DObject::render( Mat3< float > &kamMat, Bild &zRObj )
         if( textur->z( num ) )
         {
             Bild *txt = textur->z( num )->zTextur();
-            for( auto *i = &p->var->getArray(); i && i->set; i = i->next )
+            for( auto *i = &p->var->getArray(); i && i->set && txt; i = i->next )
             {
                 for( auto *j = &i->var->zListe()->getArray(); j->next->next && j->next->next->set; j = j->next )
                 {
@@ -784,6 +784,24 @@ void Model2DObject::render( Mat3< float > &kamMat, Bild &zRObj )
             }
         }
     }
+    /* Draws 2D Mesh
+    for( auto *p = &rData->vListen->getArray(); p && p->set; p = p->next, num++ )
+    {
+        Mat3< float > mat = kamMat * getObjectMatrix();
+        for( auto *i = &p->var->getArray(); i && i->set; i = i->next )
+        {
+            for( auto *j = &i->var->zListe()->getArray(); j->next->next && j->next->next->set; j = j->next )
+            {
+                Vertex a = mat * *j->var->punkt;
+                Vertex b = mat * *j->next->var->punkt;
+                Vertex c = mat * *j->next->next->var->punkt;
+                zRObj.drawLinie( a, b, 0xFFFFFFFF );
+                zRObj.drawLinie( a, c, 0xFFFFFFFF );
+                zRObj.drawLinie( b, c, 0xFFFFFFFF );
+            }
+        }
+    }
+    */
 }
 
 // constant
@@ -795,7 +813,7 @@ bool Model2DObject::istPunktInnen( Vertex p ) const
     if( p < Mat3< float >::scaling( size ) * rData->minP || p > Mat3< float >::scaling( size ) * rData->maxP || !rData->polygons )
         return 0;
     int num = 0;
-    Mat3< float > mat = Mat3< float >::rotation( -rotation ) * Mat3< float >::scaling( -size );
+    Mat3< float > mat = Mat3< float >::rotation( -rotation ) * Mat3< float >::scaling( 1 / size );
     p = mat * p;
     for( auto *polygon = &rData->polygons->getArray(); polygon && polygon->set; polygon = polygon->next, num++ )
     {
@@ -969,6 +987,44 @@ float Model2DObject::getMasse() const
     return rData->getMasse() * size * size;
 }
 
+// Gibt die Textur des ersten Polygons zurück
+Textur2D *Model2DObject::getTextur() const
+{
+    return textur->get( 0 );
+}
+
+// Gibt die Textur eines Polygons zurück
+//  polygonName: Der Name des Polygons
+Textur2D *Model2DObject::getTextur( const char *polygonName ) const
+{
+    int index = 0;
+    for( auto i = rData->polygons->getArray(); i.set; i++, index++ )
+    {
+        if( i.var.name->istGleich( polygonName ) )
+            return textur->get( index );
+    }
+    return 0;
+}
+
+// Gibt die Textur des ersten Polygons ohne erhöhten Reference Counter zurück
+Textur2D *Model2DObject::zTextur() const
+{
+    return textur->z( 0 );
+}
+
+// Gibt die Textur eines Polygons ohne erhöhten Reference Counter zurück
+//  polygonName: Der Name des Polygons
+Textur2D *Model2DObject::zTextur( const char *polygonName ) const
+{
+    int index = 0;
+    for( auto i = rData->polygons->getArray(); i.set; i++, index++ )
+    {
+        if( i.var.name->istGleich( polygonName ) )
+            return textur->z( index );
+    }
+    return 0;
+}
+
 Model2DData *Model2DObject::getModel() const
 {
     return rData ? rData->getThis() : 0;

+ 10 - 0
Model2D.h

@@ -131,6 +131,16 @@ namespace Framework
         __declspec( dllexport ) float getLuftWiederstand() const override;
         // Gibt die Masse des 2D Modells zurück (summe der Flächen der nicht transparenten Polygone)
         __declspec( dllexport ) float getMasse() const override;
+        // Gibt die Textur des ersten Polygons zurück
+        __declspec( dllexport ) Textur2D *getTextur() const;
+        // Gibt die Textur eines Polygons zurück
+        //  polygonName: Der Name des Polygons
+        __declspec( dllexport ) Textur2D *getTextur( const char *polygonName ) const;
+        // Gibt die Textur des ersten Polygons ohne erhöhten Reference Counter zurück
+        __declspec( dllexport ) Textur2D *zTextur() const;
+        // Gibt die Textur eines Polygons ohne erhöhten Reference Counter zurück
+        //  polygonName: Der Name des Polygons
+        __declspec( dllexport ) Textur2D *zTextur( const char *polygonName ) const;
         // Gibt die Model Daten zurück
         __declspec( dllexport ) Model2DData *getModel() const;
         // Gibt die Model Daten ohne erhöhten Reference Counter zurück

+ 1 - 1
Vec2.h

@@ -13,7 +13,7 @@ namespace Framework
         T x; // x Komponente des Vektors
         T y; // y Komponente des Vektors
         // Konstruktor
-        inline Vec2()
+        inline Vec2() noexcept
             : x( 0 ),
               y( 0 )
         {}

+ 18 - 0
Welt2D.cpp

@@ -270,6 +270,24 @@ void Welt2D::addObject( Object2D *obj )
     objects->add( obj );
 }
 
+void Welt2D::removeObject( Object2D *obj )
+{
+    int anz = objects->getEintragAnzahl();
+    for( int i = 0; i < anz; i++ )
+    {
+        if( objects->get( i ) == obj )
+        {
+            objects->remove( i );
+            i--;
+        }
+    }
+}
+
+void Welt2D::removeAll()
+{
+    objects->leeren();
+}
+
 void Welt2D::explosion( Vertex worldPos, float intensity, float maxRad )
 {
     maxRad = maxRad * maxRad;

+ 2 - 0
Welt2D.h

@@ -151,6 +151,8 @@ namespace Framework
         __declspec( dllexport ) void setSize( bool hasSize );
         __declspec( dllexport ) void setCircular( bool circular );
         __declspec( dllexport ) void addObject( Object2D *obj );
+        __declspec( dllexport ) void removeObject( Object2D *obj );
+        __declspec( dllexport ) void removeAll();
         __declspec( dllexport ) void explosion( Vertex worldPos, float intensity, float maxRad );
         __declspec( dllexport ) void impuls( Vertex worldPos, Vertex worldDir );
         __declspec( dllexport ) bool tick( double zeit );