tests: Added test for basename and related methods
[nit.git] / lib / gamnit / examples / triangle / src / portable_triangle.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 # Portable example of using Gamnit with custom calls to OpenGL ES 2.0
16 #
17 # References:
18 # * The book OpenGL ES 2.0 Programming Guide
19 # * https://code.google.com/p/opengles-book-samples/source/browse/trunk/LinuxX11/Chapter_2/Hello_Triangle/Hello_Triangle.c
20 module portable_triangle is
21 app_name "gamnit Triangle"
22 app_namespace "org.nitlanguage.triangle"
23 app_version(1, 0, git_revision)
24 end
25
26 import gamnit
27
28 redef class App
29
30 # Our only program for the graphic card
31 var program: GLProgram is noautoinit
32
33 # The only vertex sharder
34 var vertex_shader: GLVertexShader is noautoinit
35
36 # The only fragment sharder
37 var fragment_shader: GLFragmentShader is noautoinit
38
39 # Vertex data for the triangle
40 var vertex_array: VertexArray is noautoinit
41
42 redef fun on_create
43 do
44 super
45
46 var display = display
47 assert display != null
48
49 print "Width: {display.width}"
50 print "Height: {display.height}"
51
52 assert_no_gl_error
53 assert gl.shader_compiler else print "Cannot compile shaders"
54
55 # GL program
56 program = new GLProgram
57 if not program.is_ok then
58 print "Program is not ok: {gl.error.to_s}\nLog:"
59 print program.info_log
60 abort
61 end
62 assert_no_gl_error
63
64 # Vertex shader
65 vertex_shader = new GLVertexShader
66 assert vertex_shader.is_ok else print "Vertex shader is not ok: {gl.error}"
67 vertex_shader.source = """
68 attribute vec4 vPosition;
69 void main()
70 {
71 gl_Position = vPosition;
72 }
73 """@glsl_vertex_shader.to_cstring
74 vertex_shader.compile
75 assert vertex_shader.is_compiled else print "Vertex shader compilation failed with: {vertex_shader.info_log} {program.info_log}"
76 assert_no_gl_error
77
78 # Fragment shader
79 fragment_shader = new GLFragmentShader
80 assert fragment_shader.is_ok else print "Fragment shader is not ok: {gl.error}"
81 fragment_shader.source = """
82 precision mediump float;
83 void main()
84 {
85 gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
86 }
87 """@glsl_fragment_shader.to_cstring
88 fragment_shader.compile
89 assert fragment_shader.is_compiled else print "Fragment shader compilation failed with: {fragment_shader.info_log}"
90 assert_no_gl_error
91
92 # Attach to program
93 program.attach_shader vertex_shader
94 program.attach_shader fragment_shader
95 program.bind_attrib_location(0, "vPosition")
96 program.link
97 assert program.is_linked else print "Linking failed: {program.info_log}"
98 assert_no_gl_error
99
100 # Draw!
101 var vertices = [0.0, 0.5, 0.0, -0.5, -0.5, 0.0, 0.5, -0.5, 0.0]
102 vertex_array = new VertexArray(0, 3, vertices)
103 vertex_array.attrib_pointer
104 end
105
106 redef fun frame_core
107 do
108 var display = display
109 if display != null then
110 gl.clear_color(0.5, 0.0, 0.5, 1.0)
111
112 assert_no_gl_error
113 gl.viewport(0, 0, display.width, display.height)
114 gl.clear((new GLBuffer).color)
115 program.use
116 vertex_array.enable
117
118 glDrawArrays(new GLDrawMode.triangles, 0, 3)
119
120 display.flip
121 end
122 end
123
124 redef fun on_stop
125 do
126 # Clean up
127 program.delete
128 vertex_shader.delete
129 fragment_shader.delete
130
131 # Close gamnit
132 var display = display
133 if display != null then display.close
134 end
135 end
136
137 if "NIT_TESTING".environ == "true" then exit(0)
138 super