2D tutorial

Tutorials concerning the OpenGL® ES cross-platform API for full-function 2D and 3D graphics on the Google-Android platform.

Postby Adam Twycross » Wed Feb 25, 2009 6:05 pm

Thanks so much for checking this out, it really is much appreciated!

In fact, thanks for going to the trouble of putting it up in the first place, it's been really useful to go through!
Adam Twycross
Freshman
Freshman
 
Posts: 8
Joined: Tue Feb 24, 2009 12:40 pm

Top

Postby MrSnowflake » Wed Feb 25, 2009 7:34 pm

I just checked and it does work, but you have to start the 'game' by opening the menu and select start, forgot about that completely.

ALSO A NOTE: I saw the template doesn't take care of life cycle stuff of the Activity.
Mental Note: Fix this :).
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby zohaib1020 » Wed Feb 25, 2009 7:53 pm

The program doesn't actually give me a chance to press the menu button, it force closes right when it opens.
zohaib1020
Freshman
Freshman
 
Posts: 4
Joined: Wed Feb 25, 2009 2:18 am

Postby Adam Twycross » Thu Feb 26, 2009 2:42 pm

Yeah, I'm getting the same force close issue when I first launch the application in the emulator, but if I choose 'force close' it carries on and works fine. Once the emulator's up and running, I can run the application without generating the force close/wait issue at all.

The same thing is happening when I run the Lunar Lander example, though, so I guess it's most likely be an SDK issue than a problem with the Game Template code itself... you said you're getting the same problem on a device too, zohaib? Hmmm, I wonder how to fix it....

Ad
Adam Twycross
Freshman
Freshman
 
Posts: 8
Joined: Tue Feb 24, 2009 12:40 pm

Postby MrSnowflake » Thu Feb 26, 2009 4:08 pm

What does the log say? And which sdk are you using? 1.1? I tested on 1.0 but actually there should be no difference.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby zohaib1020 » Thu Feb 26, 2009 8:01 pm

I've mostly tried it on a device, it will always force close, when I get a chance, i'll try it on an emulator when its already running. I do use SDK 1.1.
zohaib1020
Freshman
Freshman
 
Posts: 4
Joined: Wed Feb 25, 2009 2:18 am

Top

Postby MickArea » Sat Mar 14, 2009 12:00 am

hey guys, a little present because it 's my way to say thanks for this awesome forum ( thanks to MrSnowflake) :p

the menu doesn't work :( ( i don't have time to study how to use menu, if someone can do that for us :p )
but the ball is moving
it is an example to understand how using lockcanvas, surfaceview.
in other game, I use different bitmap to create different layer of my game, by example the background don't have to be refresh every time, but the layer's ball yes ...

and yes it's from lunarview with some modification.


