mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
gg: migrate to sokol; examples: update tetris
This commit is contained in:
parent
37e3cc0e72
commit
9bcbb3868f
@ -38,8 +38,7 @@ fn main() {
|
|||||||
sapp.run(&desc)
|
sapp.run(&desc)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn init(user_data voidptr) {
|
fn init(state mut AppState) {
|
||||||
mut state := &AppState(user_data)
|
|
||||||
// dont actually alocate this on the heap in real life
|
// dont actually alocate this on the heap in real life
|
||||||
gfx.setup(&C.sg_desc{
|
gfx.setup(&C.sg_desc{
|
||||||
mtl_device: sapp.metal_get_device()
|
mtl_device: sapp.metal_get_device()
|
||||||
|
@ -7,20 +7,13 @@ module main
|
|||||||
import rand
|
import rand
|
||||||
import time
|
import time
|
||||||
import gx
|
import gx
|
||||||
import gg
|
import gg2 as gg
|
||||||
import glfw
|
import glfw
|
||||||
|
import sokol
|
||||||
|
import sokol.sapp
|
||||||
import math
|
import math
|
||||||
import freetype
|
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 (
|
const (
|
||||||
block_size = 20 // pixels
|
block_size = 20 // pixels
|
||||||
field_height = 20 // # of blocks
|
field_height = 20 // # of blocks
|
||||||
@ -141,10 +134,30 @@ struct Game {
|
|||||||
font_loaded bool
|
font_loaded bool
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn frame(game &Game) {
|
||||||
glfw.init_glfw()
|
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
|
width: win_width
|
||||||
height: win_height
|
height: win_height
|
||||||
use_ortho: true // This is needed for 2D drawing
|
use_ortho: true // This is needed for 2D drawing
|
||||||
@ -153,7 +166,7 @@ fn main() {
|
|||||||
//window_user_ptr: game
|
//window_user_ptr: game
|
||||||
}
|
}
|
||||||
|
|
||||||
fconfig := gg.Cfg{
|
fconfig := gg.Config{
|
||||||
width: win_width
|
width: win_width
|
||||||
height: win_height
|
height: win_height
|
||||||
use_ortho: true
|
use_ortho: true
|
||||||
@ -166,14 +179,15 @@ fn main() {
|
|||||||
gg: gg.new_context(gconfig)
|
gg: gg.new_context(gconfig)
|
||||||
ft: freetype.new_context(fconfig)
|
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.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
|
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
|
game.font_loaded = game.ft != 0
|
||||||
for {
|
for {
|
||||||
gg.clear(background_color)
|
|
||||||
game.draw_scene()
|
game.draw_scene()
|
||||||
game.gg.render()
|
game.gg.render()
|
||||||
if game.gg.window.should_close() {
|
if game.gg.window.should_close() {
|
||||||
@ -181,6 +195,7 @@ fn main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
fn (mut g Game) init_game() {
|
fn (mut g Game) init_game() {
|
||||||
@ -393,19 +408,19 @@ fn parse_binary_tetro(t_ int) []Block {
|
|||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: this exposes the unsafe C interface, clean up
|
fn on_event(e &sapp.Event, game mut Game) {
|
||||||
fn key_down(wnd voidptr, key, code, action, mods int) {
|
if e.typ == .key_down {
|
||||||
if action != 2 && action != 1 {
|
game.key_down(e.key_code)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
// 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
|
// global keys
|
||||||
match key {
|
match key {
|
||||||
k_escape {
|
.escape {
|
||||||
glfw.set_should_close(wnd, true)
|
exit(0)
|
||||||
}
|
}
|
||||||
k_space {
|
.space {
|
||||||
if game.state == .running {
|
if game.state == .running {
|
||||||
game.state = .paused
|
game.state = .paused
|
||||||
} else if 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
|
// keys while game is running
|
||||||
match key {
|
match key {
|
||||||
k_up {
|
.up {
|
||||||
// Rotate the tetro
|
// Rotate the tetro
|
||||||
old_rotation_idx := game.rotation_idx
|
old_rotation_idx := game.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
|
//game.pos_x = 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
k_left {
|
.left {
|
||||||
game.move_right(-1)
|
game.move_right(-1)
|
||||||
}
|
}
|
||||||
k_right {
|
.right {
|
||||||
game.move_right(1)
|
game.move_right(1)
|
||||||
}
|
}
|
||||||
k_down {
|
.down {
|
||||||
game.move_tetro() // drop faster when the player presses <down>
|
game.move_tetro() // drop faster when the player presses <down>
|
||||||
}
|
}
|
||||||
else { }
|
else { }
|
||||||
|
2
thirdparty/sokol/sokol_gfx.h
vendored
2
thirdparty/sokol/sokol_gfx.h
vendored
@ -8841,7 +8841,7 @@ _SOKOL_PRIVATE id<MTLLibrary> _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) {
|
_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);
|
SOKOL_ASSERT(shd && desc);
|
||||||
|
|
||||||
/* uniform block sizes and image types */
|
/* uniform block sizes and image types */
|
||||||
|
@ -33,6 +33,8 @@ pub:
|
|||||||
scale int
|
scale int
|
||||||
frame_fn fn(voidptr)
|
frame_fn fn(voidptr)
|
||||||
bg_color gx.Color
|
bg_color gx.Color
|
||||||
|
on_key_down fn(voidptr)
|
||||||
|
event_cb fn(voidptr, voidptr)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct GG {
|
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 {
|
pub fn new_context(cfg Config) &GG {
|
||||||
//C.printf('new_context() %p\n', cfg.user_data)
|
//C.printf('new_context() %p\n', cfg.user_data)
|
||||||
window := C.sapp_desc{
|
window := C.sapp_desc{
|
||||||
user_data: cfg.user_data
|
user_data: cfg.user_data
|
||||||
init_userdata_cb: init_sokol_window
|
init_userdata_cb: init_sokol_window
|
||||||
frame_userdata_cb: cfg.frame_fn
|
frame_userdata_cb: cfg.frame_fn
|
||||||
|
event_userdata_cb: cfg.event_cb //eventcb
|
||||||
window_title: cfg.window_title.str
|
window_title: cfg.window_title.str
|
||||||
html5_canvas_name: cfg.window_title.str
|
html5_canvas_name: cfg.window_title.str
|
||||||
width: cfg.width
|
width: cfg.width
|
||||||
height: cfg.height
|
height: cfg.height
|
||||||
high_dpi: true
|
//high_dpi: true
|
||||||
}
|
}
|
||||||
//g_font_path = cfg.font_path
|
//g_font_path = cfg.font_path
|
||||||
if cfg.use_ortho {}
|
if cfg.use_ortho {}
|
||||||
@ -101,7 +108,7 @@ pub fn new_context(cfg Config) &GG {
|
|||||||
width: cfg.width
|
width: cfg.width
|
||||||
height: cfg.height
|
height: cfg.height
|
||||||
window: window
|
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
|
scale: 1 // scale
|
||||||
fons:0
|
fons:0
|
||||||
}
|
}
|
||||||
|
@ -36,6 +36,27 @@ pub:
|
|||||||
gl_force_gles2 bool /* if true, setup GLES2/WebGL even if GLES3/WebGL2 is available */
|
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 struct C.sapp_event {
|
||||||
pub:
|
pub:
|
||||||
frame_count u64
|
frame_count u64
|
||||||
|
@ -6,3 +6,14 @@ import sokol.f
|
|||||||
pub const (
|
pub const (
|
||||||
used_import = c.used_import + f.used_import
|
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
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
Loading…
Reference in New Issue
Block a user