Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!
Werbeanzeige
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#ifndef ANIMATION_HPP_INCLUDED #define ANIMATION_HPP_INCLUDED class Animation { public: Animation(); explicit Animation(std::string File, int PicturesOnWidth, int PicturesOnHeight, float FPS); bool SetActive(bool Active); bool Draw(sf::RenderWindow &App); bool SetPosition(int X, int Y); bool CheckClock(); bool Reset(); bool MoveSubRect(); protected: // Resourcen float _FPS; //<FramesPerSecound int _POW, _POH; // <PicturesOnWidth, PicturesOnHeight int _WPI, _HPI; //< WidthPerImage, HeightPerImage int _CPW, _CPH; //<CursorPositionWidth, CursorPositionHeight sf::Clock _Clock; //<Misst die genau vergangene Zeit bool _Active; //<Gibt an ob das Elemnt Aktiv ist sf::Image _Img; //< Image sf::Sprite _Animation; //< Animationssprite }; #endif // ANIMATION_HPP_INCLUDED |
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
Animation::Animation() { } Animation::Animation(std::string File, int PicturesOnWidth, int PicturesOnHeight, float FPS) { this->_Img.LoadFromFile(File); this->_Img.SetSmooth(false); this->_Animation.SetImage(this->_Img); this->_POW = PicturesOnWidth; this->_POH = PicturesOnHeight; sf::Vector2f _WH = this->_Animation.GetSize(); this->_WPI = static_cast<int>( _WH.x )/ this->_POW; this->_HPI = static_cast<int>( _WH.y )/ this->_POH; this->_CPW = 0; this->_CPH = 0; this->_FPS = FPS; } bool Animation::SetActive(bool Active) { this->_Active = Active; return true; } bool Animation::Draw(sf::RenderWindow &App) { if (this->_Active) { if (this->CheckClock()) { this->MoveSubRect(); this->_Clock.Reset(); } App.Draw(this->_Animation); } else { this->Reset(); } return true; } bool Animation::CheckClock() { if (this->_Active) { if (this->_Clock.GetElapsedTime() > this->_FPS) { return true; } else { return false; } } else { this->_Clock.Reset(); return false; } } bool Animation::Reset() { this->_Animation.SetSubRect(sf::IntRect(1, 1, this->_WPI, this->_HPI)); this->_CPW = 0; this->_CPH = 0; return true; } bool Animation::MoveSubRect() { if (this->_CPW + 1 <= this->_POW) { this->_CPW += 1; } else { if (this->_CPH + 1 <= this->_POH) { this->_CPH += 1; this->_CPW = 0; } else { this->Reset(); } } this->_Animation.SetSubRect(sf::IntRect(this->_CPW * this->_WPI, this->_CPH * this->_HPI, (this->_CPW + 1) * this->_WPI, (this->_CPH + 1) * this->_HPI)); return true; } bool Animation::SetPosition(int X, int Y) { this->_Animation.SetPosition(X, Y); return true; } |
Community-Fossil
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 |
if (this->_Clock.GetElapsedTime() > this->_FPS) { return true; } else { return false; } |
C-/C++-Quelltext |
|
1 |
return (this->_Clock.GetElapsedTime() > this->_FPS); |
idontknow
unregistriert
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
m_animphase += m_speedAnim * m_frametime; if (m_animphase >= static_cast<float>(m_numFrames)) { m_animphase -= static_cast<float>(m_numFrames); } m_column = static_cast<int>(m_animphase) % m_numFramesX; m_row = static_cast<int>(m_animphase) / m_numFramesX; m_spriteObject->SetSubRect(sf::IntRect( static_cast<int>(m_frameSizeWidth * m_column), static_cast<int>(m_frameSizeHeight * m_row), static_cast<int>(m_frameSizeWidth * m_column + m_frameSizeWidth), static_cast<int>(m_frameSizeHeight * m_row + m_frameSizeHeight))); |
idontknow
unregistriert
C-/C++-Quelltext |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
void Animation::Move(float frametime) { m_TimeDelta += frametime; if(m_TimeDelta >= m_FrameDuration) // move one frame forward!!! { m_TimeDelta -= m_FrameDuration; m_CurrentFrame++; } if(m_CurrentFrame > (m_NumFrames - 1)) if(m_Autorepeat) m_CurrentFrame = 0; else ; // end of animation.. // calc new subrect! m_SubRect.Left = m_Offset.x + m_SubRect.Width * m_CurrentFrame; m_Sprite.SetSubRect(m_SubRect); } |
idontknow
unregistriert
Kleiner Tipp, auch an Ninjasturm: Häufig kann man es gut gebrauchen wenn eine Animation nicht einfach von Anfang bis Ende abläuft und dann wieder am Anfang beginnt, sondern wenn sie am Ende umkehrt und wieder zurück zum Anfang läuft. Stichwort Pingpong. Das könntet ihr noch einbauen.
Gruß
SaRu_
Notfalls kann man immer noch die Frames zwischen (aber ohne!) Anfang und Ende kopieren und ans Ende setzen und eben die Frame Anzahl erhöhen
Werbeanzeige