gamnit: enable dynamic window resizing on desktop
[nit.git] / lib / gamnit / gamnit.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 # Game and multimedia framework for Nit
16 module gamnit
17
18 import app
19
20 import display
21 import textures
22 import programs
23
24 import gamnit_android is conditional(android)
25 import gamnit_linux is conditional(linux)
26
27 redef class App
28
29 # Main `GamnitDisplay` initialized by `on_create`
30 var display: nullable GamnitDisplay = null
31
32 redef fun on_create
33 do
34 super
35
36 var display = new GamnitDisplay
37 display.setup
38 self.display = display
39 end
40
41 # Core of the frame logic, executed only when the display is visible
42 #
43 # This method should be redefined by user modules to customize the behavior of the game.
44 protected fun frame_core(display: GamnitDisplay) do end
45
46 # Full frame logic, executed even if the display is not visible
47 #
48 # This method wraps `frame_core` and other services to be executed in the main app loop.
49 #
50 # To customize the behavior on each turn, it is preferable to redefined `frame_core`.
51 # Still, `frame_full` can be redefined with care for more control.
52 protected fun frame_full
53 do
54 var display = display
55 if display != null then frame_core(display)
56
57 feed_events
58 end
59
60 redef fun run
61 do
62 # TODO manage exit condition
63 loop frame_full
64 end
65
66 # Loop on available events and feed them back to the app
67 #
68 # The implementation varies per platform.
69 private fun feed_events do end
70
71 # Hook to receive and respond to `event` triggered by the user or system
72 #
73 # Returns whether or not the event is used or intercepted.
74 # If `true`, the event will not be processed further by the system.
75 # Returns `false` to intercepts events like the back key on mobile devices.
76 #
77 # The instances passed as `event` may be freed (or overwritten),
78 # right after this method returns. They should not be preserved.
79 fun accept_event(event: InputEvent): Bool do return false
80
81 # The window has been resized by the user or system
82 #
83 # The framework handles resizing the viewport automatically.
84 fun on_resize(display: GamnitDisplay) do end
85 end