gg: use gg types for Events instead of sapp
parent
3341c17202
commit
d4a05bebde
|
@ -3,7 +3,6 @@ import gx
|
|||
import math
|
||||
import os
|
||||
import rand
|
||||
import sokol.sapp
|
||||
import time
|
||||
|
||||
struct App {
|
||||
|
@ -348,11 +347,10 @@ fn (mut b Board) is_game_over() bool {
|
|||
// there are remaining zeros
|
||||
return false
|
||||
}
|
||||
if (x > 0 && fidx == b.field[y][x - 1]) ||
|
||||
(x < 4 - 1 && fidx == b.field[y][x + 1]) ||
|
||||
(y > 0 && fidx == b.field[y - 1][x]) ||
|
||||
(y < 4 - 1 && fidx == b.field[y + 1][x])
|
||||
{
|
||||
if (x > 0 && fidx == b.field[y][x - 1])
|
||||
|| (x < 4 - 1 && fidx == b.field[y][x + 1])
|
||||
|| (y > 0 && fidx == b.field[y - 1][x])
|
||||
|| (y < 4 - 1 && fidx == b.field[y + 1][x]) {
|
||||
// there are remaining merges
|
||||
return false
|
||||
}
|
||||
|
@ -530,48 +528,52 @@ fn (mut app App) ai_move() {
|
|||
|
||||
fn (app &App) label_format(kind LabelKind) gx.TextCfg {
|
||||
match kind {
|
||||
.points { return {
|
||||
color: if app.state in [.over, .victory] {
|
||||
gx.white
|
||||
} else {
|
||||
app.theme.text_color
|
||||
}
|
||||
.points {
|
||||
return {
|
||||
color: if app.state in [.over, .victory] { gx.white } else { app.theme.text_color }
|
||||
align: .left
|
||||
size: app.ui.font_size / 2
|
||||
} }
|
||||
.moves { return {
|
||||
color: if app.state in [.over, .victory] {
|
||||
gx.white
|
||||
} else {
|
||||
app.theme.text_color
|
||||
}
|
||||
}
|
||||
}
|
||||
.moves {
|
||||
return {
|
||||
color: if app.state in [.over, .victory] { gx.white } else { app.theme.text_color }
|
||||
align: .right
|
||||
size: app.ui.font_size / 2
|
||||
} }
|
||||
.tile { return {
|
||||
}
|
||||
}
|
||||
.tile {
|
||||
return {
|
||||
color: app.theme.text_color
|
||||
align: .center
|
||||
vertical_align: .middle
|
||||
size: app.ui.font_size
|
||||
} }
|
||||
.victory { return {
|
||||
}
|
||||
}
|
||||
.victory {
|
||||
return {
|
||||
color: app.theme.victory_color
|
||||
align: .center
|
||||
vertical_align: .middle
|
||||
size: app.ui.font_size * 2
|
||||
} }
|
||||
.game_over { return {
|
||||
}
|
||||
}
|
||||
.game_over {
|
||||
return {
|
||||
color: app.theme.game_over_color
|
||||
align: .center
|
||||
vertical_align: .middle
|
||||
size: app.ui.font_size * 2
|
||||
} }
|
||||
.score_end { return {
|
||||
}
|
||||
}
|
||||
.score_end {
|
||||
return {
|
||||
color: gx.white
|
||||
align: .center
|
||||
vertical_align: .middle
|
||||
size: app.ui.font_size * 3 / 4
|
||||
} }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -584,12 +586,13 @@ fn (mut app App) set_theme(idx int) {
|
|||
}
|
||||
|
||||
fn (mut app App) resize() {
|
||||
mut s := sapp.dpi_scale()
|
||||
mut s := gg.dpi_scale()
|
||||
if s == 0.0 {
|
||||
s = 1.0
|
||||
}
|
||||
w := int(sapp.width() / s)
|
||||
h := int(sapp.height() / s)
|
||||
window_size := gg.window_size()
|
||||
w := int(window_size.width / s)
|
||||
h := int(window_size.height / s)
|
||||
m := f32(min(w, h))
|
||||
app.ui.dpi_scale = s
|
||||
app.ui.window_width = w
|
||||
|
@ -790,11 +793,7 @@ fn (mut app App) handle_swipe() {
|
|||
|
||||
[inline]
|
||||
fn (mut app App) next_theme() {
|
||||
app.set_theme(if app.theme_idx == themes.len - 1 {
|
||||
0
|
||||
} else {
|
||||
app.theme_idx + 1
|
||||
})
|
||||
app.set_theme(if app.theme_idx == themes.len - 1 { 0 } else { app.theme_idx + 1 })
|
||||
}
|
||||
|
||||
[inline]
|
||||
|
@ -815,7 +814,7 @@ fn (mut app App) undo() {
|
|||
}
|
||||
}
|
||||
|
||||
fn (mut app App) on_key_down(key sapp.KeyCode) {
|
||||
fn (mut app App) on_key_down(key gg.KeyCode) {
|
||||
// these keys are independent from the game state:
|
||||
match key {
|
||||
.a { app.is_ai_mode = !app.is_ai_mode }
|
||||
|
@ -843,7 +842,7 @@ fn (mut app App) on_key_down(key sapp.KeyCode) {
|
|||
}
|
||||
}
|
||||
|
||||
fn on_event(e &sapp.Event, mut app App) {
|
||||
fn on_event(e &gg.Event, mut app App) {
|
||||
match e.typ {
|
||||
.key_down {
|
||||
app.on_key_down(e.key_code)
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
module main
|
||||
|
||||
import gg
|
||||
import sokol.sapp
|
||||
import gx
|
||||
import os
|
||||
import time
|
||||
|
@ -266,13 +265,13 @@ fn (app &App) draw() {
|
|||
app.display()
|
||||
}
|
||||
|
||||
fn on_event(e &sapp.Event, mut app App) {
|
||||
fn on_event(e &gg.Event, mut app App) {
|
||||
if e.typ == .key_down {
|
||||
app.key_down(e.key_code)
|
||||
}
|
||||
}
|
||||
|
||||
fn (mut app App) key_down(key sapp.KeyCode) {
|
||||
fn (mut app App) key_down(key gg.KeyCode) {
|
||||
// global keys
|
||||
match key {
|
||||
.escape {
|
||||
|
|
|
@ -2,7 +2,6 @@ module main
|
|||
|
||||
import gx
|
||||
import gg
|
||||
import sokol.sapp
|
||||
import time
|
||||
import math
|
||||
|
||||
|
@ -20,7 +19,7 @@ fn main() {
|
|||
mut context := &Context{
|
||||
gg: 0
|
||||
}
|
||||
context.gg = gg.new_context({
|
||||
context.gg = gg.new_context(
|
||||
width: size
|
||||
height: size
|
||||
font_size: 20
|
||||
|
@ -32,7 +31,7 @@ fn main() {
|
|||
resizable: true
|
||||
bg_color: gx.white
|
||||
font_path: gg.system_font_path()
|
||||
})
|
||||
)
|
||||
context.gg.run()
|
||||
}
|
||||
|
||||
|
@ -44,34 +43,45 @@ fn frame(mut ctx Context) {
|
|||
|
||||
[live]
|
||||
fn (ctx &Context) draw() {
|
||||
mut w := sapp.width()
|
||||
mut h := sapp.height()
|
||||
if sapp.high_dpi() {
|
||||
s := gg.window_size()
|
||||
mut w := s.width
|
||||
mut h := s.height
|
||||
if gg.high_dpi() {
|
||||
w /= 2
|
||||
h /= 2
|
||||
}
|
||||
ctx.gg.draw_line(0, h/2, w, h/2, gx.gray) // x axis
|
||||
ctx.gg.draw_line(w/2, 0, w/2, h, gx.gray) // y axis
|
||||
ctx.gg.draw_line(0, h / 2, w, h / 2, gx.gray) // x axis
|
||||
ctx.gg.draw_line(w / 2, 0, w / 2, h, gx.gray) // y axis
|
||||
atime := f64(time.ticks() / 10)
|
||||
stime := math.sin(2.0 * math.pi * f64(time.ticks() % 6000) / 6000)
|
||||
mut y := 0.0
|
||||
blue := gx.Color {r:100, g:100, b:200}
|
||||
red := gx.Color {r:200, g:100, b:100}
|
||||
blue := gx.Color{
|
||||
r: 100
|
||||
g: 100
|
||||
b: 200
|
||||
}
|
||||
red := gx.Color{
|
||||
r: 200
|
||||
g: 100
|
||||
b: 100
|
||||
}
|
||||
y = 1.0
|
||||
max := f32(w)/(2*scale)
|
||||
max := f32(w) / (2 * scale)
|
||||
min := -max
|
||||
for x := min; x <= max; x += 0.01 {
|
||||
// y = x*x + 2
|
||||
// y = x * x + stime * stime
|
||||
// y = stime
|
||||
// y = stime * h
|
||||
y = stime * 1.0 * math.sin((x) + stime + atime / 32) * ((h/256) + x)
|
||||
y = stime * 1.0 * math.sin((x) + stime + atime / 32) * ((h / 256) + x)
|
||||
// y = (stime * x) * x + stime
|
||||
// y = (x + 3) * (x + 3) / stime + stime*2.5
|
||||
// y = math.sqrt(30.0 - x * x) * stime
|
||||
// y -= (stime-0.5) + stime
|
||||
// ctx.gg.draw_rect(f32((w/2) + x * scale), f32((h/2) - y * scale), 2, 2, blue)
|
||||
ctx.gg.draw_rect(f32((w/2) + x * scale), f32((h/2) - y * scale), 2, (f32(y) * scale), blue)
|
||||
ctx.gg.draw_rect(f32((w/2) + x * scale), f32((h/2) + y * scale), 2, (f32(y) * scale) + 32, red)
|
||||
ctx.gg.draw_rect(f32((w / 2) + x * scale), f32((h / 2) - y * scale), 2, (f32(y) * scale),
|
||||
blue)
|
||||
ctx.gg.draw_rect(f32((w / 2) + x * scale), f32((h / 2) + y * scale), 2, (f32(y) * scale) +
|
||||
32, red)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
import gg
|
||||
import gx
|
||||
import sokol.sapp
|
||||
// import sokol.sapp
|
||||
import time
|
||||
import rand
|
||||
|
||||
|
@ -73,7 +73,7 @@ fn (mut app App) move_food() {
|
|||
}
|
||||
|
||||
// events
|
||||
fn on_keydown(key sapp.KeyCode, mod sapp.Modifier, mut app App) {
|
||||
fn on_keydown(key gg.KeyCode, mod gg.Modifier, mut app App) {
|
||||
match key {
|
||||
.w, .up {
|
||||
if app.dir != .down {
|
||||
|
@ -156,9 +156,8 @@ fn on_frame(mut app App) {
|
|||
app.reset_game()
|
||||
}
|
||||
// checking if snake hit a wall
|
||||
if app.snake[0].x < 0 ||
|
||||
app.snake[0].x >= game_size || app.snake[0].y < 0 || app.snake[0].y >= game_size
|
||||
{
|
||||
if app.snake[0].x < 0 || app.snake[0].x >= game_size || app.snake[0].y < 0
|
||||
|| app.snake[0].y >= game_size {
|
||||
app.reset_game()
|
||||
}
|
||||
|
||||
|
|
|
@ -30,8 +30,8 @@ mut:
|
|||
texture C.sg_image
|
||||
init_flag bool
|
||||
frame_count int
|
||||
mouse_x int = -1
|
||||
mouse_y int = -1
|
||||
mouse_x int = -1
|
||||
mouse_y int = -1
|
||||
}
|
||||
|
||||
/******************************************************************************
|
||||
|
@ -383,7 +383,7 @@ fn cleanup(mut app App) {
|
|||
* event
|
||||
*
|
||||
******************************************************************************/
|
||||
fn my_event_manager(mut ev sapp.Event, mut app App) {
|
||||
fn my_event_manager(mut ev gg.Event, mut app App) {
|
||||
if ev.typ == .mouse_move {
|
||||
app.mouse_x = int(ev.mouse_x)
|
||||
app.mouse_y = int(ev.mouse_y)
|
||||
|
|
|
@ -8,7 +8,7 @@ import rand
|
|||
import time
|
||||
import gx
|
||||
import gg
|
||||
import sokol.sapp
|
||||
// import sokol.sapp
|
||||
|
||||
const (
|
||||
block_size = 20 // virtual pixels
|
||||
|
@ -426,7 +426,7 @@ fn parse_binary_tetro(t_ int) []Block {
|
|||
return res
|
||||
}
|
||||
|
||||
fn on_event(e &sapp.Event, mut game Game) {
|
||||
fn on_event(e &gg.Event, mut game Game) {
|
||||
// println('code=$e.char_code')
|
||||
if e.typ == .key_down {
|
||||
game.key_down(e.key_code)
|
||||
|
@ -455,7 +455,7 @@ fn (mut game Game) rotate_tetro() {
|
|||
}
|
||||
}
|
||||
|
||||
fn (mut game Game) key_down(key sapp.KeyCode) {
|
||||
fn (mut game Game) key_down(key gg.KeyCode) {
|
||||
// global keys
|
||||
match key {
|
||||
.escape {
|
||||
|
|
|
@ -16,7 +16,7 @@ const (
|
|||
]
|
||||
)
|
||||
|
||||
// UI
|
||||
// UI
|
||||
struct App_data {
|
||||
pub mut:
|
||||
gg &gg.Context
|
||||
|
@ -59,7 +59,7 @@ fn draw_frame(mut app App_data) {
|
|||
txt1.create_text(cframe_txt, 43)
|
||||
txt1.create_texture()
|
||||
}
|
||||
// ----- decomment if you want text rotation ----
|
||||
// ----- decomment if you want text rotation ----
|
||||
// txt1.bmp.angle = 3.141592 / 180 * f32(app.frame_c % 360)
|
||||
// txt1.draw_text_bmp(app.gg, 300, 350)
|
||||
// txt1.bmp.angle = 0
|
||||
|
@ -108,7 +108,7 @@ But Vwill prevail for sure, V is the way!!
|
|||
app.gg.end()
|
||||
}
|
||||
|
||||
fn my_event_manager(mut ev sapp.Event, mut app App_data) {
|
||||
fn my_event_manager(mut ev gg.Event, mut app App_data) {
|
||||
if ev.typ == .mouse_move {
|
||||
app.mouse_x = int(ev.mouse_x)
|
||||
app.mouse_y = int(ev.mouse_y)
|
||||
|
@ -145,7 +145,7 @@ fn main() {
|
|||
app.ttf_render << &ttf.TTF_render_Sokol{
|
||||
bmp: &ttf.BitMap{
|
||||
tf: &(app.tf[0])
|
||||
buf: unsafe {malloc(32000000)}
|
||||
buf: unsafe { malloc(32000000) }
|
||||
buf_size: (32000000)
|
||||
color: 0xFF0000FF
|
||||
// style: .raw
|
||||
|
|
|
@ -0,0 +1,127 @@
|
|||
// Copyright (c) 2019-2021 Alexander Medvednikov. All rights reserved.
|
||||
// Use of this source code is governed by an MIT license that can be found in the LICENSE file.
|
||||
module gg
|
||||
|
||||
pub enum KeyCode {
|
||||
invalid = 0
|
||||
space = 32
|
||||
apostrophe = 39 //'
|
||||
comma = 44 //,
|
||||
minus = 45 //-
|
||||
period = 46 //.
|
||||
slash = 47 ///
|
||||
_0 = 48
|
||||
_1 = 49
|
||||
_2 = 50
|
||||
_3 = 51
|
||||
_4 = 52
|
||||
_5 = 53
|
||||
_6 = 54
|
||||
_7 = 55
|
||||
_8 = 56
|
||||
_9 = 57
|
||||
semicolon = 59 //;
|
||||
equal = 61 //=
|
||||
a = 65
|
||||
b = 66
|
||||
c = 67
|
||||
d = 68
|
||||
e = 69
|
||||
f = 70
|
||||
g = 71
|
||||
h = 72
|
||||
i = 73
|
||||
j = 74
|
||||
k = 75
|
||||
l = 76
|
||||
m = 77
|
||||
n = 78
|
||||
o = 79
|
||||
p = 80
|
||||
q = 81
|
||||
r = 82
|
||||
s = 83
|
||||
t = 84
|
||||
u = 85
|
||||
v = 86
|
||||
w = 87
|
||||
x = 88
|
||||
y = 89
|
||||
z = 90
|
||||
left_bracket = 91 //[
|
||||
backslash = 92 //\
|
||||
right_bracket = 93 //]
|
||||
grave_accent = 96 //`
|
||||
world_1 = 161 // non-us #1
|
||||
world_2 = 162 // non-us #2
|
||||
escape = 256
|
||||
enter = 257
|
||||
tab = 258
|
||||
backspace = 259
|
||||
insert = 260
|
||||
delete = 261
|
||||
right = 262
|
||||
left = 263
|
||||
down = 264
|
||||
up = 265
|
||||
page_up = 266
|
||||
page_down = 267
|
||||
home = 268
|
||||
end = 269
|
||||
caps_lock = 280
|
||||
scroll_lock = 281
|
||||
num_lock = 282
|
||||
print_screen = 283
|
||||
pause = 284
|
||||
f1 = 290
|
||||
f2 = 291
|
||||
f3 = 292
|
||||
f4 = 293
|
||||
f5 = 294
|
||||
f6 = 295
|
||||
f7 = 296
|
||||
f8 = 297
|
||||
f9 = 298
|
||||
f10 = 299
|
||||
f11 = 300
|
||||
f12 = 301
|
||||
f13 = 302
|
||||
f14 = 303
|
||||
f15 = 304
|
||||
f16 = 305
|
||||
f17 = 306
|
||||
f18 = 307
|
||||
f19 = 308
|
||||
f20 = 309
|
||||
f21 = 310
|
||||
f22 = 311
|
||||
f23 = 312
|
||||
f24 = 313
|
||||
f25 = 314
|
||||
kp_0 = 320
|
||||
kp_1 = 321
|
||||
kp_2 = 322
|
||||
kp_3 = 323
|
||||
kp_4 = 324
|
||||
kp_5 = 325
|
||||
kp_6 = 326
|
||||
kp_7 = 327
|
||||
kp_8 = 328
|
||||
kp_9 = 329
|
||||
kp_decimal = 330
|
||||
kp_divide = 331
|
||||
kp_multiply = 332
|
||||
kp_subtract = 333
|
||||
kp_add = 334
|
||||
kp_enter = 335
|
||||
kp_equal = 336
|
||||
left_shift = 340
|
||||
left_control = 341
|
||||
left_alt = 342
|
||||
left_super = 343
|
||||
right_shift = 344
|
||||
right_control = 345
|
||||
right_alt = 346
|
||||
right_super = 347
|
||||
menu = 348
|
||||
}
|
47
vlib/gg/gg.v
47
vlib/gg/gg.v
|
@ -13,16 +13,46 @@ import math
|
|||
// import time
|
||||
pub type FNCb = fn (x voidptr)
|
||||
|
||||
pub type FNEvent = fn (e voidptr, x voidptr)
|
||||
pub type FNEvent = fn (e &Event, x voidptr)
|
||||
|
||||
pub type FNFail = fn (msg string, x voidptr)
|
||||
|
||||
pub type FNKeyDown = fn (c sapp.KeyCode, m sapp.Modifier, x voidptr)
|
||||
pub type FNKeyDown = fn (c KeyCode, m Modifier, x voidptr)
|
||||
|
||||
pub type FNMove = fn (x f32, y f32, z voidptr)
|
||||
|
||||
pub type FNChar = fn (c u32, x voidptr)
|
||||
|
||||
pub struct Event {
|
||||
pub:
|
||||
frame_count u64
|
||||
typ sapp.EventType
|
||||
key_code KeyCode
|
||||
char_code u32
|
||||
key_repeat bool
|
||||
modifiers u32
|
||||
mouse_button sapp.MouseButton
|
||||
mouse_x f32
|
||||
mouse_y f32
|
||||
mouse_dx f32
|
||||
mouse_dy 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 enum Modifier {
|
||||
shift = 1 //(1<<0)
|
||||
ctrl = 2 //(1<<1)
|
||||
alt = 4 //(1<<2)
|
||||
super = 8 //(1<<3)
|
||||
}
|
||||
|
||||
pub struct Config {
|
||||
pub:
|
||||
width int
|
||||
|
@ -206,7 +236,8 @@ pub fn (mut ctx Context) refresh_ui() {
|
|||
}
|
||||
|
||||
fn gg_event_fn(ce &C.sapp_event, user_data voidptr) {
|
||||
e := unsafe { &sapp.Event(ce) }
|
||||
// e := unsafe { &sapp.Event(ce) }
|
||||
e := unsafe { &Event(ce) }
|
||||
mut g := unsafe { &Context(user_data) }
|
||||
if g.config.event_fn != voidptr(0) {
|
||||
g.config.event_fn(e, g.config.user_data)
|
||||
|
@ -215,7 +246,7 @@ fn gg_event_fn(ce &C.sapp_event, user_data voidptr) {
|
|||
.key_down {
|
||||
if g.config.keydown_fn != voidptr(0) {
|
||||
kdfn := g.config.keydown_fn
|
||||
kdfn(e.key_code, sapp.Modifier(e.modifiers), g.config.user_data)
|
||||
kdfn(e.key_code, Modifier(e.modifiers), g.config.user_data)
|
||||
}
|
||||
}
|
||||
.char {
|
||||
|
@ -713,6 +744,14 @@ pub fn window_size() Size {
|
|||
return Size{int(sapp.width() / s), int(sapp.height() / s)}
|
||||
}
|
||||
|
||||
pub fn dpi_scale() f32 {
|
||||
return sapp.dpi_scale()
|
||||
}
|
||||
|
||||
pub fn high_dpi() bool {
|
||||
return C.sapp_high_dpi()
|
||||
}
|
||||
|
||||
fn C.WaitMessage()
|
||||
|
||||
/*
|
||||
|
|
Loading…
Reference in New Issue