Browse Source

add key checking example, key and mouse constants

Ondřej Hruška 11 months ago
parent
commit
b5016c6b12
Signed by: Ondřej Hruška <ondra@ondrovo.com> GPG key ID: 2C5FD5035250423D
5 changed files with 270 additions and 88 deletions
  1. 13 0
      README.md
  2. 16 16
      crsn_screen/src/exec.rs
  3. 117 1
      crsn_screen/src/lib.rs
  4. 66 71
      examples/life.csn
  5. 58 0
      examples/screen_keys.csn

+ 13 - 0
README.md View File

@@ -783,6 +783,7 @@ such as animations.
783 783
 
784 784
 ; Check key status. Keys are 0-127. Reads 1 if the key is pressed, 0 if not.
785 785
 ; A list of supported keys can be found in the extension source code.
786
+; Run the example screen_keys.scn to interactively check key codes.
786 787
 (sc-key Wr'pressed Rd'num)
787 788
 
788 789
 ; Check mouse button state. 
@@ -790,6 +791,18 @@ such as animations.
790 791
 (sc-mbtn Wr'pressed Rd'btn)
791 792
 ```
792 793
 
794
+Available constants provided by the module:
795
+
796
+- SCREEN_AUTO_BLIT, SCREEN_FPS, SCREEN_UPSCALE, 
797
+- MBTN_LEFT, MBTN_RIGHT, MBTN_MIDDLE (MBTN_MID)
798
+- KEY_0, KEY_1, KEY_2, KEY_3, KEY_4, KEY_5, KEY_6, KEY_7, KEY_8, KEY_9  (0-9)
799
+- KEY_A, KEY_B, KEY_C, KEY_D, KEY_E, KEY_F, KEY_G, KEY_H, KEY_I, KEY_J, KEY_K, KEY_L, KEY_M, KEY_N, KEY_O, KEY_P, KEY_Q, KEY_R, KEY_S, KEY_T, KEY_U, KEY_V, KEY_W, KEY_X, KEY_Y, KEY_Z (10-35)
800
+- KEY_F1, KEY_F2, KEY_F3, KEY_F4, KEY_F5, KEY_F6, KEY_F7, KEY_F8, KEY_F9, KEY_F10, KEY_F11, KEY_F12, KEY_F13, KEY_F14, KEY_F15, 
801
+- KEY_Down, KEY_Left, KEY_Right, KEY_Up, KEY_Apos, KEY_Backtick, KEY_Backslash, KEY_Comma, KEY_Equal, KEY_BracketL, KEY_Minus, KEY_Period, KEY_BracketR, KEY_Semicolon, KEY_Slash, KEY_Backspace, KEY_Delete, KEY_End, KEY_Enter, KEY_Escape, KEY_Home, KEY_Insert, KEY_Menu, KEY_PageDown, KEY_PageUp, KEY_Pause, KEY_Space, KEY_Tab, KEY_NumLock, KEY_CapsLock, KEY_ScrollLock,
802
+- KEY_KP0, KEY_KP1, KEY_KP2, KEY_KP3, KEY_KP4, KEY_KP5, KEY_KP6, KEY_KP7, KEY_KP8, KEY_KP9, 
803
+- KEY_KPDot, KEY_KPSlash, KEY_KPAsterisk, KEY_KPMinus, KEY_KPPlus, KEY_KPEnter, 
804
+- KEY_ShiftL, KEY_ShiftR, KEY_CtrlL, KEY_CtrlR, KEY_AltL, KEY_AltR, KEY_WinL, KEY_WinR, 
805
+
793 806
 ### Graphic acceleration commands
794 807
 
795 808
 ```

+ 16 - 16
crsn_screen/src/exec.rs View File

@@ -4,7 +4,6 @@ use std::time::{Duration, Instant};
4 4
 use minifb::{Key, MouseButton, MouseMode, ScaleMode, Window, WindowOptions};
5 5
 
6 6
 use crsn::asm::data::literal::{Value, is_negative};
7
-use crsn::asm::error::CrsnError;
8 7
 use crsn::asm::instr::cond::Flag;
9 8
 use crsn::module::{EvalRes, OpTrait};
10 9
 use crsn::runtime::fault::Fault;
