From 975206f38e443ddec377cc7fd27e8618fd9a848c Mon Sep 17 00:00:00 2001 From: Larpon Date: Tue, 2 Feb 2021 13:09:40 +0100 Subject: [PATCH] examples: support better placment and scaling on nonsquare viewports in cube.v (#8513) --- examples/sokol/cube.v | 59 +++++++++++++++++++++++++------------------ 1 file changed, 34 insertions(+), 25 deletions(-) diff --git a/examples/sokol/cube.v b/examples/sokol/cube.v index 261fd02a4f..a4cea15e8e 100644 --- a/examples/sokol/cube.v +++ b/examples/sokol/cube.v @@ -31,7 +31,7 @@ mut: texture C.sg_image init_flag bool frame_count int - + mouse_x int = -1 mouse_y int = -1 } @@ -60,7 +60,7 @@ fn create_texture(w int, h int, buf byteptr) C.sg_image{ ptr: buf size: sz } - + sg_img := C.sg_make_image(&img_desc) return sg_img } @@ -136,7 +136,7 @@ fn cube() { fn draw_cubes(app App) { rot := [f32(1.0)*(app.frame_count % 360), 0.5*f32(app.frame_count%360)] //rot := [f32(app.mouse_x), f32(app.mouse_y)] - + sgl.defaults() sgl.load_pipeline(app.pip_3d) @@ -240,7 +240,7 @@ fn cube_field(app App){ rot := [f32(app.mouse_x), f32(app.mouse_y)] xyz_sz := f32(2.0) field_size := 20 - + sgl.defaults() sgl.load_pipeline(app.pip_3d) @@ -249,13 +249,13 @@ fn cube_field(app App){ sgl.matrix_mode_projection() sgl.perspective(sgl.rad(45.0), 1.0, 0.1, 200.0) - + sgl.matrix_mode_modelview() - + sgl.translate(field_size, 0.0, -120.0) sgl.rotate(sgl.rad(rot[0]), 0.0, 1.0, 0.0) sgl.rotate(sgl.rad(rot[1]), 1.0, 0.0, 0.0) - + // draw field_size*field_size cubes for y in 0..field_size { for x in 0..field_size { @@ -265,41 +265,43 @@ fn cube_field(app App){ cube_t(f32(f32(x)/field_size), f32(f32(y)/field_size),1) sgl.pop_matrix() } - } + } sgl.disable_texture() } fn frame(mut app App) { - dw := app.gg.width - dh := app.gg.height - ww := dh/2 /* not a bug */ - hh := dh/2 - x0 := dw/2 - hh + ws := gg.window_size() + ratio := f32(ws.width)/ws.height + dw := ws.width + dh := ws.height + ww := int(dh/3) /* not a bug */ + hh := int(dh/3) + x0 := int(f32(dw) * 0.05) //x1 := dw/2 y0 := 0 - y1 := dh/2 - + y1 := int(f32(dh) * 0.5) + app.gg.begin() //sgl.defaults() // 2d triangle sgl.viewport(x0, y0, ww, hh, true) draw_triangle() - + // colored cubes with viewport sgl.viewport(x0, y1, ww, hh, true) draw_cubes(app) - + // textured cubed with viewport - sgl.viewport(0, 0, dw, dh, true) + sgl.viewport(0, int(dh/5), dw, int(dh*ratio), true) draw_texture_cubes(app) - + // textured field of cubes with viewport - sgl.viewport(0, 0, dw, dh, true) + sgl.viewport(0, int(dh/5), dw, int(dh*ratio), true) cube_field(app) app.frame_count++ - + app.gg.end() } @@ -310,7 +312,7 @@ fn frame(mut app App) { ******************************************************************************/ fn my_init(mut app App) { app.init_flag = true - + // set max vertices, // for a large number of the same type of object it is better use the instances!! desc := sapp.create_desc() @@ -319,7 +321,7 @@ fn my_init(mut app App) { max_vertices: 50 * 65536 } sgl.setup(&sgl_desc) - + // 3d pipeline mut pipdesc := C.sg_pipeline_desc{} unsafe {C.memset(&pipdesc, 0, sizeof(pipdesc))} @@ -334,7 +336,7 @@ fn my_init(mut app App) { cull_mode: .back } app.pip_3d = sgl.make_pipeline(&pipdesc) - + // create chessboard texture 256*256 RGBA w := 256 h := 256 @@ -359,7 +361,7 @@ fn my_init(mut app App) { tmp_txt[i+2] = byte(0) tmp_txt[i+3] = byte(0xFF) } else { - col := if ((x+y) & 1) == 1 {0xFF} else {0} + col := if ((x+y) & 1) == 1 {0xFF} else {0} tmp_txt[i ] = byte(col) // red tmp_txt[i+1] = byte(col) // green tmp_txt[i+2] = byte(col) // blue @@ -386,6 +388,13 @@ fn my_event_manager(mut ev sapp.Event, mut app App) { app.mouse_x = int(ev.mouse_x) app.mouse_y = int(ev.mouse_y) } + if ev.typ == .touches_began || ev.typ == .touches_moved { + if ev.num_touches > 0 { + touch_point := ev.touches[0] + app.mouse_x = int(touch_point.pos_x) + app.mouse_y = int(touch_point.pos_y) + } + } } /******************************************************************************