Categories
Uncategorized

Test your C++ architecture skills

Here’s the problem: Often in games when text shows up on the screen it doesn’t just pop in and pop out. Instead, it will fade in, display for some time, and fade out. CEGUI (my GUI library) doesn’t support this. So I have to write something myself. Solution: Obviously the solution is to modify the […]

Here’s the problem:
Often in games when text shows up on the screen it doesn’t just pop in and pop out. Instead, it will fade in, display for some time, and fade out.
CEGUI (my GUI library) doesn’t support this. So I have to write something myself.

Solution:
Obviously the solution is to modify the alpha of the string.

My question for you:
“What is the best way to architect this?” Composition? Inheritance? Modify the string class? Write a string manager? Something else? This is non-trivial.

Think about your answer, then scroll down

.

.

.

.

.

If you’ve read my previous posts, the best architecture has small units of functionality that do the minimum possible while accomplishing the task at hand.

Classes which are well designed
A. Have loose coupling, which means that they have generic inputs and outputs.
B. Have tight cohesion, which mean they don’t rely on other systems to do the work they are responsible for.

Think again what you would do, then scroll down for what I did:

.

.

.

.

.

Header file:

#ifndef __FADE_CONTROLLER_H
#define __FADE_CONTROLLER_H

// Assumes the object being faded has an alpha.
// This handles the math of fading in and out.
// Just have an instance of this class composited with whatever you want faded. Then multiply GetAlpha by the alpha of the object.
// Call Update with the elapsed time since the last call to Update
// You can freely change the member variables of the class
struct FadeController
{
FadeController();
~FadeController();
float GetAlpha(void) const;
bool IsExpired(void) const;
void Update(unsigned elapsedTime);

// If fadeInTime < elapsedTime, the percentile elapsed will be used as alpha unsigned int fadeInTimeMS; // If lifeTime!=0 && lifeTime < elapsedTime then this string will be removed from AlphaTextList unsigned int lifeTimeMS; // Total MS elapsed unsigned int elapsedTimeMS; // Will calculate alpha as the remaining time unsigned int fadeOutTimeMS; }; #endif

Source file:

#include "FadeController.h"

FadeController::FadeController()
{
}
FadeController::~FadeController()
{
}
float FadeController::GetAlpha(void) const
{
if (elapsedTimeMS < fadeInTimeMS) return (float) elapsedTimeMS / (float) fadeInTimeMS; else if (lifeTimeMS!=0) { if (elapsedTimeMS < fadeOutTimeMS) return 1.0f; else if (elapsedTimeMS < lifeTimeMS) return (float) (lifeTimeMS-fadeOutTimeMS) /(float) (lifeTimeMS-elapsedTimeMS); return 0.0f; } else return 1.0f; } void FadeController::Update(unsigned elapsedTime) { elapsedTimeMS+=elapsedTime; } bool FadeController::IsExpired(void) const { return lifeTimeMS!=0 && elapsedTimeMS > lifeTimeMS;
}

Seems very simple right? But consider that there are NO external dependencies. This will link fast, can work with any game, and in fact overengineers the problem beecause it can work with things other than strings. For example, shrapnel could fade out too.

Just composite this class with whatever you want faded and hook it in with two lines of code.

If you have come up with a better solution, post it in the comments.

One reply on “Test your C++ architecture skills”

This is pretty good. It’s quite like “component based design” – this FadeController is just a component that “fades stuff”. You have lots of such small, easily-reusable components and the actual “things” are just composited of them. When such a system is done right, it can be unstoppable 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *