|
|
C/C++ Quellcode |
1 2 3 4 5 |
... Rect.y = FrameWidth; //Bei allen Rect.h = FrameHeight; // Drei habe m_NumFramesX = m_pImage->w / m_FrameWidth; // ich Probleme.... ... |
|
|
C/C++ Quellcode |
1 2 3 4 5 6 7 8 |
... int Column = static_cast<int>(fFrameNumber)%m_NumFramesX; int Row = static_cast<int>(fFrameNumber)/m_NumFramesX; m_FrameRect.x = Column * m_FrameWidth; m_FrameRect.y = Row*m_FrameHeight; SDL_BlitSurface ... |

|
|
Source code |
1 2 3 |
m_NumFramesX = m_pImage->w (191) / m_FrameWidth (47) = 4; m_NumFramesY = m_pImage->h (256) / m_FrameHeight(64) = 4; TotalFrames = m_NumFramesX(4) * m_NumFramesY(4) = 16; |
|
|
Source code |
1 2 3 |
-> Zeile und Spalte aus dem Frameindex Berechnen: spalte = fFrameNumber(6)%m_NumFramesX(4) = 2(Rest); zeile = fFrameNumber(6)/m_NumFramesY(4) = 1; |
|
|
Source code |
1 2 |
x = 2*47 = 94px; y = 1*64 = 64px; |
This post has been edited 1 times, last edit by "Rushh0ur" (Mar 22nd 2011, 2:38pm)
This post has been edited 1 times, last edit by "Checkmateing" (Mar 22nd 2011, 9:50pm)
|
|
C/C++ Quellcode |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
void lwtGraphic::Graphic::Init(const string Filename, int FrameNumber, int FrameWidth, int FrameHeight, sf::Image Image) { if(!Image.LoadFromFile(Filename)) //File laden exit (3); //ist fehlgeschlagen, also schließen //Rect für Animationsphase initialisieren lwtGraphic::Graphic::m_FrameNumber = FrameNumber; lwtGraphic::Graphic::m_FrameWidth = FrameWidth; lwtGraphic::Graphic::m_FrameHeight = FrameHeight; m_FrameNumberX = lwtGraphic::Graphic::Image->GetWidth() / FrameWidth; //Berichtigen m_FrameNumberY = lwtGraphic::Graphic::Image->GetHeight() / FrameHeight; //Berichtigen m_FrameNumberXY = m_FrameNumberX * m_FrameNumberY; } |
|
|
C/C++ Quellcode |
1 2 3 4 5 6 7 |
void lwtGraphic::Graphic::Render(sf::Sprite Sprite, sf::Image Image, int FrameNumber, int Top, int Bottom, int Right, int Left) { //Aufgabe: Noch überarbeiten und Kommentare hinzufügen int Column = FrameNumber%m_FrameNumberX; int Row = FrameNumber/m_FrameNumberY; Sprite.SetSubRect(sf::IntRect(Top, Bottom, Right, Left)); } |


|
|
C/C++ Quellcode |
1 2 3 4 5 6 7 |
void lwtGraphic::Graphic::Init(const string Filename, int FrameNumber, int FrameWidth, int FrameHeight, sf::Image Image) in void lwtGraphic::Graphic::Init(const string &Filename, int FrameNumber, int FrameWidth, int FrameHeight, sf::Image &Image) |
|
|
C/C++ Quellcode |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
void lwtGraphic::Graphic::Render(sf::Sprite &Sprite, sf::Image &Image, int FrameNumber) { int Top, int Bottom, int Right, int Left; //Aufgabe: Noch überarbeiten und Kommentare hinzufügen int Column = FrameNumber%m_FrameNumberX; int Row = FrameNumber/m_FrameNumberY; // Rechtec berechnen (pseudo) Left = Column * FrameWidth; Top = Row * FrameHeight; Right = Top + FrameWidth; Bottom = Top + FrameHeight; Sprite.SetSubRect(sf::IntRect(Left, Top, Right, Bottom)); // Parameter Reihenfolge ahcten // Rendern ... } |
|
|
C/C++ Quellcode |
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 |
#include "AniSprite.h" #include <SFML/Window.hpp> #include <SFML/Graphics.hpp> int main() { // Create a Window and set Frameliment to 60fps sf::RenderWindow App(sf::VideoMode(792, 192), "Sprite Animation Example"); App.SetFramerateLimit(60); // Load a sprite to display sf::Image ImageWholeAnim; if (!ImageWholeAnim.LoadFromFile("anim.png")) return EXIT_FAILURE; // Create SubImages sf::Image ImageAnim[4]; ImageAnim[0].Create(960, 768, sf::Color(0,0,0,0)); // first animation block ImageAnim[0].Copy(ImageWholeAnim, 0,0, sf::IntRect(0, 0, 960, 768)); ImageAnim[1].Create(960, 768, sf::Color(0,0,0,0)); // second animation block ImageAnim[1].Copy(ImageWholeAnim, 0,0, sf::IntRect(0, 768, 960, 1536)); ImageAnim[2].Create(960, 1152, sf::Color(0,0,0,0)); // third animation block ImageAnim[2].Copy(ImageWholeAnim, 0,0, sf::IntRect(0, 1536, 960, 2880)); ImageAnim[3].Create(960, 768, sf::Color(0,0,0,0)); // fourth animation block ImageAnim[3].Copy(ImageWholeAnim, 0,0, sf::IntRect(0, 2880, 960, 3648)); // Create Animation Sprites AniSprite Sprite[4]; for (int i=0; i<4; ++i) { Sprite[i] = AniSprite(ImageAnim[i], 192, 192); Sprite[i].SetPosition(200 * i, 0); Sprite[i].SetLoopSpeed(20); Sprite[i].Play(); } // Start the game loop while (App.IsOpened()) { // Process events sf::Event Event; while (App.GetEvent(Event)) { if (Event.Type == sf::Event::Closed) App.Close(); } // Render Start App.Clear(sf::Color(255, 127, 0)); // Update Animation and Render Sprites for (int i=0; i<4; ++i) { Sprite[i].Update(); App.Draw(Sprite[i]); } // Flip Buffers App.Display(); // Render End } } |