libgdx学习文档.docx

上传人:b****6 文档编号:7085011 上传时间:2023-01-17 格式:DOCX 页数:17 大小:756.27KB
下载 相关 举报
libgdx学习文档.docx_第1页
第1页 / 共17页
libgdx学习文档.docx_第2页
第2页 / 共17页
libgdx学习文档.docx_第3页
第3页 / 共17页
libgdx学习文档.docx_第4页
第4页 / 共17页
libgdx学习文档.docx_第5页
第5页 / 共17页
点击查看更多>>
下载资源
资源描述

libgdx学习文档.docx

《libgdx学习文档.docx》由会员分享,可在线阅读,更多相关《libgdx学习文档.docx(17页珍藏版)》请在冰豆网上搜索。

libgdx学习文档.docx

libgdx学习文档

Libgdx是一款支持2D与3D游戏开发的游戏类库,兼容大多数微机平台(标准JavaSE实现,能运行在Mac、Linux、Windows等系统)与Android平台(以上即可利用,以上可满功率发挥),Libgdx由audio、files、graphics、math、physics、scenes、utils这些主要类库所组成,它们分别对应了Libgdx中的音频操作,文件读取,2D/3D渲染,Libgdx绘图相关运算,Box2D封装,2D/3D游戏组件(3D部分目前无组件),以及Libgdx内置工具类。

Libgdx要紧组成如下所示(Libgdx作者wiki提供):

下面开始,我将就Libgdx的具体实现,开始讲解如何正确利用Libgdx类库。

只是在正式开始之前,咱们第一还得讲讲Gdx类。

关于Libgdx中的Gdx类:

 单从表面上看,Gdx类占用空间不足2KB,乃至不具有一行能够被直接执行的函数,并没什么重要好说。

然而,真实的Gdx却是Libgdx类库运行的核心所在,没有它你将寸步难行,不单运行Graphics、Input、Files、Audio、AndroidApplication等Libgdx关键部分所必需的实例会在Libgdx初始化时注入Gdx中对应的graphics、input、files、audio、app等静态变量里面,就连Libgdx对OpenGL接口(或OpenGLES,视Libgdx运行平台而定,以下统称OpenGL)的GL10、GL11、GL20、GLCommon等封装类也会在Graphics实例化时分别注入到gl10、gl11、gl20、gl这四个同样位于Gdx的静态变量当中(在Graphics中也会继续保留它们的引用,因此无论你执行还是,其实都在调用同一个静态变量)。

事实上,如果你想不使用Gdx而正常运行Libgdx,那么除了重构源码,就再没有任何办法可想了。

PS:

若是你不清楚自己究竟在什么环境利用Libgdx,其实也没必要强分gl10或gl11,大能够通过方式挪用Libgdx中关于OpenGL接口的默许封装(执行某些非多版本共有接口时,依旧需要利用对应版本专属gl)。

想要利用Libgdx,却不明白Gdx是干什么用的,那么一切就都是空谈。

下面开始,我将具体讲解Libgdx中的图像处置与游戏组件部份:

关于Libgdx的图像处置部份:

 Mesh:

 本质上讲,Libgdx中所有可见的3D物体第一都是一个Mesh(网格,或说三维网格形式的高级图元)。

Mesh是如何生成的呢?

众所周知,数学上讲的立体几何由点、线、面三部份组成,不管何等复杂的图像也能够分解为无数细小的这三部份,或说能够由超级基础的N个这三部份所组合而成;到了3D游戏开发时,当咱们要构建复杂的3D图像,第一会以一系列有序的vertices(极点)组成这些具体的点、线、三角要素,即组成画图大体图元(Primitives),再将大体图元组合成更完整的高级图元也确实是具体3D对象。

因此,若是对Mesh概念进行简单的明白得,其实它确实是一个象征完整图像的大体图元集合体,Libgdx先让咱们把一个个细分的vertices组成大体图元,再由Mesh类将大体图元制成加倍复杂的高级图元展现出来。

具体可见Libgdx作者提供的returntomarchfeld示例,基本效果如下图所示:

(勿看FPS,一切信真机)PS:

若是对此类熟悉不足,能够去玩玩模拟人一辈子,下个修改器尝试编辑角色或物品造型后就懂了…… PS指postscript(备注,又可说明为附言、跋文)的缩写Texture:

 Libgdx所提供的游戏纹理用类,其实质可明白得为保留在显存中的Image,它以贴图的方式通过OpenGL将图片显示到游戏界面之上。