@@ -55,7 +54,7 @@ impl Default for Backend {
55 54
 }
56 55
 
57 56
 impl Backend {
58
-    pub fn draw_rect_log(&mut self, mut x: Value, mut y: Value, w: Value, h: Value, color: u32) {
57
+    pub fn draw_rect_log(&mut self, x: Value, y: Value, w: Value, h: Value, color: u32) {
59 58
         for yy in (y * self.opts.upscale)..((y + h) * self.opts.upscale).min(self.height) {
60 59
             for xx in (x * self.opts.upscale)..((x + w) * self.opts.upscale).min(self.width) {
61 60
                 self.buffer[(yy * self.width + xx) as usize] = color as u32;
@@ -414,7 +413,7 @@ fn blit(backend: &mut Backend) -> Result<(), Fault> {
414 413
 
415 414
 fn num2key(num: Value) -> Option<Key> {
416 415
     let remap = [
417
-        Key::Key0,
416
+        Key::Key0, // 0
418 417
         Key::Key1,
419 418
         Key::Key2,
420 419
         Key::Key3,
@@ -423,28 +422,29 @@ fn num2key(num: Value) -> Option<Key> {
423 422
         Key::Key6,
424 423
         Key::Key7,
425 424
         Key::Key8,
426
-        Key::Key9,
425
+        Key::Key9, // 9
426
+
427 427
         Key::A, // 10
428 428
         Key::B,
429 429
         Key::C,
430 430
         Key::D,
431 431
         Key::E,
432
-        Key::F,
432
+        Key::F, // 15
433 433
         Key::G,
434 434
         Key::H,
435 435
         Key::I,
436 436
         Key::J,
437
-        Key::K,
437
+        Key::K, // 20
438 438
         Key::L,
439 439
         Key::M,
440 440
         Key::N,
441 441
         Key::O,
442
-        Key::P,
442
+        Key::P, // 25
443 443
         Key::Q,
444 444
         Key::R,
445 445
         Key::S,
446 446
         Key::T,
447
-        Key::U,
447
+        Key::U, // 30
448 448
         Key::V,
449 449
         Key::W,
450 450
         Key::X,
@@ -455,12 +455,12 @@ fn num2key(num: Value) -> Option<Key> {
455 455
         Key::F2,
456 456
         Key::F3,
457 457
         Key::F4,
458
-        Key::F5,
458
+        Key::F5, // 40
459 459
         Key::F6,
460 460
         Key::F7,
461 461
         Key::F8,
462 462
         Key::F9,
463
-        Key::F10,
463
+        Key::F10, // 45
464 464
         Key::F11,
465 465
         Key::F12,
466 466
         Key::F13,
@@ -468,18 +468,18 @@ fn num2key(num: Value) -> Option<Key> {
468 468
         Key::F15, // 50
469 469
 
470 470
         Key::Down, // 51
471
-        Key::Left,
472
-        Key::Right,
473
-        Key::Up,
471
+        Key::Left, // 52
472
+        Key::Right, // 53
473
+        Key::Up, // 54
474 474
         Key::Apostrophe,
475
-        Key::Backquote,
475
+        Key::Backquote, // 56 (a backtick)
476 476
         Key::Backslash, // 57
477 477
         Key::Comma,
478 478
         Key::Equal,
479
-        Key::LeftBracket,
479
+        Key::LeftBracket, // 60 [
480 480
         Key::Minus,
481 481
         Key::Period,
482
-        Key::RightBracket,
482
+        Key::RightBracket, // 63 ] *This does not work due to a bug in the library we use - PR is sent upstream https://github.com/emoon/rust_minifb/pull/222*
483 483
         Key::Semicolon,
484 484
         Key::Slash, // 65
485 485
         Key::Backspace,

+ 117 - 1
crsn_screen/src/lib.rs View File

@@ -8,6 +8,7 @@ use crsn::module::{CrsnExtension, ParseRes};
8 8
 use crsn::sexp::SourcePosition;
9 9
 use crsn::asm::data::literal::Value;
10 10
 use crate::exec::{OPT_AUTO_BLIT, OPT_FRAME_RATE, OPT_UPSCALE};
11
+use minifb::Key;
11 12
 
12 13
 mod defs;
13 14
 mod parse;
@@ -36,9 +37,124 @@ impl CrsnExtension for ScreenOps {
36 37
     fn get_constant_value<'a>(&self, name: &str) -> Option<Value>
37 38
     {
38 39
         match name {
39
-            "SCREEN_AUTO_BLIT" => Some(OPT_AUTO_BLIT),
40
+            "SCREEN_AUTO_BLIT" | "SCREEN_AUTOBLIT" => Some(OPT_AUTO_BLIT),
40 41
             "SCREEN_FPS" => Some(OPT_FRAME_RATE),
41 42
             "SCREEN_UPSCALE" => Some(OPT_UPSCALE),
43
+
44
+            "MBTN_LEFT" => Some(0),
45
+            "MBTN_RIGHT" => Some(1),
46
+            "MBTN_MIDDLE" | "MBTN_MID" => Some(2),
47
+
48
+            "KEY_0" => Some(Key::Key0 as Value), // 0
49
+            "KEY_1" => Some(Key::Key1 as Value),
50
+            "KEY_2" => Some(Key::Key2 as Value),
51
+            "KEY_3" => Some(Key::Key3 as Value),
52
+            "KEY_4" => Some(Key::Key4 as Value),
53
+            "KEY_5" => Some(Key::Key5 as Value),
54
+            "KEY_6" => Some(Key::Key6 as Value),
55
+            "KEY_7" => Some(Key::Key7 as Value),
56
+            "KEY_8" => Some(Key::Key8 as Value),
57
+            "KEY_9" => Some(Key::Key9 as Value), // 9
58
+
59
+            "KEY_A" => Some(Key::A as Value), // 10
60
+            "KEY_B" => Some(Key::B as Value),
61
+            "KEY_C" => Some(Key::C as Value),
62
+            "KEY_D" => Some(Key::D as Value),
63
+            "KEY_E" => Some(Key::E as Value),
64
+            "KEY_F" => Some(Key::F as Value), // 15
65
+            "KEY_G" => Some(Key::G as Value),
66
+            "KEY_H" => Some(Key::H as Value),
67
+            "KEY_I" => Some(Key::I as Value),
68
+            "KEY_J" => Some(Key::J as Value),
69
+            "KEY_K" => Some(Key::K as Value), // 20
70
+            "KEY_L" => Some(Key::L as Value),
71
+            "KEY_M" => Some(Key::M as Value),
72
+            "KEY_N" => Some(Key::N as Value),
73
+            "KEY_O" => Some(Key::O as Value),
74
+            "KEY_P" => Some(Key::P as Value), // 25
75
+            "KEY_Q" => Some(Key::Q as Value),
76
+            "KEY_R" => Some(Key::R as Value),
77
+            "KEY_S" => Some(Key::S as Value),
78
+            "KEY_T" => Some(Key::T as Value),
79
+            "KEY_U" => Some(Key::U as Value), // 30
80
+            "KEY_V" => Some(Key::V as Value),
81
+            "KEY_W" => Some(Key::W as Value),
82
+            "KEY_X" => Some(Key::X as Value),
83
+            "KEY_Y" => Some(Key::Y as Value),
84
+            "KEY_Z" => Some(Key::Z as Value), // 35
85
+
86
+            "KEY_F1" => Some(Key::F1 as Value), // 36
87
+            "KEY_F2" => Some(Key::F2 as Value),
88
+            "KEY_F3" => Some(Key::F3 as Value),
89
+            "KEY_F4" => Some(Key::F4 as Value),
90
+            "KEY_F5" => Some(Key::F5 as Value), // 40
91
+            "KEY_F6" => Some(Key::F6 as Value),
92
+            "KEY_F7" => Some(Key::F7 as Value),
93
+            "KEY_F8" => Some(Key::F8 as Value),
94
+            "KEY_F9" => Some(Key::F9 as Value),
95
+            "KEY_F10" => Some(Key::F10 as Value), // 45
96
+            "KEY_F11" => Some(Key::F11 as Value),
97
+            "KEY_F12" => Some(Key::F12 as Value),
98
+            "KEY_F13" => Some(Key::F13 as Value),
99
+            "KEY_F14" => Some(Key::F14 as Value),
100
+            "KEY_F15" => Some(Key::F15 as Value), // 50
101
+
102
+            "KEY_Down" => Some(Key::Down as Value), // 51
103
+            "KEY_Left" => Some(Key::Left as Value), // 52
104
+            "KEY_Right" => Some(Key::Right as Value), // 53
105
+            "KEY_Up" => Some(Key::Up as Value), // 54
106
+            "KEY_Apos" => Some(Key::Apostrophe as Value),
107
+            "KEY_Backtick" => Some(Key::Backquote as Value), // 56 (a backtick)
108
+            "KEY_Backslash" => Some(Key::Backslash as Value), // 57
109
+            "KEY_Comma" => Some(Key::Comma as Value),
110
+            "KEY_Equal" => Some(Key::Equal as Value),
111
+            "KEY_BracketL" => Some(Key::LeftBracket as Value), // 60 [
112
+            "KEY_Minus" => Some(Key::Minus as Value),
113
+            "KEY_Period" => Some(Key::Period as Value),
114
+            "KEY_BracketR" => Some(Key::RightBracket as Value), // 63 ] *This does not work due to a bug in the library we use - PR is sent upstream https://github.com/emoon/rust_minifb/pull/222*
115
+            "KEY_Semicolon" => Some(Key::Semicolon as Value),
116
+            "KEY_Slash" => Some(Key::Slash as Value), // 65
117
+            "KEY_Backspace" => Some(Key::Backspace as Value),
118
+            "KEY_Delete" => Some(Key::Delete as Value),
119
+            "KEY_End" => Some(Key::End as Value),
120
+            "KEY_Enter" => Some(Key::Enter as Value),
121
+            "KEY_Escape" => Some(Key::Escape as Value), // 70
122
+            "KEY_Home" => Some(Key::Home as Value),
123
+            "KEY_Insert" => Some(Key::Insert as Value),
124
+            "KEY_Menu" => Some(Key::Menu as Value),
125
+            "KEY_PageDown" => Some(Key::PageDown as Value),
126
+            "KEY_PageUp" => Some(Key::PageUp as Value),
127
+            "KEY_Pause" => Some(Key::Pause as Value), // 76
128
+            "KEY_Space" => Some(Key::Space as Value),
129
+            "KEY_Tab" => Some(Key::Tab as Value),
130
+            "KEY_NumLock" => Some(Key::NumLock as Value),
131
+            "KEY_CapsLock" => Some(Key::CapsLock as Value),
132
+            "KEY_ScrollLock" => Some(Key::ScrollLock as Value),
133
+            "KEY_ShiftL" => Some(Key::LeftShift as Value),
134
+            "KEY_ShiftR" => Some(Key::RightShift as Value),
135
+            "KEY_CtrlL" => Some(Key::LeftCtrl as Value),
136
+            "KEY_CtrlR" => Some(Key::RightCtrl as Value),
137
+            "KEY_KP0" => Some(Key::NumPad0 as Value), // 86
138
+            "KEY_KP1" => Some(Key::NumPad1 as Value),
139
+            "KEY_KP2" => Some(Key::NumPad2 as Value),
140
+            "KEY_KP3" => Some(Key::NumPad3 as Value),
141
+            "KEY_KP4" => Some(Key::NumPad4 as Value),
142
+            "KEY_KP5" => Some(Key::NumPad5 as Value),
143
+            "KEY_KP6" => Some(Key::NumPad6 as Value),
144
+            "KEY_KP7" => Some(Key::NumPad7 as Value),
145
+            "KEY_KP8" => Some(Key::NumPad8 as Value),
146
+            "KEY_KP9" => Some(Key::NumPad9 as Value),
147
+            "KEY_KPDot" => Some(Key::NumPadDot as Value),
148
+            "KEY_KPSlash" => Some(Key::NumPadSlash as Value),
149
+            "KEY_KPAsterisk" => Some(Key::NumPadAsterisk as Value),
150
+            "KEY_KPMinus" => Some(Key::NumPadMinus as Value),
151
+            "KEY_KPPlus" => Some(Key::NumPadPlus as Value),
152
+            "KEY_KPEnter" => Some(Key::NumPadEnter as Value), // 100
153
+
154
+            "KEY_AltL" => Some(Key::LeftAlt as Value),
155
+            "KEY_AltR" => Some(Key::RightAlt as Value),
156
+            "KEY_WinL" | "KEY_SuperL" | "KEY_MetaL" => Some(Key::LeftSuper as Value),
157
+            "KEY_WinR" | "KEY_SuperR" | "KEY_MetaR" => Some(Key::RightSuper as Value),
42 158
             _ => None
43 159
         }
44 160
     }

+ 66 - 71
examples/life.csn View File

@@ -1,130 +1,125 @@
1 1
 (
2 2
     ; simple GOL with screen and a buffer + DRAWING!!!
3
-    
4
-    ; Middle-click to payse or resume 
3
+
4
+    ; Middle-click to payse or resume
5 5
     ; (hold the button until paused - there is a sleep between generations and checking buttons)
6 6
     ;
7 7
     ; When paused, draw with left (white) and right (black) mouse buttons
8 8
     ;
9 9
     ; Resume by clicking middle again.
10
-    
10
+
11 11
     (def GENERATION_MS 200)
12 12
 
13 13
     ; Real pixel size
14 14
     (sc-init 400 400)
15 15
     ; Upscaling factor (bug pixels)
16 16
     (sc-opt SCREEN_UPSCALE 10)
17
-    
17
+
18 18
     ; Number of big pixels
19 19
     (def W 40)
20 20
     (def H 40)
21
-    
21
+
22 22
     ; !!! If you change size, also update the following constants.
23 23
     ;     Compile-time math is not implemented yet.
24
-    
24
+
25 25
     (def XMAX 39) ; W-1
26 26
     (def YMAX 39) ; H-1
27 27
     (def NCELLS 1600) ; W*H
28
-        
28
+
29 29
     ; --- end of config ---
30 30
     (sc-opt SCREEN_AUTO_BLIT 0)
31
-    
31
+
32 32
     (sc-erase 0) ; all black
33 33
     (sym ng g15)
34 34
     (mkbf ng NCELLS)
35
-    
35
+
36 36
     ; one glider
37 37
     (bfwr @ng 1 1)
38 38
     (bfwr @ng 42 1)
39 39
     (bfwr @ng 80 1)
40 40
     (bfwr @ng 81 1)
41 41
     (bfwr @ng 82 1)
42
-    
42
+
43 43
     ; another glider
44 44
     (bfwr @ng 16 1)
45 45
     (bfwr @ng 55 1)
46 46
     (bfwr @ng 95 1)
47 47
     (bfwr @ng 96 1)
48 48
     (bfwr @ng 97 1)
49
-    
50
-    
51
-    (def LBTN 0)
52
-    (def RBTN 1)
53
-    (def MBTN 2)
54
-                
49
+
55 50
     (:loop)
56 51
         (sc-poll)
57
-        
52
+
58 53
         ; Drawing
59
-        (sc-mbtn _ MBTN
54
+        (sc-mbtn _ MBTN_MID
60 55
             (nz?
61 56
                 (:release)
62 57
                     (sc-poll)
63 58
                     (mslp 10)
64
-                    (sc-mbtn _ MBTN (nz? (j :release)))
65
-                    
59
+                    (sc-mbtn _ MBTN_MID (nz? (j :release)))
60
+
66 61
                 (:mousing)
67 62
                     (sc-poll)
68 63
                     (mslp 10)
69
-                    
64
+
70 65
                     (ld r3 -1)
71
-                    
72
-                    (sc-mbtn _ LBTN)
66
+
67
+                    (sc-mbtn _ MBTN_LEFT)
73 68
                     (ld.nz r3 1)
74
-                    (sc-mbtn _ RBTN)
69
+                    (sc-mbtn _ MBTN_RIGHT)
75 70
                     (ld.nz r3 0)
76
-                    
71
+
77 72
                     (tst r3 (nneg?
78 73
                         (sc-mouse r0 r1)
79
-                        
74
+
80 75
                         (tst r3)
81 76
                         (sc-wr.nz r0 r1 #ffffff)
82 77
                         (sc-wr.z r0 r1 #000000)
83
-                        
78
+
84 79
                         (sc-blit)
85 80
                         (mul r1 W)
86 81
                         (add r0 r1)
87 82
                         (ld8 r3:8 r3)
88 83
                         (bfwr @ng r0 r3)
89
-                    ))                    
90
-                    
91
-                    (sc-mbtn _ MBTN 
84
+                    ))
85
+
86
+                    (sc-mbtn _ MBTN_MID
92 87
                         (z? (j :mousing)))
93
-                        
88
+
94 89
                 (:release2)
95 90
                     (sc-poll)
96 91
                     (mslp 10)
97
-                    (sc-mbtn _ MBTN (nz? (j :release2)))
92
+                    (sc-mbtn _ MBTN_MID (nz? (j :release2)))
98 93
             )
99
-        )       
100
-        
94
+        )
95
+
101 96
         (call Display)
102 97
         (sc-blit)
103 98
         (mslp GENERATION_MS)
104 99
         (j :loop)
105
-    
100
+
106 101
     (proc CountNeighbors x y
107 102
         (sym xx r4)
108 103
         (sym yy r5)
109 104
         (sym i r6)
110
-        (sym count r7)     
111
-        
105
+        (sym count r7)
106
+
112 107
         ; yeah this sucks. it's correct, though
113
-        
108
+
114 109
         (:a)
115
-            (add yy y -1 (neg? (j :d)))        
110
+            (add yy y -1 (neg? (j :d)))
116 111
             (add xx x -1 (neg? (j :b)))
117 112
             (mul i yy W)
118 113
             (add i xx)
119 114
             (bfrd r0 @ng i)
120 115
             (and r0 0xFF (nz? (inc count)))
121
-            
116
+
122 117
         (:b)
123 118
             (mul i yy W)
124 119
             (add i x)
125 120
             (bfrd r0 @ng i)
126 121
             (and r0 0xFF (nz? (inc count)))
127
-        
122
+
128 123
         (:c)
129 124
             (add xx x 1)
130 125
             (cmp xx W (ge? (j :d)))
@@ -132,16 +127,16 @@
132 127
             (add i xx)
133 128
             (bfrd r0 @ng i)
134 129
             (and r0 0xFF (nz? (inc count)))
135
-        
136
-        (:d)     
130
+
131
+        (:d)
137 132
             (add xx x -1 (neg? (j :f)))
138 133
             (mul i y W)
139 134
             (add i xx)
140 135
             (bfrd r0 @ng i)
141 136
             (and r0 0xFF (nz? (inc count)))
142
-            
137
+
143 138
         ; there is no E
144
-            
139
+
145 140
         (:f)
146 141
             (add xx x 1)
147 142
             (cmp xx W (ge? (j :g)))
@@ -149,26 +144,26 @@
149 144
             (add i xx)
150 145
             (bfrd r0 @ng i)
151 146
             (and r0 0xFF (nz? (inc count)))
152
-                 
147
+
153 148
         (:g)
154
-            (add yy y 1)        
149
+            (add yy y 1)
155 150
             (cmp yy H (ge? (j :end)))
156 151
             (add xx x -1 (neg? (j :h)))
157 152
             (mul i yy W)
158 153
             (add i xx)
159 154
             (bfrd r0 @ng i)
160 155
             (and r0 0xFF (nz? (inc count)))
161
-            
156
+
162 157
         (:h)
163
-            (add yy y 1)        
158
+            (add yy y 1)
164 159
             (cmp yy H (ge? (j :end)))
165 160
             (mul i yy W)
166 161
             (add i x)
167 162
             (bfrd r0 @ng i)
168 163
             (and r0 0xFF (nz? (inc count)))
169
-        
164
+
170 165
         (:i)
171
-            (add yy y 1)        
166
+            (add yy y 1)
172 167
             (cmp yy H (ge? (j :end)))
173 168
             (add xx x 1)
174 169
             (cmp xx W (ge? (j :end)))
@@ -176,59 +171,59 @@
176 171
             (add i xx)
177 172
             (bfrd r0 @ng i)
178 173
             (and r0 0xFF (nz? (inc count)))
179
-        
174
+
180 175
         (:end)
181 176
         (ret count)
182 177
     )
183
-    
178
+
184 179
     (proc Display
185 180
         ; display and calc next gen
186 181
         (sym x r4)
187 182
         (sym y r5)
188
-        (sym i r6)        
183
+        (sym i r6)
189 184
         (:next)
190 185
             ; The lower byte contains 0 or 1
191 186
             ; the second byte will be filled with the next gen
192
-            
187
+
193 188
             (bfrd r0 @ng i)
194
-            
189
+
195 190
             ; Show this gen
196
-            (and r0 0xFF 
191
+            (and r0 0xFF
197 192
                 (nz? (sc-wr x y 0xffffff))
198 193
                 (z? (sc-wr x y 0x000000)))
199
-            
194
+
200 195
             (call CountNeighbors x y)
201
-            
196
+
202 197
             ; stay: 2,3
203 198
             ; die:  >3
204 199
             ; born: 3
205
-            
206
-            (cmp res0 2 
200
+
201
+            (cmp res0 2
207 202
                 (eq? (ld8 r0:8 r0)) ; stasis
208 203
                 (ne?
209
-                    (tst r0 
210
-                        (z? 
211
-                            (cmp res0 3 (eq? (ld8 r0:8 1))) ; birth                
212
-                        ) 
213
-                        (nz? 
214
-                            (cmp res0 3 (eq? (ld8 r0:8 1))) ; stay alive                
204
+                    (tst r0
205
+                        (z?
206
+                            (cmp res0 3 (eq? (ld8 r0:8 1))) ; birth
207
+                        )
208
+                        (nz?
209
+                            (cmp res0 3 (eq? (ld8 r0:8 1))) ; stay alive
215 210
                         )
216 211
                     )
217 212
                 )
218 213
             )
219
-            
214
+
220 215
             (bfwr @ng i r0)
221
-            
216
+
222 217
             (inc i)
223 218
             (inc x)
224 219
             (cmp x W
225 220
                 (ne? (j :next)))
226 221
             (ld x 0)
227 222
             (inc y)
228
-            (cmp y H 
223
+            (cmp y H
229 224
                 (eq? (j :next2)))
230 225
             (j :next)
231
-                   
226
+
232 227
         ; Shift all by 8 to the right (generation shift)
233 228
         (:next2)
234 229
             (dec i)

+ 58 - 0
examples/screen_keys.csn View File

@@ -0,0 +1,58 @@
1
+(
2
+    ; Helper to check key codes of physical keys
3
+
4
+    (sc-init 200 200)
5
+    (lds @cout "Press keys in the window to see their codes...\n")
6
+    (sym pressed r15)
7
+    (mkbf pressed 256)
8
+    (:loop)
9
+        (sc-poll)
10
+        (mslp 10)
11
+        (sym cnt r14)
12
+        (ld cnt 0)
13
+        (:next)
14
+            (sc-key _ cnt
15
+                (inval? (nop))
16
+                (nz?
17
+                    (bfrd r1 @pressed cnt
18
+                        (z?
19
+                            (call printnum cnt)
20
+                            (lds @cout " PRESS\n")
21
+                            (bfwr @pressed cnt 1)
22
+                            (rng r0 0 #ffffff)
23
+                            (sc-erase r0)
24
+                            (sc-blit)
25
+                        )
26
+                    )
27
+                )
28
+                (z?
29
+                    (bfrd r1 @pressed cnt
30
+                        (nz?
31
+                            (call printnum cnt)
32
+                            (lds @cout " RELEASE\n")
33
+                            (bfwr @pressed cnt 0)
34
+                        )
35
+                    )
36
+                )
37
+            )
38
+            (inc cnt)
39
+            (cmp cnt 256 (ne? (j :next)))
40
+            (j :loop)
41
+
42
+   ; this is a version if itoa that prints a number
43
+    (proc printnum num
44
+        (mkbf r15)
45
+        (ld r1 num)
46
+        (tst r1 (<0? (mul r1 -1)))
47
+        (:next)
48
+            (mod r0 r1 10)
49
+            (add r0 '0')
50
+            (bfrpush @r15 r0)
51
+            (div r1 10 (z?
52
+                (tst num (<0? (bfrpush @r15 '-')))
53
+                (lds @cout @r15)
54
+                (del @r15)
55
+                (ret)))
56
+            (j :next)
57
+    )
58
+)