GameTemplate.java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package template.Game;
  2.  
  3.  
  4.  
  5. import template.Game.Demoview.DemoThread;
  6.  
  7. import android.app.Activity;
  8.  
  9. import android.os.Bundle;
  10.  
  11. import android.util.Log;
  12.  
  13. import android.view.Menu;
  14.  
  15. import android.view.MenuItem;
  16.  
  17. import android.view.Window;
  18.  
  19.  
  20.  
  21. public class GameTemplate extends Activity {
  22.  
  23.  
  24.  
  25.         // Just a RANDOM ID to recognize a Message later
  26.  
  27.         protected static final int GUIUPDATEIDENTIFIER = 0x131;
  28.  
  29.  
  30.  
  31.         private DemoThread mDemoThread;
  32.  
  33.  
  34.  
  35.         /** A handle to the View in which the game is running. */
  36.  
  37.         private Demoview mDemoView;
  38.  
  39.  
  40.  
  41.  
  42.  
  43.         private static final int MENU_PAUSE = 4;
  44.  
  45.         private static final int MENU_RESUME = 5;
  46.  
  47.         private static final int MENU_START = 6;
  48.  
  49.         private static final int MENU_STOP = 7;
  50.  
  51.  
  52.  
  53.         /**
  54.  
  55.          * Invoked during init to give the Activity a chance to set up its Menu.
  56.  
  57.          *
  58.  
  59.          * @param menu the Menu to which entries may be added
  60.  
  61.          * @return true
  62.  
  63.          */
  64.  
  65.         @Override
  66.  
  67.         public boolean onCreateOptionsMenu(Menu menu) {
  68.  
  69.                 super.onCreateOptionsMenu(menu);
  70.  
  71.  
  72.  
  73.                 menu.add(0, MENU_START, 0, R.string.menu_start);
  74.  
  75.                 menu.add(0, MENU_STOP, 0, R.string.menu_stop);
  76.  
  77.                 menu.add(0, MENU_PAUSE, 0, R.string.menu_pause);
  78.  
  79.                 menu.add(0, MENU_RESUME, 0, R.string.menu_resume);
  80.  
  81.  
  82.  
  83.                 return true;
  84.  
  85.         }
  86.  
  87.  
  88.  
  89.         /**
  90.  
  91.          * Invoked when the user selects an item from the Menu.
  92.  
  93.          *
  94.  
  95.          * @param item the Menu entry which was selected
  96.  
  97.          * @return true if the Menu item was legit (and we consumed it), false
  98.  
  99.          *         otherwise
  100.  
  101.          */
  102.  
  103.         @Override
  104.  
  105.         public boolean onOptionsItemSelected(MenuItem item) {
  106.  
  107.                 switch (item.getItemId()) {
  108.  
  109.                 case MENU_START:
  110.  
  111.                         mDemoThread.doStart();
  112.  
  113.                         return true;
  114.  
  115.                 case MENU_STOP:
  116.  
  117.                         mDemoThread.setState(mDemoThread.STATE_LOSE);
  118.  
  119.                         return true;
  120.  
  121.                 case MENU_PAUSE:
  122.  
  123.                         mDemoThread.pause();
  124.  
  125.                         return true;
  126.  
  127.                 case MENU_RESUME:
  128.  
  129.                         mDemoThread.unpause();
  130.  
  131.                         return true;
  132.  
  133.                 }
  134.  
  135.  
  136.  
  137.                 return false;
  138.  
  139.         }
  140.  
  141.  
  142.  
  143.  
  144.  
  145.         /** Called when the activity is first created. */
  146.  
  147.         @Override
  148.  
  149.         public void onCreate(Bundle savedInstanceState) {
  150.  
  151.                 super.onCreate(savedInstanceState);
  152.  
  153.                 this.requestWindowFeature(Window.FEATURE_NO_TITLE);
  154.  
  155.                
  156.  
  157.                 setContentView(R.layout.main);
  158.  
  159.                
  160.  
  161.  
  162.  
  163.                  mDemoView = (Demoview) findViewById(R.id.demo);
  164.  
  165.                  mDemoThread = mDemoView.getThread();
  166.  
  167.         }
  168.  
  169.         /**
  170.  
  171.          * Invoked when the Activity loses user focus.
  172.  
  173.          */
  174.  
  175.         @Override
  176.  
  177.         protected void onPause() {
  178.  
  179.                 super.onPause();
  180.  
  181.                 mDemoView.getThread().pause(); // pause game when Activity pauses
  182.  
  183.         }
  184.  
  185.         /**
  186.  
  187.          * Notification that something is about to happen, to give the Activity a
  188.  
  189.          * chance to save state.
  190.  
  191.          *
  192.  
  193.          * @param outState a Bundle into which this Activity should save its state
  194.  
  195.          */
  196.  
  197.         @Override
  198.  
  199.         protected void onSaveInstanceState(Bundle outState) {
  200.  
  201.                 // just have the View's thread save its state into our Bundle
  202.  
  203.                 super.onSaveInstanceState(outState);
  204.  
  205.  
  206.  
  207.                 Log.w(this.getClass().getName(), "SIS called");
  208.  
  209.         }
  210.  
  211. }
