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.
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.