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
117 Box box, unclippedBox;
121 subclass(DisplayDriver) driver;
122 DisplaySystem displaySystem;
128 ColorAlpha foreground, background;
130 AlphaWriteMode alphaWrite;
144 driver.ReleaseSurface(display, this);
148 property AlphaWriteMode alphaWrite
150 set { alphaWrite = value; }
151 get { return alphaWrite; }
155 set { blend = value; }
156 get { return blend; }
158 property Bitmap bitmap
162 return ((LFBSurface)driverData).bitmap;
166 ColorAlpha GetPixel(int x, int y)
168 return driver.GetPixel(display, this, x,y);
171 void PutPixel(int x, int y)
173 driver.PutPixel(display, this, x,y);
176 void DrawLine(int x1, int y1, int x2, int y2)
178 driver.DrawLine(display, this, x1,y1,x2,y2);
181 void VLine(int y1, int y2, int x)
183 driver.DrawLine(display, this, x,y1,x,y2);
186 void HLine(int x1, int x2, int y)
188 driver.DrawLine(display, this, x1,y,x2,y);
191 void Rectangle(int x1, int y1, int x2, int y2)
193 driver.Rectangle(display, this, x1,y1,x2,y2);
196 void Area(int x1, int y1, int x2, int y2)
198 driver.Area(display, this, x1,y1,x2,y2);
201 void Clear(ClearType type)
203 driver.Clear(display, this, type);
206 void Blit(Bitmap src, int dx, int dy, int sx, int sy, int w, int h)
208 if(src.driver == driver)
209 driver.Blit(display, this, src, dx,dy, sx, sy,w,h);
210 else if(!src.driver || src.driver == class(LFBDisplayDriver))
211 driver.BlitDI(display, this, src, dx,dy, sx, sy,w,h);
214 void Stretch(Bitmap src, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
216 if(src.driver == driver)
217 driver.Stretch(display, this, src, dx,dy, sx,sy, w,h, sw, sh);
218 else if(!src.driver || src.driver == class(LFBDisplayDriver))
219 driver.StretchDI(display, this, src, dx,dy, sx,sy, w,h, sw, sh);
222 void Filter(Bitmap src, int dx, int dy, int sx, int sy, int w, int h, int sw, int sh)
224 if(src.driver == driver)
225 driver.Filter(display, this, src, dx,dy, sx,sy, w,h, sw, sh);
226 else if(!src.driver || src.driver == class(LFBDisplayDriver))
227 driver.FilterDI(display, this, src, dx,dy, sx,sy, w,h, sw, sh);
230 void Tile(Bitmap src, int dx, int dy, int w, int h)
232 if(src && src.width && src.height)
235 for(x = 0,sx = dx; x < w; x += src.width, sx += src.width)
236 for(y = 0, sy = dy; y < h; y += src.height,sy += src.height)
237 Blit(src, sx, sy, 0,0, Min(src.width, w - x), Min(src.height, h - y));
241 void HTile(Bitmap src, int dx, int dy, int w, int h)
243 if(src && src.width && src.height)
246 for(x = 0, sx = dx; x < w; x += src.width, sx += src.width)
247 Stretch(src, sx, dy, 0, 0, Min(src.width, w - x), h, Min(src.width, w - x), src.height);
251 void VTile(Bitmap src, int dx, int dy, int w, int h)
253 if(src && src.width && src.height)
256 for(y = 0, sy = dy; y < h; y += src.height, sy += src.height)
257 Stretch(src, dx, sy, 0,0, w, Min(src.height, h - y), src.width, Min(src.height, h - y));
261 void FilterHTile(Bitmap src, int dx, int dy, int w, int h)
263 if(src && src.width && src.height)
266 for(x = 0, sx = dx; x < w; x += src.width, sx += src.width)
267 Filter(src, sx, dy, 0, 0, Min(src.width, w - x), h, Min(src.width, w - x), src.height);
271 void FilterVTile(Bitmap src, int dx, int dy, int w, int h)
273 if(src && src.width && src.height)
276 for(y = 0, sy = dy; y < h; y += src.height, sy += src.height)
277 Filter(src, dx, sy, 0,0, w, Min(src.height, h - y), src.width, Min(src.height, h - y));
281 void WriteText(int x, int y, const char * text, int len)
284 driver.WriteText(display, this, x,y, text, len, 0, null); //, null);
287 void WriteText2(int x, int y, const char * text, int len, int prevGlyph, int * rPrevGlyph)
290 driver.WriteText(display, this, x,y, text, len, prevGlyph, rPrevGlyph);
293 void TextExtent(const char * text, int len, int * width, int * height)
296 driver.TextExtent(display, this, text, len, width, height, 0, null, &advance);
301 void TextExtent2(const char * text, int len, int * width, int * height, int prevGlyph, int * rPrevGlyph, int * overHang)
303 driver.TextExtent(display, this, text, len, width, height, prevGlyph, rPrevGlyph, overHang);
306 void WriteTextf(int x, int y, const char * format, ...)
310 char text[MAX_F_STRING];
312 va_start(args, format);
313 vsnprintf(text, sizeof(text), format, args);
314 text[sizeof(text)-1] = 0;
316 driver.WriteText(display, this, x,y, text, strlen(text), 0, null);
321 void CenterTextf(int x, int y, const char * format, ...)
325 char text[MAX_F_STRING];
329 va_start(args, format);
330 vsnprintf(text, sizeof(text), format, args);
331 text[sizeof(text)-1] = 0;
334 driver.TextExtent(display, this, text, len, &w, &h, 0, null, &oh);
336 driver.WriteText(display, this, x - w/2, y, text, len, 0, null);
341 void WriteTextDots(Alignment alignment, int x, int y, int width, const char * text, int len)
345 TextExtent(text, len, &w, &h);
348 if(alignment == right)
350 else if(alignment == center)
351 x += (width - w) / 2;
352 WriteText(x, y, text, len);
363 TextExtent(".", 1, &dw, &dh);
365 #define UTF8_NUM_BYTES(x) (__extension__({ byte b = x; (b & 0x80 && b & 0x40) ? ((b & 0x20) ? ((b & 0x10) ? 4 : 3) : 2) : 1; }))
366 for(c = 0; (ch = text[c]); c += nb)
368 nb = UTF8_NUM_BYTES(ch);
369 TextExtent(text+c, nb, &w, &h);
375 WriteText(x, y, text, c);
376 //TextExtent(text, c, &totalW, &h);
378 WriteText(x, y, "...", 3);
382 void WriteTextDotsf(Alignment alignment, int x, int y, int width, const char * format, ...)
386 char text[MAX_F_STRING];
388 va_start(args, format);
389 vsnprintf(text, sizeof(text), format, args);
390 text[sizeof(text)-1] = 0;
391 WriteTextDots(alignment, x,y, width, text, strlen(text));
396 void Bevel(bool inner, int x, int y, int w, int h)
398 ColorAlpha foreground = this.foreground;
400 SetForeground(inner ? Color { 128,128,128 } : formColor);
401 HLine(x, x+w - 2, y);
402 VLine(y+1, y+h - 2, x);
404 SetForeground(inner ? Color { 64,64,64 } : white);
405 HLine(x+1, x+w-3, y+1);
406 VLine(y+2, y+h-3, x+1);
408 SetForeground(inner ? formColor : Color { 128,128,128 } );
409 HLine(x+1, x+w-2, y + h -2);
410 VLine(y+1, y+h-3, x + w - 2);
412 SetForeground(inner ? white : Color { 64,64,64 });
413 HLine(x, x+w-1, y + h - 1);
414 VLine(y, y+h-2, x + w - 1);
416 SetForeground(foreground);
419 void ThinBevel(bool inner, int x, int y, int w, int h)
421 SetForeground(inner ? Color { 128,128,128 } : white);
422 HLine(x, x+w - 2, y);
423 VLine(y+1, y+h - 2, x);
424 SetForeground(inner ? white : Color { 128,128,128 });
425 HLine(x, x+w-1, y + h - 1);
426 VLine(y, y+h-2, x + w - 1);
429 void Gradient(ColorKey * keys, int numKeys, float smoothness, GradientDirection direction, int x1, int y1, int x2, int y2)
431 if(x2 >= box.left && x1 <= box.right && y2 >= box.top && y1 <= box.bottom)
433 ColorKey * key = keys, * nextKey = keys;
434 int keyNum = 0, nextKeyNum = 0;
435 int height = (direction == horizontal) ? ((x2 - x1) + 1) : ((y2 - y1) + 1);
436 float inc = 1.0f/(height-1);
439 ColorAlpha color = 0;
440 int firstPixel = (direction == horizontal) ? x1 : y1;
441 int lastPixel = (direction == horizontal) ? x2 : y2;
442 int boxLeft = (direction == horizontal) ? box.left : box.top;
443 int boxRight = (direction == horizontal) ? box.right : box.bottom;
447 if(boxLeft > firstPixel)
449 percent = (boxLeft - firstPixel) * inc;
450 firstPixel = boxLeft;
452 if(boxRight < lastPixel)
453 lastPixel = boxRight;
455 for(c = start = firstPixel; c<=lastPixel; c++)
459 while(nextKey && percent > nextKey->percent)
461 key = nextKey; keyNum = nextKeyNum;
463 if(keyNum < numKeys - 1)
466 nextKeyNum = keyNum + 1;
472 if(nextKey && nextKey->percent != key->percent)
474 float scale = ease((percent - key->percent) / (nextKey->percent - key->percent),
475 smoothness, smoothness);
476 int cr = key->color.color.r;
477 int cg = key->color.color.g;
478 int cb = key->color.color.b;
479 int nr = nextKey->color.color.r;
480 int ng = nextKey->color.color.g;
481 int nb = nextKey->color.color.b;
482 int r = (int)(cr + (nr - cr) * scale);
483 int g = (int)(cg + (ng - cg) * scale);
484 int b = (int)(cb + (nb - cb) * scale);
486 r = Max(Min(r, 255),0);
487 g = Max(Min(g, 255),0);
488 b = Max(Min(b, 255),0);
490 newColor = Color { (byte)r, (byte)g, (byte)b };
493 newColor = key ? key->color : 0;
495 if(c == firstPixel || newColor != color)
499 SetBackground(color);
501 if(direction == horizontal)
502 Area(start,y1,c-1,y2);
504 Area(x1, start,x2, c-1);
512 SetBackground(color);
513 if(direction == horizontal)
514 Area(start,y1,c-1,y2);
516 Area(x1, start,x2, c-1);
521 property ColorAlpha foreground
526 driver.SetForeground(display, this, value);
528 get { return foreground; }
531 property ColorAlpha background
536 driver.SetBackground(display, this, value);
538 get { return background; }
541 property ColorAlpha blitTint
546 driver.SetBlitTint(display, this, value);
548 get { return blitTint; }
551 property uint lineStipple
555 driver.LineStipple(display, this, value);
561 get { value = { width, height }; }
569 if(display && display.flags.text)
571 value.left *= textCellW;
572 value.top *= textCellH;
573 value.right *= textCellW;
574 value.bottom *= textCellH;
579 property Display display
581 get { return display; }
588 if(value && font != value)
590 driver.TextFont(display, this, value);
598 property bool textOpacity
603 driver.TextOpacity(display, this, value);
606 get { return textOpacity; }
609 property byte drawingChar
611 set { driver.DrawingChar(display, this, value); }
614 property Box clipping
616 set { driver.Clip(display, this, value); }
619 // TODO: Make these functions obsolete
620 void SetForeground(ColorAlpha value)
623 driver.SetForeground(display, this, value);
626 void SetBackground(ColorAlpha value)
629 driver.SetBackground(display, this, value);
632 Color GetForeground(void)
637 Color GetBackground(void)
642 void LineStipple(uint value)
644 driver.LineStipple(display, this, value);
647 void GetSize(int * w, int * h)
653 void GetBox(Box value)
656 if(display.flags.text)
658 box.left *= textCellW;
659 box.top *= textCellH;
660 box.right *= textCellW;
661 box.bottom *= textCellH;
665 Display GetDisplay(void)
670 void TextFont(Font value)
672 if(value && font != value)
674 driver.TextFont(display, this, value);
684 bool GetTextOpacity(void)
689 void TextOpacity(bool value)
692 driver.TextOpacity(display, this, value);
695 void DrawingChar(unsigned char value)
697 driver.DrawingChar(display, this, value);
702 driver.Clip(display, this, box);