4c003a61e71f155347a9e5516124e0da7d5575fa
[nit.git] / lib / mnit / mnit_app.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2011-2013 Alexis Laferrière <alexis.laf@xymus.net>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # General Mnit application structure
18 module mnit_app
19
20 import ::app
21 import mnit_display
22
23 # An App instance serves as base to every Mnit projects.
24 #
25 # This class is redefed by plateforme modules and so
26 # App can be specialized directly in the user app.
27 redef class App
28 type D: Display
29 type I: Image
30
31 # Display to use by apps
32 # Is null if the display is not available or not yet ready
33 var display: nullable D protected writable = null
34
35 # Received quit order
36 var quit: Bool writable = false
37
38 # App is visible? (vs minimized or in background)
39 fun visible: Bool is abstract
40
41 # Invoqued at each frame
42 # Usually you want to redef frame_core instead of this
43 fun full_frame
44 do
45 var display = self.display
46 if display != null then
47 display.begin
48 frame_core( display )
49 display.finish
50 end
51 end
52
53 # Main frame method to redef
54 # Is called between readying display and flipping it
55 fun frame_core( display: D ) do end
56
57 # Receive and deal with all inputs
58 fun input( event: InputEvent ): Bool
59 do
60 return false
61 end
62
63 # Internal method to generate inputs
64 protected fun generate_input
65 do
66 if "NIT_TESTING".environ == "true" then exit 0
67 print "Compiled without platform"
68 exit 1
69 end
70
71 # Main app loop
72 # Usually you want to redef frame_core instead of this
73 redef fun run
74 do
75 while not quit do
76 generate_input
77 full_frame
78 end
79 end
80 end