{"id":123,"date":"2006-08-19T11:44:57","date_gmt":"2006-08-19T15:44:57","guid":{"rendered":"http:\/\/www.rakkar.org\/blog\/?p=123"},"modified":"2006-08-19T11:44:57","modified_gmt":"2006-08-19T15:44:57","slug":"a-well-designed-class","status":"publish","type":"post","link":"https:\/\/rakkar.org\/blog\/index.php\/2006\/08\/19\/a-well-designed-class\/","title":{"rendered":"Test your C++ architecture skills"},"content":{"rendered":"<p>\t\t\t\t<b>Here&#8217;s the problem:<\/b><br \/>\nOften in games when text shows up on the screen it doesn&#8217;t just pop in and pop out.  Instead, it will fade in, display for some time, and fade out.<br \/>\nCEGUI (my GUI library) doesn&#8217;t support this.  So I have to write something myself.<\/p>\n<p><b>Solution:<\/b><br \/>\nObviously the solution is to modify the alpha of the string.<\/p>\n<p><b>My question for you:<\/b><br \/>\n&#8220;What is the best way to architect this?&#8221;  Composition?  Inheritance?  Modify the string class? Write a string manager? Something else?  This is non-trivial.<\/p>\n<p><b>Think about your answer, then scroll down<\/b><\/p>\n<p>.<\/p>\n<p>.<\/p>\n<p>.<\/p>\n<p>.<\/p>\n<p>.<\/p>\n<p>If you&#8217;ve read my previous posts, the best architecture has small units of functionality that do the minimum possible while accomplishing the task at hand.<\/p>\n<p>Classes which are well designed<br \/>\nA. Have loose coupling, which means that they have generic inputs and outputs.<br \/>\nB. Have tight cohesion, which mean they don&#8217;t rely on other systems to do the work they are responsible for.<\/p>\n<p><b>Think again what you would do, then scroll down for what I did:<\/b><\/p>\n<p>.<\/p>\n<p>.<\/p>\n<p>.<\/p>\n<p>.<\/p>\n<p>.<\/p>\n<p><b>Header file:<\/b><br \/>\n<code><br \/>\n#ifndef __FADE_CONTROLLER_H<br \/>\n#define __FADE_CONTROLLER_H<\/p>\n<p>\/\/ Assumes the object being faded has an alpha.<br \/>\n\/\/ This handles the math of fading in and out.<br \/>\n\/\/ Just have an instance of this class composited with whatever you want faded.  Then multiply GetAlpha by the alpha of the object.<br \/>\n\/\/ Call Update with the elapsed time since the last call to Update<br \/>\n\/\/ You can freely change the member variables of the class<br \/>\nstruct FadeController<br \/>\n{<br \/>\n\tFadeController();<br \/>\n\t~FadeController();<br \/>\n\tfloat GetAlpha(void) const;<br \/>\n\tbool IsExpired(void) const;<br \/>\n\tvoid Update(unsigned elapsedTime);<\/p>\n<p>\t\/\/ If fadeInTime < elapsedTime, the percentile elapsed will be used as alpha\n\tunsigned int fadeInTimeMS;\n\t\/\/ If lifeTime!=0 &#038;&#038; lifeTime < elapsedTime then this string will be removed from AlphaTextList\n\tunsigned int lifeTimeMS;\n\t\/\/ Total MS elapsed\n\tunsigned int elapsedTimeMS;\n\t\/\/ Will calculate alpha as the remaining time\n\tunsigned int fadeOutTimeMS;\n};\n\n#endif\n<\/code><\/p>\n<p><b>Source file:<\/b><br \/>\n<\/code><code><br \/>\n#include \"FadeController.h\"<\/p>\n<p>FadeController::FadeController()<br \/>\n{<br \/>\n}<br \/>\nFadeController::~FadeController()<br \/>\n{<br \/>\n}<br \/>\nfloat FadeController::GetAlpha(void) const<br \/>\n{<br \/>\n\tif (elapsedTimeMS < fadeInTimeMS)\n\t\treturn (float) elapsedTimeMS \/ (float) fadeInTimeMS;\n\telse if (lifeTimeMS!=0)\n\t{\n\t\tif (elapsedTimeMS < fadeOutTimeMS)\n\t\t\treturn 1.0f;\n\t\telse if (elapsedTimeMS < lifeTimeMS)\n\t\t\treturn (float) (lifeTimeMS-fadeOutTimeMS) \/(float) (lifeTimeMS-elapsedTimeMS);\n\t\treturn 0.0f;\n\t}\n\telse\n\t\treturn 1.0f;\n}\n\nvoid FadeController::Update(unsigned elapsedTime)\n{\n\telapsedTimeMS+=elapsedTime;\n}\nbool FadeController::IsExpired(void) const\n{\n\treturn lifeTimeMS!=0 &#038;&#038; elapsedTimeMS > lifeTimeMS;<br \/>\n}<br \/>\n<\/code><\/p>\n<p>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.<\/p>\n<p>Just composite this class with whatever you want faded and hook it in with two lines of code.<\/p>\n<p>If you have come up with a better solution, post it in the comments.\t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s the problem: Often in games when text shows up on the screen it doesn&#8217;t just pop in and pop out. Instead, it will fade in, display for some time, and fade out. CEGUI (my GUI library) doesn&#8217;t support this. So I have to write something myself. Solution: Obviously the solution is to modify the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/rakkar.org\/blog\/index.php\/wp-json\/wp\/v2\/posts\/123"}],"collection":[{"href":"https:\/\/rakkar.org\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rakkar.org\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rakkar.org\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rakkar.org\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=123"}],"version-history":[{"count":0,"href":"https:\/\/rakkar.org\/blog\/index.php\/wp-json\/wp\/v2\/posts\/123\/revisions"}],"wp:attachment":[{"href":"https:\/\/rakkar.org\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=123"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rakkar.org\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=123"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rakkar.org\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}