Libgdx的纹理能够直接从指定文件途径加载,也能够通过它提供的Pixmap类凭空创建(它的Texture(intwidth,intheight,Formatformat)构造内部直接挪用了Pixmap,不是必需在外部生成Pixmap后注入)。

另外在加载Texture时,个人建议通过Libgdx提供的函数挪用,该方式内部提供了Texture缓存治理,能够幸免无心义的资源重复加载。

另外,Texture通常会与TextureRegion类配套利用,利用TextureRegion包装Texture后,再利用SpriteBatch进行绘制,能够很方便的修订Texture为咱们需要的显示范围。

还有,Libgdx中Sprite类为TextureRegion子类,因此能够将Sprite看成TextureRegion来利用,只是Sprite类比TextureRegion有所扩展。

只是Libgdx的SpriteCache类并无继承Sprite或TextureRegion,因此起不到TextureRegion的作用,只能构建一组静态贴图集合算了,特此说明。

1.MainMainMainnternal(""), false);  

2.      }  

3.      publicvoid render(){  

1.           Main4a2a-0.5f0.5f0.0f0.0f-0.5f-0.5f0.0f1.0f0.5f-0.5f1.0f1.0f0.5f0.5f1.0f0.0fetHeight());  

2.          

3.          

4.        ();  

5.          

6.        ();  

1.        Main-0.5f-0.5f0.5f-0.5f0.5f0.5f4a4a2a0f1f0f0.2f0.2f0.2fetWidth(),  

2.              ().getHeight(), false,  

3.              true);  

4.        ();  

5.      }  

6.      publicvoid resize(int width, int height){  

7.      }  

8.      publicvoid resume(){  

9.      }  

10.  }  

11.   publicvoid onCreate(Bundlebundle){  

12.      (bundle);  

1.      Mainet()){  

1.           Main(Sequence.$(ScaleTo.$(1.1f, 1.1f, 0.3f),  

2.              ScaleTo.$(1f,1f, 0.3f))));  

3.        (img);  

4.      }  

5.      publicvoid render(){  

6.          

7.          

8.        ();  

9.      }  

10.      publicvoid dispose(){  

11.        ();  

12.        ();  

13.      }  

14.      publicvoid pause(){  

15.      }  

16.      publicvoid resize(int width, int height){  

17.      }  

18.      publicvoid resume(){  

19.      }  

20.  }  

21.   publicvoid onCreate(Bundlebundle){  

22.      (bundle);  

      2月1日2020年1月28日7aDidyounotice,however,thatthetriangleswecreatedappearstretched?

Letsstartwithanexamplethatwillmakethisissueclear.

CreatingaSquare

Createanewlibgdxdesktopprojectnamed projection-viewport-camera.Createaclasscalled ProjectionViewportCamera thatimplementsApplicationListener andputitinthepackage Forfullinstructiononhowtosetupalibgdxdesktop(andAndroid)project,lookatthe  tutorial.Unlikeinthattutorial,we'regoingtoshowasquareinsteadofarectangle.Here'sthecodefor ProjectionViewportCamera:

packageimportimportimportimportimportimportimportpublicclassProjectionViewportCameraimplementsApplicationListener{  privateMeshsquarePartOne;  privateMeshsquarePartTwo;  @Override  publicvoidcreate(){    if(squarePartOne==null){      squarePartOne=newMesh(true,3,3,          newVertexAttribute,3,"a_position"),          newVertexAttribute,4,"a_color"));      (newfloat[]{          -,-,0,(128,0,0,255),          ,-,0,(192,0,0,255),          -,,0,(192,0,0,255)});       (newshort[]{0,1,2});    }    if(squarePartTwo==null){      squarePartTwo=newMesh(true,3,3,          newVertexAttribute,3,"a_position"),          newVertexAttribute,4,"a_color"));      (newfloat[]{          ,-,0,(192,0,0,255),          -,,0,(192,0,0,255),          ,,0,(255,0,0,255)});       (newshort[]{0,1,2});    }  }  @Override  publicvoiddispose(){}  @Override  publicvoidpause(){}  @Override  publicvoidrender(){        ,0,3);    ,0,3);  }  @Override  publicvoidresize(intwidth,intheight){}  @Override  publicvoidresume(){}}

