From f0a6c6757a795b8e186de0076836a87acef59fa7 Mon Sep 17 00:00:00 2001 From: Cliff Click Date: Sun, 15 Nov 2026 11:20:40 -0800 Subject: Graphics now allow resize Reorganize docs. Fix bugs X86 LOad+cmp scheduling. Math intrinsics --- chapter31/docs/examples/Breakout.smp | 21 +++++--- chapter31/docs/examples/Clock.smp | 68 +++++++++++++------------- chapter31/docs/examples/Graphics.smp | 19 +++++-- chapter31/docs/examples/README.md | 35 ++++++++----- chapter31/docs/examples/TicTacToeGraphical.smp | 15 +++--- 5 files changed, 92 insertions(+), 66 deletions(-) (limited to 'chapter31/docs/examples') diff --git a/chapter31/docs/examples/Breakout.smp b/chapter31/docs/examples/Breakout.smp index b1d0b443..d60894ac 100644 --- a/chapter31/docs/examples/Breakout.smp +++ b/chapter31/docs/examples/Breakout.smp @@ -191,7 +191,7 @@ struct Game { for( int i = 0; i < bricks#; i++ ) { if( bricks[i] == 0 ) continue; int row = i/COLS; - i64 x = LEFT+2+(i-row*COLS)*BRICK_DX, y = BRICK_TOP+row*BRICK_DY; + i64 x = LEFT+2+(i%COLS)*BRICK_DX, y = BRICK_TOP+row*BRICK_DY; c.fillRect(x,y,BRICK_W,BRICK_H,COLORS[row]); c.fillRect(x,y,BRICK_W,2,WHITE); } @@ -235,7 +235,7 @@ struct Game { int lastMouse; // Read and act on the controls - val _input = { -> + val _input = { sys.draw.Canvas c -> val gfx = sys.gfx; bool serve = gfx.key(' ') || gfx.button(gfx.LEFT); bool pause = gfx.key('P'); @@ -243,7 +243,7 @@ struct Game { // Keyboard direction and mouse position int direction = (gfx.key(KEY_RIGHT) ? 1 : 0)-(gfx.key(KEY_LEFT) ? 1 : 0); - int mouse = gfx.mouseX(); + int mouse = c.localX(gfx.mouseX()); // Restart requested restartHeld = debounce( restart, restartHeld, { -> _reset(); sys.audio.stop(); } ); @@ -254,7 +254,7 @@ struct Game { pauseHeld = debounce( pause, pauseHeld, { -> paused = !paused; } ); // If no key and mouse moved, set paddle to mouse - if( mouse != lastMouse && direction == 0 && !paused ) { + if( mouse >= 0 && mouse != lastMouse && direction == 0 && !paused ) { _paddle(mouse*1.0); // Set paddle to mouse // If READY and not PLAY, ball tracks paddle directly if( phase == READY ) x = paddle; @@ -291,7 +291,7 @@ struct Game { // Fixed-width decimal labels avoid allocating new strings on every frame. val _number = { !sys.draw.Canvas c, i64 x, i64 y, int value, int digits, int scale, u32 color -> for( int i = 0; i < digits; i++ ) { - c.text(x+(digits-1-i)*4*scale,y,DIGITS[value-value/10*10],scale,color); + c.text(x+(digits-1-i)*4*scale,y,DIGITS[value%10],scale,color); value /= 10; } }; @@ -299,7 +299,7 @@ val _number = { !sys.draw.Canvas c, i64 x, i64 y, int value, int digits, int sca // Short, fading triangle waves; the array initializer builds PCM once at startup. val _tone = { int period -> val samples = new i16[~2400]{int i -> - int phase = i-i/period*period; + int phase = i%period; int triangle = phase < period/2 ? phase : period-phase; return (triangle*4-period)*6000/period*(2400-i)/2400; }; @@ -341,7 +341,12 @@ val main = { u8[~][] args -> while( status == gfx.OK ) { if( gfx.key('Q') || gfx.key(gfx.ESCAPE) ) break; - int direction = g._input(); + // Physics stays in base coordinates; only drawing and input scale. + if( c.resize(gfx.width(),gfx.height()) ) { + g.lastMouse = c.localX(gfx.mouseX()); + frame_ms = 0; + } + int direction = g._input(c); i64 now_ms = sys.time.monotonic(), elapsed_ms = now_ms-last_ms; last_ms = now_ms; // Drop long stalls instead of teleporting the ball after a window drag. @@ -353,7 +358,7 @@ val main = { u8[~][] args -> } if( now_ms >= frame_ms ) { g._draw(c); - status = gfx.present(c.pixels); + status = gfx.presentSize(c.pixels,c.width,c.height); frame_ms = now_ms+FRAME_ms; if( args# > 1 && ++frames == 10 ) break; } diff --git a/chapter31/docs/examples/Clock.smp b/chapter31/docs/examples/Clock.smp index ecf0fae1..fb50f84e 100644 --- a/chapter31/docs/examples/Clock.smp +++ b/chapter31/docs/examples/Clock.smp @@ -20,12 +20,13 @@ val main = { u8[~][] args -> // Main event loop while( status == gfx.OK ) { + bool resized = screen.resize(gfx.width(),gfx.height()); // Did the user press space bar? bool space = gfx.key(' '); // Did the left mouse button go down? bool down = gfx.button(gfx.LEFT); // ANd did it go down over this button? - bool click = button.update(gfx.mouseX(),gfx.mouseY(),down); + bool click = button.update(screen.localX(gfx.mouseX()),screen.localY(gfx.mouseY()),down); // If clicked, flip the clock mode if( click || (space && !held) ) analog = !analog; held = space; @@ -33,12 +34,12 @@ val main = { u8[~][] args -> // Change button label button.label = analog ? "DIGITAL CLOCK" : "ANALOG CLOCK"; // If the next timer event is the every-33-msec event - if( timers.poll(time.monotonic()) == frame_id ) { + if( timers.poll(time.monotonic()) == frame_id || resized ) { // Draw the clock screen and button _draw(screen,time.now(),analog); button.draw(screen,3,GREEN,BACKGROUND,DIM); // Presne the canvas on screen - status = gfx.present(screen.pixels); + status = gfx.presentSize(screen.pixels,screen.width,screen.height); // Testing only: show 5 frames, flip to analog, show 5 more and exit if( args# > 1 ) { frames++; @@ -66,62 +67,61 @@ val _draw = { !sys.draw.Canvas screen, i64 wall_ms, i64 analog -> // a..g bits: top, upper-right, lower-right, bottom, lower-left, upper-left, // middle. Faint unlit bars retain the old LED figure-eight silhouette. +val _digitFont = new u8[~]{ + // 0 1 2 3 4 5 6 7 8 9 + 0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f +}; +val _digitBars = new u8[~]{ + // x0 y0 x1 y1 + 14, 0, 58, 0, // a: top + 68, 12, 68, 60, // b: upper-right + 68, 84, 68, 132, // c: lower-right + 14, 144, 58, 144, // d: bottom + 4, 84, 4, 132, // e: lower-left + 4, 12, 4, 60, // f: upper-left + 14, 72, 58, 72 // g: middle +}; val _digit = { !sys.draw.Canvas c, int x, int y, i64 digit -> - int bits = digit == 0 ? 0x3f : digit == 1 ? 0x06 : digit == 2 ? 0x5b : - digit == 3 ? 0x4f : digit == 4 ? 0x66 : digit == 5 ? 0x6d : - digit == 6 ? 0x7d : digit == 7 ? 0x07 : digit == 8 ? 0x7f : digit == 9 ? 0x6f : 0; + int bits = 0 <= digit < _digitFont# ? _digitFont[digit] : 0; for( int i = 0; i < 7; i++ ) { u32 color = (bits&(1< - _digit(c,68,166,hour/10); _digit(c,154,166,hour-hour/10*10); - _digit(c,284,166,minute/10); _digit(c,370,166,minute-minute/10*10); - _digit(c,500,166,second/10); _digit(c,586,166,second-second/10*10); + _digit(c, 68,166, hour/10); _digit(c,154,166, hour%10); + _digit(c,284,166,minute/10); _digit(c,370,166,minute%10); + _digit(c,500,166,second/10); _digit(c,586,166,second%10); for( int i = 0; i < 2; i++ ) { c.fillCircle(254+i*216,214,6,GREEN); c.fillCircle(254+i*216,262,6,GREEN); } c.text(272,352,"LOCAL TIME 24H",3,GREEN); }; -// Rational sine approximation, accurate enough for pixel clock geometry. -// Arguments are nonnegative turns; no additional native math dependency. -val _sin = { f64 turn -> - i64 whole = turn; turn -= whole; - bool negative = turn > 0.5; - f64 x = (negative ? turn-0.5 : turn)*6.283185307179586; - f64 product = x*(3.141592653589793-x); - f64 numerator = negative ? -16.0 : 16.0; - return numerator*product/(49.34802200544679-4*product); -}; val _hand = { !sys.draw.Canvas c, f64 turn, int length, int thickness, u32 color -> - int x = 360+_sin(turn)*length, y = 232-_sin(turn+0.25)*length; + f64 angle = turn*2*sys.math.PI; + int x = 360+sys.math.sin(angle)*length, y = 232-sys.math.cos(angle)*length; c.line(360,232,x,y,thickness,color); }; val _analog = { !sys.draw.Canvas c, int hour, int minute, int second, int millisecond -> c.circle(360,232,154,3,GREEN); for( int i = 0; i < 60; i++ ) { - f64 turn = i/60.0; - bool major = i-i/5*5 == 0; + f64 angle = i/60.0*2*sys.math.PI; + bool major = i%5 == 0; int inner = major ? 133 : 143; - int x0 = 360+_sin(turn)*inner, y0 = 232-_sin(turn+0.25)*inner; - int x1 = 360+_sin(turn)*149, y1 = 232-_sin(turn+0.25)*149; + f64 sine = sys.math.sin(angle), cosine = sys.math.cos(angle); + int x0 = 360+sine*inner, y0 = 232-cosine*inner; + int x1 = 360+sine* 149 , y1 = 232-cosine* 149 ; c.line(x0,y0,x1,y1,major ? 3 : 1,GREEN); } c.text(349,100,"12",2,GREEN); c.text(484,227,"3",2,GREEN); - c.text(357,354,"6",2,GREEN); c.text(230,227,"9",2,GREEN); + c.text(357,354, "6",2,GREEN); c.text(230,227,"9",2,GREEN); f64 seconds = second+millisecond/1000.0; f64 minutes = minute+seconds/60.0; - _hand(c,(hour-hour/12*12+minutes/60.0)/12.0,78,8,GREEN); + _hand(c,(hour%12+minutes/60.0)/12.0,78,8,GREEN); _hand(c,minutes/60.0,112,5,GREEN); _hand(c,seconds/60.0,128,2,0xc3ffcb); c.fillCircle(360,232,6,GREEN); diff --git a/chapter31/docs/examples/Graphics.smp b/chapter31/docs/examples/Graphics.smp index 8b251fa2..1878e97d 100644 --- a/chapter31/docs/examples/Graphics.smp +++ b/chapter31/docs/examples/Graphics.smp @@ -2,13 +2,22 @@ // Pass any argument for a short, self-closing test; otherwise close or press Esc. val main = { u8[~][] args -> if( sys.gfx.open("Simple pixels",320,240) != sys.gfx.OK ) return 1; - val pixels = new u32[320*240]; - for( int y = 0; y < 240; y++ ) - for( int x = 0; x < 320; x++ ) - pixels[y*320+x] = (x*255/319 << 16) | (y*255/239 << 8) | 64; - int result = sys.gfx.present(pixels); + val c = new sys.draw.Canvas(320,240); + int result = sys.gfx.OK; + bool dirty = true; int frames = 0; while( result == sys.gfx.OK ) { + if( c.resize(sys.gfx.width(),sys.gfx.height()) ) dirty = true; + if( dirty ) { + // The gradient fills the window, including non-4:3 shapes. + int dx = c.width > 1 ? c.width-1 : 1, dy = c.height > 1 ? c.height-1 : 1; + for( int y = 0; y < c.height; y++ ) + for( int x = 0; x < c.width; x++ ) + c.pixels[y*c.width+x] = (x*255/dx << 16) | (y*255/dy << 8) | 64; + result = sys.gfx.presentSize(c.pixels,c.width,c.height); + if( result != sys.gfx.OK ) break; + dirty = false; + } result = sys.gfx.poll(true); if( args# > 1 && ++frames == 5 ) break; } diff --git a/chapter31/docs/examples/README.md b/chapter31/docs/examples/README.md index 20b644cf..05dcf6cb 100644 --- a/chapter31/docs/examples/README.md +++ b/chapter31/docs/examples/README.md @@ -2,25 +2,30 @@ These programs live beside the documentation so readers can find and run them. Their non-graphical native regression tests run as part of `make tests`. +All four graphical demos support window resizing. Clock, Tic-tac-toe, and +Breakout scale their base layouts uniformly and center them in the window; +mouse input follows the same transform. Breakout keeps its physics in base +coordinates, so resizing preserves the current game and ball speed. The pixel +gradient fills the entire window at its new resolution. The optional [pixel-window example](Graphics.smp) uses MiniFB; see -[graphics setup](../graphics.md). `make run-graphics` opens it explicitly. +[graphics setup](../../src/main/smp/sys/gfx.md). `make run-graphics` opens it explicitly. 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 +[drawing library](../../src/main/smp/sys/draw/README.md). Run it with +`make run-graphical-tictactoe`; 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. +Optional [background audio](../../src/main/smp/sys/audio.md) supplies soft move and win/loss cues. -The [digital and analog clock](Clock.smp) demonstrates the [time library](../time.md), +The [digital and analog clock](Clock.smp) demonstrates the [time library](../../src/main/smp/sys/time/README.md), green seven-segment digits, smooth clock hands, and a reusable software button. -Run `make run-clock CTAGS=`; click the button or press Space to switch faces, +Run `make run-clock`; click the button or press Space to switch faces, and Q/Escape to exit. Both faces show system-local time. -`make clock-example CTAGS=` builds it without starting a window. +`make clock-example` builds it without starting a window. The [Breakout game](Breakout.smp) has 24 columns and 8 rows, with colors and point values by depth: 1 at the bottom through 8 at the top, 864 points total. Clear the wall before losing all three balls. Mouse movement or the arrow keys move the paddle; Space/left click serves, P pauses, R restarts, Q/Escape exits. -Run `make run-breakout CTAGS=` or build only with `make breakout CTAGS=`. +Run `make run-breakout` or build only with `make breakout`. The familiar paddle, serve, and limited-ball rules follow [Atari's Breakout instructions](https://atari.com/pages/breakout). @@ -43,7 +48,7 @@ the boost, and clearing the whole wall without a display or audio device. From `chapter31`, build the tic-tac-toe executable (without starting it): ```sh -make lib tictactoe CTAGS= +make lib tictactoe ``` Run the Windows build from PowerShell: @@ -52,9 +57,15 @@ Run the Windows build from PowerShell: .\build\examples\x86_64_v2_win64\TicTacToe.exe ``` -Or from Cygwin: +Or from Command Prompt (`cmd.exe`): -```sh +```bat +.\build\examples\x86_64_v2_win64\TicTacToe.exe +``` + +Or from Bash (Cygwin, using the Windows build): + +```bash ./build/examples/x86_64_v2_win64/TicTacToe.exe ``` @@ -66,7 +77,7 @@ directory in WSL. Separate Java and native output directories let the Windows and Linux builds coexist: ```sh -make lib tictactoe CLZDIR=build/classes-linux CTAGS= +make lib tictactoe CLZDIR=build/classes-linux ./build/examples/x86_64_v2_SystemV/TicTacToe ``` diff --git a/chapter31/docs/examples/TicTacToeGraphical.smp b/chapter31/docs/examples/TicTacToeGraphical.smp index d3d575a7..72ee6522 100644 --- a/chapter31/docs/examples/TicTacToeGraphical.smp +++ b/chapter31/docs/examples/TicTacToeGraphical.smp @@ -9,7 +9,7 @@ val main = { u8[~][] args -> 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); + int result = sys.gfx.presentSize(screen.pixels,screen.width,screen.height); bool mouseHeld = false, keyHeld = false, replayHeld = false; int hover = -1, frames = 0; @@ -19,8 +19,9 @@ val main = { u8[~][] args -> if( sys.gfx.key('Q') ) break; bool down = sys.gfx.button(sys.gfx.LEFT); bool replay = sys.gfx.key('R') || sys.gfx.key(' '); - int square = _square(sys.gfx.mouseX(),sys.gfx.mouseY()); - bool dirty = square != hover; + bool resized = screen.resize(sys.gfx.width(),sys.gfx.height()); + int square = _square(screen.localX(sys.gfx.mouseX()),screen.localY(sys.gfx.mouseY())); + bool dirty = resized || square != hover; hover = square; if( replay && !replayHeld ) { sys.audio.stop(); _reset(board); dirty = true; } bool digit = false; @@ -41,7 +42,7 @@ val main = { u8[~][] args -> replayHeld = replay; if( dirty ) { _draw(screen,board,hover); - result = sys.gfx.present(screen.pixels); + result = sys.gfx.presentSize(screen.pixels,screen.width,screen.height); } if( args# > 1 && ++frames == 5 ) break; } @@ -143,13 +144,13 @@ val _draw = { !sys.draw.Canvas screen, u8[~] board, int hover -> screen.text(484,145,"CPU",2,0xa2b9b7); screen.fillRect(48,168,504,504,paper); if( !finished && hover >= 0 && hover < 9 && board[hover] == ' ' ) - screen.fillRect(48+(hover-hover/3*3)*168,168+hover/3*168,168,168,0xd9e8de); + screen.fillRect(48+(hover%3)*168,168+hover/3*168,168,168,0xd9e8de); for( int i = 1; i < 3; i++ ) { screen.fillRect(48+i*168-3,168,6,504,ink); screen.fillRect(48,168+i*168-3,504,6,ink); } for( int i = 0; i < 9; i++ ) { - int cx = 132+(i-i/3*3)*168, cy = 252+i/3*168; + int cx = 132+(i%3)*168, cy = 252+i/3*168; if( board[i] == 'X' ) screen.line(cx-38,cy-38,cx+38,cy+38,12,cross).line(cx+38,cy-38,cx-38,cy+38,12,cross); else if( board[i] == 'O' ) screen.circle(cx,cy,48,11,ring); @@ -160,7 +161,7 @@ val _draw = { !sys.draw.Canvas screen, u8[~] board, int hover -> if( line >= 0 ) { int first = line < 3 ? line*3 : line < 6 ? line-3 : line == 6 ? 0 : 2; int last = line < 3 ? first+2 : line < 6 ? first+6 : line == 6 ? 8 : 6; - screen.line(132+(first-first/3*3)*168,252+first/3*168,132+(last-last/3*3)*168,252+last/3*168,5,0xeab64e); + screen.line(132+(first%3)*168,252+first/3*168,132+(last%3)*168,252+last/3*168,5,0xeab64e); } screen.text(48,704,"CLICK A SQUARE OR PRESS 1-9",3,paper); screen.text(48,738,"R OR SPACE REPLAY Q OR ESC QUIT",3,0xa2b9b7); -- cgit v1.2.3