gg: migrate to sokol; examples: update tetris
parent
37e3cc0e72
commit
9bcbb3868f
|
@ -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()
|
||||
|
|
|
@ -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 <down>
|
||||
}
|
||||
else { }
|
||||
|
|
|
@ -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) {
|
||||
puts("CREATE SHAER");
|
||||
puts("sokol: create Metal shader");
|
||||
SOKOL_ASSERT(shd && desc);
|
||||
|
||||
/* uniform block sizes and image types */
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
}
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue