summaryrefslogtreecommitdiff
path: root/chapter31/docs/examples/Clock.smp
diff options
context:
space:
mode:
authorCliff Click <cliffc@acm.org>2026-11-15 11:20:40 -0800
committerCliff Click <cliffc@acm.org>2026-11-15 11:20:40 -0800
commitf0a6c6757a795b8e186de0076836a87acef59fa7 (patch)
treeef45c1b58093420bbd2de6647274139e46539bfe /chapter31/docs/examples/Clock.smp
parent7e66b7ae6b00a2e1652db32f01f0bf480508ef05 (diff)
downloadSimple-f0a6c6757a795b8e186de0076836a87acef59fa7.tar.gz
Simple-f0a6c6757a795b8e186de0076836a87acef59fa7.zip
Graphics now allow resizemain
Reorganize docs. Fix bugs X86 LOad+cmp scheduling. Math intrinsics
Diffstat (limited to 'chapter31/docs/examples/Clock.smp')
-rw-r--r--chapter31/docs/examples/Clock.smp68
1 files changed, 34 insertions, 34 deletions
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<<i)) != 0 ? GREEN : DIM;
- if( i == 0 || i == 3 || i == 6 ) {
- int dy = i == 0 ? 0 : i == 3 ? 144 : 72;
- c.line(x+14,y+dy,x+58,y+dy,10,color);
- } else {
- bool right = i == 1 || i == 2;
- int dx = right ? 68 : 4, dy = i == 1 || i == 5 ? 12 : 84;
- c.line(x+dx,y+dy,x+dx,y+dy+48,10,color);
- }
+ int bar = i*4;
+ c.line(x+_digitBars[bar ],y+_digitBars[bar+1],
+ x+_digitBars[bar+2],y+_digitBars[bar+3],10,color);
}
};
val _digital = { !sys.draw.Canvas c, int hour, int minute, int second ->
- _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);