Debian Codegraph @

src/base/castletimeutils_now.inc

raw
1 {
2 Copyright 2019-2019 Michalis Kamburelis.
3
4 This file is part of "Castle Game Engine".
5
6 "Castle Game Engine" is free software; see the file COPYING.txt,
7 included in this distribution, for details about the copyright.
8
9 "Castle Game Engine" is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 ----------------------------------------------------------------------------
14 }
15
16 { Part of CastleTimeUtils unit: CastleNow. }
17
18 {$ifdef read_interface}
19
20 { Just like standard Now function, but works on all platforms supported by CGE.
21 On Nintendo Switch the standard function will crash now. }
22 function CastleNow: TDateTime;
23
24 { Just like standard Today function, but works on all platforms supported by CGE.
25 On Nintendo Switch the standard function will crash now. }
26 function CastleToday: TDateTime;
27
28 { Just like standard Yesterday function, but works on all platforms supported by CGE.
29 On Nintendo Switch the standard function will crash now. }
30 function CastleYesterday: TDateTime;
31
32 { Just like standard Tomorrow function, but works on all platforms supported by CGE.
33 On Nintendo Switch the standard function will crash now. }
34 function CastleTomorrow: TDateTime;
35
36 { Just like standard Randomize function, but works on all platforms supported by CGE.
37 On Nintendo Switch the standard function will crash now.
38 Note that you seldom need to call this explicitly: CastleUtils calls it already
39 in the initialization. }
40 procedure CastleRandomize;
41
42 {$endif read_interface}
43
44 {$ifdef read_implementation}
45
46 {$ifdef CASTLE_NINTENDO_SWITCH}
47 function CgeNxGetUnixTime(): QWord; cdecl; external;
48 {$endif}
49
50 function CastleNow: TDateTime;
51 {$ifdef CASTLE_NINTENDO_SWITCH}
52 begin
53 Result := UnixToDateTime(CgeNxGetUnixTime());
54 {$else}
55 begin
56 Result := Now;
57 {$endif}
58 end;
59
60 function CastleToday: TDateTime;
61 begin
62 Result := DateOf(CastleNow);
63 end;
64
65 function CastleYesterday: TDateTime;
66 begin
67 Result := DateOf(CastleNow) - 1;
68 end;
69
70 function CastleTomorrow: TDateTime;
71 begin
72 Result := DateOf(CastleNow) + 1;
73 end;
74
75 procedure CastleRandomize;
76 {$ifdef CASTLE_NINTENDO_SWITCH}
77
78 procedure XorShift64(var c64: QWord);
79 begin
80 c64 := c64 xor (c64 shl 12);
81 c64 := c64 xor (c64 shr 25);
82 c64 := c64 xor (c64 shl 27);
83 end;
84
85 var
86 NewRandSeed: QWord;
87 begin
88 NewRandSeed := CgeNxGetUnixTime();
89 XorShift64(NewRandSeed);
90 XorShift64(NewRandSeed);
91 XorShift64(NewRandSeed);
92 XorShift64(NewRandSeed);
93 {$I norqcheckbegin.inc}
94 RandSeed := UInt32(NewRandSeed);
95 {$I norqcheckend.inc}
96 {$else}
97 begin
98 // On NX this crashes with "invalid system call", because it accesses clock.
99 Randomize;
100 {$endif}
101 end;
102
103 {$endif read_implementation}