PageRenderTime 31ms CodeModel.GetById 1ms app.highlight 24ms RepoModel.GetById 1ms app.codeStats 1ms

/src/Animation.cpp

http://github.com/clintbellanger/flare
C++ | 109 lines | 65 code | 16 blank | 28 comment | 25 complexity | 88b4142e24eaef04b2729f163d29871e MD5 | raw file
  1/*
  2Copyright 2011 kitano
  3
  4This file is part of FLARE.
  5
  6FLARE is free software: you can redistribute it and/or modify it under the terms
  7of the GNU General Public License as published by the Free Software Foundation,
  8either version 3 of the License, or (at your option) any later version.
  9
 10FLARE is distributed in the hope that it will be useful, but WITHOUT ANY
 11WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 12PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 13
 14You should have received a copy of the GNU General Public License along with
 15FLARE.  If not, see http://www.gnu.org/licenses/
 16*/
 17
 18/**
 19 * class Animation
 20 *
 21 * The Animation class handles the logic of advancing frames based on the animation type
 22 * and returning a renderable frame.
 23 * 
 24 * The intention with the class is to keep it as flexible as possible so that the animations
 25 * can be used not only for character animations but any animated in-game objects.
 26 */
 27
 28#include "Animation.h"
 29
 30Animation::Animation(std::string _name, Point _render_size, Point _render_offset, int _position, int _frames, int _duration, std::string _type)
 31	: name(_name), sprites(NULL),
 32	  render_size(_render_size), render_offset(_render_offset),
 33	  position(_position), frames(_frames), duration(_duration), type(_type),
 34	  cur_frame(0), disp_frame(0), mid_frame(0), max_frame(0), timesPlayed(0) {
 35
 36	if (type == "play_once" || type == "looped") {
 37		max_frame = frames * duration;
 38	}
 39	else if (type == "back_forth") {
 40		mid_frame = frames * duration;
 41		max_frame = mid_frame + mid_frame;
 42	
 43	}
 44}
 45
 46void Animation::advanceFrame() {
 47
 48	if (type == "play_once") {
 49		if (cur_frame < max_frame - 1) {
 50			cur_frame++;
 51		}
 52		else {
 53			timesPlayed = 1;
 54		}
 55		disp_frame = (cur_frame / duration) + position;
 56	}
 57	else if (type == "looped") {
 58		cur_frame++;
 59		if (cur_frame == max_frame) {
 60			cur_frame = 0;
 61			//animation has completed one loop
 62			timesPlayed++;
 63		}
 64		disp_frame = (cur_frame / duration) + position;	
 65
 66	}
 67	else if (type == "back_forth") {
 68		cur_frame++;
 69				
 70		if (cur_frame == max_frame) {
 71			cur_frame = 0;
 72			//animation has completed one loop
 73			timesPlayed++;
 74		}
 75
 76		if (cur_frame >= mid_frame) {
 77			disp_frame = (max_frame -1 - cur_frame) / duration + position;
 78		}
 79		else {
 80			disp_frame = cur_frame / duration + position;
 81		}
 82
 83	}
 84}
 85
 86Renderable Animation::getCurrentFrame(int direction) {
 87	Renderable r;
 88
 89	// if the animation contains the spritesheet
 90	if (sprites != NULL) {
 91		r.sprite = sprites;
 92	}
 93
 94	r.src.x = render_size.x * disp_frame;
 95	r.src.y = render_size.y * direction;
 96	r.src.w = render_size.x;
 97	r.src.h = render_size.y;
 98	r.offset.x = render_offset.x;
 99	r.offset.y = render_offset.y; // 112
100	r.object_layer = true;
101	return r;
102}
103
104void Animation::reset() {
105	cur_frame = 0;
106	disp_frame = (cur_frame / duration) + position;
107	timesPlayed = 0;
108}
109