PixelBuffer becomes an interface
[org.ibex.core.git] / src / org / ibex / graphics / PixelBuffer.java
1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the GNU General Public License version 2 ("the License").
3 // You may not use this file except in compliance with the License.
4
5 package org.ibex.graphics;
6 import org.ibex.util.*;
7
8 /**
9  *  <p>
10  *  A block of pixels which can be drawn on.
11  *  </p>
12  *
13  *  <p>
14  *  Implementations of the Platform class should return objects
15  *  supporting this interface from the _createPixelBuffer()
16  *  method. These implementations may choose to use off-screen video
17  *  ram for this purpose (for example, a Pixmap on X11).
18  *  </p>
19  */
20 public interface PixelBuffer {
21     public abstract void drawLine(int x1, int y1, int x2, int y2, int color);
22     public abstract void fillTrapezoid(int x1, int x2, int y1, int x3, int x4, int y2, int color);
23     public abstract void drawGlyph(Font.Glyph source, int dx1, int dy1, int cx1, int cy1, int cx2, int cy2, int rgb, int bg);
24     public abstract void stroke(Polygon p, int color);
25     public abstract void fill(Polygon p, Paint paint);
26 }
27
28
29
30
31 /*
32     // FEATURE: we want floats (inter-pixel spacing) for antialiasing, but this hoses the fastpath line drawing... argh!
33     // draws a line of width <tt>w</tt>; note that the coordinates here are <i>post-transform</i> 
34     public void drawLine(int x1, int y1, int x2, int y2, int w, int color, boolean capped) {
35
36         if (y1 > y2) { int t = x1; x1 = x2; x2 = t; t = y1; y1 = y2; y2 = t; }
37
38         if (x1 == x2) {
39             fillTrapezoid(x1 - w / 2, x2 + w / 2, y1 - (capped ? w / 2 : 0),
40                           x1 - w / 2, x2 + w / 2, y2 + (capped ? w / 2 : 0), color);
41             return;
42         }
43
44         // fastpath for single-pixel width lines
45         if (w == 1) {
46             float slope = (float)(y2 - y1) / (float)(x2 - x1);
47             int last_x = x1;
48             for(int y=y1; y<=y2; y++) {
49                 int new_x = (int)((float)(y - y1) / slope) + x1;
50                 if (slope >= 0) fillTrapezoid(last_x + 1, y != y2 ? new_x + 1 : new_x, y,
51                                               last_x + 1, y != y2 ? new_x + 1 : new_x, y + 1, color);
52                 else fillTrapezoid(y != y2 ? new_x : new_x + 1, last_x, y,
53                                    y != y2 ? new_x : new_x + 1, last_x, y + 1, color);
54                 last_x = new_x;
55             }
56             return;
57         }
58
59         // actually half-width
60         float width = (float)w / 2;
61         float phi = (float)Math.atan((y2 - y1) / (x2 - x1));
62         if (phi < 0.0) phi += (float)Math.PI * 2;
63         float theta = (float)Math.PI / 2 - phi;
64
65         // dx and dy are the x and y distance between each endpoint and the corner of the stroke
66         int dx = (int)(width * Math.cos(theta));
67         int dy = (int)(width * Math.sin(theta));
68
69         // slice is the longest possible length of a horizontal line across the stroke
70         int slice = (int)(2 * width / Math.cos(theta));
71
72         if (capped) {
73             x1 -= width * Math.cos(phi);
74             x2 += width * Math.cos(phi);
75             y1 -= width * Math.sin(phi);
76             y2 += width * Math.sin(phi);
77         }
78
79         fillTrapezoid(x1 + dx, x1 + dx, y1 - dy, x1 - dx, x1 - dx + slice, y1 + dy, color);           // top corner
80         fillTrapezoid(x2 + dx - slice, x2 + dx, y2 - dy, x2 - dx, x2 - dx, y2 + dy, color);           // bottom corner
81         fillTrapezoid(x1 - dx, x1 - dx + slice, y1 + dy, x2 + dx - slice, x2 + dx, y2 - dy, color);   // middle
82     }
83
84 }
85 */