Skip to content

Commit b3e5bca

Browse files
committed
add touch support for Android; fix macos audio
1 parent 0190862 commit b3e5bca

File tree

1 file changed

+55
-29
lines changed

1 file changed

+55
-29
lines changed

examples/breakout/breakout.v

+55-29
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,27 @@ fn (mut g Game) goto_next_level() {
158158
g.nlevels++
159159
}
160160

161+
fn (mut g Game) move(k f32) {
162+
if k < 0 {
163+
if g.paddle_x <= 0 {
164+
return
165+
}
166+
} else if k > 0 {
167+
if g.paddle_x >= g.width - g.paddle_w {
168+
return
169+
}
170+
}
171+
g.paddle_x += k * g.paddle_dx
172+
}
173+
161174
fn (mut g Game) update() {
175+
if g.ctx.pressed_keys[gg.KeyCode.left] {
176+
g.move(-1.0)
177+
}
178+
if g.ctx.pressed_keys[gg.KeyCode.right] {
179+
g.move(1.0)
180+
}
181+
//
162182
g.ball_x, g.ball_y = g.ball_x + g.ball_dx, g.ball_y + g.ball_dy
163183
// Wall collisions
164184
if g.ball_x < g.ball_r || g.ball_x > g.width - g.ball_r {
@@ -211,31 +231,13 @@ fn (mut g Game) update() {
211231
}
212232
}
213233

214-
fn frame(mut g Game) {
215-
if g.ctx.pressed_keys[gg.KeyCode.left] {
216-
if g.paddle_x > 0 {
217-
g.paddle_x -= g.paddle_dx
218-
}
219-
}
220-
if g.ctx.pressed_keys[gg.KeyCode.right] {
221-
if g.paddle_x < g.width - g.paddle_w {
222-
g.paddle_x += g.paddle_dx
223-
}
224-
}
225-
g.update()
226-
g.draw()
227-
}
228-
229-
fn key_down(key gg.KeyCode, _ gg.Modifier, mut g Game) {
230-
g.handle_event()
231-
match key {
232-
.r {
233-
g.game_over()
234-
}
235-
.escape {
236-
exit(0)
237-
}
238-
else {}
234+
fn (mut g Game) touch_event(touch_point gg.TouchPoint) {
235+
ws := gg.window_size()
236+
tx := touch_point.pos_x
237+
if tx <= f32(ws.width) * 0.5 {
238+
g.move(-1.0)
239+
} else {
240+
g.move(1.0)
239241
}
240242
}
241243

@@ -244,7 +246,8 @@ fn (mut g Game) handle_event() {
244246
if g.nevent > 0 {
245247
return
246248
}
247-
// the audio has to be started when the wasm canvas has received user interaction, unlike on desktop platforms
249+
// the audio has to be started when the wasm canvas has received user
250+
// interaction, unlike on desktop platforms
248251
audio.setup(buffer_frames: 1024)
249252
g.sound.init()
250253
g.nevent++
@@ -254,7 +257,7 @@ fn main() {
254257
mut g := Game.new()
255258
mut fpath := asset.get_path('../assets', 'fonts/RobotoMono-Regular.ttf')
256259
$if !wasm32_emscripten {
257-
audio.setup(buffer_frames: 128)
260+
audio.setup(buffer_frames: 512) // too small values lead to cracking sounds or no sound at all on macos
258261
g.sound.init()
259262
fpath = ''
260263
}
@@ -263,11 +266,34 @@ fn main() {
263266
height: g.height
264267
window_title: 'V Breakout'
265268
sample_count: 2
266-
frame_fn: frame
267-
keydown_fn: key_down
269+
frame_fn: fn (mut g Game) {
270+
g.update()
271+
g.draw()
272+
}
268273
click_fn: fn (x f32, y f32, btn gg.MouseButton, mut g Game) {
269274
g.handle_event()
270275
}
276+
event_fn: fn (e &gg.Event, mut g Game) {
277+
g.handle_event()
278+
if e.typ == .touches_began || e.typ == .touches_moved {
279+
if e.num_touches > 0 {
280+
touch_point := e.touches[0]
281+
g.touch_event(touch_point)
282+
}
283+
}
284+
}
285+
keydown_fn: fn (key gg.KeyCode, _ gg.Modifier, mut g Game) {
286+
g.handle_event()
287+
match key {
288+
.r {
289+
g.game_over()
290+
}
291+
.escape {
292+
exit(0)
293+
}
294+
else {}
295+
}
296+
}
271297
user_data: g
272298
font_path: fpath
273299
)

0 commit comments

Comments
 (0)