PDA

View Full Version : MIDLet Pascal: Stop Watch



cairnswm
27-01-2006, 09:25 AM
Not really game related so I put it in Off-Topic

A friend of mine heard I was playing with CellPhone development and asked me to write them a simple stop watch application.

I would appreciate any comments of performance imprevements and on which phones this application has problems (or works).


program StopWatch;
// ************************************************** **
// Stop Watch application for phones that do not have a
// Stop Watch installed.
// Two modes:
// Off: Allows an exit or a start stop watch command
// On: Allows stop or create split
// Only 9 splits allowed. When limit of 9 is reached,
// split option is removed.
//
// Date: 2006/01/27
// Author: William Cairns
// ************************************************** **

Var
StartTime : Integer;
LastSplit : Integer;
Splits : Array[0..9] of Integer;
SplitDisplay : Array[0..9] of String;
Start, Exit, Stop, Split : Command;

// Convert a Millisecond Time value to a MM:SS:SSS string Value.
Function MakeTimeStr(InTime : Integer): String;
Var
MS, S, M : Integer;
SMS, SS, SM : String;
Begin
MS := InTime mod 1000;
S := InTime div 1000;
M := S div 60;
S := S mod 60;
SMS := IntegerToString(MS);
SS := IntegerToString(S);
If Length(SS) = 1 then
SS := '0'+SS;
SM := IntegerToString(M);
If Length(SM) = 1 then
SM := '0'+SM;
MakeTimeStr := SM+':'+SS+':'+SMS;
End;

// Display current splits one under each other
Procedure DisplaySplits;
Var
I,Y : Integer;
Begin
Y := 20;
For I := 0 to LastSplit do
Begin
DrawText(SplitDisplay[I],0,Y);
Y := Y + GetStringHeight(SplitDisplay[I]);
End;
End;

// Clear the Split Arrays
Procedure ClearSplits;
Var
I : Integer;
Begin
For I := 0 to 9 do
Begin
Splits[I] := 0;
SplitDisplay[I] := '';
End;
End;

// This procedure is used when the stop watch is running.
// After 9 splits the plsit option is removed and it can only be stopped
Procedure ModeOn;
Var
C : Command;
Begin
Stop := CreateCommand('Stop',CM_OK,1);
Split := CreateCommand('Split',CM_EXIT,1);
Addcommand(Split);
AddCommand(Stop);
Repeat
setColor(0, 0, 0);
fillRect(0, 0, getWidth, getHeight);
setColor(255,255,255);
DrawText(MakeTimeStr(GetRelativeTimeMs - StartTime),40,0);
DisplaySplits;
Repaint;
C := getClickedCommand;
If (C = Split) or (C = Stop) then
Begin
Splits[LastSplit] := GetRelativeTimeMs - StartTime;
SplitDisplay[LastSplit] := MakeTimeStr(Splits[LastSplit]);
LastSplit := LastSplit + 1;
End;
If LastSplit = 9 then
RemoveCommand(Split);
Until C = Stop;
RemoveCommand(Stop);
RemoveCommand(Split);
End;

// Displays Title Screen - only used when there are no splits in Off Mode
Procedure ShowTitle;
var
textToDisplay : string;
textXPos : integer;
textYPos : integer;
begin
setColor(255, 255, 255);
fillRect(0, 0, getWidth, getHeight);

setColor(0, 0, 255);
setFont(FONT_FACE_PROPORTIONAL, FONT_STYLE_BOLD, FONT_SIZE_LARGE);

textToDisplay := 'Stop Watch';
textXPos := (getWidth - getStringWidth(textToDisplay)) div 2;
textYPos := (getHeight - getStringHeight(textToDisplay)) / 2;
drawText(textToDisplay, textXPos, textYPos);
textYPos := textYPos + getStringHeight(textToDisplay);

setColor(0, 0, 0);

setFont(FONT_FACE_PROPORTIONAL, FONT_STYLE_PLAIN, FONT_SIZE_SMALL);

textToDisplay := 'Created with MIDletPascal';
textXPos := (getWidth - getStringWidth(textToDisplay)) div 2;
drawText(textToDisplay, textXPos, textYPos);
textYPos := textYPos + getStringHeight(textToDisplay);
textYPos := textYPos + getStringHeight(textToDisplay);

textToDisplay := 'by William Cairns ';
textXPos := (getWidth - getStringWidth(textToDisplay)) div 2;
drawText(textToDisplay, textXPos, textYPos);
repaint;
End;

// Used when Stop Watch is not running.
Procedure ModeOff;
Var
C : Command;
Begin
Start := CreateCommand('Start',CM_OK,1);
Exit := CreateCommand('Exit',CM_EXIT,1);
Addcommand(Start);
AddCommand(Exit);
ShowTitle;
Repeat
C := getClickedCommand;
If C = Start then
Begin
RemoveCommand(Start);
RemoveCommand(Exit);
ClearSplits;
StartTime := GetRelativeTimeMs;
LastSplit := 0;
ModeOn;
AddCommand(Start);
AddCommand(Exit);
If LastSplit > 0 then
Begin
setColor(0, 0, 0);
fillRect(0, 0, getWidth, getHeight);
setColor(255,255,255);
DisplaySplits;
End;
Repaint;
End;
Until C = Exit;
RemoveCommand(Start);
RemoveCommand(Exit);
End;

