In fact, I am trying to load all the bitmaps which I need for the whole game. I don't think this is the right thing to do, I would much more prefer to load only the bitmaps which I need for the first level, and load bitmaps for another level as and when I start that particular level, but I don't know how to do it either - to load the bitmaps later, I need the Context and I don't know how to pass it into onDrawFrame function, where is my main game loop (as explained in my other post http://www.gamedev.n...__fromsearch__1)
Ahhh
You don't want to pass the context to onDrawFrame the interface won't let you. What you can do is pass the context into the Renderer so that it is available for use elsewhere.
This might get a bit long and so I apologise if i am being too verbose and at too simple a level. Also I am not making any claims that this is the only way to do things.
You have your Activity (Generally you will have an activity for the game, and 1 or more activities for menu screens and so forth). Moving between your activities is a place where you can decide to do some loading of the things you need for the next activity and free up any memory (If this is going to be a time consuming process you display a lightweight loading screen while the loading takes place).
In your activity for your game I am assuming you will have your GlSurfaceView and your GlRenderer of some kind. The surface view as far as I recall has a setRenderer method that allows you to inject the renderer you want to use. The GlRenderer exposes the onDrawFrame(GL10 gl) method which will get called constantly by the surfaceView. It sounds like your onDrawFrame(GL10 gl) looks a little like the below.
public void onDrawFrame(GL10 gl) {
endTime = System.currentTimeMillis();
long dt = endTime - startTime;
int fps = Math.round(1000f / dt);
if (fps < TargetFps) {
try {
Thread.sleep(TargetFps - fps);
} catch (InterruptedException e) {}
}
startTime = System.currentTimeMillis();
// update the game state
// render the game using the gl context
}So your question is how do I get my Bitmaps into memory such that my Level has access to them?
I would very much recommend using a class purely for loading the bitmaps (This can be a Singleton but you have to be careful about how you work with a Singleton in Android as your application can be killed at anytime and thus your Singleton has to rebuild its state when the app resumes. For managers of things like bitmaps and sounds this generally is not a big deal and easily handled.).
You could possibly do something like the below if you use a singleton style object for handling your bitmap management.
// This is just off the cuff code, i don't expect it to compile it's just to give you an idea.
public class GameActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyGameLevel level = createTheLevel();
GlSurfaceView view = // Get a hold of your view or create it
Renderer r = new LevelRenderer(level);
view.setRenderer(r);
// If this causes noticeable issues on starting the activity don't do it
Runtime.getRuntime().gc();
}
private MyGameLevel createTheLevel() {
// Create a level it can load the textures
MyGameLevel level = new MyGameLevel();
// Load all the textures i need (this could be done by the level class this is just an example for arguments sake)
MyTextureManager.instance().loadBitmap(context, R.drawable.whatever);
MySoundManager.instance().loadSound(context, R.raw.sound1);
// etc...but all your resources are loaded
}
}
public class LevelRenderer extends Renderer {
MyGameLevel level;
public LevelRenderer(MyGameLevel l) {
level = l;
}
@Override
public void onDrawFrame(GL10 gl) {
// Game loop
level.update(dt);
level.render(gl);
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
// You can do this or you can wait until you call level.render before binding the textures, your implementation will suit your needs
level.bindGlTextures(gl, config);
}
}
public class MyGameLevel() {
public void bindGlTextures(Gl10 gl, EGLConfig config) {
// here you can use the MyTextureManager to get the bitmaps and bind them to your gl context as you call this as soon as the surface is
// created...if you wan't to, delay this until the render call it is fine the important thing is the Textures exist already in the manager in bitmap form.
// something like ....can't quite remember the syntax
gl.bindTexture(GL_TEXTURE_2D, MyTextureManager.instance().getBitmap(R.drawable.whatever);
}
public void render(GL10 gl) { // draw the level }
public void update(long dt) { // update the level }
}There are many ways to achieve the same thing so what I have posted above is one method for loading the Bitmaps and providing access to them. In the onfinish method of the activity you can clear the texture manager and recycle any bitmaps you no longer need. Does this help at all?
I realised I didn't post any code for the MyTextureManager class I made up in the example above...however I think you can get the idea. You are just using that as a cache to store your Bitmaps. The method to load a bitmap takes the context which is readily available in the Activity (where I used it in my example..but it can be wherever you pass the context...you could for example call getApplicationContext() in your Activity and pass that context into the Level and have the Level manage putting the Bitmaps it needs into the MyTextureManager) and a resource ID. Thus you would just call BitmapFactory.decodeResource(context.getResources(), resourceId). Obviously how you architect things will be suited to your needs I am just proposing ideas to get you thinking.

Find content
Male