Frame Animation manager – 2D

This week at university we have covered 2D frame animation – simple idea every frame is separate image.

what we need are two classes:

  • Animation
  • AnimationManager
// Animation.h
#pragma once
#ifndef ANIMATION_H
#define ANIMATION_H

#include 

class Animation
{
private: 
  IDirect3DTexture9* texture;
  int frameWidth;
  int frameHeight;
  int startFrame;
  int endFrame;
  int frameCount;
  float frameTime;
  bool isLooping;
public:
  Animation(IDirect3DTexture9* texture);
  Animation(IDirect3DTexture9* texture, float frameTime, bool isLooping);
  Animation(IDirect3DTexture9* texture, float frameTime, bool isLooping, int startFrame, int endFrame,int frameWidth, int frameHeight);
  ~Animation();

  void setTexture(IDirect3DTexture9* newTexture){ texture = newTexture; }
  IDirect3DTexture9* getTexture(){ return texture;}

  void setFrameTime(float newFrameTime){ frameTime = newFrameTime;};
  float getFrameTime(){return frameTime;}

  void setIsLooping(bool newIsLooping){ isLooping = newIsLooping; }
  bool getIsLooping() { return isLooping; }

  void setStartFrame(int newStartFrame) { startFrame = newStartFrame;}
  int getStartFrame() { return startFrame;}

  void setEndFrame(int newEndFrame) { endFrame = newEndFrame; }
  int getEndFrame() { return endFrame; }

  void setFrameCount(int newFrameCount) { frameCount = newFrameCount; }
  int getFrameCount() { return frameCount;}

  void setFrameWidth(int newFrameWidht) { frameWidth = newFrameWidht; }
  int getFrameWidth() { return frameWidth; }

  void setFrameHeight(int newFrameHeight) { frameHeight = newFrameHeight; } 
  int getFrameHeight() { return frameHeight; }
};
#endif

as we can see just bunch of attributes and geters/seters

// Animation.cpp
#include "Animation.h"

Animation::Animation(IDirect3DTexture9* texture): texture(texture){
  frameTime = 1.0f/ 30.0f;
  isLooping = true;
  D3DSURFACE_DESC desc;
  texture->GetLevelDesc(0, &desc);
  startFrame = 0;
  endFrame = desc.Width / desc.Height;
  frameCount = endFrame - startFrame;
  frameWidth = desc.Height; 
  frameHeight = desc.Height;
}

Animation::Animation(IDirect3DTexture9* texture, float frameTime, bool isLooping)
: texture(texture), frameTime(frameTime),isLooping(isLooping){
  D3DSURFACE_DESC desc;
  texture->GetLevelDesc(0, &desc);
  startFrame = 0;
  endFrame = desc.Width / desc.Height;
  frameCount = endFrame - startFrame;
  frameWidth = desc.Height; 
  frameHeight = desc.Height;
}

Animation::Animation(IDirect3DTexture9* texture, float frameTime, bool isLooping, int startFrame, int endFrame,int frameWidth, int frameHeight)
: texture(texture),
frameTime(frameTime),
isLooping(isLooping),
startFrame(startFrame),
endFrame(endFrame),
frameWidth(frameWidth),
frameHeight(frameHeight) {
  frameCount = endFrame - startFrame;
}

Animation::~Animation(){}
// AnimationManager.h
#pragma once
#ifndef ANIMATIONPLAYER_H
#define ANIMATIONPLAYER_H

#include "Animation.h"
[...]
class AnimationPlayer
{
private:
  Animation* currentAnimation;
  std::string playing;
  std::map animations;
  int frameIndex;
  float time;
public:
  AnimationPlayer();
  void playAnimation(std::string name);
  void addAnimation(std::string name, Animation* anim);
  void update(float frameTime);
  void draw(ID3DXSprite* spriteBatch, D3DXVECTOR3 position, D3DXMATRIX transform);

  ~AnimationPlayer();
  Animation* getCurrentAnimation(){return currentAnimation;}

