1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

gg: use gg types for Events instead of sapp

This commit is contained in:
Alexander Medvednikov
2021-02-17 06:44:01 +01:00
parent 3341c17202
commit d4a05bebde
9 changed files with 247 additions and 74 deletions

View File

@ -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)

View File

@ -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 {

View File

@ -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)
}
}

View File

@ -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()
}

View File

@ -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)

View File

@ -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 {

View File

@ -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