android application structure for game?

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

android application structure for game?

Postby edzillion » Fri Oct 16, 2009 9:04 pm

Hi

Just getting into android coding and I was looking at this very useful tutorial 'Getting Started in Android Game Development'.

It outlines a basic 'main loop':

public void run() {
while (isRunning) {
while (isPaused && isRunning) {
sleep(100);
}
update();
}
}

private void update() {
updateState();
updateInput();
updateAI();
updatePhysics();
updateAnimations();
updateSound();
updateVideo();
}


Ok, so far so good. But he also states that games should have their own thread so that thread interrupts handle well.

So should I have a main activity that spawns a surfaceview with a thread in it? (like the lunarlander game), and shouldnt the main loop be in that thread then?

So in that case I would have a main activity, perhaps a main menu, which spawns a new activity for the game / screen and also contains the main thread? My game will have two modes, which should each have an activity, right? How do I make sure the thread is calling both activities correctly? I know one activity can be inside another but it seems messy. I would have thought that the better way to do it would be to have the main thread independent of the activities?

Little bit confused here about activities, threads etc. Please ask me if you don't understand me; when starting out it is often not easy to decribe what you don't know very well!

Cheers
Ed
edzillion
Junior Developer
Junior Developer
 
Posts: 17
Joined: Fri Oct 16, 2009 8:48 pm

Top

Postby kdiddy » Sat Oct 17, 2009 2:16 pm

Hi edzillion,

How you structure the game is going to depend a lot on what type of game you want to make. If your going to be moving graphics around then yes, an activity that is using a surfaceview would be the right direction. If your going to have a lot of static images, you can use an xml layout as your view. With surfaceview, you'll have a main activity that will "setContentView" to your view class. You would handle your canvas drawings through a thread. Here's a basic example.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  
  3.  
  4. public class MainActivityClass extends Activity {
  5.  
  6.  
  7.  
  8.      private TheGameView gameview;
  9.  
  10.     @Override
  11.  
  12.     public void onCreate(Bundle savedInstanceState) {
  13.  
  14.         super.onCreate(savedInstanceState);        
  15.  
  16.         gameview= new TheGameView(this);        
  17.  
  18.         setContentView(gameview);
  19.  
  20.     }
  21.  
  22. }
  23.  
  24.  
  25.  
  26.  
  27.  
  28. class TheGameView extends SurfaceView implements SurfaceHolder.Callback {
  29.  
  30.       private Context context;  
  31.  
  32.      
  33.  
  34.     Preview(Context context){
  35.  
  36.      super(context);
  37.  
  38.        
  39.  
  40.  
  41.  
  42. }
  43.  
  44.  
  45.  
  46.      @Override
  47.  
  48.      public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
  49.  
  50.           // TODO Auto-generated method stub
  51.  
  52.          
  53.  
  54.      }
  55.  
  56.  
  57.  
  58.      @Override
  59.  
  60.      public void surfaceCreated(SurfaceHolder holder) {
  61.  
  62.           // TODO Auto-generated method stub
  63.  
  64.          
  65.  
  66.      }
  67.  
  68.  
  69.  
  70.      @Override
  71.  
  72.      public void surfaceDestroyed(SurfaceHolder holder) {
  73.  
  74.           // TODO Auto-generated method stub
  75.  
  76.          
  77.  
  78.      }
  79.  
  80.      
  81.  
  82.      
  83.  
  84. }
  85.  
  86. class GameThread extends Thread {
  87.  
  88.         private SurfaceHolder _surfaceHolder;
  89.  
  90.         private TheGameView _theGameView ;
  91.  
  92.         private boolean _run = false;
  93.  
  94.        
  95.  
  96.        
  97.  
  98.         public GameThread (TheGameView gameview) {
  99.  
  100.           _theGameView = gameview;
  101.  
  102.        
  103.  
  104.         }
  105.  
  106.        
  107.  
  108.         public GameThread (SurfaceHolder surfaceHolder, TheGameView gameview) {
  109.  
  110.             _surfaceHolder = surfaceHolder;
  111.  
  112.             _theGameView = gameview;
  113.  
  114.         }
  115.  
  116.      
  117.  
  118.         public void setRunning(boolean run) {
  119.  
  120.             _run = run;
  121.  
  122.         }
  123.  
  124.      
  125.  
  126.         public SurfaceHolder getSurfaceHolder() {
  127.  
  128.             return _surfaceHolder;
  129.  
  130.         }
  131.  
  132.  
  133.  
  134.         public boolean isRunning() {
  135.  
  136.             return _run;
  137.  
  138.         }
  139.  
  140.         @Override
  141.  
  142.         public void run() {
  143.  
  144.           Canvas c;
  145.  
  146.             while (_run) {
  147.  
  148.                 c = null;
  149.  
  150.                 try {
  151.  
  152.                     c = _surfaceHolder.lockCanvas(null);
  153.  
  154.                     synchronized (_surfaceHolder) {
  155.  
  156.                          _theGameView.onDraw(c);
  157.  
  158.                     }
  159.  
  160.                 } finally {
  161.  
  162.                  
  163.  
  164.                     if (c != null) {
  165.  
  166.                         _surfaceHolder.unlockCanvasAndPost(c);
  167.  
  168.                     }
  169.  
  170.                 }
  171.  
  172.             }
  173.  
  174.  
  175.  
  176.         }
  177.  
  178. }
  179.  
  180.  
  181.  
  182.  
