首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

②Libgde发动机:HelloWorld项目

2012-11-23 
②Libgde引擎:HelloWorld项目Libgde引擎:HelloWorld项目前面的文章中介绍了整个Libgdx的框架以及框架中的各

②Libgde引擎:HelloWorld项目

Libgde引擎:HelloWorld项目

前面的文章中介绍了整个Libgdx的框架以及框架中的各个模块。那些都是一个概述性的东西,不能没有,看完后对这个引擎有一个大概的了解后,接触到实际代码的时候就不会那么陌生。下面就开始今天的内容:

1.搭建Libgex引擎环境。

1).从网址:http://code.google.com/p/libgdx/downloads/list里面下载这个包:libgdx-0.81.zip。解压到libgdx-0.81文件夹中。

2).参考解压后文件夹下面的README文件(用任何文本编辑器打开),新建一个helloWorld项目。

2.使用Libgdx框架在屏幕上画一个点。

在新建的helloWorld项目中新建一个包:com.helloworld.fneg,在包中新建一个类:MyFirstPoint.java,这个类实现了ApplicationListener接口(此接口在以前文章中有过介绍)。再添加类HelloWorld.java类,这个类中带有main方法。

此时MyFirstPoint.java类中有如下代码:

package com.helloworld.fneg;

import com.badlogic.gdx.ApplicationListener;

import com.badlogic.gdx.Gdx;

import com.badlogic.gdx.graphics.GL10;

import com.badlogic.gdx.graphics.Mesh;

import com.badlogic.gdx.graphics.VertexAttribute;

import com.badlogic.gdx.graphics.VertexAttributes.Usage;

/**

*@Copyright:Copyright (c) 2008 - 2100

*@Company:Sagret

*@Author:fengcunhanfengcunhan@gmail.com

*@Package:com.helloworld.fneg

*@FileName:MyFirstPoint.java

*@Time:2011-1-10

*@User:feng

*/

public class MyFirstPoint implements ApplicationListener {

@Override

public void create() {

// TODO Auto-generated method stub

}

@Override

public void dispose() {

// TODO Auto-generated method stub

}

@Override

public void pause() {

// TODO Auto-generated method stub

}

@Override

public void render() {

}

@Override

public void resize(int arg0, int arg1) {

// TODO Auto-generated method stub

}

@Override

public void resume() {

// TODO Auto-generated method stub

}

}

在类中添加一个成员变量:private Mesh pointMesh;

在create()方法中初始化该变量:

VertexAttribute vertex= newVertexAttribute(Usage.Position, 3,"point_position");

pointMesh=new Mesh(true, 4, 4, vertex);

pointMesh.setVertices(newfloat[]{

0.5f,0.5f,0

});//设置顶点数组

pointMesh.setIndices(newshort[]{0});//设置索引

 

以下对这一些方法做一下说明:

VertexAttribute vertex= newVertexAttribute(Usage.Position, 3,"point_position");

VertexAttribute类从字面理解上来看就是顶点属性类,它做的也是封装顶点信息的功能。

官方文档如下:

com.badlogic.gdx.graphics.VertexAttribute.VertexAttribute(int usage, int numComponents,Stringalias)

Constructs a new VertexAttribute.

Parameters:

usage the usage, used for the fixed function pipeline. Generic attributes are not supported in the fixed function pipeline.

numComponents the number of components of this attribute, must be between 1 and 4.

alias the alias used in a shader for this attribute. Can be changed after construction.

大致的说一下这几个参数的意思:

Usage.Position:指定是一个点的数据。

3:这个点是有3个数据组成

point_position:这个顶点属性的别名。(可以随便写一个)。

pointMesh=new Mesh(true, 4, 4, vertex);

我把这个方法的文档贴上来:

com.badlogic.gdx.graphics.Mesh.Mesh(boolean isStatic, int maxVertices, int maxIndices,VertexAttribute... attributes)

Creates a new Mesh with the given attributes.

Parameters:

isStatic whether this mesh is static or not. Allows for internal optimizations.

maxVertices the maximum number of vertices this mesh can hold

maxIndices the maximum number of indices this mesh can hold

attributes the VertexAttributes. Each vertex attribute defines one property of a vertex such as position, normal or texture coordinate

大致的说一下这几个参数的意思:

True:表示这个图元是静态的

4:表示这个图元一个顶点最多可以有4个数字组成。

4:表示这个图元最多可以有4个索引

vertex:顶点属性对象(上面才提到)。

在render()方法中添加如下代码:

pointMesh.render(GL10.GL_POINTS, 0, 1);

我把这个方法的文档贴上来:

void com.badlogic.gdx.graphics.Mesh.render(int primitiveType, int offset, int count)

Renders the mesh using the given primitive type. offset specifies the offset into vertex buffer and is ignored for the index buffer. Count specifies the number of vertices or indices to use thus count / #vertices per primitive primitives are rendered.

This method is intended for use with OpenGL ES 1.x and will throw an IllegalStateException when OpenGL ES 2.0 is used.

Parameters:

primitiveType the primitive type

offset the offset into the vertex buffer, ignored for indexed rendering

count number of vertices or indices to use

GL10.GL_POINTS:指定画一个点(OpenGL中的东西)

0:数组开始位置是0表示使用顶点数组的全部数据。

1:表示使用1个顶点。

在HelloWorld.java的main方法中添加一下代码:

new JoglApplication(new MyFirstPoint(),"Hello world", 480, 320, false);

右击HelloWorld.java就可以运行了。

3.在Android中运行以上的程序。

新建一个Android项目,按照README中的方法配置好环境。将上述Java项目添加到这个项目里面,然后将自动生成的那个Activity改成继承自AndroidApplication,然后删除setContentView(R.layout.main).添加一行代码:initialize(new MyFirstPoint(), false);然后运行这个Android就行了,是不是很简单啊?哈哈

以上项目的工程源码请从这里下载:http://u.115.com/file/f659b13f2#libgdxHelloWorld.rar

版权所有,转载请注明出处:

 

热点排行