examples: update users of `mnit::mnit_fps`
[nit.git] / examples / mnit_ballz / src / ballz_linux.nit
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 # Linux part of mnit_ballz
16 module ballz_linux
17
18 import mnit::linux
19 import display
20
21
22 redef class App
23
24 private var up_arrow_down = false
25 private var down_arrow_down = false
26 private var left_arrow_down = false
27 private var right_arrow_down = false
28 private var game: nullable Game is noautoinit
29
30 redef fun run
31 do
32 maximum_fps = 50.0
33 super
34 end
35
36 redef fun on_create
37 do
38 super
39 game = new Game(display.width.to_f, display.height.to_f)
40 end
41
42 redef fun frame_core(display)
43 do
44 if up_arrow_down then input(new SDLKeyEvent("up", true))
45 if down_arrow_down then input(new SDLKeyEvent("down", true))
46 if left_arrow_down then input(new SDLKeyEvent("left", true))
47 if right_arrow_down then input(new SDLKeyEvent("right", true))
48
49 var game = game
50 if game != null then
51 game.do_turn
52 game.draw(display, assets)
53 end
54 end
55
56 redef fun input(ie)
57 do
58 if ie isa QuitEvent then
59 quit = true
60 return true
61 end
62 var game = game
63 if game != null then
64 return game.input(ie)
65 end
66 return false
67 end
68 end
69
70 redef class Ball
71
72 redef fun intercepts(event)
73 do
74 var value = 5.0
75 if event isa SDLKeyEvent then
76 if event.is_arrow_left then
77 acceleration(value, 0.0)
78 app.left_arrow_down = event.is_down
79 end
80 if event.is_arrow_right then
81 acceleration(-value, 0.0)
82 app.right_arrow_down = event.is_down
83 end
84 if event.is_arrow_up then
85 acceleration(0.0, -value)
86 app.up_arrow_down = event.is_down
87 end
88 if event.is_arrow_down then
89 acceleration(0.0, value)
90 app.down_arrow_down = event.is_down
91 end
92 end
93 return false
94 end
95 end
96
97 redef class Game
98
99 redef fun input(ie)
100 do
101 ball.intercepts(ie)
102 return false
103 end
104 end