Parsed in 0.044 seconds, using GeSHi 1.0.8.4


DemoView.java
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package template.Game;
  2.  
  3.  
  4.  
  5.  
  6.  
  7. import android.content.Context;
  8.  
  9. import android.content.res.Resources;
  10.  
  11. import android.graphics.Bitmap;
  12.  
  13. import android.graphics.Canvas;
  14.  
  15. import android.graphics.Color;
  16.  
  17. import android.graphics.Paint;
  18.  
  19. import android.graphics.Point;
  20.  
  21. import android.graphics.Bitmap.Config;
  22.  
  23. import android.util.AttributeSet;
  24.  
  25. import android.util.Log;
  26.  
  27. import android.view.KeyEvent;
  28.  
  29. import android.view.SurfaceHolder;
  30.  
  31. import android.view.SurfaceView;
  32.  
  33.  
  34.  
  35. public class Demoview extends SurfaceView implements SurfaceHolder.Callback {
  36.  
  37.  
  38.  
  39.         private boolean[] keys;
  40.  
  41.         /*Keys code :*/
  42.  
  43.         private static final int UP = 0;
  44.  
  45.         private static final int DOWN = 1;
  46.  
  47.         private static final int LEFT = 2;
  48.  
  49.         private static final int RIGHT = 3;
  50.  
  51.  
  52.  
  53.         public DemoThread thread;
  54.  
  55.  
  56.  
  57.         enum direction {UP,DOWN};
  58.  
  59.  
  60.  
  61.         public class DemoThread extends Thread {
  62.  
  63.  
  64.  
  65.                 protected static final int GUIUPDATEIDENTIFIER = 0x231;
  66.  
  67.  
  68.  
  69.                 /*
  70.  
  71.                  * State-tracking constants
  72.  
  73.                  */
  74.  
  75.                 public static final int STATE_LOSE = 1;
  76.  
  77.                 public static final int STATE_PAUSE = 2;
  78.  
  79.                 public static final int STATE_READY = 3;
  80.  
  81.                 public static final int STATE_RUNNING = 4;
  82.  
  83.                 public static final int STATE_WIN = 5;
  84.  
  85.  
  86.  
  87.         public static final int PHYS_SLEW_SEC = 120;
  88.  
  89.  
  90.  
  91.                 private static final int SIZE_BALL = 0;
  92.  
  93.  
  94.  
  95.                 private static final int SIZE = 15;
  96.  
  97.  
  98.  
  99.                 private SurfaceHolder mSurfaceHolder;
  100.  
  101.  
  102.  
  103.  
  104.  
  105.                 Paint paint;
  106.  
  107.  
  108.  
  109.                 /*Bitmap for drawing :*/
  110.  
  111.                 private Bitmap BitmapTop;
  112.  
  113.                 /*Canvas*/
  114.  
  115.                 private Canvas canvasTop;
  116.  
  117.  
  118.  
  119.  
  120.  
  121.                 /*Time*/
  122.  
  123.                 private long mLastTime=0;
  124.  
  125.                 private long now;
  126.  
  127.                 private long elapsed;
  128.  
  129.  
  130.  
  131.                 /*screensize*/
  132.  
  133.                 private int W,H;
  134.  
  135.  
  136.  
  137.                 /*GameMode*/
  138.  
  139.                 private int mMode=STATE_RUNNING;
  140.  
  141.                 private boolean mRun=false;
  142.  
  143.  
  144.  
  145.  
  146.  
  147.                 /*ball's position*/
  148.  
  149.                 Point ballPosition;
  150.  
  151.  
  152.  
  153.                 /*Ball direction*/
  154.  
  155.                 direction dir;
  156.  
  157.  
  158.  
  159.                 private boolean update=false;
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.                 public DemoThread(SurfaceHolder surfaceHolder, Context context) {
  170.  
  171.                         mSurfaceHolder = surfaceHolder;
  172.  
  173.  
  174.  
  175.                         // arbitrary :
  176.  
  177.                         W=100;H=100;
  178.  
  179.  
  180.  
  181.                         BitmapTop = Bitmap.createBitmap(W, H, Config.ARGB_4444);
  182.  
  183.  
  184.  
  185.                         // init paint :
  186.  
  187.                         paint = new Paint();
  188.  
  189.                         paint.setStyle(Paint.Style.FILL_AND_STROKE);
  190.  
  191.                         paint.setColor(Color.WHITE);
  192.  
  193.                         paint.setAntiAlias(true);
  194.  
  195.  
  196.  
  197.  
  198.  
  199.                         // assign the canvas to a specific bitmap
  200.  
  201.                         canvasTop= new Canvas();
  202.  
  203.                         canvasTop.setBitmap(BitmapTop);
  204.  
  205.  
  206.  
  207.                 }
  208.  
  209.  
  210.  
  211.                 public void setRunning(boolean b) {
  212.  
  213.                         mRun = b;
  214.  
  215.                         if(mRun) {
  216.  
  217.                                 Log.d("state","dostart");
  218.  
  219.                                 doStart();
  220.  
  221.                         }
  222.  
  223.                 }
  224.  
  225.  
  226.  
  227.                 /**
  228.  
  229.                  * Sets the game mode. That is, whether we are running, paused, in the
  230.  
  231.                  * failure state, in the victory state, etc.
  232.  
  233.                  *
  234.  
  235.                  */
  236.  
  237.                 public void setState(int mode) {
  238.  
  239.                         synchronized (mSurfaceHolder) {
  240.  
  241.                                 mMode=mode;
  242.  
  243.                         }
  244.  
  245.                 }
  246.  
  247.  
  248.  
  249.                 /* Callback invoked when the surface dimensions change. */
  250.  
  251.                 public void setSurfaceSize(int width, int height) {
  252.  
  253.                         // synchronized to make sure these all change atomically
  254.  
  255.                         synchronized (mSurfaceHolder) {
  256.  
  257.                                 W = width;
  258.  
  259.                                 H = height;
  260.  
  261.  
  262.  
  263.                                 BitmapTop = Bitmap.createBitmap(W, H, Config.ARGB_4444);
  264.  
  265.  
  266.  
  267.                                 canvasTop.setBitmap(BitmapTop);
  268.  
  269.  
  270.  
  271.                         }
  272.  
  273.                 }
  274.  
  275.  
  276.  
  277.                 public void doStart() {
  278.  
  279.  
  280.  
  281.                         // init the ball position :
  282.  
  283.                         ballPosition=new Point(W/2,H/2);
  284.  
  285.                         // init direction :
  286.  
  287.                         dir=direction.DOWN;
  288.  
  289.  
  290.  
  291.  
  292.  
  293.                         mLastTime = System.currentTimeMillis() + 100;
  294.  
  295.                         setState(STATE_RUNNING);
  296.  
  297.                 }
  298.  
  299.                 /**
  300.  
  301.                  * Resumes from a pause.
  302.  
  303.                  */
  304.  
  305.                 public void unpause() {
  306.  
  307.                         // Move the real time clock up to now
  308.  
  309.                         synchronized (mSurfaceHolder) {
  310.  
  311.                                 // Move the real time clock up to now
  312.  
  313.                                 synchronized (mSurfaceHolder) {
  314.  
  315.                                         mLastTime = System.currentTimeMillis() + 100;
  316.  
  317.                                 }
  318.  
  319.                                 setState(STATE_RUNNING);
  320.  
  321.  
  322.  
  323.                         }
  324.  
  325.  
  326.  
  327.                 }
  328.  
  329.                 /**
  330.  
  331.                  * Pauses the physics update & animation.
  332.  
  333.                  */
  334.  
  335.                 public void pause() {
  336.  
  337.                         synchronized (mSurfaceHolder) {
  338.  
  339.                                 if (mMode == STATE_RUNNING) setState(STATE_PAUSE);
  340.  
  341.                         }
  342.  
  343.                 }
  344.  
  345.  
  346.  
  347.                 @Override
  348.  
  349.                 public void run() {
  350.  
  351.                         while (mRun) {
  352.  
  353.                                 Canvas c = null;
  354.  
  355.                                 try {
  356.  
  357.                                         c = mSurfaceHolder.lockCanvas(null);
  358.  
  359.                                         synchronized (mSurfaceHolder) {
  360.  
  361.                                                 if (mMode == STATE_RUNNING) {
  362.  
  363.                                                         updatePhysics();
  364.  
  365.                                                         doDraw(c);
  366.  
  367.                                                 }
  368.  
  369.                                         }
  370.  
  371.                                 } finally {
  372.  
  373.                                         // do this in a finally so that if an exception is thrown
  374.  
  375.                                         // during the above, we don't leave the Surface in an
  376.  
  377.                                         // inconsistent state
  378.  
  379.                                         if (c != null) {
  380.  
  381.                                                 mSurfaceHolder.unlockCanvasAndPost(c);
  382.  
  383.                                         }
  384.  
  385.                                 }
  386.  
  387.  
  388.  
  389.                         }
  390.  
  391.                 }
  392.  
  393.                 private void doDraw(Canvas c) {
  394.  
  395.                        
  396.  
  397.                         if(update) {
  398.  
  399.                                 canvasTop.drawColor(Color.BLACK);
  400.  
  401.                                 canvasTop.drawCircle(ballPosition.x, ballPosition.y, SIZE/2, paint);
  402.  
  403.                                 update=false;
  404.  
  405.                         }
  406.  
  407.                         // drawing here
  408.  
  409.                        
  410.  
  411.                         // erase the canvas :
  412.  
  413.                         c.drawColor(Color.BLACK);
  414.  
  415.                         // print the bitmap :
  416.  
  417.                         c.drawBitmap(BitmapTop, 0, 0, null);
  418.  
  419.                        
  420.  
  421.  
  422.  
  423.                 }
  424.  
  425.  
  426.  
  427.  
  428.  
  429.                 private void updatePhysics() {
  430.  
  431.                         now = System.currentTimeMillis();
  432.  
  433.  
  434.  
  435.                         // Do nothing if mLastTime is in the future.
  436.  
  437.                         // This allows the game-start to delay the start of the physics
  438.  
  439.                         // by 100ms or whatever.
  440.  
  441.                         if (mLastTime > now) return;
  442.  
  443.  
  444.  
  445.                         double elapsed = (now - mLastTime) / 1000.0;
  446.  
  447.                         //Physics here
  448.  
  449.                         boolean ok=false;
  450.  
  451.                         Point virtualPosition;
  452.  
  453.                         do {
  454.  
  455.                                 virtualPosition=new Point(ballPosition);
  456.  
  457.                                
  458.  
  459.                                 // move the virtual point
  460.  
  461.                                 if(dir==direction.UP) {
  462.  
  463.                                         virtualPosition.y-=2*elapsed*PHYS_SLEW_SEC;
  464.  
  465.                                 }else {
  466.  
  467.                                         virtualPosition.y+=2*elapsed*PHYS_SLEW_SEC;
  468.  
  469.                                        
  470.  
  471.                                 }
  472.  
  473.                                 // change the position if it's needed
  474.  
  475.                                 if( (virtualPosition.y-SIZE_BALL/2)<0 || (virtualPosition.y+SIZE_BALL/2)>H) {
  476.  
  477.                                         if(dir==direction.UP)dir=direction.DOWN;
  478.  
  479.                                         else dir=direction.UP;
  480.  
  481.                                 }else ok=true;
  482.  
  483.  
  484.  
  485.                         }while(!ok);
  486.  
  487.  
  488.  
  489.                         // update position
  490.  
  491.                         ballPosition=virtualPosition;
  492.  
  493.                        
  494.  
  495.                         // update the bitmap :
  496.  
  497.                         update=true;
  498.  
  499.  
  500.  
  501.                         // update last time
  502.  
  503.                         mLastTime = now;
  504.  
  505.  
  506.  
  507.                 }
  508.  
  509.  
  510.  
  511.  
  512.  
  513.  
  514.  
  515.  
  516.  
  517.                 ///////////
  518.  
  519.         }
  520.  
  521.  
  522.  
  523.  
  524.  
  525.         public Demoview(Context context, AttributeSet attrs) {
  526.  
  527.                 super(context, attrs);
  528.  
  529.  
  530.  
  531.  
  532.  
  533.                 // register our interest in hearing about changes to our surface
  534.  
  535.                 SurfaceHolder holder = getHolder();
  536.  
  537.                 holder.addCallback(this);
  538.  
  539.  
  540.  
  541.                 thread = new DemoThread(holder, context);
  542.  
  543.  
  544.  
  545.  
  546.  
  547.                 setFocusable(true); // make sure we get key events
  548.  
  549.  
  550.  
  551.         }
  552.  
  553.  
  554.  
  555.         public DemoThread getThread() {
  556.  
  557.                 return thread;
  558.  
  559.         }
  560.  
  561.         // key manager :
  562.  
  563.         @Override
  564.  
  565.         public boolean onKeyDown(int keyCode, KeyEvent event){
  566.  
  567.  
  568.  
  569.                 if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
  570.  
  571.                         keys[ UP ]=true;
  572.  
  573.                 }
  574.  
  575.                 //
  576.  
  577.                 else if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
  578.  
  579.                         keys[ DOWN ]=true;
  580.  
  581.                 }
  582.  
  583.                 //
  584.  
  585.                 else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
  586.  
  587.                         keys[ LEFT ]=true;
  588.  
  589.                 }
  590.  
  591.  
  592.  
  593.                 //
  594.  
  595.                 else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
  596.  
  597.                         keys[ RIGHT ]=true;
  598.  
  599.                 }
  600.  
  601.                 //
  602.  
  603.  
  604.  
  605.                 return true;
  606.  
  607.         }
  608.  
  609.         @Override
  610.  
  611.         public boolean onKeyUp(int keyCode, KeyEvent msg) {
  612.  
  613.  
  614.  
  615.                 if (keyCode == KeyEvent.KEYCODE_DPAD_UP)keys[ UP ]=false;
  616.  
  617.                 //
  618.  
  619.                 else if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN)keys[ DOWN ]=false;
  620.  
  621.                 //
  622.  
  623.                 else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT)keys[ LEFT ]=false;
  624.  
  625.                 //
  626.  
  627.                 else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT)keys[ RIGHT ]=false;
  628.  
  629.                 //
  630.  
  631.  
  632.  
  633.  
  634.  
  635.  
  636.  
  637.                 return true;
  638.  
  639.         }
  640.  
  641.  
  642.  
  643.         /**
  644.  
  645.          * Standard window-focus override. Notice focus lost so we can pause on
  646.  
  647.          * focus lost. e.g. user switches to take a call.
  648.  
  649.          */
  650.  
  651.         @Override
  652.  
  653.         public void onWindowFocusChanged(boolean hasWindowFocus) {
  654.  
  655.                 if (!hasWindowFocus) thread.pause();
  656.  
  657.         }
  658.  
  659.  
  660.  
  661.         /* Callback invoked when the surface dimensions change. */
  662.  
  663.         public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
  664.  
  665.                 thread.setSurfaceSize(width, height);
  666.  
  667.         }
  668.  
  669.  
  670.  
  671.         /*
  672.  
  673.          * Callback invoked when the Surface has been created and is ready to be
  674.  
  675.          * used.
  676.  
  677.          */
  678.  
  679.         public void surfaceCreated(SurfaceHolder holder) {
  680.  
  681.                 // start the thread here so that we don't busy-wait in run()
  682.  
  683.                 // waiting for the surface to be created
  684.  
  685.                 thread.setRunning(true);
  686.  
  687.                 thread.start();
  688.  
  689.         }
  690.  
  691.  
  692.  
  693.         /*
  694.  
  695.          * Callback invoked when the Surface has been destroyed and must no longer
  696.  
  697.          * be touched. WARNING: after this method returns, the Surface/Canvas must
  698.  
  699.          * never be touched again!
  700.  
  701.          */
  702.  
  703.         public void surfaceDestroyed(SurfaceHolder holder) {
  704.  
  705.                 //Log.i("test","destruction surface");
  706.  
  707.                 // we have to tell thread to shut down & wait for it to finish, or else
  708.  
  709.                 // it might touch the Surface after we return and explode
  710.  
  711.                 boolean retry = true;
  712.  
  713.                 thread.setRunning(false);
  714.  
  715.                 while (retry) {
  716.  
  717.                         try {
  718.  
  719.                                 thread.join();
  720.  
  721.                                 retry = false;
  722.  
  723.                         } catch (InterruptedException e) {
  724.  
  725.                         }
  726.  
  727.                 }
  728.  
  729.         }
  730.  
  731.  
  732.  
  733. }
