Skip to content

Commit

Permalink
Ready for restructuring Animation and Image
Browse files Browse the repository at this point in the history
  • Loading branch information
liy committed Jul 30, 2011
1 parent bfdb0af commit 071c010
Show file tree
Hide file tree
Showing 17 changed files with 328 additions and 179 deletions.
28 changes: 13 additions & 15 deletions GraphicsEngine/AAnimation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ AFrame::AFrame(const std::string& $fileName, const Recti& $rect, unsigned short
}

AFrame::~AFrame(void){
free(texCoord);
// TODO: how to free the texture coordinate

// keep a copy record of the filename, to remove the
std::string fileName = texture_sp->fileName();
Expand All @@ -37,17 +37,17 @@ void AFrame::setRect(const Recti& $rect){
_rect = $rect;

// calculate bottom left of the image in texture coordinate.
float x = (float)_rect.x/texture_sp->width();
float y = (float)_rect.y/texture_sp->height();
float u = (float)_rect.x/texture_sp->width();
float v = (float)_rect.y/texture_sp->height();
// Calculate the the width and height in texture coordinate.
float w = (float)_rect.width/texture_sp->width();
float h = (float)_rect.height/texture_sp->height();

// assign the texture coordinate
texCoord[0].Set(x, y);
texCoord[1].Set(x + w, y);
texCoord[2].Set(x + w, y + h);
texCoord[3].Set(x, y + h);
texCoord[0].Set(u, v);
texCoord[1].Set(u + w, v);
texCoord[2].Set(u + w, v + h);
texCoord[3].Set(u, v + h);
}

void AFrame::setRect(int $x, int $y, int $width, int $height){
Expand All @@ -57,25 +57,23 @@ void AFrame::setRect(int $x, int $y, int $width, int $height){
_rect.height = $height;

// calculate bottom left of the image in texture coordinate.
float x = (float)_rect.x/texture_sp->width();
float y = (float)_rect.y/texture_sp->height();
float u = (float)_rect.x/texture_sp->width();
float v = (float)_rect.y/texture_sp->height();
// Calculate the the width and height in texture coordinate.
float w = (float)_rect.width/texture_sp->width();
float h = (float)_rect.height/texture_sp->height();

// assign the texture coordinate
texCoord[0].Set(x, y);
texCoord[1].Set(x + w, y);
texCoord[2].Set(x + w, y + h);
texCoord[3].Set(x, y + h);
texCoord[0].Set(u, v);
texCoord[1].Set(u + w, v);
texCoord[2].Set(u + w, v + h);
texCoord[3].Set(u, v + h);
}

const Recti& AFrame::rect() const{
return _rect;
}



AAnimation::AAnimation(void): AIGraphics(), _frameIndex(0), pingpong(false), repeat(true), _direction(1), _firstRound(true), _stopped(true), _frameTimer(1)
{
scale.Set(1.0f, 1.0f);
Expand Down
34 changes: 17 additions & 17 deletions GraphicsEngine/AImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ AImage::AImage(const std::string& $fileName, const Recti& $rect){

AImage::~AImage(void)
{
free(_texCoord);
// FIXME how to free the image texture coordinate????

// keep a copy record of the filename, to remove the
std::string fileName = _texture_sp->fileName();
Expand Down Expand Up @@ -107,10 +107,10 @@ void AImage::Draw(float x, float y, float z, float rotation){
glTranslatef(-width()*anchorRatio.x, -height()*anchorRatio.y, 0.0f);//anchor translation transform

glBegin(GL_QUADS);
glTexCoord2f(_texCoord[0].x, _texCoord[0].y); glVertex3f(0.0f, 0.0f, z);
glTexCoord2f(_texCoord[1].x, _texCoord[1].y); glVertex3f(_rect.width, 0.0f, z);
glTexCoord2f(_texCoord[2].x, _texCoord[2].y); glVertex3f(_rect.width, _rect.height, z);
glTexCoord2f(_texCoord[3].x, _texCoord[3].y); glVertex3f(0.0f, _rect.height, z);
glTexCoord2f(_texCoord[0].u, _texCoord[0].v); glVertex3f(0.0f, 0.0f, z);
glTexCoord2f(_texCoord[1].u, _texCoord[1].v); glVertex3f(_rect.width, 0.0f, z);
glTexCoord2f(_texCoord[2].u, _texCoord[2].v); glVertex3f(_rect.width, _rect.height, z);
glTexCoord2f(_texCoord[3].u, _texCoord[3].v); glVertex3f(0.0f, _rect.height, z);
glEnd();

// finished drawing disable texture 2d.
Expand All @@ -124,17 +124,17 @@ void AImage::setRect(const Recti& $rect){
_rect = $rect;

// calculate bottom left of the image in texture coordinate.
float x = (float)_rect.x/_texture_sp->width();
float y = (float)_rect.y/_texture_sp->height();
float u = (float)_rect.x/_texture_sp->width();
float v = (float)_rect.y/_texture_sp->height();
// Calculate the the width and height in texture coordinate.
float w = (float)_rect.width/_texture_sp->width();
float h = (float)_rect.height/_texture_sp->height();

// assign the texture coordinate
_texCoord[0].Set(x, y);
_texCoord[1].Set(x + w, y);
_texCoord[2].Set(x + w, y + h);
_texCoord[3].Set(x, y + h);
_texCoord[0].Set(u, v);
_texCoord[1].Set(u + w, v);
_texCoord[2].Set(u + w, v + h);
_texCoord[3].Set(u, v + h);
}

void AImage::setRect(int $x, int $y, int $width, int $height){
Expand All @@ -144,17 +144,17 @@ void AImage::setRect(int $x, int $y, int $width, int $height){
_rect.height = $height;

// calculate bottom left of the image in texture coordinate.
float x = (float)_rect.x/_texture_sp->width();
float y = (float)_rect.y/_texture_sp->height();
float u = (float)_rect.x/_texture_sp->width();
float v = (float)_rect.y/_texture_sp->height();
// Calculate the the width and height in texture coordinate.
float w = (float)_rect.width/_texture_sp->width();
float h = (float)_rect.height/_texture_sp->height();

// assign the texture coordinate
_texCoord[0].Set(x, y);
_texCoord[1].Set(x + w, y);
_texCoord[2].Set(x + w, y + h);
_texCoord[3].Set(x, y + h);
_texCoord[0].Set(u, v);
_texCoord[1].Set(u + w, v);
_texCoord[2].Set(u + w, v + h);
_texCoord[3].Set(u, v + h);
}

const Recti& AImage::rect() const{
Expand Down
3 changes: 3 additions & 0 deletions GraphicsEngine/AImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ class AImage : public AIGraphics
const float height() const;

protected:
// TODO: make sure copy constructor is doing things correct with the shared pointer.
// I assume it will simply do a copy with the pointer, and when the cloned image destructor is called, which will NULL the cloned pointer
// And try to remove the texture from the TextureManager. So it should be fine to use machine generated copy constructor.
std::tr1::shared_ptr<ATexture> _texture_sp;

// The actual offset position, width and height, related to the texture resolution.
Expand Down
14 changes: 7 additions & 7 deletions Platformer/Actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "AImage.h"
#include "AAnimation.h"
#include "acBody.h"
#include "Tile.h"
#include "PhysicalTile.h"
#include <GL\glut.h>
#include <GL\GL.h>
#include "acCollision.h"
Expand Down Expand Up @@ -80,15 +80,15 @@ Vec2f gravity(0.0f, -0.24f);
void Actor::Update(unsigned short deltaTime){
body_ptr->velocity += gravity;

Tile* tiles = scene_ptr->tiles;
PhysicalTile* tiles = scene_ptr->tiles;

////std::cout << "update called\n";
float tmin = FLT_MAX;
Vec2f normal;
bool collide = false;
Tile* collidedTile;
PhysicalTile* collidedTile;
for(int i=0; i<NUM_TILES; ++i){
AABB2f mdAABB = tiles[i].GetBody()->aabb - body_ptr->aabb;
AABB2f mdAABB = tiles[i].body()->aabb - body_ptr->aabb;

RayCastInput input;
input.maxFraction = 1.0f;
Expand All @@ -114,7 +114,7 @@ void Actor::Update(unsigned short deltaTime){
body_ptr->position += tmin * body_ptr->velocity;

// Because of the floating point error, we have to readjust the position of the body.
AdjustTOI(tmin, normal, *body_ptr, collidedTile->GetBody());
AdjustTOI(tmin, normal, *body_ptr, collidedTile->body());

// TODO: you can add collision response code here, dispatch event etc.

Expand Down Expand Up @@ -150,7 +150,7 @@ void Actor::Update(unsigned short deltaTime){
}

// in order to persist the wall grabbing. We should not update the horizontal velocity.
// Only when user is not grabbing the walls, eg. grounded and hit the wall, we shall 0 the horizontal velocity.
// Only when user is not grabbing the walls, e.g. grounded and hit the wall, we shall 0 the horizontal velocity.
// This ensures the wall collision detection is consistent across all updates in the update loops.
if(!_wallGrabbed){
body_ptr->velocity.x = rv.x;
Expand All @@ -165,7 +165,7 @@ void Actor::Update(unsigned short deltaTime){
for(int i=0; i<NUM_TILES; ++i){
// Using AABB directly to calculate SAT require body's AABB is up to date! Therefore we need to recompute AABB depending on the current body transform.
body_ptr->Synchronize();
if(AABBSAT(tiles[i].GetBody()->aabb, body_ptr->aabb, penetrationNormal, minTransV)){
if(AABBSAT(tiles[i].body()->aabb, body_ptr->aabb, penetrationNormal, minTransV)){
//body_ptr->position += sd*penetrationNormal;// + penetrationNormal;
//std::cout << "New [" << minTransV.x <<", "<< minTransV.y << "]\n";
// Adding 1px to complete separate two object, so future time actor will not hitting the edges of the tile.
Expand Down
17 changes: 4 additions & 13 deletions Platformer/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,11 @@ void Camera::Init(float viewportWidth, float viewportHeight, float anchorRatioX,

position.SetZero();

SetAnchorRatio(anchorRatioX, anchorRatioY);

scale = 1.0f;

lockedTarget = NULL;
}
anchorRatio.Set(anchorRatioX, anchorRatioY);

void Camera::SetAnchorRatio(float ratioX, float ratioY){
anchorRatio.Set(ratioX, ratioY);
anchorShift.Set(-currentWidth * anchorRatio.x, -currentHeight * anchorRatio.y);
lockedTarget = NULL;
}

// Resize the camera's viewport
Expand All @@ -41,8 +36,6 @@ void Camera::ResizeViewport(float w, float h){
currentHeight = h;

scale = oWidth/currentWidth;

SetAnchorRatio(anchorRatio.x, anchorRatio.y);
}

void Camera::ZoomTo(const Rectf& rect){
Expand All @@ -51,8 +44,6 @@ void Camera::ZoomTo(const Rectf& rect){

currentWidth = oWidth*scale;
currentHeight = oHeight*scale;

SetAnchorRatio(anchorRatio.x, anchorRatio.y);
}

void Camera::TweenTo(const Vec2f& tp){
Expand All @@ -74,7 +65,7 @@ void Camera::Move(const Vec2f& dis){
position += dis;
}

void Camera::Update(){
void Camera::Update(unsigned short delta){
if(lockedTarget != NULL){
TweenTo(lockedTarget->position());
}
Expand All @@ -85,7 +76,7 @@ void Camera::Setup(){
// scale, related the shifted position.
glScalef(scale, scale, 1.0f);
// do further shift translation.
glTranslatef(-anchorShift.x, -anchorShift.y, 0.0f);
glTranslatef(currentWidth * anchorRatio.x, currentHeight * anchorRatio.y, 0.0f);
// rotate about the position
glRotatef(rotation, 0.0f, 0.0f, 1.0f);
// move the camera to specified position, ready for rotation.
Expand Down
14 changes: 7 additions & 7 deletions Platformer/Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Camera
void ResizeViewport(float w, float h);

// update camera, such as, position, rotation...
void Update();
void Update(unsigned short delta);

// Setup the camera before we transform any models.
void Setup();
Expand All @@ -37,9 +37,14 @@ class Camera

float rotation;

Game* game;

// The ratio will be apply to the viewport width and height, and shift gluLookAt eye position and centre position.
// so camera is actually pointed to the (position + shift).
void SetAnchorRatio(float ratioX, float ratioY);
Vec2f anchorRatio;


//void SetAnchorRatio(float ratioX, float ratioY);

Vec2f GetViewportSize() const;

Expand All @@ -59,12 +64,7 @@ class Camera

float scale;

Vec2f anchorRatio;
Vec2f anchorShift;

// You can lock the camera to the target so the camera will always follows to it.
const Actor* lockedTarget;

Game* game;
};

68 changes: 68 additions & 0 deletions Platformer/GraphicalTile.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include "GraphicalTile.h"
#include "AIGraphics.h"
#include "AAnimation.h"
#include "AImage.h"

GraphicalTile::GraphicalTile(void): _rotation(0.0f)
{
_position.SetZero();
}

GraphicalTile::GraphicalTile(AImage* img): _rotation(0.0f){
// copy the image passed in. Since it can be stored in stack
_graphic_ptr = img;

_position.SetZero();
}

GraphicalTile::GraphicalTile(AAnimation* ani): _rotation(0.0f){
// copy the animation passed in
_graphic_ptr = ani;

_position.SetZero();
}

GraphicalTile::~GraphicalTile(void)
{
delete _graphic_ptr;
}

void GraphicalTile::SetAnimation(const AAnimation& ani){
_graphic_ptr = new AAnimation(ani);
}

void GraphicalTile::SetImage(const AImage& img){
_graphic_ptr = new AImage(img);
}

void GraphicalTile::Update(unsigned short delta){
_graphic_ptr->Update(delta);
}

void GraphicalTile::Draw(){
_graphic_ptr->Draw(_position, 0.0f, _rotation);
}

float GraphicalTile::rotation() const{
return _rotation;
}

void GraphicalTile::SetRotation(float r){
_rotation = r;
}

Vec2f& GraphicalTile::position(){
return _position;
}

void GraphicalTile::SetPosition(float x, float y){
_position.Set(x, y);
}

void GraphicalTile::SetPosition(const Vec2f& pos){
_position = pos;
}

AIGraphics* GraphicalTile::graphics(){
return _graphic_ptr;
}
Loading

0 comments on commit 071c010

Please sign in to comment.