begin
ModeOff;
end.


Bug Report: The Midlet does not restart properly once the splits are full - still working on it.

savage
27-01-2006, 12:46 PM
Not related to your question, but I have a Motorola V525 phone that has a few Java games on it, how do I upload your midlet to it? The only option I see is to download them from Vodaphone live, which I don't really fancy doing.

cairnswm
27-01-2006, 01:28 PM
I'll load it to my web site a bit later tonight and let you know the address.

cairnswm
27-01-2006, 01:37 PM
Try this address:
http://www.cairnsgames.co.za/files/stopwatch.jar

savage
27-01-2006, 01:47 PM
Works Great! Does not seem to have any restart issues.

My only problem is that only 7 splits fit on my screen, with the counter going at the top ( so eight lines in all ), so if I go for the full 9 splits I can't see the 8th 9th and the final stop time as I can't scroll to them.

savage
27-01-2006, 01:53 PM
A couple of work around solutions :
1. Implement a scroll feature, which may be too much hassle ( depending on how much you are being paid for this ),
2. Alternatively move the stop watch counter part to the bottom of the display so that the latest split and counter are always visible, and scroll appropriately based on the height of the screen.
3. It looks like 2 columns of information would fit, so you could implement 5 splits per column with the stop watch counter at the bottom centered.

I would also add the number 1. 2. etc infront of each split just for clarity.

I hope this helps.

savage
27-01-2006, 01:57 PM
Sorry a couple more things... No icon is associated with the application and when I look at Details it says "MIDletPascal", it would be nice if it said "Cairns Games" instead :), but I don't know if you need the licenced version to fix that.

cairnswm
27-01-2006, 02:24 PM
Thanks Savage!!!

Version 1.0.1 added
- Smaller gap between Lines
- Smaller Text Size
- Adds Number infront of each line
- CairnsGames added as Midlet Vendor
- Funny Green Icon added.

Same web link as above.

savage
27-01-2006, 03:13 PM
Hmm, still no icon on the phone, other games have icons, but none of the MIDletPascal ones do ( I downloaded Dave.jar from MIDlet as well ). I now see CairnsGames, yeah!

Everything fits on the screen now, but because there is no spacing at all between each split they overlap by about 2 pixels, which makes them a hard to read.

You might gain a slight speed improvement if you cache the height of a standard split as it is unlikely to change between splits and you would save on the function call overhead and possibly on the array lookup. Something like this...

Var
SplitTextHeight : Integer;

Procedure ModeOn;
begin
...
SplitTextHeight := GetStringHeight( '00:00:000' );
...
end;

Procedure DisplaySplits;
Var
I,Y : Integer;
Begin
Y := 19;
For I := 0 to LastSplit do
Begin
DrawText( SplitDisplay[ I ], 0, Y );
Y := Y + SplitTextHeight;
End;
End;


I would also make Y := 19; and adjust SplitTextHeight so that there is at least 1 pixel space between each split.

If you fix these, you can start selling your first MIDletPascal app! :D.

cairnswm
27-01-2006, 06:16 PM
Thanks Savage. The latest version has a hardcoded text size and while it works on some phones others overlap like you say. Only solution I can come up with is to use a small text size.

I have seen the icon being diaplayed on some phones.

The speed improvement from caching the text height is probably worth doing for this little app.

savage
27-01-2006, 06:33 PM
I noticed something else while playing that "how fast can I start stop the stop watch" game. If for example you get a time under 00:00:100 it shows up as 00:00:94 instead of 00:00:094.

I would suggest leaving both leading and trailing zeros, so it is clear exactly how many hundredth of a second there are.

PS. I did not notice any boss in this game :).

cairnswm
27-01-2006, 06:46 PM
Version 1.0.2 is now released (Same link).

Fixed:
1. Leading zeros for Milliseconds
2. Change To Text height calculations - will probably no longer fit your screen size.

The icon problem may be related to the missing JAD file. For some reason my web site will not allow me to download the JAD file. :(


PS. I did not notice any boss in this game .

RUDE!

Actually thats why its in Off Topic - its not a game!

savage
27-01-2006, 07:51 PM
PS. I did not notice any boss in this game .

RUDE!

It was not meant to be rude, just a joke.

This version works much better, but as you said, it does not fit now :(. Ah well you can't please all the people all the time.

PerIvar
31-01-2006, 02:00 PM
Hi Cairnswm

Testet this on my Sony Ericsson K750i and it works very well!

Only thing is that spit 9 is only showing top half of the text. Rest is outside the screen. But it seems like you are aware of problems like this ;)

But everything else works flawless.. Icon is also showing.

savage
09-02-2006, 01:32 PM
WILL has activated the MIDletPascal forum over @ http://www.pascalgamedevelopment.com/viewforum.php?f=65