Parsed in 0.097 seconds, using GeSHi 1.0.8.4



main.xml into res/layout/
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4.  
  5.         android:layout_width="fill_parent" android:layout_height="fill_parent">
  6.  
  7.  
  8.  
  9.         <template.Game.Demoview android:id="@+id/demo"
  10.  
  11.                 android:layout_width="fill_parent" android:layout_height="fill_parent" />
  12.  
  13.  
  14.  
  15.  
  16.  
  17. </FrameLayout>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


strings.xml into res/values/
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <resources>
  4.  
  5.     <string name="hello">Hello World, GameTemplate</string>
  6.  
  7.     <string name="app_name">GameTemplate</string>
  8.  
  9.    
  10.  
  11.     <string name="menu_start">Start</string>
  12.  
  13.     <string name="menu_stop">Stop</string>
  14.  
  15.     <string name="menu_pause">Pause</string>
  16.  
  17.     <string name="menu_resume">Resume</string>
  18.  
  19.    
  20.  
  21.     <string name="message_stopped">Stopped</string>
  22.  
  23. </resources>
Parsed in 0.003 seconds, using GeSHi 1.0.8.4


androidmanifest :

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  5.  
  6.      package="template.Game"
  7.  
  8.      android:versionCode="1"
  9.  
  10.      android:versionName="1.0.0">
  11.  
  12.     <application android:icon="@drawable/icon" android:label="@string/app_name">
  13.  
  14.         <activity android:name=".GameTemplate"
  15.  
  16.                  android:label="@string/app_name">
  17.  
  18.             <intent-filter>
  19.  
  20.                 <action android:name="android.intent.action.MAIN" />
  21.  
  22.                 <category android:name="android.intent.category.LAUNCHER" />
  23.  
  24.             </intent-filter>
  25.  
  26.         </activity>
  27.  
  28.     </application>
  29.  
  30. </manifest>
