Report no pressed keys -- now it properly plays the input.
[synaesthesia.git] / polygon.h
blobf711c030bd658781bd37f1a5d4599be3e8db7428
1 #include <string.h>
3 template<class Pixel>
4 struct Bitmap {
5 int width, height, extra;
6 Pixel *data;
8 Bitmap(int e=0) : extra(e), data(0) { };
9 ~Bitmap() { delete[] data; };
11 void size(int w,int h) {
12 delete[] data;
13 width = w;
14 height = h;
15 data = new Pixel[w*h+extra];
16 clear();
19 void clear() {
20 memset(data,0,sizeof(Pixel)*(width*height+extra));
24 template<class Pixel, class Combiner, int superSampleShift>
25 struct PolygonEngine : public Bitmap<Pixel> {
26 PolygonEngine() : Bitmap<Pixel>(1) { }
28 #define super (1<<superSampleShift)
29 void apply(Pixel *dest) {
30 Pixel sum=0;
31 int count = this->width*this->height;
32 Pixel *src = this->data;
33 while(count--) {
34 sum += *(src++);
35 if (sum)
36 *dest = Combiner::combine(sum,*dest);
37 dest++;
41 void add(Pixel color,int x,int y) {
42 if (y < 0) return;
43 if (y >= this->height) return;
44 if (x < 0) x = 0;
45 if (x > this->width) x = this->width;
46 this->data[x+y*this->width] += color;
49 /* Color is char[layers] */
51 // zwoosh, yknow, it goes... zwoosh an all these bars and lines and
52 // crap intersect.
53 Pixel colorTable[2][super+1];
54 void pen(Pixel color) {
55 for(int i=0;i<super+1;i++) {
56 colorTable[0][i] = color*i;
57 colorTable[1][i] = -(color*i);
61 void line(int x1,int y1,int x2,int y2) {
62 Pixel *colors;
63 if (y2 < y1) {
64 int temp;
65 temp = x2; x2 = x1; x1 = temp;
66 temp = y2; y2 = y1; y1 = temp;
67 colors = colorTable[1];
68 } else {
69 if (y1 == y2) return;
71 colors= colorTable[0];
74 int slope = (x1-x2 << 16)/(y1-y2);
75 int x = x1<<16, y = y1;
76 while(y < y2) {
77 add(colors[super-((x>>16)&(super-1))],
78 x>>(16+superSampleShift),y>>superSampleShift);
79 add(colors[(x>>16)&(super-1)],
80 1+(x>>(16+superSampleShift)),y>>superSampleShift);
81 x += slope;
82 y++;
86 void icon(double icon[][4],Pixel color,double x,double y,
87 double scaleX, double scaleY) {
88 pen(color);
89 x *= super;
90 y *= super;
91 scaleX *= super;
92 scaleY *= super;
93 for(int i=0;icon[i][1] != icon[i][3];i++)
94 line(int(icon[i][0]*scaleX+x),int(icon[i][1]*scaleY+y),
95 int(icon[i][2]*scaleX+x),int(icon[i][3]*scaleY+y));
97 #undef super