From d1ef0c415c1442a2ed275cf04568f2546b6320b2 Mon Sep 17 00:00:00 2001 From: Nick Gasson Date: Fri, 5 Jun 2009 12:33:50 +0100 Subject: [PATCH] Tidy up AnimatedImage a bit --- src/AnimatedImage.cpp | 43 +++++++++++++++++++++++-------------------- src/AnimatedImage.hpp | 4 +++- src/Key.cpp | 11 +++-------- src/Key.hpp | 4 ++-- src/ObjectGrid.cpp | 2 +- src/ObjectGrid.hpp | 2 +- 6 files changed, 33 insertions(+), 33 deletions(-) diff --git a/src/AnimatedImage.cpp b/src/AnimatedImage.cpp index b88b03d..d2dfb10 100644 --- a/src/AnimatedImage.cpp +++ b/src/AnimatedImage.cpp @@ -20,7 +20,7 @@ AnimatedImage::AnimatedImage(const string& fileName, int frameWidth, int frameHeight, int frameCount) : Image(fileName), frameWidth(frameWidth), frameHeight(frameHeight), - frameCount(frameCount) + frameCount(frameCount), currFrame(0) { if (frameCount == 0) { if (Image::GetWidth() % frameWidth != 0) { @@ -33,13 +33,17 @@ AnimatedImage::AnimatedImage(const string& fileName, int frameWidth, } this->frameCount = FramesPerRow() * FramesPerCol(); } - currFrameX = 0; - currFrameY = 0; } -void AnimatedImage::Draw(int x, int y, double rotate, double scale, - double alpha, double white) const +// Draw a particular frame +void AnimatedImage::DrawFrame(int frame, int x, int y, double rotate, double scale, + double alpha, double white) const { + assert(frame >= 0 && frame < frameCount); + + int frameX = frame % FramesPerRow(); + int frameY = frame / FramesPerRow(); + int width = Image::GetWidth(); int height = Image::GetHeight(); @@ -52,10 +56,10 @@ void AnimatedImage::Draw(int x, int y, double rotate, double scale, glRotated(rotate, 0.0, 0.0, 1.0); glColor4d(white, white, white, alpha); - double tex_l = ((double)(currFrameX * frameWidth))/(double)width; + double tex_l = ((double)(frameX * frameWidth))/(double)width; double tex_r = tex_l + (double)frameWidth/(double)width; - double tex_t = ((double)(currFrameY * frameHeight))/(double)height; + double tex_t = ((double)(frameY * frameHeight))/(double)height; double tex_b = tex_t + (double)frameHeight/(double)height; glBegin(GL_QUADS); @@ -66,6 +70,13 @@ void AnimatedImage::Draw(int x, int y, double rotate, double scale, glEnd(); } +// Draw the current frame +void AnimatedImage::Draw(int x, int y, double rotate, double scale, + double alpha, double white) const +{ + DrawFrame(currFrame, x, y, rotate, scale, alpha, white); +} + int AnimatedImage::FramesPerRow() const { return Image::GetWidth() / frameWidth; @@ -78,27 +89,19 @@ int AnimatedImage::FramesPerCol() const void AnimatedImage::NextFrame() { - currFrameX = (currFrameX + 1) % FramesPerRow(); - if (currFrameX == 0) - currFrameY = (currFrameY + 1) % FramesPerCol(); - if (GetFrame() >= frameCount) { - currFrameX = 0; - currFrameY = 0; - } + currFrame = (currFrame + 1) % frameCount; } void AnimatedImage::SetFrame(int f) { - if (f >= frameCount) + if (f < 0 || f >= frameCount) throw runtime_error("SetFrame frame out of range"); - else { - currFrameX = f % FramesPerRow(); - currFrameY = f / FramesPerRow(); - } + else + currFrame = f; } int AnimatedImage::GetFrame() const { - return (currFrameY * FramesPerRow()) + currFrameX; + return currFrame; } diff --git a/src/AnimatedImage.hpp b/src/AnimatedImage.hpp index ae82c25..3675134 100644 --- a/src/AnimatedImage.hpp +++ b/src/AnimatedImage.hpp @@ -27,6 +27,8 @@ public: void Draw(int x, int y, double rotate=0.0, double scale=1.0, double alpha=1.0, double white=1.0) const; + void DrawFrame(int frame, int x, int y, double rotate=0.0, double scale=1.0, + double alpha=1.0, double white=1.0) const; void NextFrame(); void SetFrame(int f); @@ -38,7 +40,7 @@ private: int FramesPerCol() const; int frameWidth, frameHeight, frameCount; - int currFrameX, currFrameY; + int currFrame; }; #endif diff --git a/src/Key.cpp b/src/Key.cpp index 2cbd9b3..a1dfb80 100644 --- a/src/Key.cpp +++ b/src/Key.cpp @@ -46,7 +46,7 @@ void Key::DrawKey(Viewport* viewport) alpha -= 0.02f; } -void Key::DrawArrow(Viewport* viewport) +void Key::DrawArrow(Viewport* viewport) const { if (active && !ObjectInScreen(viewport)) { int ax = xpos*OBJ_GRID_SIZE - viewport->GetXAdjust(); @@ -77,15 +77,10 @@ void Key::DrawArrow(Viewport* viewport) } } -void Key::DrawIcon(int offset, float minAlpha) +void Key::DrawIcon(int offset, float minAlpha) const { - const int prevFrame = image.GetFrame(); - image.SetFrame(5); - double drawAlpha = alpha > minAlpha ? alpha : minAlpha; - image.Draw(offset, 10, 0.0, 1.0, drawAlpha); - - image.SetFrame(prevFrame); + image.DrawFrame(5, offset, 10, 0.0, 1.0, drawAlpha); } bool Key::CheckCollision(Ship& ship) const diff --git a/src/Key.hpp b/src/Key.hpp index 3f534f6..5397901 100644 --- a/src/Key.hpp +++ b/src/Key.hpp @@ -29,8 +29,8 @@ public: Key(bool active, int xpos, int ypos, ArrowColour acol); void DrawKey(Viewport* viewport); - void DrawArrow(Viewport* viewport); - void DrawIcon(int offset, float minAlpha); + void DrawArrow(Viewport* viewport) const; + void DrawIcon(int offset, float minAlpha) const; bool CheckCollision(Ship& ship) const; void Collected() { active = false; } diff --git a/src/ObjectGrid.cpp b/src/ObjectGrid.cpp index e4618bf..36b59f5 100644 --- a/src/ObjectGrid.cpp +++ b/src/ObjectGrid.cpp @@ -138,7 +138,7 @@ void ObjectGrid::Offset(int ox, int oy, int* x, int* y) *y = OBJ_GRID_TOP + oy*OBJ_GRID_SIZE; } -bool StaticObject::ObjectInScreen(Viewport* viewport) +bool StaticObject::ObjectInScreen(Viewport* viewport) const { return viewport->ObjectInScreen(xpos, ypos, width, height); } diff --git a/src/ObjectGrid.hpp b/src/ObjectGrid.hpp index 4a3320f..1a85595 100644 --- a/src/ObjectGrid.hpp +++ b/src/ObjectGrid.hpp @@ -54,7 +54,7 @@ public: StaticObject() { StaticObject(0, 0, 1, 1); } virtual ~StaticObject() {} - bool ObjectInScreen(Viewport* viewport); + bool ObjectInScreen(Viewport* viewport) const; int GetX() const { return xpos; } int GetY() const { return ypos; } -- 2.39.2