13 public enum GradientDirection { vertical, horizontal };
15 public void PaletteGradient(ColorAlpha * palette, int numColors, ColorKey * keys, int numKeys, float smoothness)
17 ColorKey * key = keys, * nextKey = keys;
18 int keyNum = 0, nextKeyNum = 0;
19 float inc = 1.0f/(numColors-1);
25 for(c = start = 0; c<numColors; c++)
27 ColorAlpha newColor = 0;
29 while(nextKey && percent > nextKey->percent)
31 key = nextKey; keyNum = nextKeyNum;
33 if(keyNum < numKeys - 1)
36 nextKeyNum = keyNum + 1;
42 if(nextKey && nextKey->percent != key->percent)
44 float scale = ease((percent - key->percent) / (nextKey->percent - key->percent),
45 smoothness, smoothness);
46 int cr = key->color.color.r;
47 int cg = key->color.color.g;
48 int cb = key->color.color.b;
49 int nr = nextKey->color.color.r;
50 int ng = nextKey->color.color.g;
51 int nb = nextKey->color.color.b;
52 int r = (int)(cr + (nr - cr) * scale);
53 int g = (int)(cg + (ng - cg) * scale);
54 int b = (int)(cb + (nb - cb) * scale);
56 r = Max(Min(r, 255),0);
57 g = Max(Min(g, 255),0);
58 b = Max(Min(b, 255),0);
60 newColor = Color { (byte)r, (byte)g, (byte)b };
63 newColor = key ? key->color : 0;
65 if(c == 0 || newColor != color)
70 for(i = start; i<c; i++)
82 for(i = start; i<c; i++)
87 float ease(float t, float a, float b)
92 if (s == 0.0f) return t;
99 if (t < a) return (k/a)*t*t;
100 if (t < 1.0f - b) return k*(2.0f * t - a);
102 return 1.0f - (k/b)*t*t;
105 public enum AlphaWriteMode
114 public int width, height;
116 subclass(DisplayDriver) driver;
117 DisplaySystem displaySystem;
121 public Box box, unclippedBox;
125 ColorAlpha foreground, background;
127 public void * driverData;
128 AlphaWriteMode alphaWrite;
142 driver.ReleaseSurface(display, this);
146 property AlphaWriteMode alphaWrite
148 set { alphaWrite = value; }
149 get { return alphaWrite; }
153 set { blend = value; }
154 get { return blend; }
156 property Bitmap bitmap
160 return ((LFBSurface)driverData).bitmap;
164 ColorAlpha GetPixel(int x, int y)
166 return driver.GetPixel(display, this, x,y);
169 void PutPixel(int x, int y)
171 driver.PutPixel(display, this, x,y);
174 void DrawLine(int x1, int y1, int x2, int y2)
176 driver.DrawLine(display, this, x1,y1,x2,y2);
179 void VLine(int y1, int y2, int x)
181 driver.DrawLine(display, this, x,y1,x,y2);
184 void HLine(int x1, int x2, int y)
186 driver.DrawLine(display, this, x1,y,x2,y);
189 void Rectangle(int x1, int y1, int x2, int y2)
191 driver.Rectangle(display, this, x1,y1,x2,y2);
194 void Area(int x1, int y1, int x2, int y2)
196 driver.Area(display, this, x1,y1,x2,y2);
199 void Clear(ClearType type)
201 driver.Clear(display, this, type);
204 void Blit(Bitmap src, int dx, int dy, int sx, int sy, int w, int h)
206 if(src.driver == driver)
207 driver.Blit(display, this, src, dx,dy, sx, sy,w,h);
208 else if(!src.driver || src.driver == class(LFBDisplayDriver))
209 driver.BlitDI(display, this, src, dx,dy, sx, sy,w,h);
212 void Stretch(Bitmap src, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
214 if(src.driver == driver)
215 driver.Stretch(display, this, src, dx,dy, sx,sy, w,h, sw, sh);
216 else if(!src.driver || src.driver == class(LFBDisplayDriver))
217 driver.StretchDI(display, this, src, dx,dy, sx,sy, w,h, sw, sh);
220 void Filter(Bitmap src, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
222 if(src.driver == driver)
223 driver.Filter(display, this, src, dx,dy, sx,sy, w,h, sw, sh);
224 else if(!src.driver || src.driver == class(LFBDisplayDriver))
225 driver.FilterDI(display, this, src, dx,dy, sx,sy, w,h, sw, sh);
228 void Tile(Bitmap src, int dx, int dy, int w, int h)
230 if(src && src.width && src.height)
233 for(x = 0,sx = dx; x < w; x += src.width, sx += src.width)
234 for(y = 0, sy = dy; y < h; y += src.height,sy += src.height)
235 Blit(src, sx, sy, 0,0, Min(src.width, w - x), Min(src.height, h - y));
239 void HTile(Bitmap src, int dx, int dy, int w, int h)
241 if(src && src.width && src.height)
244 for(x = 0, sx = dx; x < w; x += src.width, sx += src.width)
245 Stretch(src, sx, dy, 0, 0, Min(src.width, w - x), h, Min(src.width, w - x), src.height);
249 void VTile(Bitmap src, int dx, int dy, int w, int h)
251 if(src && src.width && src.height)
254 for(y = 0, sy = dy; y < h; y += src.height, sy += src.height)
255 Stretch(src, dx, sy, 0,0, w, Min(src.height, h - y), src.width, Min(src.height, h - y));
259 void FilterHTile(Bitmap src, int dx, int dy, int w, int h)
261 if(src && src.width && src.height)
264 for(x = 0, sx = dx; x < w; x += src.width, sx += src.width)
265 Filter(src, sx, dy, 0, 0, Min(src.width, w - x), h, Min(src.width, w - x), src.height);
269 void FilterVTile(Bitmap src, int dx, int dy, int w, int h)
271 if(src && src.width && src.height)
274 for(y = 0, sy = dy; y < h; y += src.height, sy += src.height)
275 Filter(src, dx, sy, 0,0, w, Min(src.height, h - y), src.width, Min(src.height, h - y));
279 void WriteText(int x, int y, char * text, int len)
282 driver.WriteText(display, this, x,y, text, len);
285 void TextExtent(char * text, int len, int * width, int * height)
287 driver.TextExtent(display, this, text, len, width, height);
290 void WriteTextf(int x, int y, char * format, ...)
294 char text[MAX_F_STRING];
296 va_start(args, format);
297 vsprintf(text, format, args);
298 driver.WriteText(display, this, x,y, text, strlen(text));
303 void CenterTextf(int x, int y, char * format, ...)
307 char text[MAX_F_STRING];
312 va_start(args, format);
313 vsprintf(text, format, args);
316 driver.TextExtent(display, this, text, len, &w, &h);
317 driver.WriteText(display, this, x - w/2, y, text, len);
322 void WriteTextDots(Alignment alignment, int x, int y, int width, char * text, int len)
326 TextExtent(text, len, &w, &h);
329 if(alignment == right)
331 else if(alignment == center)
332 x += (width - w) / 2;
333 WriteText(x, y, text, len);
344 TextExtent(".", 1, &dw, &dh);
346 #define UTF8_NUM_BYTES(x) (__extension__({ byte b = x; (b & 0x80 && b & 0x40) ? ((b & 0x20) ? ((b & 0x10) ? 4 : 3) : 2) : 1; }))
347 for(c = 0; (ch = text[c]); c += nb)
349 nb = UTF8_NUM_BYTES(ch);
350 TextExtent(text+c, nb, &w, &h);
356 WriteText(x, y, text, c);
357 //TextExtent(text, c, &totalW, &h);
359 WriteText(x, y, "...", 3);
363 void WriteTextDotsf(Alignment alignment, int x, int y, int width, char * format, ...)
367 char text[MAX_F_STRING];
369 va_start(args, format);
370 vsprintf(text, format, args);
371 WriteTextDots(alignment, x,y, width, text, strlen(text));
376 void Bevel(bool inner, int x, int y, int w, int h)
378 ColorAlpha foreground = this.foreground;
380 SetForeground(inner ? Color { 128,128,128 } : formColor);
381 HLine(x, x+w - 2, y);
382 VLine(y+1, y+h - 2, x);
384 SetForeground(inner ? Color { 64,64,64 } : white);
385 HLine(x+1, x+w-3, y+1);
386 VLine(y+2, y+h-3, x+1);
388 SetForeground(inner ? formColor : Color { 128,128,128 } );
389 HLine(x+1, x+w-2, y + h -2);
390 VLine(y+1, y+h-3, x + w - 2);
392 SetForeground(inner ? white : Color { 64,64,64 });
393 HLine(x, x+w-1, y + h - 1);
394 VLine(y, y+h-2, x + w - 1);
396 SetForeground(foreground);
399 void ThinBevel(bool inner, int x, int y, int w, int h)
401 SetForeground(inner ? Color { 128,128,128 } : white);
402 HLine(x, x+w - 2, y);
403 VLine(y+1, y+h - 2, x);
404 SetForeground(inner ? white : Color { 128,128,128 });
405 HLine(x, x+w-1, y + h - 1);
406 VLine(y, y+h-2, x + w - 1);
409 void Gradient(ColorKey * keys, int numKeys, float smoothness, GradientDirection direction, int x1, int y1, int x2, int y2)
411 if(x2 >= box.left && x1 <= box.right && y2 >= box.top && y1 <= box.bottom)
413 ColorKey * key = keys, * nextKey = keys;
414 int keyNum = 0, nextKeyNum = 0;
415 int height = (direction == horizontal) ? ((x2 - x1) + 1) : ((y2 - y1) + 1);
416 float inc = 1.0f/(height-1);
419 ColorAlpha color = 0;
420 int firstPixel = (direction == horizontal) ? x1 : y1;
421 int lastPixel = (direction == horizontal) ? x2 : y2;
422 int boxLeft = (direction == horizontal) ? box.left : box.top;
423 int boxRight = (direction == horizontal) ? box.right : box.bottom;
427 if(boxLeft > firstPixel)
429 percent = (boxLeft - firstPixel) * inc;
430 firstPixel = boxLeft;
432 if(boxRight < lastPixel)
433 lastPixel = boxRight;
435 for(c = start = firstPixel; c<=lastPixel; c++)
439 while(nextKey && percent > nextKey->percent)
441 key = nextKey; keyNum = nextKeyNum;
443 if(keyNum < numKeys - 1)
446 nextKeyNum = keyNum + 1;
452 if(nextKey && nextKey->percent != key->percent)
454 float scale = ease((percent - key->percent) / (nextKey->percent - key->percent),
455 smoothness, smoothness);
456 int cr = key->color.color.r;
457 int cg = key->color.color.g;
458 int cb = key->color.color.b;
459 int nr = nextKey->color.color.r;
460 int ng = nextKey->color.color.g;
461 int nb = nextKey->color.color.b;
462 int r = (int)(cr + (nr - cr) * scale);
463 int g = (int)(cg + (ng - cg) * scale);
464 int b = (int)(cb + (nb - cb) * scale);
466 r = Max(Min(r, 255),0);
467 g = Max(Min(g, 255),0);
468 b = Max(Min(b, 255),0);
470 newColor = Color { (byte)r, (byte)g, (byte)b };
473 newColor = key ? key->color : 0;
475 if(c == firstPixel || newColor != color)
479 SetBackground(color);
481 if(direction == horizontal)
482 Area(start,y1,c-1,y2);
484 Area(x1, start,x2, c-1);
492 SetBackground(color);
493 if(direction == horizontal)
494 Area(start,y1,c-1,y2);
496 Area(x1, start,x2, c-1);
501 property ColorAlpha foreground
506 driver.SetForeground(display, this, value);
508 get { return foreground; }
511 property ColorAlpha background
516 driver.SetBackground(display, this, value);
518 get { return background; }
521 property ColorAlpha blitTint
526 driver.SetBlitTint(display, this, value);
528 get { return blitTint; }
531 property uint lineStipple
535 driver.LineStipple(display, this, value);
541 get { value = { width, height }; }
549 if(display && display.flags.text)
551 value.left *= textCellW;
552 value.top *= textCellH;
553 value.right *= textCellW;
554 value.bottom *= textCellH;
559 property Display display
561 get { return display; }
568 if(value && font != value)
570 driver.TextFont(display, this, value);
578 property bool textOpacity
583 driver.TextOpacity(display, this, value);
586 get { return textOpacity; }
589 property byte drawingChar
591 set { driver.DrawingChar(display, this, value); }
594 property Box clipping
596 set { driver.Clip(display, this, value); }
599 // TODO: Make these functions obsolete
600 void SetForeground(ColorAlpha value)
603 driver.SetForeground(display, this, value);
606 void SetBackground(ColorAlpha value)
609 driver.SetBackground(display, this, value);
612 Color GetForeground(void)
617 Color GetBackground(void)
622 void LineStipple(uint value)
624 driver.LineStipple(display, this, value);
627 void GetSize(int * w, int * h)
633 void GetBox(Box value)
636 if(display.flags.text)
638 box.left *= textCellW;
639 box.top *= textCellH;
640 box.right *= textCellW;
641 box.bottom *= textCellH;
645 Display GetDisplay(void)
650 void TextFont(Font value)
652 if(value && font != value)
654 driver.TextFont(display, this, value);
664 bool GetTextOpacity(void)
669 void TextOpacity(bool value)
672 driver.TextOpacity(display, this, value);
675 void DrawingChar(unsigned char value)
677 driver.DrawingChar(display, this, value);
682 driver.Clip(display, this, box);