additional tests for `do catch` structure
[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 `create_gamnit`
30 var display: nullable GamnitDisplay = null
31
32 # Hook to setup the OpenGL context: compiling shaders, creating VBO, reloading textures, etc.
33 #
34 # The gamnit services redefine this method to prepare optimizations and more.
35 # Clients may also refine this method to prepare custom OpenGL resources.
36 fun create_gamnit do end
37
38 # Hook to prepare for recreating the OpenGL context
39 #
40 # Some gamnit services refine this method to reset caches before the
41 # next call to `create_gamnit`.
42 fun recreate_gamnit do end
43
44 # Create and set `self.display`
45 fun create_display
46 do
47 var display = new GamnitDisplay
48 display.setup
49 self.display = display
50
51 # Print the current GL configuration, for debugging
52 print "GL vendor: {glGetString(gl_VENDOR)}"
53 print "GL renderer: {glGetString(gl_RENDERER)}"
54 print "GL version: {glGetString(gl_VERSION)}"
55 print "GLSL version: {glGetString(gl_SHADING_LANGUAGE_VERSION)}"
56 print "GL extensions: {glGetString(gl_EXTENSIONS)}"
57 print "GL max texture size: {glGetIntegerv(gl_MAX_TEXTURE_SIZE, 0)}"
58 end
59
60 # Hook for client programs to setup the scene
61 #
62 # Refine this method to build the game world or the main menu,
63 # creating instances of `Sprite` and `Actor` as needed.
64 #
65 # This method is called only once per execution of the program and it should
66 # be considered as the entry point of most game logic.
67 fun create_scene do end
68
69 # Core of the frame logic, executed only when the display is visible
70 #
71 # This method should be redefined by user modules to customize the behavior of the game.
72 protected fun frame_core(display: GamnitDisplay) do end
73
74 # Full frame logic, executed even if the display is not visible
75 #
76 # This method wraps `frame_core` and other services to be executed in the main app loop.
77 #
78 # To customize the behavior on each turn, it is preferable to redefined `frame_core`.
79 # Still, `frame_full` can be redefined with care for more control.
80 protected fun frame_full
81 do
82 var display = display
83 if display != null then frame_core(display)
84
85 feed_events
86 end
87
88 redef fun run
89 do
90 # TODO manage exit condition
91 loop frame_full
92 end
93
94 # Loop on available events and feed them back to the app
95 #
96 # The implementation varies per platform.
97 private fun feed_events do end
98
99 # Hook to receive and respond to `event` triggered by the user or system
100 #
101 # Returns whether or not the event is used or intercepted.
102 # If `true`, the event will not be processed further by the system.
103 # Returns `false` to intercepts events like the back key on mobile devices.
104 #
105 # The instances passed as `event` may be freed (or overwritten),
106 # right after this method returns. They should not be preserved.
107 fun accept_event(event: InputEvent): Bool do return false
108
109 # The window has been resized by the user or system
110 #
111 # The framework handles resizing the viewport automatically.
112 fun on_resize(display: GamnitDisplay) do end
113 end