+/TECS
git clone https://git.julia.blue/+/TECS
b17cc4b
master
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; 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 if(color){ 52 do Memory.poke(temp, Memory.peek(temp) | mask[x % 16]); 53 } 54 else{ 55 do Memory.poke(temp, Memory.peek(temp) & ~mask[x % 16]); 56 } 57 return; 58 } 59 60 /** Draws a line from pixel (x1,y1) to pixel (x2,y2), using the current color. */ 61 function void drawLine(int x1, int y1, int x2, int y2) { 62 var int dx, dy, temp, sign; 63 if((x2 > 511) | (x1 < 0) | (y2 > 255) | (y1 < 0)){ 64 do Sys.error(8); 65 } 66 if(x1 > x2){ 67 let dx = x1; 68 let x1 = x2; 69 let x2 = dx; 70 let dy = y1; 71 let y1 = y2; 72 let y2 = dy; 73 } 74 let dx = x2 - x1; 75 if(dx = 0){ 76 if(y1 > y2){ 77 let dy = y1; 78 let y1 = y2; 79 let y2 = dy; 80 } 81 do Screen.drawRectangle(x1, y1, x2, y2); 82 return; 83 } 84 let dy = y2 - y1; 85 if(dy == 0){ 86 do Screen.drawRectangle(x1, y1, x2, y2); 87 return; 88 } 89 do Screen.drawPixel(x1, y1); 90 let sign = 1; 91 if(dy < 0){ 92 let dy = -dy; 93 let sign = -1; 94 } 95 if(dy > dx){ 96 let temp = dx + dx - dy; 97 while(y1 < y2){ 98 if(temp > 0){ 99 let x1 = x1 + 1; 100 let temp = temp - dy - dy; 101 } 102 let y1 = y1 + 1; 103 let temp = temp + dx + dx; 104 do Screen.drawPixel(x1, y1); 105 } 106 } 107 else{ 108 let temp = dy + dy - dx; 109 while(x1 < x2){ 110 if(temp > 0){ 111 let y1 = y1 + sign; 112 let temp = temp - dx - dx; 113 } 114 let x1 = x1 + 1; 115 let temp = temp + dy + dy; 116 do Screen.drawPixel(x1, y1); 117 } 118 } 119 return; 120 } 121 122 function void drawCollumn(int pos, int height, int fill){ 123 while(height > 0){ 124 if(color){ 125 do Memory.poke(pos, Memory.peek(pos) | fill); 126 } 127 else{ 128 do Memory.poke(pos, Memory.peek(pos) & ~fill); 129 } 130 let pos = pos + 32; 131 let height = height - 1; 132 } 133 return; 134 } 135 136 /** Draws a filled rectangle whose top left corner is (x1, y1) 137 * and bottom right corner is (x2,y2), using the current color. */ 138 function void drawRectangle(int x1, int y1, int x2, int y2) { 139 var int modx1, modx2, side, left, right; 140 if((x1 > x2) | (y1 > y2) | (x2 > 511) | (x1 < 0) | (y2 > 255) | (y1 < 0)){ 141 do Sys.error(9); 142 } 143 let left = x1 / 16; 144 let right = x2 / 16; 145 let modx1 = x1 - (left * 16); 146 let modx2 = x2 - (right * 16); 147 let side = 0; 148 if(left < right){ 149 while(modx1 < 16){ 150 let side = side + mask[modx1]; 151 let modx1 = modx1 + 1; 152 } 153 do Screen.drawCollumn(16384 + (y1 * 32) + left, 1 + y2 - y1, side); 154 while(left < (right - 1)){ 155 let left = left + 1; 156 do Screen.drawCollumn(16384 + (y1 * 32) + left, 1 + y2 - y1, -1); 157 } 158 let side = 0; 159 while(modx2 >= 0){ 160 let side = side + mask[modx2]; 161 let modx2 = modx2 - 1; 162 } 163 do Screen.drawCollumn(16384 + (y1 * 32) + right, 1 + y2 - y1, side); 164 } 165 else{ 166 while(modx1 <= modx2){ 167 let side = side + mask[modx1]; 168 let modx1 = modx1 + 1; 169 } 170 do Screen.drawCollumn(16384 + (y1 * 32) + right, 1 + y2 - y1, side); 171 } 172 return; 173 } 174 175 /** Draws a filled circle of radius r<=181 around (x,y), using the current color. */ 176 function void drawCircle(int x, int y, int r) { 177 var int i, temp; 178 if((x > 511) | (x < 0) | (y > 255) | (y < 0)){ 179 do Sys.error(12); 180 } 181 if(((x + r) > 511) | ((x - r) < 0) | ((y + r) > 255) | ((y - r) < 0)){ 182 do Sys.error(13); 183 } 184 let i = -r; 185 while(i <= r){ 186 let temp = Math.sqrt((r * r) - (i * i)); 187 do Screen.drawLine(x - temp, y + i, x + temp, y + i); 188 let i = i + 1; 189 } 190 return; 191 } 192}