Skeleton for an Android app

Android as a platform is very hot nowadays. Many people want to develop software for it and the base of end users is growing all the time. Probably because Android based devices are everywhere, they exist in form of smart phones, tablets and TVs.

One basic rule for an Android app is that it’s graphics shouldn’t be fixed sized, the graphics should be scaled into target Android devices resolution. In this short blog I present my skeleton Android program that scales it’s graphics into target android devices maximum resolution and as a convenience has an exit button. Application’s own exit button is one feature that I miss in many Android applications.

The code is written in Monkey X Pro, which is a great multiplatform programming language from Blitz Research Ltd.

To the code…

Import mojo

Function Main()
  New MyApp
End

Class MyApp Extends App

Const BG_WIDTH:Int = 800
Const BG_HEIGHT:Int = 600

Const RADIUS:Int = 100
Const ORIGIN_X:Float = (BG_WIDTH - 100) / 2.0
Const ORIGIN_Y:Float = (BG_HEIGHT - 100) / 2.0

Global touchX:Int, touchY:Int
Global ovalX:Float, ovalY:Float
Global scaleX:Float, scaleY:Float
Global devWidth:Float, devHeight:Float
Global angle:Float = 0

Method OnCreate()

  devWidth = DeviceWidth()
  devHeight = DeviceHeight()

  scaleX = devWidth / BG_WIDTH
  scaleY = devHeight / BG_HEIGHT

  SetUpdateRate(60)
End

Method OnUpdate()

  ' Is exit button touched?
  If TouchHit() > 0 Then
  touchX = TouchX()
  touchY = TouchY()
  If touchX >= (BG_WIDTH - 50) * scaleX And touchX <= (BG_WIDTH - 25) * scaleX And touchY >= 25 * scaleY And touchY <= (25 + 25) * scaleY Then ExitApp()
Endif

  angle = angle + 1
  ovalX = ORIGIN_X + Cos(angle) * RADIUS
  ovalY = ORIGIN_Y + Sin(angle) * RADIUS
End

Method OnRender()

  ' Scale the graphics
  PushMatrix()
  Scale (scaleX,scaleY)

  SetColor 255,255,0
  ' Here could be bg image
  ' drawn with DrawImage
  DrawRect 0,0,BG_WIDTH,BG_HEIGHT

  ' Circle
  SetColor 0,0,255
  DrawOval ovalX,ovalY,20,20

  ' "Exit button"
  SetColor 255,0,0
  DrawRect BG_WIDTH - 50, 25, 25, 25

  PopMatrix()
End

Function ExitApp:Void()
  Error "" ' Exit without error
End

End Class

The program draws fullscreen yellow background, an exit button and a ball that moves around the screen.

android

Compiled to desktop

The scaling is made by pushing current drawing matrix to internal matrix stack and then scaling it by the screen ratio.

With free Monkey X you can compile to desktop and HTML5 and it comes with MinGW that is also needed in Monkey X Pro.

How to retrieve the %AppData% folder

AppData folder is used to store data like hiscores, config, etc. of a game (or app). The AppData folder is a special folder that’s location is related to Windows version one is using. This is why AppData folder must be retrieved with the aid of the system.

In Blitz3D retrieving the AppData folder is as simple as this:

AppDataFolder$ = GetEnv$(”AppData”)

In BlitzMax retrieving the AppData folder isn’t this straight forward. The easiest way to get the location of this special folder is to use external module, BAH.Volumes. Once this module is installed for BlitzMax and imported in the beginning of the code, retrieving the AppData folder is as easy as this:

AppDataFolder = GetUserAppDir()

Above is assumed that the variable AppDataFolder is defined as String.

In order to install external modules to BlitzMax, MinGW must be installed and configured for BlitzMax. The BlitzMax product page has a link to forum topic that explains how to set up MinGW for BlitzMax.

Link: BAH.Volumes (from Google Code)

Update!

With good old ANSI C the %AppData% folder can be retrieved as follows:

#include <stdio.h>
#include <stdlib.h>

void main(void) {

	char *appdata;
	appdata = getenv("AppData");
	printf("%s\n",appdata);

}