欢迎来到淘文阁 - 分享文档赚钱的网站! | 帮助中心 好文档才是您的得力助手!
淘文阁 - 分享文档赚钱的网站
全部分类
  • 研究报告>
  • 管理文献>
  • 标准材料>
  • 技术资料>
  • 教育专区>
  • 应用文书>
  • 生活休闲>
  • 考试试题>
  • pptx模板>
  • 工商注册>
  • 期刊短文>
  • 图片设计>
  • ImageVerifierCode 换一换

    3D游戏编程高级C++技术.pdf

    • 资源ID:70013180       资源大小:6.29MB        全文页数:65页
    • 资源格式: PDF        下载积分:15金币
    快捷下载 游客一键下载
    会员登录下载
    微信登录下载
    三方登录下载: 微信开放平台登录   QQ登录  
    二维码
    微信扫一扫登录
    下载资源需要15金币
    邮箱/手机:
    温馨提示:
    快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
    如填写123,账号就是123,密码也是123。
    支付方式: 支付宝    微信支付   
    验证码:   换一换

     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    3D游戏编程高级C++技术.pdf

    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;轻轻的我走了,正如我轻轻的来;轻轻的我走了,正如我轻轻的来;我我轻轻的招手,作别轻轻的招手,作别西天的云彩。西天的云彩。那那河畔的金柳,是夕阳中的新娘;河畔的金柳,是夕阳中的新娘;波光波光里的艳影,在我里的艳影,在我的心头荡漾。的心头荡漾。软泥软泥上的青荇,油油的在水底招摇;上的青荇,油油的在水底招摇;在在康河的柔波里,康河的柔波里,我甘心做一条水草!我甘心做一条水草!那那榆荫下的一潭,不是清泉,榆荫下的一潭,不是清泉,是是天上虹揉碎在浮藻间,天上虹揉碎在浮藻间,沉淀着彩虹似的梦。沉淀着彩虹似的梦。今年今年中秋节,恰逢农历八月十五,黄昏的傍晚中秋节,恰逢农历八月十五,黄昏的傍晚,我,我和和弟弟,弟弟也和我,我俩站在院子中央,抬着头,仰弟弟,弟弟也和我,我俩站在院子中央,抬着头,仰脖看那天上的月亮,我说:“看脖看那天上的月亮,我说:“看 哪,这边的月亮升起哪,这边的月亮升起来了!”弟弟说:“看哪,那边的月亮也升起来了!”来了!”弟弟说:“看哪,那边的月亮也升起来了!”这时,在院子里的全家人,也包括爸爸、妈妈、哥哥、这时,在院子里的全家人,也包括爸爸、妈妈、哥哥、姐姐、弟弟、妹妹几乎全都异口同姐姐、弟弟、妹妹几乎全都异口同 声地一起喊道:声地一起喊道:“看哪,东边的月亮真的升起来了!“看哪,东边的月亮真的升起来了!”int sum(int*array,int n)int result=0;for(int i=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 cascade 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!When 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 to 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 behaviors 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;double 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);break;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 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(Iteratori(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 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 newbie?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-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 have 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_Velocity=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 multiple 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 wsaData;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 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=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 PrintLog(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;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 network 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 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_msg);printToNetWork(log_msg);void PrintLog()if(CurPriority=priority)printToFile(log_msg);/printToScreen(log_msg);printToNetWork(log_msg);Customer 1:Why I cannot see logs on screen?Customer 2:Why I cannot see logs on network?Customer 3:Why I cannot see logs in file?Sorry,I give you a wrong version It is a bad idea to maintain multiple versions of same software Game Programming IIGame Programming II Evil Customer Fork on your career road again Route 1:advanced coding slave Route 2:designer/architect Game Programming IIGame Programming II Further Improvement class CLog public:void addTarget(ITarget*vTarget);private:std:vector m_TargetList;class ITarget public:virtual void writeLog(LogMsg&vMsg)=0;Algorithms are written in terms of to-be-specified-later types Instantiated when needed for specific types provided as parameters Best examples:Standard Template Library(STL)Decouple sequence data structures and algorithms operating on them Game Programming IIGame Programming II Overview:Generic Programming Game Programming IIGame Programming II A Simple Example int sum(int*array,int n)int result=0;for(int i=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;The first and most important part of the Generic Programming process Seeks to discover a generic algorithm by answering the following fundamental question What are the minimal requirements that my data types need to fulfill for the algorithm to operate correctly and efficiently?Game Programming IIGame Programming II Lifting The first and most important part of the Generic Programming process Game Programming IIGame Programming II Example of Lifting template T sum(T*array,int n)T result=0;/need to be able to initialize to 0 for(int i=0;i n;+i)result=result+arrayi;/supports addition and assignment operations return result;/requires a copy constructor Game Programming IIGame Programming II More Lifting 1/2 template T sum(T*array,int n)T result=0;for(int i=0;i n;+i)result=result+arrayi;return result;std:string sum(std:string*array,int n)std:string result=“”;for(int i=0;i n;+i)result=result+arrayi;return result;Game Programming IIGame Programming II More Lifting 2/2 template T sum(T*array,int n)T result=T();/T must have a default constructor producing the identity value for(int i=0;i n;+i)result=result+arrayi;/supports addition and assignment operations return result;/requires a copy constructor Game Programming IIGame Programming II Another Simple Example A function to return bigger one from two input parameters If the data type of input parameters is integer Write different functions for float,double,short,char,even a strcuture?int max(int x,int y)return x y?x:y;Game Programming IIGame Programming II Another Simple Example Template is useful here Extra advantages:safer int a=3,b=7;double x=3.14,y=2.71;cout max(a,b)endl;/Instantiated with type int cout max(x,y)endl;/Instantiated with type double template T max(T x,T y)return x y?y:x;cout max(x,b)endl;/compile error Game Programming IIGame Programming II STL STL:Standard Template Library C+software library evolved into C+standard library Provides four components:algorithms,containers,functors,iterators Implements the most common data structures and algorithms dynamic array,list,stack,queue,map,hash table search,sort Applicable to almost all data types Supported by almost all C+compilers Game Programming IIGame Programming II Container Objects holding other templated objects Two main types Sequence container Linear list Example:vector,queue,list Associative container Store key/value pairs Allow efficient retrieval of values based on keys Example:map,multimap,set,multiset Each container defines meaningful functions Game Programming IIGame Programming II Container Container Description Required Header deque A double ended queue list A linear list map Stores key/value pairs multimap Stores key/value pairs multiset A set in which each element may not be unique priority_queue A priority queue queue A queue set A set of unique elements stack A stack vector A dynamic array Game Programming IIGame Programming II Iterator Five types Input:can only be used to read Output:can only be used to write Forward:can be read,written and move forward Bidirectional:move forward and backward Random access:move freely Bridge between containers and algorithms Major feature allowing generality of STL Generality comes at a price of efficiency Game Programming IIGame Programming II Algorithm Perform operations such as searching and sorting Work on containers through iterators Game Programming IIGame Programming II Vector The most common used container Implement dynamic array Dynamically allocate and delete memory is done by STL vector cv1;/create zero-length char vector vector iv1(10);/create 10 element int vector vector cv2(5,a);/initialize a 5 element char vector vector iv2(iv1);/create int vector from an int vector Game Programming IIGame Programming II Useful Member Functions Member Description iterator begin()Returns an iterator to the first element in the vector.iterator end()Returns an iterator to the end of the vector.bool empty()Returns true if the vector contains no elements.iterator erase(iterator i)Removes the element pointed to by i.Iterator insert(iterator i,constT&v)Inserts v immediately before element specified by i void push_back(const T&v)Adds element with value v to the end of the vector.size_type size()Returns the number of elements in the vector.operator Access vector elements using standard array subscripting notation.Game Programming IIGame Programming II Example vector v(10);/create vector of length 10 printf(“Size=%in”,v.size();/display original size for(i=0;i10;i+)vi=i;/Assign elements values for(i=0;i10;i+)printf(“%i“,v*i+);/display elements for(i=0;i5;i+)/Add 5 elements to vector v.push_back(i+10);printf(“New size=%in”,v.size();/display new size for(i=0;i10;i+)vi=-vi;/Change element values OUTPUT:Size=10 0 1 2 3 4 5 6 7 8 9 New size=15 Game Programming IIGame Programming II Tips Size of vector can only be increased using push_back()or insert()It can NOT be increased by using the normal array subscription notation Using reserve()to gain performance if you roughly know the size of array vector v(10);v12=10;/wrong!Game Programming IIGame Programming II Iterator of Vector vector v;for(i=0;i10;i+)v.push_back(i);/Assign 10 new elements vector:iterator p=v.begin();while(p!=v.end()printf(%i,*p);/Print elements forwards p+;p=v.end();while(p!=v.begin()p-;printf(%i,*p);/Print elements backwards Print elements forwards:0 1 2 3 4 5 6 7 8 9 Print elements backwards:9 8 7 6 5 4 3 2 1 0 Game Programming IIGame Programming II Execute Algorithm on Vector int a=1,2,3,2,3,2,1,2;vector v1(a,a+8);vector v3(4);list v2(8);i=count(v1.begin(),v1.end(),2)cout Count#of 2s in v1=i endl;copy(v1.begin(),v1.end(),v2.begin();remove_copy(v1.begin(),v1.end(),v3.begin(),2);vector:iterator p=v3.begin();cout “v3:”;while(p!=v3.end()cout *p+;Count#of 2s in v1=4 v3:1 3 3 1 Game Programming IIGame Programming II What is Boost?Strong and free C+source libraries Founded in 1998 by Beman Dawes,a member of C+Standards Committee Many of the people involved with development of C+standard are involved with Boost Proving ground for new C+libraries Extension of STL Contain over 80 individual libraries Home page:http:/www.boost.org Game Programming IIGame Programming II Evil Pointer Problems of raw C/C+pointers Allocate memory on heap

    注意事项

    本文(3D游戏编程高级C++技术.pdf)为本站会员(qwe****56)主动上传,淘文阁 - 分享文档赚钱的网站仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知淘文阁 - 分享文档赚钱的网站(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    关于淘文阁 - 版权申诉 - 用户使用规则 - 积分规则 - 联系我们

    本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

    工信部备案号:黑ICP备15003705号 © 2020-2023 www.taowenge.com 淘文阁 

    收起
    展开