Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


My custom class for android gaming


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
No replies to this topic

#1 sheep19   Members   -  Reputation: 413

Like
0Likes
Like

Posted 22 June 2012 - 01:42 PM

Here's my class that I created to use for android games. It creates a separate thread to call the update method.

Screen.java
package com.greenbits.RiverCross;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
public abstract class Screen extends View
{
private UpdateThread thread; // tread that calls the update method
private boolean isPaused;
private int screenWidth, screenHeight;

// constructor #1
public Screen(Context context)
{
  super(context);
 
}
// constructor #2
public Screen(Context context, AttributeSet attrs)
{
  super(context, attrs);
}

// constructor #3
public Screen(Context context, AttributeSet attrs, int defStyle)
{
  super(context, attrs, defStyle);
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh)
{
  screenWidth = w;
  screenHeight = h;
 
  super.onSizeChanged(w, h, oldw, oldh);
}

/**
  * @return The width of this object in pixels
  */
public int screenWidth() { return screenWidth; }

/**
  * @return The height of this object in pixels
  */
public int screenHeight() { return screenHeight; }

/**
  * Pauses the update thread (actually stops it). Should be called when onPause() is called.
  */
public void pause()
{
  thread.running = false; // will cause it to exit the update loop
}

public boolean isPaused() { return isPaused; }

/**
  * Resumes running the update() method. Should be called when onResume() is called
  */
public void resume()
{
  isPaused = false;
  thread = new UpdateThread(this);
  thread.start();
}

public abstract void update(float dt);

@Override
public abstract void draw(Canvas canvas);

/**
  * Should be called when the Screen object is no longer needed, to free
  * allocated resources.
  */
public abstract void dispose();

private class UpdateThread extends Thread
{
  Screen s;
  public boolean running = true;
 
  public UpdateThread(Screen _s)
  {
   s = _s;
  }
 
  public void run()
  {
   long then, now;
   float dt;
   then = now = System.currentTimeMillis();
  
   while(running)
   {
    now = System.currentTimeMillis();
    dt = (now - then) / 1000.0f;
    s.update(dt);
    then = now;
   }
  }
}
}

Note that update() that not call draw - the screen isn't redrawn all the time. A screen redraw must be scheduled by calling postInvalide(). I did this because I'm worried about performance, as I am going to use it for games that use default android 2D library which is not hardware accelerated.

In an Activity, the programmer has to call some methods in onCreate(), onPause() and onDestroy(). This is needed because the thread (that calls update()) must be paused/stopped when the user pauses the application.

Here's a small example. Note: Menu2 is a child of Screen.
package com.greenbits.RiverCross;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
public class RiverCrossActivity extends Activity
{
private static String TAG = "RiverCross";

Screen screen;

    /** Called when the activity is first created. */
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
	 screen = new Menu2(this);
	
	    super.onCreate(savedInstanceState);
	    setContentView(screen);
	   
	    Log.e(TAG, "onCreate()");
    }
   
    @Override
    protected void onPause()
    {
	 super.onPause();
	 screen.pause();
	
	 Log.e(TAG, "onPause()");
    }
   
    @Override
    protected void onResume()
    {
	 super.onResume();
	 screen.resume();
	 Log.e(TAG, "onResume()");
    }
   
    @Override
    protected void onDestroy()
    {
	 super.onDestroy();
	 screen.dispose();
    }
   
    @Override
public boolean onKeyDown(int keyCode, KeyEvent event)
    {
	 if( keyCode == KeyEvent.KEYCODE_BACK )
	  return true;
	
	 return super.onKeyDown(keyCode, event);
    }
}

Please share your thoughts. Is it going to be enough for simple 2D games? Should I redraw every frame even though I use software rendering?

Sponsor:



Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS