11package main
22
33import " core:fmt"
4+ import " core:intrinsics"
45import glm " core:math/linalg/glsl"
56import " core:mem"
67import " core:runtime"
@@ -15,8 +16,8 @@ shader_vertex := #load("shader_vertex.glsl", string)
1516dpr: f32
1617res: [2 ]i32
1718canvas_pos: [2 ]f32
18- canvas_rect : [2 ]f32
19- window_rect : [2 ]f32
19+ canvas_size : [2 ]f32
20+ window_size : [2 ]f32
2021mouse_pos: [2 ]f32
2122
2223scale: f32 = 1
@@ -89,24 +90,24 @@ main :: proc() {
8990 colors_buffer = webgl.CreateBuffer ()
9091
9192 dpr = f32 (dom.device_pixel_ratio ())
92- canvas_rect = { 640 , 480 }
93- window_rect = canvas_rect + 200
94- mouse_pos = window_rect / 2
93+ window_size = cast_vec2 ( f32 , dom. get_window_inner_size ())
94+ canvas_size = window_size - 200
95+ mouse_pos = window_size / 2
9596}
9697
9798on_mouse_move :: proc (e: dom.Event) {
98- mouse_pos = { f32 (e.data.mouse.client.x), f32 ( e.data.mouse.client.y)}
99+ mouse_pos = cast_vec2 ( f32 , e.data.mouse.client)
99100}
100101on_wheel :: proc (e: dom.Event) {
101102 scale += f32 (e.data.wheel.delta.y) * 0.001
102103 scale = clamp (scale, scale_min, scale_max)
103104}
104105@(export)
105- on_window_resize :: proc " c " (vw, vh, cw, ch, cx, cy: f32 ) {
106- window_rect = {vw, vh}
107- canvas_rect = {cw, ch}
106+ on_window_resize :: proc " contextless " (vw, vh, cw, ch, cx, cy: f32 ) {
107+ window_size = {vw, vh}
108+ canvas_size = {cw, ch}
108109 canvas_pos = {cx, cy}
109- res = { i32 ( f32 (canvas_rect.x) * dpr), i32 ( f32 (canvas_rect.y) * dpr)}
110+ res = cast_vec2 ( i32 , canvas_size * dpr)
110111}
111112
112113@(export)
@@ -135,56 +136,58 @@ frame :: proc "c" (delta: i32, ctx: ^runtime.Context) {
135136
136137 webgl.BindBuffer (webgl.ARRAY_BUFFER, positions_buffer)
137138 webgl.BufferDataSlice (webgl.ARRAY_BUFFER, positions[:], webgl.STATIC_DRAW)
138-
139- // Tell the attribute how to get data out of positionBuffer (ARRAY_BUFFER)
140139 webgl.VertexAttribPointer (a_position, 2 , webgl.FLOAT, false , 0 , 0 )
141140
142- // bind, and fill color buffer
143141 webgl.BindBuffer (webgl.ARRAY_BUFFER, colors_buffer)
144142 webgl.BufferDataSlice (webgl.ARRAY_BUFFER, colors[:], webgl.STATIC_DRAW)
145143 webgl.VertexAttribPointer (a_color, 4 , webgl.UNSIGNED_BYTE, true , 0 , 0 )
146144
147- // set the resolution
148- webgl.Uniform2f (u_resolution, canvas_rect.x, canvas_rect.y)
145+ webgl.Uniform2f (u_resolution, canvas_size.x, canvas_size.y)
149146
150- // Tell WebGL how to convert from clip space to pixels
151147 webgl.Viewport (0 , 0 , res.x, res.y)
152-
153- // Clear the canvas
154148 webgl.ClearColor (0 , 0.01 , 0.02 , 0 )
155149 webgl.Clear (webgl.COLOR_BUFFER_BIT)
156150
157- rotation += 0.01 * f32 (delta) * (window_rect.x / 2 - mouse_pos.x) / window_rect.x
151+
152+ rotation += 0.01 * f32 (delta) * (window_size.x / 2 - mouse_pos.x) / window_size.x
158153 mat :=
159154 mat3_translate (mouse_pos - canvas_pos) *
160155 mat3_scale ({scale, scale}) *
161156 mat3_rotate (rotation) *
162157 mat3_translate (-box_size / 2 )
158+
163159 webgl.UniformMatrix3fv (u_matrix, mat)
164160
165- // draw
166161 webgl.DrawArrays (webgl.TRIANGLES, 0 , 6 ) // 2 triangles, 6 vertices
167162}
168163
164+ cast_vec2 :: proc " contextless" (
165+ $To : typeid ,
166+ v: [2 ]$From ,
167+ ) -> [2 ]To where intrinsics.type_is_numeric (From) &&
168+ intrinsics.type_is_numeric (To) {
169+ return {To (v.x), To (v.y)}
170+ }
171+
169172// odinfmt: disable
170173@(require_results)
171- mat3_translate :: proc (v: [2 ]f32 ) -> glm.mat3 {
174+ mat3_translate :: proc " contextless " (v: [2 ]f32 ) -> glm.mat3 {
172175 return {
173176 1 , 0 , v.x,
174177 0 , 1 , v.y,
175178 0 , 0 , 1 ,
176179 }
177180}
178181@(require_results)
179- mat3_scale :: proc (v: [2 ]f32 ) -> glm.mat3 {
182+ mat3_scale :: proc " contextless " (v: [2 ]f32 ) -> glm.mat3 {
180183 return {
181184 v.x, 0 , 0 ,
182185 0 , v.y, 0 ,
183186 0 , 0 , 1 ,
184187 }
185188}
186189@(require_results)
187- mat3_rotate :: proc (angle: f32 ) -> glm.mat3 {
190+ mat3_rotate :: proc " contextless " (angle: f32 ) -> glm.mat3 {
188191 c := glm.cos (angle)
189192 s := glm.sin (angle)
190193 return {
0 commit comments