Autor Beitrag
Abu36
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 59



BeitragVerfasst: Mi 22.03.06 20:06 
Hi,

ich wollte für ein spiel ein stoppuhr einbauen, weiss nur nich ob das geht.
die laufende zeit soll dabei angezeigt werden.
starten soll die zeit auch einfach über buttonclick und enden auch über click auf den selben button.

desweiteren soll diese zeit dann irgendwie zu einer bestenliste eingetragen werden.
ich hoffe ihr helft mir irgendwie weiter

danke
starsurfer
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 334

Win 95, Win 98, Win XP, Win Vista, Linux
D5 Enterprise ,D2005, D6 Personal, Visual C++ Express 2005, C++ Builder 6 E, Dev-C++
BeitragVerfasst: Mi 22.03.06 20:10 
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
var aktuelle_zeit:real=0;


procedure TForm1.Timer1Timer(Sender: TObject);
begin
aktuelle_zeit:=aktuelle_zeit+1/86400;
if aktuelle_zeit>=1 then aktuelle_zeit:=0;
end;


und dann halt einfach timer.enabled:=false bedeutet zeit pausieren und aktuelle_zeit auf 0 setzen bedeutet das gleiche wie reset...

und den timerinterval auf 1000 ms stellen

Ausgabe:
ausblenden Delphi-Quelltext
1:
timetostr(aktuelle_zeit);					

_________________
GEIZ IST GEIL! - Ihr Sozialamt
Kroko
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1284

W98 W2k WXP
Turbo D
BeitragVerfasst: Do 23.03.06 08:05 
nimm die Unit, ist immer wieder praktisch!
ausblenden volle Höhe Delphi-Quelltext
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:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
unit unitClock;

interface

{-- neue Stoppuhr starten --}
procedure StartPerformance;
{-- neue Stoppuhr anhalten --}
function StoppPerformance: Int64;
{--------------------}
{-- neue Stoppzeit --}
{--------------------}
function PerformanceStr: String;
function PerformanceString: String;

implementation

uses
  Windows, SysUtils;

var
  vFaktor,
  vStart,
  vStopp: Int64;
{---------------------------}
{-- neue Stoppuhr starten --}
{---------------------------}
procedure StartPerformance;
begin
  QueryPerformanceFrequency(vFaktor);
  QueryPerformanceCounter(vStart);
end;
{----------------------------}
{-- neue Stoppuhr anhalten --}
{----------------------------}
function StoppPerformance: Int64;
begin
  QueryPerformanceCounter(vStopp);
  Result := (vStopp-vStart)*1000 div vFaktor;
end;
{--------------------}
{-- neue Stoppzeit --}
{--------------------}
function PerformanceString: String;
begin
  Result := FloatToStr((vStopp-vStart)*1000/vFaktor);
end;

function PerformanceStr: String;
begin
  Result := FloatToStrF ((vStopp-vStart)/vFaktor,ffNumber,16,2);
end;

end.

_________________
Die F1-Taste steht nicht unter Naturschutz und darf somit regelmäßig und oft benutzt werden! oder Wer lesen kann, ist klar im Vorteil!
Abu36 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 59



BeitragVerfasst: Do 23.03.06 16:14 
habs jetzt so wie starsurfer gemacht.

kann ich die stoppuhr, oder den timer dann auch irgendwie wieder auf null setzten, während dem das prog läuft, also von mir aus auch ein extra-button?????
Miri
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 657


Delphi 3 Prof., Delphi 2005 PE
BeitragVerfasst: Do 23.03.06 16:20 
klar... neuer button und in der procedure

ausblenden Delphi-Quelltext
1:
2:
timer.enabled := false; 
aktuelle_zeit:= 0;
Abu36 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 59



BeitragVerfasst: Do 23.03.06 18:35 
ein kleiner fehler is noch, so funktioniert allerdings alles.
mein timer befindet sich in form2. wenn ich das prog starte, also form1, fängt der timer in form2 schon mit an zulaufen. beim öffnen von form2 läuft er dann auch einfach weiter aber der rest funktioniert wies soll???
Fighter#1
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 787

Win XP, Ubuntu 8.04
Turbo Delphi 2006, Delphi 2005 Pe, Delphi 5 Pe, Netbeans 6.1, Eclipse, Microsoft VisualC#, Dev C++, PHP, HTML, CSS
BeitragVerfasst: Do 23.03.06 18:54 
Ganz einfach bei OnCreate von Form2 Timer1.Enabled:=False;
Und im Ereignis wo du Form2.Show; reingeschrieben hast dann,
Timer1.Enabled:=True;

_________________
Wer andere beherrscht ist stark,
wer sich selbst beherrscht ist mächtig. Lao Tse
Abu36 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 59



BeitragVerfasst: Do 23.03.06 19:00 
danke, funktioniert
Kroko
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1284

