SFML и C++ Уроки \ Разработка игр › Форумы › SFML Graphics › Анимация , класс Анимации › Ответ в теме: Анимация , класс Анимации
Что бы не создавать новую тему, напишу здесь.
По одному из сторонних уроков создал класс анимации, который загружает всю анимацию персонажа из XML-файла.
C++
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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
#ifndef ANIM_H #define ANIM_H #include "TinyXML/tinyxml.h" #include <SFML/Graphics.hpp> using namespace sf; class Animation { public: std::vector<IntRect> frames, frames_flip; float currentFrame, speed; bool loop, flip, isPlaying; Sprite sprite; Animation() { currentFrame = 0; isPlaying=true; flip=false; } void tick(float time) { if (!isPlaying) return; currentFrame += speed * time; if (currentFrame > frames.size()) { currentFrame -= frames.size(); if (!loop) {isPlaying=false; return;} } int i = currentFrame; sprite.setTextureRect( frames[i] ); if (flip) sprite.setTextureRect( frames_flip[i] ); } }; class AnimationManager { public: String currentAnim; std::map<String, Animation> animList; AnimationManager() {} ~AnimationManager() { animList.clear(); } void create(String name, Texture &texture, int x, int y, int w, int h, int count, float speed, int step=0, bool Loop=true) { Animation a; a.speed = speed; a.loop = Loop; a.sprite.setTexture(texture); a.sprite.setOrigin(0,h); for (int i=0;i<count;i++) { a.frames.push_back( IntRect(x+i*step, y, w, h) ); a.frames_flip.push_back( IntRect(x+i*step+w, y, -w, h) ); } animList[name] = a; currentAnim = name; } //загрузка из файла XML void loadFromXML(std::string fileName,Texture &t) { TiXmlDocument animFile(fileName.c_str()); animFile.LoadFile(); TiXmlElement *head; head = animFile.FirstChildElement("sprites"); TiXmlElement *animElement; animElement = head->FirstChildElement("animation"); while(animElement) { Animation anim; currentAnim = animElement->Attribute("title"); int delay = atoi(animElement->Attribute("delay")); anim.speed = 1.0/delay; anim.sprite.setTexture(t); TiXmlElement *cut; cut = animElement->FirstChildElement("cut"); while (cut) { int x = atoi(cut->Attribute("x")); int y = atoi(cut->Attribute("y")); int w = atoi(cut->Attribute("w")); int h = atoi(cut->Attribute("h")); anim.frames.push_back( IntRect(x,y,w,h) ); anim.frames_flip.push_back( IntRect(x+w,y,-w,h) ); cut = cut->NextSiblingElement("cut"); } anim.sprite.setOrigin(0,anim.frames[0].height); animList[currentAnim] = anim; animElement = animElement->NextSiblingElement("animation"); } } void set(String name) { currentAnim = name; animList[currentAnim].flip=0; } void draw(RenderWindow &window,int x=0, int y=0) { animList[currentAnim].sprite.setPosition(x,y); window.draw( animList[currentAnim].sprite ); } void flip(bool b=1) {animList[currentAnim].flip = b;} void tick(float time) {animList[currentAnim].tick(time);} void pause() {animList[currentAnim].isPlaying=false;} void play() {animList[currentAnim].isPlaying=true;} void play(String name) {animList[name].isPlaying=true;} bool isPlaying() {return animList[currentAnim].isPlaying;} float getH() {return animList[currentAnim].frames[0].height;} float getW() {return animList[currentAnim].frames[0].width;} }; #endif ANIM_H |
В классе int main() прописал функцию вызова
C++
1 2 |
AnimationManager anim; anim.loadFromXML("files/hero_anim.xml",_); |
но не как не могу понять что нужно прописать во втором аргументе(где знак подчеркивания). Пробовал напрямую обратится к Player p…., но выдает ошибки. Каким образом можно реализовать эту функцию что бы работало все корректно?
ЗЫ: код практически такой, как в 20м уроке.