  std::string getPlaying(){ return playing; }
  int getFrameIndes() { return frameIndex;}
};
#endif

Animation manager keeps all animations in map, pointer to current animation, playing are easy to guess.

same with methods: playAnimation, addAnimation, draw and getCurrentAnimation but why there is an update method?
well code should make that clear.

// AnimationManager.cpp
#include "AnimationPlayer.h"

AnimationPlayer::AnimationPlayer(){}

AnimationPlayer::~AnimationPlayer(){
  std::map::iterator iter = animations.begin();
  for(; iter != animations.end(); ++iter){
    delete iter->second;
  }
  animations.clear();
}

void AnimationPlayer::playAnimation(std::string name){
  Animation* anim = animations[name];
  if(anim == currentAnimation)
    return;

  currentAnimation = anim;
  playing = name;
  frameIndex = 0;
  time = 0.0f;
}

void AnimationPlayer::update(float frameTime){
  if(currentAnimation == 0)
    return;

  time += frameTime;

  while(time > currentAnimation->getFrameTime()){
    time -=currentAnimation->getFrameTime();
    if(currentAnimation->getIsLooping())
      frameIndex = (++frameIndex) % currentAnimation->getFrameCount();
    else
      frameIndex = min(++frameIndex, currentAnimation->getFrameCount() - 1);
  }
}

void AnimationPlayer::draw(ID3DXSprite* sprite, D3DXVECTOR3 position, D3DXMATRIX transform){
  if(currentAnimation == 0)
    return;
  D3DXMATRIX currentTransform;
  sprite->GetTransform(¤tTransform);
  sprite->SetTransform(&transform);

  int frameX = (frameIndex + currentAnimation->getStartFrame());
  frameX *= currentAnimation->getFrameWidth();

  int frameY = (frameIndex + currentAnimation->getStartFrame());
  frameY *= currentAnimation->getFrameWidth();
  frameY %= currentAnimation->getFrameWidth();

  RECT rect;
  rect.left = frameX;
  rect.right = frameX + currentAnimation->getFrameWidth();
  rect.top = frameY;
  rect.bottom = frameY + currentAnimation->getFrameHeight();

  sprite->Draw(currentAnimation->getTexture(), &rect, 0, &position, D3DCOLOR_XRGB(255,255,255));
  sprite->SetTransform(¤tTransform);
}

I am bit worried because all stuff we do leads us – students to develop 2D based platformer game.. next week we are going to do simple 2d physics and particles.. which is not really necessary in my game.

but good news is.. I have managed to get back on track with my 3D Zombeesh.. some tech demo should be ready by end of the week!! can’t wait!

PlayerManager – prototype class

Just a quick note. I have managed to write Player Manager class which is kind a prototype.. the two issues that I am having are how to get an offset of a map.. if I am trying to set up offset to static DXDVECTOR3 variable inside Level class linker is complaining, passing as attribute well it is a option but then I need to modify other classes.

Second problem is click in a frame. actually I have just figured this out :).. wow sometimes one sec break and step back can do the trick.

Anyway deep beta presents PlayerManager prototype class – probably it will be totally modified but as far as now I have something like that:

// PlayerManager.h

#pragma once
#ifndef PLAYERMANAGER_H
#define PLAYERMANAGER_H

[...]
#include "Entity.h"
#include "Level.h"

class PlayerManager
{
private:
  //players entities
  static std::map playersEntities;
  static std::map playersSelectedEntities;	
public:
  static PlayerManager* instance;
  static void init();
  static void registerPlayersEntity(Entity* playersEntity); // called in players unity type constructor
  static void removePlayersEntity(Entity* playersEntity);
  static Entity* getPlayersEntity(int id);
  static void clearSelected();
  static void addEntityToSelected(POINT mousePosition);

  PlayerManager();
  ~PlayerManager();
};
#endif
// PlayerManager.cpp
#include "PlayerManager.h"
#include "Collision.h"