It'sverysimilartotheclassdescribedintheMyFirstTriangleTutorialexceptthatithastwotrianglesinsteadofone.Thetwotrianglesarepositionedtoformasquare,withcornersatposition(x,y)coordinates,,,,,,and,.Whenyourunthedesktopappitshouldlooklikethis:

Obviously,eventhoughwespecifiedthecoordinatesofasquare,theresultingshapeturnedouttobeawiderectangle.Beforetacklingthisissue,Iwilldigressforamomentandofferarenderingoptimzationforthissquare.

Thetwotriangleseachhaveavertexat,andavertexat,.Wecaneliminatetheduplicationofverticesatidenticalcoordinatesbyimplementingthesquareusing .Usingtrianglestripsallowthetwotrianglestoshareverticesandthereforeusememorymoreefficiently(checkout  forabetterillustration).Here'sthenewimplementation:

packageimportimportimportimportimportimportimportpublicclassProjectionViewportCameraimplementsApplicationListener{  privateMeshsquareMesh;  @Override  publicvoidcreate(){    if(squareMesh==null){      squareMesh=newMesh(true,4,4,          newVertexAttribute,3,"a_position"),          newVertexAttribute,4,"a_color"));      (newfloat[]{          -,-,0,(128,0,0,255),          ,-,0,(192,0,0,255),          -,,0,(192,0,0,255),          ,,0,(255,0,0,255)});       (newshort[]{0,1,2,3});16    }  }  @Override  publicvoiddispose(){}  @Override  publicvoidpause(){}  @Override  publicvoidrender(){        ,0,4);  }  @Override  publicvoidresize(intwidth,intheight){}  @Override  publicvoidresume(){}}

Thetwotriangleshavebeencombinedinasinglemeshwithfourverticesandfourindices.Inside render(),wespecifythatthesquarewasdefinedusingtrianglestrips.Fewerverticesarenecessaryandthecodeissimplertoo.

Viewport

Backtotheissueofthestretchedsquare.Bydefaulttheviewportrevealsarectangularareawithaleftedgeatx=-1,rightedgeatx=1,topedgeaty=1,andbottomedgeaty=-1.Here'sapictureofwhatablankarealikethatwouldlooklikeifweweretocreatemockup:

Whenweplaceoutredsquareontothatarea,theresultlookslikethis:

However,theviewportitselfiswiderthanasquare.Inordertofilluptheextraspaceonthesides,theareaisstretched.Imagineapainterstretchingasquarecanvastofitarectangularframe.Theresultlookslikewhatweseeinthe  tutorial:

Camera

Tocorrectthiseffect,weallowtheviewporttorenderanareathatisrectangularinsteadofsquare.Theshapeofthisareawillmatchtheshapeofviewport,sortoflikeapainterchoosingacanvassizebasedonthepictureframe'ssize.Toaccomplishthis,weusethe Camera class,inthisacaseasubclasscalled OrthographicCamera.The Camera classallowsustochangetheareashownbytheviewport.Here'sthenewcode:

packageimportimportimportimportimportimportimportimportpublicclassProjectionViewportCameraimplementsApplicationListener{  privateMeshsquareMesh;  privateOrthographicCameracamera;  @Override  publicvoidcreate(){    if(squareMesh==null){      squareMesh=newMesh(true,4,4,          newVertexAttribute,3,"a_position"),          newVertexAttribute,4,"a_color"));      (newfloat[]{          -,-,0,(128,0,0,255),          ,-,0,(192,0,0,255),          -,,0,(192,0,0,255),          ,,0,(255,0,0,255)});       (newshort[]{0,1,2,3});    }  }  @Override  publicvoiddispose(){}  @Override  publicvoidpause(){}  @Override  publicvoidrender(){    ();    ;        ,0,4);  }  @Override  publicvoidresize(intwidth,intheight){    floataspectRatio=(float)width/(float)height;    camera=newOrthographicCamera(2f*aspectRatio,2f);  }  @Override  publicvoidresume(){}}

In resize(),weconstructanewinstanceof OrthographicCamera,specifiyingtheareawewanttodisplay.Inthisexample,wetellthecameratodisplayanareatwounitstall,withawidththatisdeterminedbytheshapeoftheviewport.In render(),wecallsimplycall update() andapply() onthe Camera instance.Here'showitlookswhenweruntheapp:

Thedesktopviewportinourtutorialshavebeen480pixelswideand320pixelshigh.Thesearethenumbersarepassedinto resize() whentheappl

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > IT计算机 > 电脑基础知识

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1