+/TECS
git clone https://git.julia.blue/+/TECS
e36a3e1
v1.0
1/** 2 * A library of functions for displaying graphics on the screen. 3 * The Hack physical screen consists of 512 rows (indexed 0..511, top to bottom) 4 * of 256 pixels each (indexed 0..255, left to right). The top left pixel on 5 * the screen is indexed (0,0). 6 */ 7class Screen { 8 static boolean color; 9 static Array mask; 10 11 /** Initializes the Screen. */ 12 function void init() { 13 var int temp, i; 14 let color = true; 15 let mask = Array.new(16); 16 let temp = 1; 17 let i = 0; 18 while(i < 16){ 19 let mask[i] = temp; 20 let temp = temp + temp; 21 let i = i + 1; 22 } 23 return; 24 } 25 26 /** Erases the entire screen. */ 27 function void clearScreen() { 28 var int i; 29 let i = 24575; //16384 + 255 * 32 + 511/16 30 while(i > 16383){ 31 do Memory.poke(i, 0); 32 let i = i - 1; 33 } 34 return; 35 } 36 37 /** Sets the current color, to be used for all subsequent drawXXX commands. 38 * Black is represented by true, white by false. */ 39 function void setColor(boolean b) { 40 let color = b; 41 return; 42 } 43 44 /** Draws the (x,y) pixel, using the current color. */ 45 function void drawPixel(int x, int y) { 46 var int temp, mod; 47 if((x > 511) | (x < 0) | (y > 255) | (y < 0)){ 48 do Sys.error(7); 49 } 50 let temp = 16384 + (y * 32) + (x / 16); 51 let mod = x - ((x / 16) * 16); 52 if(color){ 53 do Memory.poke(temp, Memory.peek(temp) | mask[mod]); 54 } 55 else{ 56 do Memory.poke(temp, Memory.peek(temp) & ~mask[mod]); 57 } 58 return; 59 } 60 61 /** Draws a line from pixel (x1,y1) to pixel (x2,y2), using the current color. */ 62 function void drawLine(int x1, int y1, int x2, int y2) { 63 var int dx, dy, temp, sign; 64 if((x2 > 511) | (x1 < 0) | (y2 > 255) | (y1 < 0)){ 65 do Sys.error(8); 66 } 67 if(x1 > x2){ 68 let dx = x1; 69 let x1 = x2; 70 let x2 = dx; 71 let dy = y1; 72 let y1 = y2; 73 let y2 = dy; 74 } 75 let dx = x2 - x1; 76 if(dx = 0){ 77 if(y1 > y2){ 78 let dy = y1; 79 let y1 = y2; 80 let y2 = dy; 81 } 82 do Screen.drawRectangle(x1, y1, x2, y2); 83 return; 84 } 85 let dy = y2 - y1; 86 if(dy = 0){ 87 do Screen.drawRectangle(x1, y1, x2, y2); 88 return; 89 } 90 do Screen.drawPixel(x1, y1); 91 let sign = 1; 92 if(dy < 0){ 93 let dy = -dy; 94 let sign = -1; 95 } 96 if(dy > dx){ 97 let temp = dx + dx - dy; 98 while(y1 < y2){ 99 if(temp > 0){ 100 let x1 = x1 + 1; 101 let temp = temp - dy - dy; 102 } 103 let y1 = y1 + 1; 104 let temp = temp + dx + dx; 105 do Screen.drawPixel(x1, y1); 106 } 107 } 108 else{ 109 let temp = dy + dy - dx; 110 while(x1 < x2){ 111 if(temp > 0){ 112 let y1 = y1 + sign; 113 let temp = temp - dx - dx; 114 } 115 let x1 = x1 + 1; 116 let temp = temp + dy + dy; 117 do Screen.drawPixel(x1, y1); 118 } 119 } 120 return; 121 } 122 123 function void drawCollumn(int pos, int height, int fill){ 124 while(height > 0){ 125 if(color){ 126 do Memory.poke(pos, Memory.peek(pos) | fill); 127 } 128 else{ 129 do Memory.poke(pos, Memory.peek(pos) & ~fill); 130 } 131 let pos = pos + 32; 132 let height = height - 1; 133 } 134 return; 135 } 136 137 /** Draws a filled rectangle whose top left corner is (x1, y1) 138 * and bottom right corner is (x2,y2), using the current color. */ 139 function void drawRectangle(int x1, int y1, int x2, int y2) { 140 var int modx1, modx2, side, left, right; 141 if((x1 > x2) | (y1 > y2) | (x2 > 511) | (x1 < 0) | (y2 > 255) | (y1 < 0)){ 142 do Sys.error(9); 143 } 144 let left = x1 / 16; 145 let right = x2 / 16; 146 let modx1 = x1 - (left * 16); 147 let modx2 = x2 - (right * 16); 148 let side = 0; 149 if(left < right){ 150 while(modx1 < 16){ 151 let side = side + mask[modx1]; 152 let modx1 = modx1 + 1; 153 } 154 do Screen.drawCollumn(16384 + (y1 * 32) + left, 1 + y2 - y1, side); 155 while(left < (right - 1)){ 156 let left = left + 1; 157 do Screen.drawCollumn(16384 + (y1 * 32) + left, 1 + y2 - y1, -1); 158 } 159 let side = 0; 160 while(modx2 > -1){ 161 let side = side + mask[modx2]; 162 let modx2 = modx2 - 1; 163 } 164 do Screen.drawCollumn(16384 + (y1 * 32) + right, 1 + y2 - y1, side); 165 } 166 else{ 167 while(~(modx1 > modx2)){ 168 let side = side + mask[modx1]; 169 let modx1 = modx1 + 1; 170 } 171 do Screen.drawCollumn(16384 + (y1 * 32) + right, 1 + y2 - y1, side); 172 } 173 return; 174 } 175 176 /** Draws a filled circle of radius r<=181 around (x,y), using the current color. */ 177 function void drawCircle(int x, int y, int r) { 178 var int i, temp; 179 if((x > 511) | (x < 0) | (y > 255) | (y < 0)){ 180 do Sys.error(12); 181 } 182 if((x + r > 511) | (x - r < 0) | (y + r > 255) | (y - r < 0)){ 183 do Sys.error(13); 184 } 185 let i = -r; 186 while(~(i > r)){ 187 let temp = Math.sqrt((r * r) - (i * i)); 188 do Screen.drawLine(x - temp, y + i, x + temp, y + i); 189 let i = i + 1; 190 } 191 return; 192 } 193}