W98 W2k WXP
Turbo D
BeitragVerfasst: Do 23.03.06 19:34 
Meiner Meinung nach ist ein Timer als Stoppuhr, das schlechteste was gibt!

_________________
Die F1-Taste steht nicht unter Naturschutz und darf somit regelmäßig und oft benutzt werden! oder Wer lesen kann, ist klar im Vorteil!
starsurfer
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 334

Win 95, Win 98, Win XP, Win Vista, Linux
D5 Enterprise ,D2005, D6 Personal, Visual C++ Express 2005, C++ Builder 6 E, Dev-C++
BeitragVerfasst: Do 23.03.06 19:50 
es ist aber die einfachste Möglichkeit...

und ich denke mal Abu36 wird keine Stopuhr brauchen die auf de 1000senstel genau misst...

_________________
GEIZ IST GEIL! - Ihr Sozialamt
Abu36 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 59



BeitragVerfasst: Do 23.03.06 20:02 
ja, is schon gut so, streitet euch nich :wink:
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Sa 10.06.06 15:47 
Um Kroko's Unit für mehrere Theads nutzbar zu machen, müssten die Variablen-Deklarationen in den Zeilen 20ff wie folgt geändert werden:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
var
  vFaktor: Int64;
threadvar
  vStart,
  vStopp: Int64;


Weiterhin muss Zeile 29 (QueryPerformanceFrequency(vFaktor);) in den Initialization-Abschnitt der Unit verschoben werden:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
initialization
  QueryPerformanceFrequency(vFaktor);
finalization
end.


Der Zugriff auf die gemessenen Zeiten erfolgt wie gewohnt. Ein Zugriff von außen auf die Zeiten (d.h. von außerhalb eines Threads) muss selbstständig ergänzt werden. Es ist nur möglich, innerhalb des eigenen Threads auf die gemessenen Zeiten zuzugreifen. Brauch man auch die Messzeiten eines anderen Threads, so muss man diesen Austausch selber implementieren.

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
klezmor
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 558


delphi 6 personal delphi 2005 personal
BeitragVerfasst: So 11.06.06 16:39 
Kann mir jemand sagen, worin der Unterschied zwischen normalen Variablen und Thread Variablen besteht. Gibt es noch weitere Arten von Variablen in Object-Pascal?

_________________
"Beware of bugs in the above code; I have only proved it correct, not tried it." Donald Knuth
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: So 11.06.06 21:52 
Nope, weitere Typen von Vars gibt's in ObjPas nicht.

Zum Unterschied mal grob gesagt so viel: Normale Variablen werden entweder auf dem Heap (Variablen in einer Unit) oder auf dem Stack (lokale Variablen einer Prozedur\Function\Methode) abgelegt. Der Zugriff kann dabei relativ direkt erfolgen:

ausblenden Zugriff auf eine globale Variable
1:
2:
3:
4:
asm
    MOV EAX, DWORD PTR [$12345678]
    //$12345678 ist die Adresse der Variable im Speicher 
end;


ausblenden Zugriff auf eine lokale Variable
1:
2:
3:
4:
asm
    MOV EAX, DWORD PTR [EBP+$00000004] 
    //EBP+$00000004 ist ein Displacement relativ zum aktuellen Stackframe
end;


Anders sieht das bei Thread-Variablen aus:
ausblenden Zugriff auf eine Thread-Variable
1:
2:
3:
4:
5:
6:
7:
8:
9:
asm
    XOR EAX, EAX
    MOV EAX, DWORD PTR FS:[EAX]
    MOV EAX, DWORD PTR [EAX+$00000018]
    // Auflösen des aktuellen TLS (Thread local Storage)

    MOV EAX, DWORD PTR [EAX+$00000004]
    //$00000004 ist der Offset derVariable im TLS-Segment
end;


Ähnliches wie für globale Variablen gilt übrigens für (nahezu) alle Konstanten beim Zugriff. Sind jedoch "Writeable Consts" aktiviert, kann es bei lokalen Konstanten zu einem Downgrading zu lokalen Variablen kommen, wodurch der Zugriff verlangsamt wird.

Ich hoff mal, die ASM-Sources sind halbwegs einleuchtend... Wenn Du mehr über Object Pascal erfahren willst, kann ich Dir nur die "Object Pascal Reference" Sektion der DOH ans Herz legen ... Dort steht eigentlich alles drin erklärt ... (Selbst, wie man mit ASM die Compiler-Magic von Delphi aufruft und solche Späße).

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
klezmor
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 558


delphi 6 personal delphi 2005 personal
BeitragVerfasst: Mo 12.06.06 12:21 
Kannst du mir auch sagen was die DOH ist?

_________________
"Beware of bugs in the above code; I have only proved it correct, not tried it." Donald Knuth
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Mo 12.06.06 17:55 
DOH = Nettes Teil, was sich unter Delphi --> Hilfe --> F1 befindet ... AKA "Delphi Online Hilfe" ...

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.