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, char * text, int len)
284 driver.WriteText(display, this, x,y, text, len);
287 void TextExtent(char * text, int len, int * width, int * height)
289 driver.TextExtent(display, this, text, len, width, height);
292 void WriteTextf(int x, int y, char * format, ...)
296 char text[MAX_F_STRING];
298 va_start(args, format);
299 vsnprintf(text, sizeof(text), format, args);
300 text[sizeof(text)-1] = 0;
301 driver.WriteText(display, this, x,y, text, strlen(text));
306 void CenterTextf(int x, int y, char * format, ...)
310 char text[MAX_F_STRING];
314 va_start(args, format);
315 vsnprintf(text, sizeof(text), format, args);
316 text[sizeof(text)-1] = 0;
319 driver.TextExtent(display, this, text, len, &w, &h);
320 driver.WriteText(display, this, x - w/2, y, text, len);
325 void WriteTextDots(Alignment alignment, int x, int y, int width, char * text, int len)
329 TextExtent(text, len, &w, &h);
332 if(alignment == right)
334 else if(alignment == center)
335 x += (width - w) / 2;
336 WriteText(x, y, text, len);
347 TextExtent(".", 1, &dw, &dh);
349 #define UTF8_NUM_BYTES(x) (__extension__({ byte b = x; (b & 0x80 && b & 0x40) ? ((b & 0x20) ? ((b & 0x10) ? 4 : 3) : 2) : 1; }))
350 for(c = 0; (ch = text[c]); c += nb)
352 nb = UTF8_NUM_BYTES(ch);
353 TextExtent(text+c, nb, &w, &h);
359 WriteText(x, y, text, c);
360 //TextExtent(text, c, &totalW, &h);
362 WriteText(x, y, "...", 3);
366 void WriteTextDotsf(Alignment alignment, int x, int y, int width, char * format, ...)
370 char text[MAX_F_STRING];
372 va_start(args, format);
373 vsnprintf(text, sizeof(text), format, args);
374 text[sizeof(text)-1] = 0;
375 WriteTextDots(alignment, x,y, width, text, strlen(text));
380 void Bevel(bool inner, int x, int y, int w, int h)
382 ColorAlpha foreground = this.foreground;
384 SetForeground(inner ? Color { 128,128,128 } : formColor);
385 HLine(x, x+w - 2, y);
386 VLine(y+1, y+h - 2, x);
388 SetForeground(inner ? Color { 64,64,64 } : white);
389 HLine(x+1, x+w-3, y+1);
390 VLine(y+2, y+h-3, x+1);
392 SetForeground(inner ? formColor : Color { 128,128,128 } );
393 HLine(x+1, x+w-2, y + h -2);
394 VLine(y+1, y+h-3, x + w - 2);
396 SetForeground(inner ? white : Color { 64,64,64 });
397 HLine(x, x+w-1, y + h - 1);
398 VLine(y, y+h-2, x + w - 1);
400 SetForeground(foreground);
403 void ThinBevel(bool inner, int x, int y, int w, int h)
405 SetForeground(inner ? Color { 128,128,128 } : white);
406 HLine(x, x+w - 2, y);
407 VLine(y+1, y+h - 2, x);
408 SetForeground(inner ? white : Color { 128,128,128 });
409 HLine(x, x+w-1, y + h - 1);
410 VLine(y, y+h-2, x + w - 1);
413 void Gradient(ColorKey * keys, int numKeys, float smoothness, GradientDirection direction, int x1, int y1, int x2, int y2)
415 if(x2 >= box.left && x1 <= box.right && y2 >= box.top && y1 <= box.bottom)
417 ColorKey * key = keys, * nextKey = keys;
418 int keyNum = 0, nextKeyNum = 0;
419 int height = (direction == horizontal) ? ((x2 - x1) + 1) : ((y2 - y1) + 1);
420 float inc = 1.0f/(height-1);
423 ColorAlpha color = 0;
424 int firstPixel = (direction == horizontal) ? x1 : y1;
425 int lastPixel = (direction == horizontal) ? x2 : y2;
426 int boxLeft = (direction == horizontal) ? box.left : box.top;
427 int boxRight = (direction == horizontal) ? box.right : box.bottom;
431 if(boxLeft > firstPixel)
433 percent = (boxLeft - firstPixel) * inc;
434 firstPixel = boxLeft;
436 if(boxRight < lastPixel)
437 lastPixel = boxRight;
439 for(c = start = firstPixel; c<=lastPixel; c++)
443 while(nextKey && percent > nextKey->percent)
445 key = nextKey; keyNum = nextKeyNum;
447 if(keyNum < numKeys - 1)
450 nextKeyNum = keyNum + 1;
456 if(nextKey && nextKey->percent != key->percent)
458 float scale = ease((percent - key->percent) / (nextKey->percent - key->percent),
459 smoothness, smoothness);
460 int cr = key->color.color.r;
461 int cg = key->color.color.g;
462 int cb = key->color.color.b;
463 int nr = nextKey->color.color.r;
464 int ng = nextKey->color.color.g;
465 int nb = nextKey->color.color.b;
466 int r = (int)(cr + (nr - cr) * scale);
467 int g = (int)(cg + (ng - cg) * scale);
468 int b = (int)(cb + (nb - cb) * scale);
470 r = Max(Min(r, 255),0);
471 g = Max(Min(g, 255),0);
472 b = Max(Min(b, 255),0);
474 newColor = Color { (byte)r, (byte)g, (byte)b };
477 newColor = key ? key->color : 0;
479 if(c == firstPixel || newColor != color)
483 SetBackground(color);
485 if(direction == horizontal)
486 Area(start,y1,c-1,y2);
488 Area(x1, start,x2, c-1);
496 SetBackground(color);
497 if(direction == horizontal)
498 Area(start,y1,c-1,y2);
500 Area(x1, start,x2, c-1);
505 property ColorAlpha foreground
510 driver.SetForeground(display, this, value);
512 get { return foreground; }
515 property ColorAlpha background
520 driver.SetBackground(display, this, value);
522 get { return background; }
525 property ColorAlpha blitTint
530 driver.SetBlitTint(display, this, value);
532 get { return blitTint; }
535 property uint lineStipple
539 driver.LineStipple(display, this, value);
545 get { value = { width, height }; }
553 if(display && display.flags.text)
555 value.left *= textCellW;
556 value.top *= textCellH;
557 value.right *= textCellW;
558 value.bottom *= textCellH;
563 property Display display
565 get { return display; }
572 if(value && font != value)
574 driver.TextFont(display, this, value);
582 property bool textOpacity
587 driver.TextOpacity(display, this, value);
590 get { return textOpacity; }
593 property byte drawingChar
595 set { driver.DrawingChar(display, this, value); }
598 property Box clipping
600 set { driver.Clip(display, this, value); }
603 // TODO: Make these functions obsolete
604 void SetForeground(ColorAlpha value)
607 driver.SetForeground(display, this, value);
610 void SetBackground(ColorAlpha value)
613 driver.SetBackground(display, this, value);
616 Color GetForeground(void)
621 Color GetBackground(void)
626 void LineStipple(uint value)
628 driver.LineStipple(display, this, value);
631 void GetSize(int * w, int * h)
637 void GetBox(Box value)
640 if(display.flags.text)
642 box.left *= textCellW;
643 box.top *= textCellH;
644 box.right *= textCellW;
645 box.bottom *= textCellH;
649 Display GetDisplay(void)
654 void TextFont(Font value)
656 if(value && font != value)
658 driver.TextFont(display, this, value);
668 bool GetTextOpacity(void)
673 void TextOpacity(bool value)
676 driver.TextOpacity(display, this, value);
679 void DrawingChar(unsigned char value)
681 driver.DrawingChar(display, this, value);
686 driver.Clip(display, this, box);