mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
time: consolidate the different sleep functions into time.wait(Duration) (#8853)
This commit is contained in:
@ -14,6 +14,6 @@ fn main() {
|
||||
l := r.read_line() or { break }
|
||||
println('$l')
|
||||
// Make it nice and obvious that we are doing this line by line
|
||||
time.sleep_ms(100)
|
||||
time.wait(100 * time.millisecond)
|
||||
}
|
||||
}
|
||||
|
@ -4,7 +4,7 @@ import time
|
||||
// Simulate expensive computing using sleep function
|
||||
fn expensive_computing(id int, duration int, mut wg sync.WaitGroup) {
|
||||
println('Executing expensive computing task ($id)...')
|
||||
time.sleep_ms(duration)
|
||||
time.wait(duration * time.millisecond)
|
||||
println('Finish task $id on $duration ms')
|
||||
wg.done()
|
||||
}
|
||||
|
@ -206,7 +206,7 @@ fn main() {
|
||||
fn (mut app App) run() {
|
||||
for {
|
||||
app.update()
|
||||
time.sleep_ms(app.timer_period_ms)
|
||||
time.wait(app.timer_period_ms * time.millisecond)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -20,6 +20,6 @@ fn main() {
|
||||
for {
|
||||
a.update()
|
||||
print_automaton(a)
|
||||
time.sleep_ms(100)
|
||||
time.wait(100 * time.millisecond)
|
||||
}
|
||||
}
|
||||
|
@ -8,20 +8,20 @@ import time
|
||||
|
||||
struct Game {
|
||||
mut:
|
||||
gg &gg.Context
|
||||
x int
|
||||
y int
|
||||
dy int
|
||||
dx int
|
||||
height int
|
||||
width int
|
||||
draw_fn voidptr
|
||||
gg &gg.Context
|
||||
x int
|
||||
y int
|
||||
dy int
|
||||
dx int
|
||||
height int
|
||||
width int
|
||||
draw_fn voidptr
|
||||
}
|
||||
|
||||
const (
|
||||
window_width = 400
|
||||
window_width = 400
|
||||
window_height = 300
|
||||
width = 50
|
||||
width = 50
|
||||
)
|
||||
|
||||
fn main() {
|
||||
@ -33,7 +33,7 @@ fn main() {
|
||||
width: window_width
|
||||
draw_fn: 0
|
||||
}
|
||||
game.gg = gg.new_context({
|
||||
game.gg = gg.new_context(
|
||||
width: window_width
|
||||
height: window_height
|
||||
font_size: 20
|
||||
@ -44,22 +44,22 @@ fn main() {
|
||||
frame_fn: frame
|
||||
bg_color: gx.white
|
||||
font_path: gg.system_font_path()
|
||||
})
|
||||
)
|
||||
// window.onkeydown(key_down)
|
||||
println('Starting the game loop...')
|
||||
go game.run()
|
||||
game.gg.run()
|
||||
}
|
||||
|
||||
|
||||
// Try uncommenting or changing the lines inside the live functions.
|
||||
// Guess what will happen:
|
||||
[live]
|
||||
fn frame (mut game Game) {
|
||||
fn frame(mut game Game) {
|
||||
game.gg.begin()
|
||||
game.gg.draw_text_def(10, 5, 'Modify examples/hot_reload/bounce.v to get instant updates')
|
||||
game.gg.draw_rect(game.x, game.y, width, width, gx.blue)
|
||||
game.gg.draw_rect(window_width - width - game.x + 10, 200 - game.y + width, width, width, gx.rgb(228, 10, 55))
|
||||
game.gg.draw_rect(window_width - width - game.x + 10, 200 - game.y + width, width,
|
||||
width, gx.rgb(228, 10, 55))
|
||||
game.gg.draw_rect(game.x - 25, 250 - game.y, width, width, gx.rgb(28, 240, 55))
|
||||
game.gg.end()
|
||||
}
|
||||
@ -80,10 +80,6 @@ fn (mut game Game) update_model() {
|
||||
fn (mut game Game) run() {
|
||||
for {
|
||||
game.update_model()
|
||||
//glfw.post_empty_event() // Refresh
|
||||
time.sleep_ms(17) // 60fps
|
||||
time.wait(16 * time.millisecond) // 60fps
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -13,6 +13,6 @@ fn print_message() {
|
||||
fn main() {
|
||||
for {
|
||||
print_message()
|
||||
time.sleep_ms(500)
|
||||
time.wait(500 * time.millisecond)
|
||||
}
|
||||
}
|
||||
|
@ -8,6 +8,7 @@ struct Moon {
|
||||
|
||||
struct Mars {
|
||||
}
|
||||
|
||||
fn (m Mars) dust_storm() bool {
|
||||
return rand.int() >= 0
|
||||
}
|
||||
@ -15,10 +16,11 @@ fn (m Mars) dust_storm() bool {
|
||||
struct Venus {
|
||||
}
|
||||
|
||||
type World = Moon | Mars | Venus
|
||||
type World = Mars | Moon | Venus
|
||||
|
||||
struct Lander {
|
||||
}
|
||||
|
||||
fn (l Lander) deorbit() {
|
||||
println('leaving orbit')
|
||||
}
|
||||
@ -28,7 +30,7 @@ fn (l Lander) open_parachutes(n int) {
|
||||
|
||||
fn wait() {
|
||||
println('waiting...')
|
||||
time.sleep(1)
|
||||
time.wait(1 * time.second)
|
||||
}
|
||||
|
||||
fn (l Lander) land(w World) {
|
||||
@ -53,7 +55,7 @@ fn (l Lander) land(w World) {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
l := Lander {}
|
||||
l := Lander{}
|
||||
l.land(Venus{})
|
||||
l.land(Mars{})
|
||||
}
|
||||
|
@ -2,11 +2,13 @@ import gg
|
||||
import gx
|
||||
import sokol.audio
|
||||
|
||||
const credits = 'Based on the ByteBeat formula from: https://www.youtube.com/watch?v=V4GfkFbDojc \n "Techno" by Gabriel Miceli'
|
||||
|
||||
struct AppState {
|
||||
mut:
|
||||
gframe int // the current graphical frame
|
||||
frame_0 int // offset of the current audio frames, relative to the start of the music
|
||||
frames [2048]f32 // a copy of the last rendered audio frames
|
||||
gframe int // the current graphical frame
|
||||
frame_0 int // offset of the current audio frames, relative to the start of the music
|
||||
frames [2048]f32 // a copy of the last rendered audio frames
|
||||
gg &gg.Context // used for drawing
|
||||
}
|
||||
|
||||
@ -14,10 +16,8 @@ fn my_audio_stream_callback(buffer &f32, num_frames int, num_channels int, mut a
|
||||
mut soundbuffer := buffer
|
||||
for frame := 0; frame < num_frames; frame++ {
|
||||
t := int(f32(acontext.frame_0 + frame) * 0.245)
|
||||
// Credits for the formula below: https://www.youtube.com/watch?v=V4GfkFbDojc
|
||||
// "Techno" by Gabriel Miceli
|
||||
y := (t * (((t / 10 | 0) ^ ((t / 10 | 0) -
|
||||
1280)) % 11) / 2 & 127) +
|
||||
y := (t * (((t / 10 | 0) ^ ((t / 10 | 0) - 1280)) % 11) / 2 & 127) +
|
||||
(t * (((t / 640 | 0) ^ ((t / 640 | 0) - 2)) % 13) / 2 & 127)
|
||||
for ch := 0; ch < num_channels; ch++ {
|
||||
idx := frame * num_channels + ch
|
||||
@ -32,14 +32,15 @@ fn my_audio_stream_callback(buffer &f32, num_frames int, num_channels int, mut a
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println(credits)
|
||||
mut state := &AppState{
|
||||
gg: 0
|
||||
}
|
||||
audio.setup({
|
||||
audio.setup(
|
||||
stream_userdata_cb: my_audio_stream_callback
|
||||
user_data: state
|
||||
})
|
||||
state.gg = gg.new_context({
|
||||
)
|
||||
state.gg = gg.new_context(
|
||||
bg_color: gx.rgb(50, 50, 50)
|
||||
width: 1024
|
||||
height: 400
|
||||
@ -48,7 +49,7 @@ fn main() {
|
||||
window_title: 'ByteBeat Music'
|
||||
frame_fn: graphics_frame
|
||||
user_data: state
|
||||
})
|
||||
)
|
||||
state.gg.run()
|
||||
audio.shutdown()
|
||||
}
|
||||
|
@ -19,14 +19,14 @@ fn my_audio_stream_callback(buffer &f32, num_frames int, num_channels int) {
|
||||
for frame := 0; frame < num_frames; frame++ {
|
||||
for ch := 0; ch < num_channels; ch++ {
|
||||
idx := frame * num_channels + ch
|
||||
if ms < 500 {
|
||||
soundbuffer[idx] = sintone(20, frame, num_frames)
|
||||
} else if ms < 1000 {
|
||||
soundbuffer[idx] = sintone(25, frame, num_frames)
|
||||
if ms < 250 {
|
||||
soundbuffer[idx] = 0.5 * sintone(20, frame, num_frames)
|
||||
} else if ms < 300 {
|
||||
soundbuffer[idx] = 0.5 * sintone(25, frame, num_frames)
|
||||
} else if ms < 1500 {
|
||||
soundbuffer[idx] *= sintone(22, frame, num_frames)
|
||||
} else {
|
||||
soundbuffer[idx] = sintone(25, frame, num_frames)
|
||||
soundbuffer[idx] = 0.5 * sintone(25, frame, num_frames)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -34,9 +34,9 @@ fn my_audio_stream_callback(buffer &f32, num_frames int, num_channels int) {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
audio.setup({
|
||||
audio.setup(
|
||||
stream_cb: my_audio_stream_callback
|
||||
})
|
||||
time.sleep_ms(2500)
|
||||
)
|
||||
time.wait(2000 * time.millisecond)
|
||||
audio.shutdown()
|
||||
}
|
||||
|
@ -72,7 +72,7 @@ fn (mut p Player) play_wav_file(fpath string) ? {
|
||||
p.samples << samples
|
||||
p.finished = false
|
||||
for !p.finished {
|
||||
time.sleep_ms(16)
|
||||
time.wait(16 * time.millisecond)
|
||||
}
|
||||
p.free()
|
||||
}
|
||||
|
@ -218,7 +218,7 @@ fn (mut g Game) run() {
|
||||
g.delete_completed_lines()
|
||||
}
|
||||
// glfw.post_empty_event() // force window redraw
|
||||
time.sleep_ms(timer_period)
|
||||
time.wait(timer_period * time.millisecond)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -32,7 +32,7 @@ fn (mut app App) sse() vweb.Result {
|
||||
data := '{"time": "$time.now().str()", "random_id": "$rand.ulid()"}'
|
||||
session.send_message(event: 'ping', data: data) or { return app.server_error(501) }
|
||||
println('> sent event: $data')
|
||||
time.sleep_ms(1000)
|
||||
time.wait(1 * time.second)
|
||||
}
|
||||
return app.server_error(501)
|
||||
}
|
||||
|
@ -5,10 +5,10 @@ import os
|
||||
import x.websocket
|
||||
|
||||
fn main() {
|
||||
println('press enter to quit...\n')
|
||||
go start_server()
|
||||
time.sleep_ms(100)
|
||||
time.wait(100 * time.millisecond)
|
||||
go start_client()
|
||||
println('press enter to quit...')
|
||||
os.get_line()
|
||||
}
|
||||
|
||||
@ -23,25 +23,21 @@ fn start_server() ? {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
})?
|
||||
}) ?
|
||||
s.on_message(fn (mut ws websocket.Client, msg &websocket.Message) ? {
|
||||
ws.write(msg.payload, msg.opcode) or {
|
||||
panic(err)
|
||||
}
|
||||
ws.write(msg.payload, msg.opcode) or { panic(err) }
|
||||
})
|
||||
s.on_close(fn (mut ws websocket.Client, code int, reason string) ? {
|
||||
// println('client ($ws.id) closed connection')
|
||||
})
|
||||
s.listen() or {
|
||||
// println('error on server listen: $err')
|
||||
}
|
||||
s.listen() or { println('error on server listen: $err') }
|
||||
unsafe {
|
||||
s.free()
|
||||
}
|
||||
}
|
||||
|
||||
fn start_client() ? {
|
||||
mut ws := websocket.new_client('ws://localhost:30000')?
|
||||
mut ws := websocket.new_client('ws://localhost:30000') ?
|
||||
// mut ws := websocket.new_client('wss://echo.websocket.org:443')?
|
||||
// use on_open_ref if you want to send any reference object
|
||||
ws.on_open(fn (mut ws websocket.Client) ? {
|
||||
@ -67,15 +63,9 @@ fn start_client() ? {
|
||||
// ws.on_message_ref(fn (mut ws websocket.Client, msg &websocket.Message, r &SomeRef)? {
|
||||
// // println('type: $msg.opcode payload:\n$msg.payload ref: $r')
|
||||
// }, &r)
|
||||
ws.connect() or {
|
||||
println('error on connect: $err')
|
||||
}
|
||||
go write_echo(mut ws) or {
|
||||
println('error on write_echo $err')
|
||||
}
|
||||
ws.listen() or {
|
||||
println('error on listen $err')
|
||||
}
|
||||
ws.connect() or { println('error on connect: $err') }
|
||||
go write_echo(mut ws) or { println('error on write_echo $err') }
|
||||
ws.listen() or { println('error on listen $err') }
|
||||
unsafe {
|
||||
ws.free()
|
||||
}
|
||||
@ -85,12 +75,8 @@ fn write_echo(mut ws websocket.Client) ? {
|
||||
message := 'echo this'
|
||||
for i := 0; i <= 10; i++ {
|
||||
// Server will send pings every 30 seconds
|
||||
ws.write_str(message) or {
|
||||
println('panicing writing $err')
|
||||
}
|
||||
time.sleep_ms(100)
|
||||
}
|
||||
ws.close(1000, 'normal') or {
|
||||
println('panicing $err')
|
||||
ws.write_str(message) or { println('panicing writing $err') }
|
||||
time.wait(100 * time.millisecond)
|
||||
}
|
||||
ws.close(1000, 'normal') or { println('panicing $err') }
|
||||
}
|
||||
|
Reference in New Issue
Block a user