PlayerManager* PlayerManager::instance =0;
std::map PlayerManager::playersEntities = std::map();
std::map PlayerManager::playersSelectedEntities = std::map();

PlayerManager::PlayerManager(){}

void PlayerManager::init(){
  PlayerManager::instance = new PlayerManager();
}

PlayerManager::~PlayerManager(){
  //clear selected units list
  std::map::iterator iter = playersSelectedEntities.begin();
  for(; iter != playersSelectedEntities.end(); ++iter){
    delete iter->second;
    iter->second = 0;
  }
  playersSelectedEntities.clear();
  playersEntities.clear();
}

//register entity in players list
void PlayerManager::registerPlayersEntity(Entity* entity){
  //register entity called only once and after registration in entity manager
  // we have entity ID and I am pretty sure it will not dublicate.
  playersEntities[entity->getID()] = entity;
}


void PlayerManager::removePlayersEntity(Entity* entity){
  //removes selecred entity from players entities lise
  std::map::iterator iter = playersEntities.find(entity->getID());
  if(iter != playersEntities.end())
    playersEntities.erase(iter);
}

Entity* PlayerManager::getPlayersEntity(int id){
  //returns pointer to entity defined by id
  std::map::iterator iter = playersEntities.find(id);
  if(iter != playersEntities.end())
    return iter->second;
  return 0;
}

void PlayerManager::addEntityToSelected(POINT mousePosition){
  D3DXVECTOR3 position(0.0f,0.0f,0.0f);
  float radius = 1.0f;

  D3DXVECTOR3 mPosition(mousePosition.x, mousePosition.y, 0.0f);

  Circle mouseCircle = {mPosition, radius };
  Circle unitCircle = {position, radius };
  mouseCircle.position.x = mousePosition.x; // need to figure out how to apply offset
  mouseCircle.position.y = mousePosition.y;
  mouseCircle.position.z = 0.0f;
  mouseCircle.radius = 2.0f;
	
  std::map::iterator iter = playersEntities.begin();
  for(; iter != playersEntities.end(); ++iter){
    unitCircle.position = iter->second->getPosition();
    unitCircle.radius = 20.0f; // need to set this global instead of calculated based on number images etc like TILEBASEWIDTH or something
    CollisionResults collisionResult = TestCollisionCircle(mouseCircle, unitCircle);
    if(collisionResult == OVERLAPPING){
      std::cout < < "AWESOME";
    }else {
      PlayerManager::clearSelected();
      std::cout << "NOT";
    }
  }
}

void PlayerManager::clearSelected(){
  std::map::iterator iter = playersSelectedEntities.begin();
  for(; iter != playersSelectedEntities.end(); ++iter){
    if(iter->second){
      delete iter->second;
      iter->second = 0;
    }
  }
  playersSelectedEntities.clear();	
}

Zombeesh overview 1 – Collision detection circle based

After less than one week – weekend not counted in (I have had over 24hrs of sleep during weekend, I really hate to be sick) I have cought up with university material. Material covered:

  • Tile system for map loaded from a file
  • offset for moving map
  • entity manager for all objects on the map – plus improvement
  • messaging system – communication between instances of objects
  • collision detection – and my own improvement
  • moving around the map – mouse based
  • font manager

Plans for next week

  • Selecting objects by mouse click – hoping to select more than one element
  • collision detection applied for object on a map
  • placing building on the map
  • shooting to zombies and other stuff.

Meanwhile I will post some code – I haven’t done that for a while.

Collision detection class with improvements

// Collision.h
#pragma once
#ifndef COLLISION_H
#define COLLISION_H

const float TOUCH_DISTANCE = 0.000000000001;

static enum CollisionResults {
  NO_COLLISION, TOUCHING, OVERLAPPING
};

struct Circle{
  D3DXVECTOR3& position;
  float radius;
};

struct BoundingBox{
  D3DXVECTOR3& position;
  D3DXVECTOR3& size;
};

