Using andEngine from emacs

I was trying to test some game pro­gram­ming in android, a good looking library is AndEngine, the tutorials I found were Eclipse centered, but after trying and ending a couple of times with a segfault, importing a project! it's time to go back to the classics, so let's see how to do it with emacs.

Install android-mode

We'll get android-mode, to install it download it to the path, if we want it to be global we'd do it to /usr/share/emacs/site-lisp/ (to do this, root per­mis­sions may be required), or else we could add the directory to the path with

1
(add-to-list 'load-path "<directory>")

After that, we have to load it and set the android SDK path, I placed it on /home/kenkeiras/.android-sdk/, so...

1
2
(require 'android-mode)
(setq android-mode-sdk-dir "/home/kenkeiras/.android-sdk/")

This things can be done in the *Scratch* buffer, but we'd have to repeat it every time we'll use the plugin, or write it on the init.el file to do it on startup, if we want to apply the changes at the moment we'll do it pressing C-x C-e (Control-x, Control-x) on each day end.

Create a project

Once we have this installed we'll have to create a project, to do this type M-x android-create-project (Alt-x android-create-project) on emacs, the plugin will ask for a directory to place the project on, a name for the package and the main activity and a target. The target is the Android version to “compile” the code for, push Tab twice probably will make it show the available options (I'm not sure, since it may be emacs-live doing).

With that we'll have created the project, if we type

1
M-x android-build-debug (Alt-x android-build-debug)

and

1
M-x android-build-install (Alt-x android-build-install)

it'll build the project and transfer it to the active device, and then it can be launched.

Hello world

Building AndEngine

To do this we'll download the project on other directory, for example from the repository

1
git clone https://github.com/nicolasgramlich/AndEngine

Inside the directory we'll create a file named local.properties with the SDK path, in my case

1
sdk.dir=/home/kenkeiras/.android-sdk

With this done, we may build (taking into account that the API version is 15 by default, this can be changed in project.properties).

1
ant debug

Will produce a classes.jar file in the bin directory, which has to be copied to the libs directory of our project.

Sample code

At last, we'll check if it works with some code taken from Beggining andengine

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package com.example.sampleandengineapp;

import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.ui.activity.SimpleBaseGameActivity;

public class MainActivity extends SimpleBaseGameActivity{

    static final int CAMERA_WIDTH = 800;
    static final int CAMERA_HEIGHT = 480;


    @Override
    public EngineOptions onCreateEngineOptions(){
        Camera mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
        return new EngineOptions(
            true,
            ScreenOrientation.LANDSCAPE_SENSOR,
            new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT),
            mCamera);
    }


    @Override
    protected void onCreateResources(){
    }


    @Override
    protected Scene onCreateScene(){
        Scene scene = new Scene();
        scene.setBackground(new Background(0.09704f, 0.3454f, 0));
        return scene;
    }
}

It's not the big thing, but it works

just a background

(Yes, is a green screen :P)