Android game guidance (2. Basic game settings)

Source: Internet
Author: User

Android game guidance (2. Basic game settings)

The previous section has learned a basic OpenGL framework. Today, we will further set up 2D game-related things. I love 2D games more than 3D games. I believe most people do the same.

Table of contents
  • 1 game full screen display
  • 2. Set the OpenGL 2D Environment
    • 2.1 onsurfacecreated
    • 2.2 onsurfacechanged
    • 2.3 ondrawframe
  • 3 triangles-test
1 game full screen display

Two lines of code can be done, one is to let the application remove its own title bar, one is to set full screen, put in the oncreate function of the application:

 

Public class glgame extends activity {/** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); requestwindowfeature (window. feature_no_title); getwindow (). setflags (windowmanager. layoutparams. flag_fullscreen, windowmanager. layoutparams. flag_fullscreen );//... other omitted }}

 

2. Set the OpenGL 2D Environment

First, we need to design the world Positioning method to be established, that is, the coordinate system. The general OpenGL program is the standard Cartesian coordinate system, that is, the lower left corner is the origin, X is horizontally right, and Y is vertically extended. Like the famous cocos2d engine, this method is used. I prefer another method of describing the origin in the upper left corner, so this set of coordinate system is used:

The Renderer of glsurfaceview provides three interfaces: drawing when the size is changed during creation. The operations we take in the three functions are as follows:

  • During the creation, onsurfacecreated performs basic OpenGL settings, such as shadow smoothing and deep testing.
  • When the size changes, onsurfacechanged, projection, and view.
  • Draw, ondrawframe, which is the main function for subsequent game plotting.

2.1 onsurfacecreated

Not to mention, they are all basic settings.

 

Public void onsurfacecreated (gl10 GL, eglconfig config) {// notify the system to modify GL for pivoting. glhint (gl10.gl _ perspective_correction_hint, gl10.gl _ fastest); // The background is black GL. glclearcolor (0, 0, 0, 1); // enables shadow smooth GL. glshademodel (gl10.gl _ smooth); // sets the deep cache GL. glcleardepthf (1.0f); // enable the deep test GL. glable (gl10.gl _ depth_test); // type of the deep test. gldepthfunc (gl10.gl _ lequal );}

 

2.2 onsurfacechanged

 

Public void onsurfacechanged (gl10 GL, int width, int height) {// set the size of GL in OpenGL scenarios. glviewport (0, 0, width, height); // sets the GL projection matrix. glmatrixmode (gl10.gl _ projection); // reset the projection matrix GL. glloadidentity (); // set the size of the viewport // GL. glorthof (0, width, height, 0,-1000,100 0); Glu. gluortho2d (GL, 0, width, height, 0); // reset the model matrix GL. glmatrixmode (gl10.gl _ modelview); GL. glloadidentity ();}

 

In the code, glviewport is used to set the scene size. All our OpenGL worlds are projected into this scenario. Here, we set the scene size to the window surface size.

The projection matrix defines how the OpenGL world is reflected in your scenario. in OpenGL, there are two projection modes: Perspective and orthogonal. The method of perspective is closer to our reality. The light emitted by your eyes forms an angle. The closer your eyes are, the larger the things are, the smaller the range is. The closer your eyes are, the smaller the things, the wider the field of view. Therefore, it is mostly used in 3D. While orthogonal is that an object in the world is projected to a piece of paper (your canvas) According to parallel light, as if it is compressed on it. No matter how far the object is in the world, the projection result is still the original size, 2D games use this projection.

Set the gluperspective of glfrustum and Glu library for Perspective Projection. You can set the angle, distance, and other parameters in the Perspective Projection above. You can also set the position of the eye (CAMERA) by using glulookat.

Set gluorth2d with glortho and Glu libraries for orthogonal projection:

Glortho (gldouble left, gldouble right, gldouble bottom, gldouble top, gldouble near, gldouble far );
Gluorth2d (gldouble left, gldouble right, gldouble bottom, gldouble top );

We do not need to set the y-axis parameter of the Glu version because it is not used. Since our coordinate system uses the upper left corner as the origin, the left and top parameters are 0.

The glmatrixmode () function specifies the matrix under which the Code operates. Two types of matrix transformations are used here: Projection Transformation and Model View transformation. In the projection transformation, we change the crop Range of the world space. In the model view transformation, we can move and rotate objects in the world, therefore, when we plot the data, we are in the matrix transformation of the model view. Glloadidentity () is used to reset the matrix and clear the last residual information.

The code is finally set to model view transformation, so that we can move and transform the object model in the rendering function.

2.3 ondrawframe

The Renderer of glsurfaceview is drawn in an independent thread, and this function is continuously called cyclically.

We have already set the Model View matrix. Each ondrawframe operation requires a glloadidentity reset to clear the residual information caused by the previous painting.

 

Public void ondrawframe (gl10 GL) {// clear the screen and depth cache GL. glclear (gl10.gl _ color_buffer_bit | gl10.gl _ depth_buffer_bit); // reset the model matrix GL. glloadidentity ();//... specific Drawing code to add}

 

3 triangles-test

Well, we have already set it up. Let's take a simple test and draw a Red Triangle in your window. First, define the three angles of the triangle. The upper left corner is (60,200), the upper right corner is (180,200), and the margin is (120,300 ).

 

Private floatbuffer triggerbuffer = floatbuffer. wrap (new float [] {60,200, // 180,200 in the upper left corner, // 120,300 in the upper right corner, // bottom top corner}); Public void ondrawframe (gl10 GL) {// clear the screen and deep cache GL. glclear (gl10.gl _ color_buffer_bit | gl10.gl _ depth_buffer_bit); // reset the model matrix GL. glloadidentity (); GL. glpushmatrix (); GL. glcolor4f (1, 0, 0, 0); // allows you to set the vertex GL. glableclientstate (gl10.gl _ vertex_array); GL. glvertexpointer (2, gl10.gl _ float, 0, triggerbuffer); GL. gldrawarrays (gl10.gl _ triangles, 0, 3); // unsets the vertex GL. gldisableclientstate (gl10.gl _ vertex_array); GL. glpopmatrix ();}

 

The main code is surrounded by glpushmatrix () and glpopmatrix (). OpenGL maintains various matrix stacks internally, and stack operations are push and pop. Here, we operate on the model view matrix transformation, so we operate on the model view stack. We do not want to draw the matrix information of triangles to pollute the outer matrix, such as color settings. Therefore, we try to use the push and pop operations when drawing a unit body.

Use glcolor4f to set the painting color to red. In opengles, standard glbegin and glend functions are missing. The array vertices are used to set coordinates. The performance is mainly considered. This option is disabled by default, so it is enabled first. If you do not need to, disable it again: glableclientstate (gl10.glvertexarray), gldisableclientstate (gl10.glvertexarray ).

Glvertexpointer is used to specify the array vertices. After specifying the vertex, gldrawarray () can be used to draw the vertex in the specified array. Here it is necessary to peat these two functions, because these two functions are used too much. First look at the first one:

Void glvertexpointer (INT size, int type, int stride, buffer pointer)

Parameters:

Size
Each vertex has several numerical values. The value must be one of 2, 3, and 4. The initial value is 4.
Type
The type of each vertex coordinate in the array. Valid values: glbyte, glshort, glfixed, and glfloat. The initial value is glfloat.
Stride
The interval and step (byte displacement) between each vertex in the array ). If the value is 0, the array is continuous. The initial value is 0.
Pointer
Your array stores the coordinates of each vertex. The initial value is 0.

Note that type tells OpenGL about your array type. Glbyte, glshort, and glfloat correspond to byte [], short [], float []. glfixed corresponds to int []. in particular, when glfixed is described, the coordinate units of all vertices are 0x10000. For example, when a vertex is (60,120) and glfixed is used, it must be set to (60*0x10000,120*0x10000), so we often see the int one = 0x10000 programming statement. This is the value.

Let's take a look at gldrawarray:

Void gldrawarrays (INT mode, int first, int count );

Parameters:

Mode
Specify the elements you want to draw. The elements in OpenGL include glpoints, gllinestrip, gllineloop, gllines, gltrianglestrip, gltrianglefan, and gltriangles.
First
Start position (index location) in the specified array)
Count
Number of points to be drawn. For example, if we draw a triangle, we will draw three vertices, that is, this parameter is 3.

These two functions are introduced so much that they are enough to deal with this program. If you have any questions, please check the official function documentation: http://www.khronos.org/opengles/sdk/1.1/docs/man/

All right, the code is complete. You should have drawn a triangle. You can try other elements, points, lines, quadrilateral, polygon, and so on.

Happy Learning.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.