I need an explanation for the code "beginner games for Android"

Since the last couple, I’ve been reading the book “Getting Android Games.” But I am stuck to understand the code.

You can check or download the code here: http://code.google.com/p/beginnginandroidgames2/downloads/list

I mean the ch06-mr-mom project. The action is called MrNomGame :

 public abstract class AndroidGame extends Activity implements Game { AndroidFastRenderView renderView; Graphics graphics; Audio audio; Input input; FileIO fileIO; Screen screen; WakeLock wakeLock; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); boolean isLandscape = getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE; int frameBufferWidth = isLandscape ? 480 : 320; int frameBufferHeight = isLandscape ? 320 : 480; Bitmap frameBuffer = Bitmap.createBitmap(frameBufferWidth, frameBufferHeight, Config.RGB_565); float scaleX = (float) frameBufferWidth / getWindowManager().getDefaultDisplay().getWidth(); float scaleY = (float) frameBufferHeight / getWindowManager().getDefaultDisplay().getHeight(); renderView = new AndroidFastRenderView(this, frameBuffer); graphics = new AndroidGraphics(getAssets(), frameBuffer); fileIO = new AndroidFileIO(this); audio = new AndroidAudio(this); input = new AndroidInput(this, renderView, scaleX, scaleY); screen = getStartScreen(); setContentView(renderView); PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE); wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "GLGame"); } @Override public void onResume() { super.onResume(); wakeLock.acquire(); screen.resume(); renderView.resume(); } @Override public void onPause() { super.onPause(); wakeLock.release(); renderView.pause(); screen.pause(); if (isFinishing()) screen.dispose(); } public Input getInput() { return input; } public FileIO getFileIO() { return fileIO; } public Graphics getGraphics() { return graphics; } public Audio getAudio() { return audio; } public void setScreen(Screen screen) { if (screen == null) throw new IllegalArgumentException("Screen must not be null"); this.screen.pause(); this.screen.dispose(); screen.resume(); screen.update(0); this.screen = screen; } public Screen getCurrentScreen() { return screen; } } 

The class implements the Game interface:

 package com.badlogic.androidgames.framework; public interface Game { public Input getInput(); public FileIO getFileIO(); public Graphics getGraphics(); public Audio getAudio(); public void setScreen(Screen screen); public Screen getCurrentScreen(); public Screen getStartScreen(); } 

My first question: I missed the implementation of the getStartScreen() method of the Game interface. Usually I have to implement all the interface methods.

In any case, onCreate is now executed. On this line:

 screen = getStartScreen(); 

The program returns to the first class, MrNomGame , where the getStartScreen() method returns a LoadingScreen object of the LoadingScreen variable. Class LoadingScreen :

 public class LoadingScreen extends Screen { public LoadingScreen(Game game) { super(game); } public void update(float deltaTime) { Graphics g = game.getGraphics(); Assets.background = g.newPixmap("background.png", PixmapFormat.RGB565); Assets.logo = g.newPixmap("logo.png", PixmapFormat.ARGB4444); Assets.mainMenu = g.newPixmap("mainmenu.png", PixmapFormat.ARGB4444); Assets.buttons = g.newPixmap("buttons.png", PixmapFormat.ARGB4444); Assets.help1 = g.newPixmap("help1.png", PixmapFormat.ARGB4444); Assets.help2 = g.newPixmap("help2.png", PixmapFormat.ARGB4444); Assets.help3 = g.newPixmap("help3.png", PixmapFormat.ARGB4444); Assets.numbers = g.newPixmap("numbers.png", PixmapFormat.ARGB4444); Assets.ready = g.newPixmap("ready.png", PixmapFormat.ARGB4444); Assets.pause = g.newPixmap("pausemenu.png", PixmapFormat.ARGB4444); Assets.gameOver = g.newPixmap("gameover.png", PixmapFormat.ARGB4444); Assets.headUp = g.newPixmap("headup.png", PixmapFormat.ARGB4444); Assets.headLeft = g.newPixmap("headleft.png", PixmapFormat.ARGB4444); Assets.headDown = g.newPixmap("headdown.png", PixmapFormat.ARGB4444); Assets.headRight = g.newPixmap("headright.png", PixmapFormat.ARGB4444); Assets.tail = g.newPixmap("tail.png", PixmapFormat.ARGB4444); Assets.stain1 = g.newPixmap("stain1.png", PixmapFormat.ARGB4444); Assets.stain2 = g.newPixmap("stain2.png", PixmapFormat.ARGB4444); Assets.stain3 = g.newPixmap("stain3.png", PixmapFormat.ARGB4444); Assets.click = game.getAudio().newSound("click.ogg"); Assets.eat = game.getAudio().newSound("eat.ogg"); Assets.bitten = game.getAudio().newSound("bitten.ogg"); Settings.load(game.getFileIO()); game.setScreen(new MainMenuScreen(game)); } public void present(float deltaTime) { } public void pause() { } public void resume() { } public void dispose() { } } 

Class screen :

 public abstract class Screen { protected final Game game; public Screen(Game game) { this.game = game; } public abstract void update(float deltaTime); public abstract void present(float deltaTime); public abstract void pause(); public abstract void resume(); public abstract void dispose(); } 

After a while, the onCreate() method completes. But in Debug mode, the screen of my mobile phone is already black and dark. The program seems to be in a loop. I can press F6 as often as I want. But when I return, the main menu will appear. If you look in the code, you will see that the Main-Menu will call the update() LoadingScreen .

My big question is: What happens after the onCreate() method completes and how does the program get into the update() LoadingScreen ?

I know a lot of code. But it would be very useful for me to understand this.

+4
source share
1 answer

For your first question: AndroidGame is an abstract class. This means that he does not need to implement all the Game methods if one of the classes extending AndroidGame does this. As you can see from the code, this is done in MrNomGame :

 public class MrNomGame extends AndroidGame { public Screen getStartScreen() { return new LoadingScreen(this); } } 

Now for your big question:

A renderer is created in onCreate :

 renderView = new AndroidFastRenderView(this, frameBuffer); 

If you look at this class, you will see that onResume creates a new thread that runs the following code:

 public void run() { Rect dstRect = new Rect(); long startTime = System.nanoTime(); while(running) { if(!holder.getSurface().isValid()) continue; float deltaTime = (System.nanoTime()-startTime) / 1000000000.0f; startTime = System.nanoTime(); game.getCurrentScreen().update(deltaTime); game.getCurrentScreen().present(deltaTime); Canvas canvas = holder.lockCanvas(); canvas.getClipBounds(dstRect); canvas.drawBitmap(framebuffer, null, dstRect, null); holder.unlockCanvasAndPost(canvas); } } 

Thus, it repeatedly calls update on the current screen, which is an instance of LoadingScreen .

+4
source

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


All Articles