Parsed in 0.040 seconds, using GeSHi 1.0.8.4


Kinda of a basic setup. Each class should be in their own file. Also Mr. Snowflake put together this tutorial - http://www.anddev.org/2d_tutorial-t3120.html - which is really helpful.

K
Diddy
kdiddy
Junior Developer
Junior Developer
 
Posts: 19
Joined: Wed Apr 01, 2009 2:30 am

Postby edzillion » Mon Oct 19, 2009 4:07 pm

Thanks for that.

Just having trouble filling in the gaps with your code; I decided instead to open up LunarLander again and try to change that into the structure you have outlined.

The lunar lander has two files, with three classes: main activity in LunarLander.java, and the surface and the thread in LunarView.java

So it should just be a case of taking the LunarThread class and putting it into its own file, and call it from the others.

I presume that doStart, doRun, doPause etc. should be in the thread class, and the paint stuff should be in the surface class?

As per the original question, the main update() loop should be in the thread class right?

Cheers
ed
edzillion
Junior Developer
Junior Developer
 
Posts: 17
Joined: Fri Oct 16, 2009 8:48 pm

Postby kdiddy » Tue Oct 20, 2009 3:33 pm

Hi edzillion,

Regarding your main question about the update() method, it really depends on what you are doing. For example I have programmed stuff that runs the main update() method on a timer. If you run it in the thread it is happening constantly which is more geared toward updating graphics positions and/or physics.

Can you give me a general idea of what it is your trying to do?

K
Diddy
kdiddy
Junior Developer
Junior Developer
 
Posts: 19
Joined: Wed Apr 01, 2009 2:30 am

Postby edzillion » Wed Oct 21, 2009 5:13 pm

Can you give me a general idea of what it is your trying to do?


Sure. Making a simple enough game with two main screens: the main screen showing a large map that the player travels around, and then a gameplay screen for the encounters/battles.
I would like to create my own ui in both game modes/screens and want the player to animate as he is moving around, so I presume these will both be SurfaceViews.

Would these share an animation thread or have one each?

There will be an AI/game logic phase of the update() function but it probably wont need to do something every turn.

Cheers
Ed
edzillion
Junior Developer
Junior Developer
 
Posts: 17
Joined: Fri Oct 16, 2009 8:48 pm

Postby kdiddy » Tue Oct 27, 2009 1:00 am

I would start by going through the following tutorial.

http://www.droidnova.com/playing-with-g ... i,147.html

and

http://www.droidnova.com/2d-sprite-anim ... d,471.html

There is also plenty of stuff in anddev. I'm not sure I could post how to program the game without just posting code.

Good luck

K
Diddy
kdiddy
Junior Developer
Junior Developer
 
Posts: 19
Joined: Wed Apr 01, 2009 2:30 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 10 guests