Skip to content

Commit

Permalink
Make a few pure functions in GameObject non-pure.
Browse files Browse the repository at this point in the history
There's no reason to force derived classes to implement draw() and
update(). We may as well just provide "do nothing" default
implementations for objects that don't care about the passage of time
or are not drawing anything.

Also fix up the types of draw() arguments - references are nicer than
pointers here since they can't be null.
  • Loading branch information
jjuhl committed Jan 23, 2012
1 parent 5bc2fcd commit 2cddf3d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
28 changes: 15 additions & 13 deletions playstate.cc
Expand Up @@ -182,15 +182,17 @@ void Board::draw(SDL_Surface* screen)
if (!*it)
continue;

(*it)->draw(&surf, &srect);
(*it)->draw(surf, srect);
centerDraw(*it, srect, drect);
SDL_BlitSurface(surf, &srect, screen, &drect);
if (surf)
SDL_BlitSurface(surf, &srect, screen, &drect);
}

// Draw the player on top
m_player->draw(&surf, &srect);
m_player->draw(surf, srect);
centerDraw(m_player, srect, drect);
SDL_BlitSurface(surf, &srect, screen, &drect);
if (surf)
SDL_BlitSurface(surf, &srect, screen, &drect);
}

std::vector<std::pair<Uint16, Uint16> > Board::freeTiles() const
Expand Down Expand Up @@ -325,10 +327,10 @@ void Block::update(Uint32 delta_time)
}
}

void Block::draw(SDL_Surface** surface, SDL_Rect* rect)
void Block::draw(SDL_Surface*& surface, SDL_Rect& rect)
{
*surface = m_current_frame;
*rect = m_current_frame_rect;
surface = m_current_frame;
rect = m_current_frame_rect;
}

void Block::collision(GameObject* other)
Expand Down Expand Up @@ -368,11 +370,11 @@ void Wall::update(Uint32)
{
}

void Wall::draw(SDL_Surface** surface, SDL_Rect* rect)
void Wall::draw(SDL_Surface*& surface, SDL_Rect& rect)
{
*surface = m_current_frame;
surface = m_current_frame;
SDL_Rect r = m_current_frame_rect;
*rect = r;
rect = r;
}

void Wall::collision(GameObject* other)
Expand Down Expand Up @@ -493,7 +495,7 @@ void Player::update(Uint32 delta_time)
m_time_since_move = 0;
}

void Player::draw(SDL_Surface** surface, SDL_Rect* rect)
void Player::draw(SDL_Surface*& surface, SDL_Rect& rect)
{
// clear our frame surface
Uint32 col = SDL_MapRGBA(m_frame->format, 0, 0, 0, 0);
Expand Down Expand Up @@ -556,8 +558,8 @@ void Player::draw(SDL_Surface** surface, SDL_Rect* rect)
r.y = 0;
r.w = m_frame->w;
r.h = m_frame->h;
*rect = r;
*surface = m_frame;
rect = r;
surface = m_frame;
}

void Player::setEffects(const Effect& e)
Expand Down
14 changes: 9 additions & 5 deletions playstate.hh
Expand Up @@ -67,14 +67,18 @@ class GameObject {
public:
virtual ~GameObject() { }

virtual void update(Uint32 delta_time) = 0;
virtual void update(Uint32 delta_time) { }
// This draw method is called by the Board class when drawing. This
// method should not do any actual drawing itself, just return a
// pointer to the surface to be used a source and a rectangle
// describing the area of that surface the GameObject wishes to have
// drawn. The Board class then takes care of centering this on the
// tile the object occupies and do the actual drawing.
virtual void draw(SDL_Surface** surface, SDL_Rect* rect) = 0;
virtual void draw(SDL_Surface*& surface, SDL_Rect& rect)
{
surface = 0;
rect.x = rect.y = rect.w = rect.h = 0;
}

virtual Uint16 x() const { return m_x; }
virtual Uint16 y() const { return m_y; }
Expand Down Expand Up @@ -110,7 +114,7 @@ public:
BLOCK_COLOR color() { return m_col; }

virtual void update(Uint32 delta_time);
virtual void draw(SDL_Surface** surface, SDL_Rect* rect);
virtual void draw(SDL_Surface*& surface, SDL_Rect& rect);
virtual void collision(GameObject* other);
private:
Block(const Block&);
Expand All @@ -129,7 +133,7 @@ public:
virtual ~Wall();

virtual void update(Uint32 delta_time);
virtual void draw(SDL_Surface** surface, SDL_Rect* rect);
virtual void draw(SDL_Surface*& surface, SDL_Rect& rect);
virtual void collision(GameObject*);

virtual bool isBlocked() { return true; }
Expand All @@ -154,7 +158,7 @@ public:
~Player();

void update(Uint32 delta_time);
void draw(SDL_Surface** surface, SDL_Rect* rect);
void draw(SDL_Surface*& surface, SDL_Rect& rect);

void goUp() { m_direction = UP; }
void goDown() { m_direction = DOWN; }
Expand Down

0 comments on commit 2cddf3d

Please sign in to comment.