3D游戏编程高级C++技术.pdf
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_1.gif)
![资源得分’ title=](/images/score_05.gif)
《3D游戏编程高级C++技术.pdf》由会员分享,可在线阅读,更多相关《3D游戏编程高级C++技术.pdf(65页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Advanced C+Programming Yanci Zhang Game Programming IIGame Programming II Game Programming IIGame Programming II Do You Really Know Coding?在在一个伸不见五指的晚上,池塘的蝌蚪在一个伸不见五指的晚上,池塘的蝌蚪在晒太阳。晒太阳。我跌了一跤,好痛,觉得很伤心,露出黯然神伤的我跌了一跤,好痛,觉得很伤心,露出黯然神伤的神情。神情。老师老师有一张有一张爪子脸。爪子脸。for(int i=0;i100;i+)int Sum=0;Sum+=i;轻轻的我走了,正如我轻轻
2、的来;轻轻的我走了,正如我轻轻的来;我我轻轻的招手,作别轻轻的招手,作别西天的云彩。西天的云彩。那那河畔的金柳,是夕阳中的新娘;河畔的金柳,是夕阳中的新娘;波光波光里的艳影,在我里的艳影,在我的心头荡漾。的心头荡漾。软泥软泥上的青荇,油油的在水底招摇;上的青荇,油油的在水底招摇;在在康河的柔波里,康河的柔波里,我甘心做一条水草!我甘心做一条水草!那那榆荫下的一潭,不是清泉,榆荫下的一潭,不是清泉,是是天上虹揉碎在浮藻间,天上虹揉碎在浮藻间,沉淀着彩虹似的梦。沉淀着彩虹似的梦。今年今年中秋节,恰逢农历八月十五,黄昏的傍晚中秋节,恰逢农历八月十五,黄昏的傍晚,我,我和和弟弟,弟弟也和我,我俩站在院
3、子中央,抬着头,仰弟弟,弟弟也和我,我俩站在院子中央,抬着头,仰脖看那天上的月亮,我说:“看脖看那天上的月亮,我说:“看 哪,这边的月亮升起哪,这边的月亮升起来了!”弟弟说:“看哪,那边的月亮也升起来了!”来了!”弟弟说:“看哪,那边的月亮也升起来了!”这时,在院子里的全家人,也包括爸爸、妈妈、哥哥、这时,在院子里的全家人,也包括爸爸、妈妈、哥哥、姐姐、弟弟、妹妹几乎全都异口同姐姐、弟弟、妹妹几乎全都异口同 声地一起喊道:声地一起喊道:“看哪,东边的月亮真的升起来了!“看哪,东边的月亮真的升起来了!”int sum(int*array,int n)int result=0;for(int i=
4、0;i n;+i)result=result+arrayi;return result;float sum(float*array,int n)float result=0;for(int i=0;i n;+i)result=result+arrayi;return result;All systems change during their life cycles How can we create designs that are stable in face of change?Nightmare:a single change to a program results in a cas
5、cade of changes to dependent modules Bad design,fragile,rigid,unpredictable,unreusable Game Programming IIGame Programming II How to Handle Change?Software entities(classes,modules,functions,etc.)should be open for extension,but closed for modification You should design modules that never change!Whe
6、n requirements change Extend modules behavior by adding new code instead of changing old code that already works Game Programming IIGame Programming II Open-Closed Principle Open for extension Behavior of module can be extended Make module behave in new and different ways as requirements change or t
7、o meet new needs Closed for modification Source code of such module is inviolate Two opposing features?Game Programming IIGame Programming II Features Abstraction is the Key!Create fixed abstractions presenting an unbounded group of possible behaviors Abstractions are abstract base class Unbounded b
8、ehaviors is represented by derivative classes Handle changes by adding new code without changing existing code Game Programming IIGame Programming II Solution Game Programming IIGame Programming II Example enum ShapeType circle,square;struct Shape ShapeType itsType;struct Circle ShapeType itsType;do
9、uble itsRadius;Point itsCenter;struct Square ShapeType itsType;double itsSide;Point itsTopLeft;void DrawSquare(struct Square*)void DrawCircle(struct Circle*);typedef struct Shape*ShapePointer;void DrawAllShapes(ShapePointer list,int n)int i;for(i=0;iitsType)case square:DrawSquare(struct Square*)s);b
10、reak;case circle:DrawCircle(struct Circle*)s);break;Work perfectly if there are only two types of shape Are we satisfied?Not conform to open-closed principle It is not closed against new kinds of shape DrawAllShapes()has to be modified for any new type of shape Game Programming IIGame Programming II
11、 Take A Closer Look Game Programming IIGame Programming II Better Solution class Shape public:virtual void Draw()const=0;/pure virtual function;class Square:public Shape public:virtual void Draw()const;class Circle:public Shape public:virtual void Draw()const;void DrawAllShapes(Set&list)for(Iterator
12、i(list);i;i+)(*i)-Draw();Conform to open-closed principle If we want to add a new type of shape Add a new derivative of class Shape DrawAllShapes()does not need to change Is it completely closed?What if we decide that all Circles should be drawn before any Squares?Game Programming IIGame Programming
13、 II Take A Closer Look No program can be 100%closed No matter how“closed”a module is,there always be some kind of change against which it is not closed Closure must be strategic Experienced designer can make sure that open-closed principle is invoked for the most probable changes What if I am a newb
14、ie?Your weapon:code refactor Start with na ve design but keep refactoring code once open-closed principle is broke Game Programming IIGame Programming II Welcome to Real World A well-known OOD rule:all member variables should be declared private,rather than public or protected From the view of open-
15、closed principle We expect that methods of class are not closed to changes in member variables of that class We do expect that any other class,including subclasses are closed against changes to those variables Game Programming IIGame Programming II Member Variables Initial requirement:car should hav
16、e velocity property New requirement:cars velocity should not be larger than some maximum value Modify your code everywhere to adapt the new requirement It is really painful,I mean it!Game Programming IIGame Programming II Example class Car public:double m_Velocity;void func1()Car MyCar;MyCar.m_Veloc
17、ity=100.0;void func2()Car MyCar;MyCar.m_Velocity=200.0;Process of recording events,with an automated computer program Provide an audit trail that can be used to understand activity of system and to diagnose problems Requirements Multiple levels:fatal error,normal error,warning,information May have m
18、ultiple output targets:screen,file Game Programming IIGame Programming II Example:Event Log Example:initialize a network server Initial solution Output logs to screen No support for multiple levels Game Programming IIGame Programming II Initial Solution int main()printf(“Server runing.n”);WSADATA ws
19、aData;int rc=WSAStartup(MAKEWORD(2,0),&wsaData);if(rc!=0)printf(“Failed to start winsockn”);return 1;/initialize network printf(WSAStartup call Success!n);Game Programming IIGame Programming II Add Multiple Levels Support#define LOG_DEBUG 0#define LOG_INFO 1#define LOG_ERR 2#define LOG_NOPRINT 3 int
20、 CurPriority;int main()if(CurPriority=LOG_INFO)printf(“Server runing.n”);WSADATA wsaData;int rc=WSAStartup(MAKEWORD(2,0),&wsaData);if(rc!=0)if(CurPriority=LOG_ERR)printf(“Failed to start winsockn”);return 1;/initialize network if(CurPriority=LOG_ERR)printf(“Failed to start winsockn”);if(CurPriority=
21、LOG_ERR)printToFile(Failed to start winsockn);printf(“Failed to start winsockn”);If thousands of files contain such printf()Fork on your career road Route 1:toward coding slave,replace code Route 2:toward advanced programmer,write a function!Game Programming IIGame Programming II Are You Sure?void P
22、rintLog(int Priority,int curPriority,char*log_msg)if(CurPriority=priority)printToFile(log_msg);printToScreen(log_msg);Game Programming IIGame Programming II A More Delicate Version int main()int CurPriority=0;CurPriority=GetPriority();PrintLog(LOG_INFO,CurPriority,”Server runing.n”);WSADATA wsaData;
23、int rc=WSAStartup(MAKEWORD(2,0),&wsaData);if(rc!=0)PrintLog(LOG_ERR,CurPriority,”Failed to start winsockn”);return 1;PrintLog(LOG_DEBUG,CurPriority,”Failed to start winsockn”);I am genius,no matter where you want to output logs,I only have to change my PrintLog()function!If you want to output to net
24、work Game Programming IIGame Programming II A More Delicate Version void PrintLog(int Priority,int curPriority,char*log_msg)if(CurPriority=priority)printToFile(log_msg);printToScreen(log_msg);printToNetWork(log_msg);Customer 1:I only want to output to screen Customer 2:I want to output to screen and
25、 network Customer 3:I want to output to file and network Game Programming IIGame Programming II Evil Customer void PrintLog()if(CurPriority=priority)/printToFile(log_msg);printToScreen(log_msg);/printToNetWork(log_msg);void PrintLog()if(CurPriority=priority)/printToFile(log_msg);printToScreen(log_ms
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 游戏 编程 高级 C+ 技术
![提示](https://www.taowenge.com/images/bang_tan.gif)
限制150内