@@ -11,7 +11,12 @@ static struct GraphicsState {
1111 int x , y ;
1212} gstate = {0 };
1313
14- static uint8_t BUFFER [GRAPHICS_MAX_HEIGHT ][GRAPHICS_MAX_WIDTH ]= {0 };
14+ static uint8_t __BUFFER [GRAPHICS_MAX_HEIGHT ][GRAPHICS_MAX_WIDTH ]= {0 };
15+ static inline write_buffer (int x , int y , uint8_t color ) {
16+ if (x >=0 && x < GRAPHICS_MAX_WIDTH && y >=0 && y < GRAPHICS_MAX_HEIGHT ) {
17+ __BUFFER [y ][x ] = color ;
18+ } // otherwise just ignore
19+ }
1520
1621void initgraph (int * graphdetect , int * graphmode , char * not_used ) {
1722 if (* graphdetect == DETECT ) {
@@ -50,12 +55,12 @@ static void _autoflushnow() {
5055}
5156
5257int graphflush () {
53- gstate .last_err = SYSCALL_A2 (SYSCALL_GRAPHICS , SYSCALL_GRAPHICS_COPYBUFFER , BUFFER );
58+ gstate .last_err = SYSCALL_A2 (SYSCALL_GRAPHICS , SYSCALL_GRAPHICS_COPYBUFFER , __BUFFER );
5459 return gstate .last_err ;
5560}
5661
5762void cleardevice () {
58- memset (BUFFER , gstate .bkcolor , sizeof (BUFFER ));
63+ memset (__BUFFER , gstate .bkcolor , sizeof (__BUFFER ));
5964 moveto (0 , 0 );
6065 _autoflushnow ();
6166}
@@ -89,12 +94,31 @@ int getbkcolor() {
8994}
9095
9196void putpixel (int x , int y , int color ) {
92- BUFFER [ y ][ x ] = color ;
97+ write_buffer ( x , y , color ) ;
9398 _autoflushnow ();
9499}
95100
96101void line (int x1 , int y1 , int x2 , int y2 ) {
97102 const int color = getcolor ();
103+ if (x1 == x2 ) {
104+ if (y1 > y2 ) {
105+ int t = y1 ; y1 = y2 ; y2 = t ;
106+ }
107+ for (int y = y1 ; y <=y2 ; y ++ ) {
108+ write_buffer (x1 , y , color );
109+ }
110+ return ;
111+ }
112+ if (y1 == y2 ) {
113+ if (x1 > x2 ) {
114+ int t = x1 ; x1 = x2 ; x2 = t ;
115+ }
116+ for (int x = x1 ; x <=x2 ; x ++ ) {
117+ write_buffer (x , y1 , color );
118+ }
119+ return ;
120+ }
121+
98122 if (abs (x1 - x2 )>=abs (y1 - y2 )) {
99123 // horizontal length is longer
100124 if (x1 > x2 ) {
@@ -106,8 +130,8 @@ void line(int x1, int y1, int x2, int y2) {
106130 const int xdiff = x2 - x1 ;
107131 const int ydiff = y2 - y1 ;
108132 for (int x = x1 ; x <= x2 ; x ++ ) {
109- int y = ydiff * (x - x1 )/xdiff ;
110- BUFFER [ y ][ x ] = color ;
133+ int y = ydiff * (x - x1 )/xdiff + y1 ;
134+ write_buffer ( x , y , color ) ;
111135 }
112136 } else {
113137 // vertical length is longer
@@ -120,8 +144,8 @@ void line(int x1, int y1, int x2, int y2) {
120144 const int xdiff = x2 - x1 ;
121145 const int ydiff = y2 - y1 ;
122146 for (int y = y1 ; y <= y2 ; y ++ ) {
123- int x = y * xdiff /ydiff + x1 ;
124- BUFFER [ y ][ x ] = color ;
147+ int x = ( y - y1 ) * xdiff /ydiff + x1 ;
148+ write_buffer ( x , y , color ) ;
125149 }
126150 }
127151 _autoflushnow ();
@@ -139,12 +163,12 @@ void rectangle(int left, int top, int right, int bottom) {
139163 const int color = getcolor ();
140164
141165 for (int x = left ;x <=right ;x ++ ) {
142- BUFFER [ top ][ x ] = color ;
143- BUFFER [ bottom ][ x ] = color ;
166+ write_buffer ( x , top , color ) ;
167+ write_buffer ( x , bottom , color ) ;
144168 }
145169 for (int y = top + 1 ;y < bottom ;y ++ ) {
146- BUFFER [ y ][ left ] = color ;
147- BUFFER [ y ][ right ] = color ;
170+ write_buffer ( left , y , color ) ;
171+ write_buffer ( right , y , color ) ;
148172 }
149173 _autoflushnow ();
150174}
@@ -154,7 +178,7 @@ void bar(int left, int top, int right, int bottom) {
154178
155179 for (int y = top ;y <=bottom ;y ++ ) {
156180 for (int x = left ;x <=right ;x ++ ) {
157- BUFFER [ y ][ x ] = color ;
181+ write_buffer ( x , y , color ) ;
158182 }
159183 }
160184 _autoflushnow ();
@@ -181,7 +205,7 @@ void fillellipse(int xcenter, int ycenter, int x_radius, int y_radius) {
181205 int fx = xcenter + x ;
182206 int fy = ycenter + y ;
183207 if (fx >=0 && fy >=0 && fx < GRAPHICS_MAX_WIDTH && fy < GRAPHICS_MAX_HEIGHT ) {
184- BUFFER [ fy ][ fx ] = color ;
208+ write_buffer ( fx , fy , color ) ;
185209 }
186210 }
187211 }
@@ -339,7 +363,7 @@ static void draw_font(char c, int x, int y) {
339363 uint8_t frow = font [c ][j ];
340364 for (int i = 0 ; i < 8 ; i ++ ) if (x + i >=0 && x + i < GRAPHICS_MAX_WIDTH ) {
341365 if ((frow & mask )) {
342- BUFFER [ y + j ][ x + i ] = color ;
366+ write_buffer ( x + i , y + j , color ) ;
343367 }
344368 mask <<=1 ;
345369 }
0 commit comments