From 9bcbb3868f420222d4224cde0b5ef2ff1888b260 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Mon, 1 Jun 2020 12:57:04 +0200 Subject: [PATCH] gg: migrate to sokol; examples: update tetris --- examples/sokol/fonts.v | 3 +- examples/tetris/tetris.v | 77 ++++++++++++++++++++-------------- thirdparty/sokol/sokol_gfx.h | 8 ++-- vlib/gg2/gg.v | 11 ++++- vlib/sokol/sapp/sapp_structs.v | 21 ++++++++++ vlib/sokol/sokol.v | 11 +++++ 6 files changed, 92 insertions(+), 39 deletions(-) diff --git a/examples/sokol/fonts.v b/examples/sokol/fonts.v index cb96f25a32..0e25f2147b 100644 --- a/examples/sokol/fonts.v +++ b/examples/sokol/fonts.v @@ -38,8 +38,7 @@ fn main() { sapp.run(&desc) } -fn init(user_data voidptr) { - mut state := &AppState(user_data) +fn init(state mut AppState) { // dont actually alocate this on the heap in real life gfx.setup(&C.sg_desc{ mtl_device: sapp.metal_get_device() diff --git a/examples/tetris/tetris.v b/examples/tetris/tetris.v index a210ab66b0..bce61b6a2a 100644 --- a/examples/tetris/tetris.v +++ b/examples/tetris/tetris.v @@ -7,20 +7,13 @@ module main import rand import time import gx -import gg +import gg2 as gg import glfw +import sokol +import sokol.sapp import math import freetype -const ( - k_up = glfw.key_up - k_left = glfw.key_left - k_right = glfw.key_right - k_down = glfw.key_down - k_escape = glfw.key_escape - k_space = glfw.key_space -) - const ( block_size = 20 // pixels field_height = 20 // # of blocks @@ -141,10 +134,30 @@ struct Game { font_loaded bool } -fn main() { - glfw.init_glfw() +fn frame(game &Game) { + game.gg.begin() + game.draw_scene() + game.gg.end() +} - gconfig := gg.Cfg { +fn main() { + mut game := &Game{} + game.gg = gg.new_context( + bg_color: gx.white + width: win_width + height: win_height + use_ortho: true // This is needed for 2D drawing + create_window: true + window_title: 'V tetris' + frame_fn: frame + user_data: game + //on_key_down: key_down + event_cb: on_event + ) + //font_path: os.resource_abs_path('assets/fonts/RobotoMono-Regular.ttf') + +/* + gconfig := gg.Config{ width: win_width height: win_height use_ortho: true // This is needed for 2D drawing @@ -153,7 +166,7 @@ fn main() { //window_user_ptr: game } - fconfig := gg.Cfg{ + fconfig := gg.Config{ width: win_width height: win_height use_ortho: true @@ -166,14 +179,15 @@ fn main() { gg: gg.new_context(gconfig) ft: freetype.new_context(fconfig) } - game.gg.window.set_user_ptr(game) // TODO remove this when `window_user_ptr:` works + */ + //game.gg.window.set_user_ptr(game) // TODO remove this when `window_user_ptr:` works game.init_game() - game.gg.window.onkeydown(key_down) + //game.gg.window.onkeydown(key_down) go game.run() // Run the game loop in a new thread - gg.clear(background_color) + game.gg.run() // Run the render loop in the main thread + /* game.font_loaded = game.ft != 0 for { - gg.clear(background_color) game.draw_scene() game.gg.render() if game.gg.window.should_close() { @@ -181,6 +195,7 @@ fn main() { return } } + */ } fn (mut g Game) init_game() { @@ -393,19 +408,19 @@ fn parse_binary_tetro(t_ int) []Block { return res } -// TODO: this exposes the unsafe C interface, clean up -fn key_down(wnd voidptr, key, code, action, mods int) { - if action != 2 && action != 1 { - return +fn on_event(e &sapp.Event, game mut Game) { + if e.typ == .key_down { + game.key_down(e.key_code) } - // Fetch the game object stored in the user pointer - mut game := &Game(glfw.get_window_user_pointer(wnd)) +} + +fn (game mut Game) key_down(key sapp.KeyCode) { // global keys match key { - k_escape { - glfw.set_should_close(wnd, true) + .escape { + exit(0) } - k_space { + .space { if game.state == .running { game.state = .paused } else if game.state == .paused { @@ -423,7 +438,7 @@ fn key_down(wnd voidptr, key, code, action, mods int) { } // keys while game is running match key { - k_up { + .up { // Rotate the tetro old_rotation_idx := game.rotation_idx game.rotation_idx++ @@ -439,13 +454,13 @@ fn key_down(wnd voidptr, key, code, action, mods int) { //game.pos_x = 1 } } - k_left { + .left { game.move_right(-1) } - k_right { + .right { game.move_right(1) } - k_down { + .down { game.move_tetro() // drop faster when the player presses } else { } diff --git a/thirdparty/sokol/sokol_gfx.h b/thirdparty/sokol/sokol_gfx.h index 676902089a..de3c4f3b42 100755 --- a/thirdparty/sokol/sokol_gfx.h +++ b/thirdparty/sokol/sokol_gfx.h @@ -3266,7 +3266,7 @@ _SOKOL_PRIVATE bool _sg_is_compressed_pixel_format(sg_pixel_format fmt) { case SG_PIXELFORMAT_ETC2_RGB8A1: case SG_PIXELFORMAT_ETC2_RGBA8: case SG_PIXELFORMAT_ETC2_RG11: - case SG_PIXELFORMAT_ETC2_RG11SN: + case SG_PIXELFORMAT_ETC2_RG11SN: return true; default: return false; @@ -3373,7 +3373,7 @@ _SOKOL_PRIVATE int _sg_row_pitch(sg_pixel_format fmt, int width) { case SG_PIXELFORMAT_BC7_RGBA: case SG_PIXELFORMAT_ETC2_RGBA8: case SG_PIXELFORMAT_ETC2_RG11: - case SG_PIXELFORMAT_ETC2_RG11SN: + case SG_PIXELFORMAT_ETC2_RG11SN: pitch = ((width + 3) / 4) * 16; pitch = pitch < 16 ? 16 : pitch; break; @@ -3417,7 +3417,7 @@ _SOKOL_PRIVATE int _sg_surface_pitch(sg_pixel_format fmt, int width, int height) case SG_PIXELFORMAT_ETC2_RGB8A1: case SG_PIXELFORMAT_ETC2_RGBA8: case SG_PIXELFORMAT_ETC2_RG11: - case SG_PIXELFORMAT_ETC2_RG11SN: + case SG_PIXELFORMAT_ETC2_RG11SN: case SG_PIXELFORMAT_BC2_RGBA: case SG_PIXELFORMAT_BC3_RGBA: case SG_PIXELFORMAT_BC5_RG: @@ -8841,7 +8841,7 @@ _SOKOL_PRIVATE id _sg_mtl_library_from_bytecode(const uint8_t* ptr, } _SOKOL_PRIVATE sg_resource_state _sg_create_shader(_sg_shader_t* shd, const sg_shader_desc* desc) { - puts("CREATE SHAER"); + puts("sokol: create Metal shader"); SOKOL_ASSERT(shd && desc); /* uniform block sizes and image types */ diff --git a/vlib/gg2/gg.v b/vlib/gg2/gg.v index 88d7767dd2..4515c24686 100644 --- a/vlib/gg2/gg.v +++ b/vlib/gg2/gg.v @@ -33,6 +33,8 @@ pub: scale int frame_fn fn(voidptr) bg_color gx.Color + on_key_down fn(voidptr) + event_cb fn(voidptr, voidptr) } pub struct GG { @@ -82,17 +84,22 @@ fn init_sokol_window(user_data voidptr) { */ } +fn eventcb(e &C.sapp_event, b voidptr){ + println("EVENT") +} + pub fn new_context(cfg Config) &GG { //C.printf('new_context() %p\n', cfg.user_data) window := C.sapp_desc{ user_data: cfg.user_data init_userdata_cb: init_sokol_window frame_userdata_cb: cfg.frame_fn + event_userdata_cb: cfg.event_cb //eventcb window_title: cfg.window_title.str html5_canvas_name: cfg.window_title.str width: cfg.width height: cfg.height - high_dpi: true + //high_dpi: true } //g_font_path = cfg.font_path if cfg.use_ortho {} @@ -101,7 +108,7 @@ pub fn new_context(cfg Config) &GG { width: cfg.width height: cfg.height window: window - clear_pass: gfx.create_clear_pass(0,0,0,0) //f64(cfg.bg_color.r) / 255.0, f64(cfg.bg_color.g) / 255.0, f64(cfg.bg_color.b) / 255.0, 1.0) + clear_pass: gfx.create_clear_pass( f64(cfg.bg_color.r) / 255.0, f64(cfg.bg_color.g) / 255.0, f64(cfg.bg_color.b) / 255.0, 1.0) scale: 1 // scale fons:0 } diff --git a/vlib/sokol/sapp/sapp_structs.v b/vlib/sokol/sapp/sapp_structs.v index a1dbc07b21..7d65a41445 100644 --- a/vlib/sokol/sapp/sapp_structs.v +++ b/vlib/sokol/sapp/sapp_structs.v @@ -36,6 +36,27 @@ pub: gl_force_gles2 bool /* if true, setup GLES2/WebGL even if GLES3/WebGL2 is available */ } +pub struct Event { +pub: + frame_count u64 + typ EventType + key_code KeyCode + char_code u32 + key_repeat bool + modifiers u32 + mouse_button MouseButton + mouse_x f32 + mouse_y f32 + scroll_x f32 + scroll_y f32 + num_touches int + touches [8]C.sapp_touchpoint + window_width int + window_height int + framebuffer_width int + framebuffer_height int +} + pub struct C.sapp_event { pub: frame_count u64 diff --git a/vlib/sokol/sokol.v b/vlib/sokol/sokol.v index 2e80d9aa5d..49a4234360 100644 --- a/vlib/sokol/sokol.v +++ b/vlib/sokol/sokol.v @@ -6,3 +6,14 @@ import sokol.f pub const ( used_import = c.used_import + f.used_import ) + +/* +pub enum Key { + up=C.SAPP_KEYCODE_UP + left = C.SAPP_KEYCODE_LEFT + right =C.SAPP_KEYCODE_RIGHT + down = C.SAPP_KEYCODE_DOWN + escape = C.SAPP_KEYCODE_ESCAPE + space = C.SAPP_KEYCODE_SPACE +} +*/