Parsed in 0.003 seconds, using GeSHi 1.0.8.4
Attachments
GameTemplate.zip
it's better than copy and past :p
isn't it ?
(40.01 KiB) Downloaded 553 times
MickArea
Junior Developer
Junior Developer
 
Posts: 23
Joined: Mon Feb 23, 2009 4:42 pm

Postby MrSnowflake » Sat Mar 14, 2009 10:33 am

MickArea wrote:hey guys, a little present because it 's my way to say thanks for this awesome forum ( thanks to MrSnowflake) :p
You are very much welcome!!! Nice of you to post your own sollution. The game template is up for some improvement :). I'll ge to that when I have the time.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby MrSnowflake » Wed Mar 18, 2009 1:52 pm

I'm working on an improved template. It's now more the basics of a platform and it now functions when onResume and onPause get called (so no more crashing :) ).
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby MickArea » Sat Mar 21, 2009 12:06 am

yeah it's a good idea :p because i don't know how to stop an activity specialy when i have a surfaceview ;)
MickArea
Junior Developer
Junior Developer
 
Posts: 23
Joined: Mon Feb 23, 2009 4:42 pm

Is LunarLander buggy?

Postby a_droid » Fri Mar 27, 2009 6:58 pm

Hi!

It seems to me that the LunarLander example has a serious bug (or I'm seriously stupid :) ):

If you press the home button, then you try to go back to the application (on a real G1 with the latest software), it will tell you that LunarLander has stopped unexpectedly.

The reason I've found:
- It creates the thread on the View's create.
- It destroys the thread (tells the thread to stop and waits for it to exit) on surface destroy.
- Android won't destroy/recreate your view on pressing the home button (going to the home screen and back)
- Android destroys the surface and creates the surface on pressing the home button (going to the home screen and back)

All this breaks one rule: It tries to restart a thread with an exited run() loop. ..and it gives you this error.

So, in my app, I've moved the creation/starting of the thread into the surface's code.
This way I'm always re-creating the thread after I destroy it (at the surface's destroy, like in the original code).