bool TestCollision(const BoundingBox& a, const BoundingBox& b);

CollisionResults TestCollisionCircle(const Circle& a, const Circle& b);
#endif

Pretty easy stuff here – two structs for BoundingBox and Circle enum for collision between circles – I have added it because I want to base selecting objects and – well all collision on two circles.

// Collision.cpp
#include "Collision.h"

bool TestCollision(const BoundingBox& a, const BoundingBox& b){
  float t;
  if((t = a.position.x - b.position.x) > b.size.x || -t > a.size.x)
    return false;
  if((t = a.position.y - b.position.y) > b.size.y || -t > a.size.y)
    return false;
  if((t = a.position.z - b.position.z) > b.size.z || -t > a.size.z)
    return false;
  return true;
}

CollisionResults TestCollisionCircle(const Circle& a, const Circle& b){
  //for math
  CollisionResults colliding;
  float distance_squared;
  float radii_squared;

  //a*a + b*b = c*c
  distance_squared = ((a.position.x - b.position.x)* (a.position.x - b.position.x))+
                     ((a.position.y - b.position.y)* (a.position.y - b.position.y));

  //Multiplication is faster than taking a square root
  radii_squared = (a.radius + b.radius) * (a.radius + b.radius);

  if( -TOUCH_DISTANCE < radii_squared - distance_squared &&radii_squared - distance_squared < TOUCH_DISTANCE) 		
    colliding = TOUCHING;
  else if(radii_squared > distance_squared)
    colliding = OVERLAPPING;
  else
    colliding = NO_COLLISION;

  return colliding;
}

pretty simple stuff here as well – Pythagorean theorem based. if distance between two points is bigger than sum of radius of circles then there is no collision if it is equal there is a collision but if it is smaller they overlap – so we have covered all 3 states first two for collision detection on the map and third one for selecting. simple

Day 11: Well I have done the textures

I cannot admit.. I wanted to finish couple more things..
well so now we have space to render divided to quads.. increase performance, texture, lighting, info about graphic card and memory left on card.

Now I am ready to do game logic.

what should I start with?
random placing objects on a map?
word!

I need to think of a build name for this project.. any suggestions?

Day 10: Height Map

I am satisfied with that height map – no multi textures, blending, water or even trees.. I need to concentrate on logic now, polishing details will be last thing.

plan for today/tomorrow.

picking + adding objects on the map..

good luck Luke..

thanks Luke.

edit:
ok 2 more thing left before I ll be ready to go with picking and objects,

1. need to set up camera in 45 degree
2. I need to think of optimisation.. some sort of quad tree or so..

Deffme – 2D Terrain Protection Game

At this stage I can’t say I have done much – Uni, work, life :), but I have something:

First of all I have spent about half an hour on thinking how to write this game, mechanic and engine..

so the idea of game is pretty similar to famous “Plants vs Zombie”

2D game, we can place “tower/flower/anything that shoot” on a map, and there is horde of evil things trying to bypass our things – lets be clear at this stage this can be anything.. that’s just matter of sprites/sounds. hope that will be enough.. but if not – changing to 3D should be fairly easy anyway.

Our “things” can shoot bullet goes from left to the right and horde is going from right to the left.
every object at the map has life points.

My game is future proof and I have thought about different types of objects – eg slowing down bullets, faster enemies, more life points – upgrades etc,

lets remember about simple rule – everything on a map has to move with some speed.. and we need to make that the same for all devices – delta time 🙂

I am thinking about adding some sort of currency – sun, gold, points,tokens – whatever that will allow player to buy upgrades or new things and place it on the map

anyway.. lets see some code!
Continue reading Deffme – 2D Terrain Protection Game

3D Framework for Android sample

I finally made it!

I have found couple bugs that was causing problems in my game.

first of all and I guess that was only error I have had was texturing for blocks – my program was using custom created mipmap function that was using probably not supported by hardware on desire, however.. it is working now!

Check this out! 3D Framework