How to save activity state with GLSurfaceView

My problem is that our game can instantly switch to menu and settings mode, but it takes 4-6 seconds to load the texture, and in GL-GL rendering mode, in the end I just used 6 simple textures to create 6 sprites in the game .

Please help me answer two questions: 1. How can I preload our resources in android os in order to start our game faster? 2. To use the trick to create instance switching between activities, how can I maintain my activity with the GLSurfaceView state?

I want to help you understand my situation, please read the following code:

A game using 3 actions, as you can see in the following configuration:

<application android:label="@string/app_name" android:icon="@drawable/icon" android:allowBackup="true"> <activity android:name=".Menu" android:screenOrientation="portrait" android:theme="@android:style/Theme.NoTitleBar.Fullscreen" android:launchMode="singleTop"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".ReTouch" android:screenOrientation="portrait" /> <activity android:name=".Preference" android:screenOrientation="portrait" android:theme="@android:style/Theme.NoTitleBar.Fullscreen" /> </application> 

My .ReTouch class is an extended class from RokonActivity (I use the rokon engine for my game), this engine will create a GLSurefaceView to render my game in OpenGL ES. You can get the RokonAcitivity source code here: http://code.google.com/ p / rokon / source / browse / tags / release / 1.1.1 / src / com / stickycoding / Rokon / RokonActivity.java

  public class ReTouch extends RokonActivity { public static final int REPLAY_DELAY_INTERVAL = 1000; private ReTouchGameBoard reTouchGame; 

and .Menu, .Preference are two normal standard actions in an Android application.

I use this method to start and switch between actions:

  playButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { soundPool.play(soundId, 1, 1, 1, 0, 1); startActivity(new Intent(Menu.this, ReTouch.class)); } }); settingButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { soundPool.play(soundId, 1, 1, 1, 0, 1); startActivity(new Intent(Menu.this, Preference.class)); } }); quitButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { soundPool.play(soundId, 1, 1, 1, 0, 1); finish(); } }); 
+4
source share
1 answer

I avoided this problem, but did not solve it for my game:

I use "FrameLayout" as the main layout, and "GLSurfaceView" is the first layout inside it (that is, at the bottom of the stack), the following menu view group and the opaque loading screen (set match_parent to fill the screen):

 <FrameLayout android:id="@+id/graphics_frameLayout1" android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="fill_parent"> <android.opengl.GLSurfaceView android:id="@+id/graphics_glsurfaceview1" android:layout_width="fill_parent" android:layout_height="fill_parent"> </android.opengl.GLSurfaceView> <LinearLayout android:id="@+id/menu_linearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:visiblility="gone" android:background="#000000">// opaque background // menus etc be here </LinearLayout> <LinearLayout android:layout_height="fill_parent" android:id="@+id/loading_linearLayout1" android:layout_width="fill_parent" android:orientation="vertical" android:background="#000000">// opaque background <ProgressBar style="?android:attr/progressBarStyleLarge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/loading_progressBar1"> </ProgressBar> <TextView android:layout_width="wrap_content" android:id="@+id/loading_textView1" android:layout_height="wrap_content" android:text="Loading..."> </TextView> </LinearLayout> </FrameLayout> 

When the download is finished, I set the viewing group to view invisible or left, and the menu viewing group to visible. Once the user has chosen the option to start the game, I start the logical flow of the game and again force the menu

I have a hierarchy of actions ( ReTouchMenuActivity extends ReTouchActivity , and then in the new ReTouchActivity extends RokonActivity ), so I do not get the whole game in one unmanaged file

I load other assets into separate streams (e.g. vertex data for 3D models) and then load them into vertices, texture codes, etc. immediately before drawing arrays (i.e. gl.glVertexPointer(3, GL10.GL_FLOAT, 0, meshVertices);

The real problem is that you need to load textures in the GL stream, which can block the user interface stream (albeit without failures), which does not lead to a response to user input when loading large textures. My own game using the ZTE Blade running Android 2.1 above can take up to two seconds to load some large textures (1mb +), and even the circular progress bar stops spinning during this time.

I noticed that you finish() activity when the user presses the "Exit" button, but when the user leaves the application without pressing the "exit" button (say, when they receive an incoming call or select something from the notification panel), the action / app is still running in the background. You must reload the textures when they return the action / application back to the top of the stack (i.e. the user will open it again). If you do not overload the textures in such a scenario, the user will simply get the load empty instead of textures

If you try to save the GL descriptor / object as a variable, which android goes to your Renderer class' onDraw , for use by the texture load stream, for example, it goes out of context after onDraw returns and texture loading does not work.

I assume that the android erases all graphic data / OpenGL state when the user leaves the application, in case other applications want to use it. Therefore, I assume that you cannot do exactly what you are trying to do (pre-load textures into android os or something else). Although, if someone knows better, I will be interested to hear.

Edit:

This question has good answers about speeding up actual texture load times.

+1
source

Source: https://habr.com/ru/post/1310702/


All Articles