I hope this helps someone ;)
a_droid
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Mar 27, 2009 6:49 pm

Postby Adam Twycross » Tue Apr 07, 2009 2:21 pm

Still having loads of fun learning from this template, it's great that so many people are taking the time to share and help out!

I'm trying to figure out how to disable the automatic rotation that occurs when the keyboard slides out on the G1... going from portrait to landsape. Anyone got any ideas what needs to be changed to stop that happening? I've added

android:screenOrientation="portrait"

to the Manifest, but it's not stopping it!

Adam
Adam Twycross
Freshman
Freshman
 
Posts: 8
Joined: Tue Feb 24, 2009 12:40 pm

Postby Adam Twycross » Tue Apr 07, 2009 4:16 pm

Also, at the risk of being a real pain, MrSnowFlake, I wonder if you could give me a few pointers as to exactly how the GameTemplate as it stands (and the Lunar Lander on which it's based) is incomplete in it's onResume and onPause methods?

I'm getting the fact that this is what is causing it to crash with a "stopped unexpectedly" message, but I'm a little unclear as to why.

Really looking forward to unpicking your new template with these issues addressed, and it'd be really useful to know what you're adding to get the onResume and onPause methods functioning correctly (in advance if possible!)


Maybe that way I could even try to debug my version and then check it against your finished template and see how well I understood and implemented it! :)

Again, many thanks for such useful code and help for all us newbies!
Adam Twycross
Freshman
Freshman
 
Posts: 8
Joined: Tue Feb 24, 2009 12:40 pm

Postby WarrenFaith » Thu Apr 09, 2009 4:37 pm

@MrSnowFlake
What is the difference between your DroidGaming and the GameTemplate?
Which one are you going to developing further?
thanks in advance
WarrenFaith
Moderator
Moderator
 
Posts: 227
Joined: Fri Mar 13, 2009 10:59 am
Location: Berlin, Germany

2D Tutorial

Postby robotissues » Fri Apr 24, 2009 7:20 pm

How is the new and improved 2D tutorial coming along? I am having issues with the onPause and onResume as well ... with a twist. When I call a different view while the SurfaceView is active, in my case a listview with properties I would like to assign elements of my interface with, on resume/return the thread is dead, and so nothing draws. I noticed that in this case, if I pause the thread while the list does its thing, the list draws funny.

j
robotissues
Once Poster
Once Poster
 
Posts: 1
Joined: Fri Apr 24, 2009 7:11 pm

Top
PreviousNext

Return to Android 2D/3D Graphics - OpenGL Tutorials

Who is online

Users browsing this forum: No registered users and 4 guests