Skip to content

Commit ff48d41

Browse files
committed
Merge: gamnit: use the same code to limit fps than mnit and fix errors
Fixed a few bugs in `mnit::mnit_fps`, a typo invalidated the value of `current_fps` and the use of long Ints were broken probably since they are tagged. Using floats should fix this last issue. Finally, the goal of this PR is to copy the module `mnit::mnit_fps` to `gamnit::limit_fps` and adapt it to the gamnit framework. There is some code duplication, but the mnit version should be deleted with the engine when gamnit is completed. Pull-Request: #1879 Reviewed-by: Jean Privat <jean@pryen.org>
2 parents a898a5d + 7c6c90f commit ff48d41

File tree

8 files changed

+104
-17
lines changed

8 files changed

+104
-17
lines changed

contrib/crazy_moles/src/moles.nit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ redef class App
318318
do
319319
super
320320

321-
maximum_fps = 50
321+
maximum_fps = 50.0
322322
end
323323

324324
redef fun frame_core(display)

contrib/tinks/src/client/client.nit

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ redef class App
8080
redef fun on_create
8181
do
8282
super
83-
maximum_fps = 60
83+
maximum_fps = 60.0
8484
assets.assign_images_to_story context.game.story
8585
end
8686

@@ -127,7 +127,7 @@ redef class App
127127
return new LocalServerContext
128128
else
129129
print "Connecting to:{address}:{port}"
130-
maximum_fps = 0
130+
maximum_fps = 0.0
131131

132132
# Args are: tinks server_address {port}
133133
#var address = "riph" # args[0]

examples/mnit_ballz/src/ballz_android.nit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ redef class App
3838
gyroscope.enabled = true
3939
light.enabled = true
4040
proximity.enabled = true
41-
maximum_fps = 50
41+
maximum_fps = 50.0
4242
sensors_support_enabled = true
4343
super
4444
end

examples/mnit_ballz/src/ballz_linux.nit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ redef class App
2929

3030
redef fun run
3131
do
32-
maximum_fps = 50
32+
maximum_fps = 50.0
3333
super
3434
end
3535

examples/mnit_dino/src/dino.nit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ redef class App
3939
do
4040
super
4141

42-
maximum_fps = 80
42+
maximum_fps = 80.0
4343

4444
var display = display
4545
assert display != null

lib/core/time.nit

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ redef class Sys
3232
`}
3333
end
3434

35+
redef class Float
36+
# Sleep approximately `self` seconds
37+
fun sleep `{
38+
time_t s = self;
39+
long ns = (self-s) * 1000000000.0;
40+
const struct timespec req = {s, ns};
41+
nanosleep(&req, NULL);
42+
`}
43+
end
44+
3545
# Time since epoch
3646
extern class TimeT `{time_t`}
3747

lib/gamnit/limit_fps.nit

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# This file is part of NIT ( http://www.nitlanguage.org ).
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# Frame-rate control for applications
16+
module limit_fps
17+
18+
import gamnit
19+
private import realtime
20+
21+
redef class App
22+
# Limit the frame-rate to a given frequency
23+
#
24+
# This basically limits how much `frame_core` is called per second.
25+
# Zero (or a negative value) means no limit.
26+
#
27+
# Applications can modify this value even during the main-loop.
28+
var maximum_fps = 60.0 is writable
29+
30+
# Current frame-rate
31+
#
32+
# Updated each 5 seconds.
33+
var current_fps = 0.0
34+
35+
redef fun frame_full
36+
do
37+
super
38+
limit_fps
39+
end
40+
41+
# The clock for limit_fps
42+
private var clock = new Clock
43+
44+
# Number of frames since the last deadline
45+
#
46+
# Used to compute `current_fps`.
47+
private var frame_count = 0
48+
49+
# Deadline used to compute `current_fps`
50+
private var frame_count_deadline = 0
51+
52+
# Check and sleep to maintain a frame-rate bellow `maximum_fps`
53+
#
54+
# Also periodically update `current_fps`
55+
# Is automatically called at the end of `full_frame`.
56+
fun limit_fps
57+
do
58+
var t = clock.total.sec
59+
if t >= frame_count_deadline then
60+
var cfps = frame_count.to_f / 5.0
61+
self.current_fps = cfps
62+
frame_count = 0
63+
frame_count_deadline = t + 5
64+
end
65+
frame_count += 1
66+
67+
var mfps = maximum_fps
68+
if mfps <= 0.0 then return
69+
var lapse = clock.lapse
70+
var dt = lapse.to_f
71+
var target_dt = 1.0 / mfps
72+
if dt < target_dt then
73+
var sleep_t = target_dt - dt
74+
sleep_t.sleep
75+
clock.lapse
76+
end
77+
end
78+
end

lib/mnit/mnit_fps.nit

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ redef class App
2424
# Zero (or a negative value) means no limit.
2525
#
2626
# Applications can modify this value even during the main-loop.
27-
var maximum_fps = 60 is writable
27+
var maximum_fps = 60.0 is writable
2828

2929
# Current frame-rate
3030
# Updated each 5 seconds.
@@ -53,23 +53,22 @@ redef class App
5353
do
5454
var t = clock.total.sec
5555
if t >= frame_count_deadline then
56-
var cfps = frame_count_deadline.to_f / 5.0
56+
var cfps = frame_count.to_f / 5.0
5757
self.current_fps = cfps
5858
frame_count = 0
5959
frame_count_deadline = t + 5
6060
end
6161
frame_count += 1
6262

6363
var mfps = maximum_fps
64-
if mfps <= 0 then return
65-
var dt = clock.lapse
66-
var target_dt = 1000000000 / mfps
67-
var sec = dt.sec
68-
var nanosec = dt.nanosec
69-
if sec == 0 and nanosec < target_dt then
70-
var sleep_t = target_dt - nanosec
71-
sys.nanosleep(0, sleep_t)
72-
dt = clock.lapse
64+
if mfps <= 0.0 then return
65+
var lapse = clock.lapse
66+
var dt = lapse.to_f
67+
var target_dt = 1.0 / mfps
68+
if dt < target_dt then
69+
var sleep_t = target_dt - dt
70+
sleep_t.sleep
71+
clock.lapse
7372
end
7473
end
7574
end

0 commit comments

Comments
 (0)