diff options
| author | Cliff Click <cliffc@acm.org> | 2026-11-12 13:16:00 -0800 |
|---|---|---|
| committer | Cliff Click <cliffc@acm.org> | 2026-11-12 13:52:46 -0800 |
| commit | 467338ee01565234762db8077c067df42c3b40a8 (patch) | |
| tree | 42c44a77da87c4811b1eb7611d0952dc2c55e022 /chapter31/docs/examples | |
| parent | 0720fd8ed0aa92248622a9f3c3ebd522b002fd91 (diff) | |
| download | Simple-467338ee01565234762db8077c067df42c3b40a8.tar.gz Simple-467338ee01565234762db8077c067df42c3b40a8.zip | |
Add audio, overhaul makefiles
Adds simple audio support to Simple
Overhaul makefiles; remove common boilerplate; make them more uniform
Diffstat (limited to 'chapter31/docs/examples')
| -rw-r--r-- | chapter31/docs/examples/README.md | 1 | ||||
| -rw-r--r-- | chapter31/docs/examples/TicTacToeGraphical.smp | 15 |
2 files changed, 14 insertions, 2 deletions
diff --git a/chapter31/docs/examples/README.md b/chapter31/docs/examples/README.md index db95487e..a1d81be2 100644 --- a/chapter31/docs/examples/README.md +++ b/chapter31/docs/examples/README.md @@ -8,6 +8,7 @@ The [graphical tic-tac-toe](TicTacToeGraphical.smp) uses the Simple [drawing library](../drawing.md). Run it with `make run-graphical-tictactoe CTAGS=`; click a cell or press 1–9, R/Space to replay, and Q/Escape to quit. The terminal game below remains available. +Optional [background audio](../audio.md) supplies soft move and win/loss cues. From `chapter31`, build the tic-tac-toe executable (without starting it): diff --git a/chapter31/docs/examples/TicTacToeGraphical.smp b/chapter31/docs/examples/TicTacToeGraphical.smp index 7daf6710..b4f8bcf4 100644 --- a/chapter31/docs/examples/TicTacToeGraphical.smp +++ b/chapter31/docs/examples/TicTacToeGraphical.smp @@ -7,6 +7,7 @@ val main = { u8[~][] args -> val board = new u8[9]; _reset(board); if( sys.gfx.open("Simple tic-tac-toe",screen.width,screen.height) != sys.gfx.OK ) return 1; + sys.audio.open(); // Audio is optional; failure leaves the game silent. _draw(screen,board,-1); int result = sys.gfx.present(screen.pixels); bool mouseHeld = false, keyHeld = false, replayHeld = false; @@ -20,7 +21,7 @@ val main = { u8[~][] args -> int square = _square(sys.gfx.mouseX(),sys.gfx.mouseY()); bool dirty = square!=hover; hover = square; - if( replay && !replayHeld ) { _reset(board); dirty=true; } + if( replay && !replayHeld ) { sys.audio.stop(); _reset(board); dirty=true; } bool digit = false; int choice = down && !mouseHeld ? square : -1; for( int i=0; i<9; i++ ) { @@ -30,7 +31,10 @@ val main = { u8[~][] args -> if( !keyHeld ) choice=i; } } - if( !(replay && !replayHeld) && _move(board,choice) ) dirty=true; + if( !(replay && !replayHeld) && _move(board,choice) ) { + sys.audio.play(_sound(board)); + dirty=true; + } mouseHeld = down; keyHeld = digit; replayHeld = replay; @@ -40,10 +44,17 @@ val main = { u8[~][] args -> } if( args#>1 && ++frames==5 ) break; } + sys.audio.close(); sys.gfx.close(); return result==sys.gfx.ERROR ? 1 : 0; }; +// Called only for an accepted move; invalid input never requests a sound. +val _sound = { u8[~] board -> + int winner = _winner(board); + return winner=='X' ? sys.audio.WIN : winner=='O' ? sys.audio.LOSE : sys.audio.BOOP; +}; + val _reset = { u8[] board -> for( int i=0; i<9; i++ ) board[i]=' '; }; |
